一.需求分析
1. 创建北京、上海 2 所学校
2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开
3. 课程包含,周期,价格,通过学校创建课程
4. 通过学校创建班级, 班级关联课程、讲师
5. 创建学员时,选择学校,关联班级
6. 创建讲师角色时要关联学校,
6. 提供两个角色接口
6.1 学员视图, 可以注册, 交学费, 选择班级,
6.2 讲师视图, 讲师可管理自己的班级, 上课时选择班级, 查看班级学员列表 , 修改所管理的学员的成绩
6.3 管理视图,创建讲师, 创建班级,创建课程
7. 上面的操作产生的数据都通过pickle序列化保存到文件里
二.程序设计原理图
三.代码实现
工程的创建按照较为简便的格式来描述,大致目录结构如图:
选课系统/
|-- bin
| |-- __init__.py
|
|-- db/
| |--database/
| |-- 北京电影学院.pickle
| |-- 上海戏剧学院.pickle
| |-- __init__.py
|
|-- core/
| |-- __init__.py
| |-- db_handle.py
| |-- db_opt.py
| |-- main.py
| |-- school_class.py
|
|-- conf/
| |-- __init_py.py
| |-- setting.py
|
|-- __init__.py
|-- requirements.txt
|-- README
setting.py程序代码如下:
1 import os 2 import sys 3 4 BASE_DIR = os.path.abspath(r'..') 5 6 # print(os.environ) 7 8 DATA_BASE ={ 9 'engine' :'file_storage', 10 'suffix':'pickle', 11 'name': 'database', 12 'path':'%s\db'%BASE_DIR 13 } 14 15 # print(DATA_BASE['path'])
db_handle.py程序代码如下:
1 ''' 2 handle all the database interactions 3 ''' 4 5 def file_handle_db(conn_param): 6 ''' 7 parse the db file path 8 :param conn_param: the db connection params set in settings 9 :return: file path 10 ''' 11 db_path = '%s'%conn_param['path'] 12 return db_path 13 14 def mysql_db_handle(conn_parms): 15 ''' 16 17 :param conn_parms: 18 :return: 19 ''' 20 pass 21 22 def db_handle(conn_param): 23 ''' 24 prase all db type 25 :param conn_param: db config 26 :return: 27 ''' 28 if conn_param['engine'] == 'file_storage': 29 return file_handle_db(conn_param) 30 elif conn_param['engine'] == 'mysql': 31 return mysql_db_handle(conn_param) 32 else: 33 pass
db_opt.py程序代码如下:
1 import pickle 2 import json 3 import os 4 5 from 课后作业.选课系统.conf import setting 6 from 课后作业.选课系统.core import db_handle 7 8 9 def file_opt_read(account_file,conn_params): 10 ''' 11 use pickle to load data 12 :param account_file: file path 13 :param conn_params: data_base config 14 :return: file data 15 ''' 16 with open(account_file, 'rb') as f: 17 if conn_params['suffix'] == 'pickle': 18 account_data = pickle.load(f) 19 elif conn_params['suffix'] == 'json': 20 account_data = json.load(f) 21 else: 22 return 23 return account_data 24 25 26 def file_opt_wtite(school_file,account_data,conn_params): 27 ''' 28 use pickle to dump data 29 :param school_file: file path 30 :param account_data: jump data 31 :param conn_params: data_base config 32 :return: 33 ''' 34 with open(school_file, 'wb') as f: 35 if conn_params['suffix'] == 'pickle': 36 acc_data = pickle.dump(account_data, f) 37 elif conn_params['suffix'] == 'json': 38 acc_data = json.dump(account_data, f) 39 return True 40 41 42 def database_read(name): 43 ''' 44 to read school data from database 45 :param name: file name 46 :return: 47 ''' 48 db_path = db_handle.db_handle(setting.DATA_BASE)#获取路径 49 account_file = "%s\%s\%s.%s" % (db_path,setting.DATA_BASE['name'],name,setting.DATA_BASE['suffix']) 50 if os.path.isfile(account_file): 51 return file_opt_read(account_file,setting.DATA_BASE) 52 else: 53 return False 54 55 56 57 def database_write(account_data): 58 ''' 59 after updated transaction or account data , dump it back to file db 60 :param account_data: 61 :return: 62 ''' 63 db_path = db_handle.db_handle(setting.DATA_BASE) 64 school_file = "%s/%s/%s.%s" %(db_path,setting.DATA_BASE['name'],account_data.name,setting.DATA_BASE['suffix']) 65 return file_opt_wtite(school_file,account_data,setting.DATA_BASE)
school_class.py程序代码如下:
1 from 课后作业.选课系统.core import db_opt 2 3 class Course(object): 4 ''' 5 create a course class 6 :param name:Course name price: Course price time :Course learning cycle 7 :return: 8 ''' 9 def __init__(self,name,price,time): 10 self.name = name 11 self.price = price 12 self.time = time 13 14 def tell(self): 15 print(''' 16 --- Info of Course [%s] --- 17 Name = %s 18 Price = %s 19 Time = %s 20 '''%(self.name,self.name,self.price,self.time)) 21 22 class Class(object): 23 ''' 24 create a class class 创建一个班级类 25 :param name:class name ,course:a course class ,teacher:a teacher class 26 :return: 27 ''' 28 def __init__(self,name,course,teacher): 29 self.name = name 30 self.course = course 31 self.teacher = teacher 32 self.student = [] 33 def tell(self): 34 print(''' 35 --- Info of %s --- 36 Class :%s 37 Course :%s 38 Teacher :%s 39 '''%(self.name,self.name,self.course,self.teacher)) 40 41 class School(object): 42 ''' 43 create a school class 创建一个班级类 44 :param name:school name ,addr:school addr ,teachers[]:a list save in memory that info of teachers be hired 45 :param students[]:a list save in memory that info of students be enrolled 46 :param courses[]:a list save in memory that info of courses be created 47 :param classes[]:a list save in memory that info of classes be created 48 :return: 49 ''' 50 def __init__(self,name,addr): 51 self.name = name 52 self.addr = addr 53 self.teachers = [] 54 self.students = [] 55 self.courses = [] 56 self.classes = [] 57 def tell(self): 58 print(''' 59 --- Info of School :%s --- 60 Name : %s 61 Addr : %s 62 '''%(self.name,self.name,self.addr)) 63 64 def hire(self,teacher,salary): 65 teacher.school = self.name 66 teacher.salary =salary 67 self.teachers.append(teacher) 68 print("%s has hire %s to be a teacher"%(self.name,teacher.name)) 69 70 def enroll(self,student,student_class): 71 self.students.append(student) 72 student_class.student.append(student) 73 student.choose_school(self.name) 74 student.choose_class(student_class) 75 print("%s has enroll %s to be a student"%(self.name,student.name)) 76 77 def create_course(self,course_name,price,time):#创建课程类 78 self.courses.append(Course(course_name,price,time)) 79 print("%s has creat course[%s]"%(self.name,course_name)) 80 81 def create_class(self,Class_name,course,teacher): 82 info = Class(Class_name,course.name,teacher.name) 83 self.classes.append(info) 84 teacher.Class.append(info) 85 86 class SchoolMember(object): 87 ''' 88 it's a base class include of teacher and student 89 :param 90 :return 91 ''' 92 def __init__(self,name,age,sex): 93 self.name = name 94 self.age = age 95 self.sex = sex 96 def tell(self):#个人信息,子类来完善 97 pass 98 99 class Teacher(SchoolMember): 100 ''' 101 it's a subclass class Inheritance by SchoolMember to create a Teacher object 102 :param 103 :return 104 ''' 105 def __init__(self,name,age,sex,course,salary='null',school='null'): 106 super(Teacher,self).__init__(name,age,sex) 107 self.salary = salary 108 self.course = course 109 self.school = school 110 self.Class =[] 111 112 def tell(self): 113 print(''' 114 --- Info of %s --- 115 Name = %s 116 Age = %s 117 Sex = %s 118 Salary = %s 119 Course = %s 120 Shool = %s 121 '''%(self.name,self.name,self.age,self.sex,self.salary,self.course,self.school)) 122 123 class Student(SchoolMember): 124 ''' 125 it's a subclass class Inheritance by SchoolMember to create a Student object 126 :param 127 :return 128 ''' 129 def __init__(self, name, age, sex,school='null',grade='null',Class='null',tuition = False): 130 super(Student, self).__init__(name, age, sex) 131 self.__school = school 132 self.grade = grade 133 self.__Class = Class 134 self.__tuition = tuition 135 136 def choose_school(self,name): 137 self.__school = name 138 139 def choose_grade(self,grade): 140 self.grade = grade 141 print("%s grade change success !!!"%self.name) 142 143 def choose_class(self,Class): 144 self.__Class = Class.name 145 print("%s choose class success !!!"%self.name) 146 147 def tuition(self): 148 self.__tuition = True 149 print("%s tuituin success !!!"%self.name) 150 151 def tell(self): 152 print(''' 153 --- Info of %s --- 154 Name = %s 155 Age = %s 156 Sex = %s 157 School = %s 158 Class = %s 159 Grade = %s 160 tuition = %s 161 '''%(self.name,self.name,self.age,self.sex,self.__school,self.__Class,self.__grade,self.__tuition))
main.py程序代码如下:
1 from 课后作业.选课系统.core import db_opt 2 from 课后作业.选课系统.core import school_class 3 4 5 def creat_school(): 6 ''' 7 create a school class 8 :return: 9 ''' 10 name = input('please input the school name:').strip() 11 addr = input('Plsase input the school addr:').strip() 12 13 School = school_class.School(name,addr) 14 if(db_opt.database_write(School)): 15 print("\033[31;1mSchool [%s] has be created!\033[0m"%School.name) 16 return School 17 else: 18 return False 19 20 def read_school_info(school): 21 ''' 22 load school information from data base 23 :param school: school name 24 :return: school data 25 ''' 26 return db_opt.database_read(school) 27 def write_school_info(school): 28 ''' 29 dump school information from data base 30 :param school: school name 31 :return: school data 32 ''' 33 return db_opt.database_write(school) 34 35 ''' 36 student interface 37 ''' 38 def student_enroll(school): 39 ''' 40 to handle the student enroll 41 :param school: a school class 42 :return: 43 ''' 44 name = input("please input the student name : ") 45 age = input("please input the student age : ") 46 sex = input("please input the student sex : ") 47 print("---------info of class----------") 48 for i,info in enumerate(school.classes): 49 print('%s. %s %s %s '%(i+1,info.name,info.course,info.teacher)) 50 student_num = int(input((">>:")).strip()) - 1 51 52 student= school_class.Student(name,age,sex)#creat a student class 53 school.enroll(student,school.classes[student_num])#enroll a student 54 write_school_info(school) 55 def student_pay(school): 56 ''' 57 to handle the student tuition 58 :param school: a school class 59 :return: 60 ''' 61 name = input("please input student name : ") 62 for info in school.students: 63 if info.name == name: 64 info.tuition() 65 write_school_info(school) 66 def student_choose_class(school): 67 pass 68 def studnt_view(school): 69 ''' 70 student interface 71 :param school: a school class 72 :return: 73 ''' 74 menu = u''' 75 ------- Bank --------- 76 \033[32;1m 77 1. 注册 78 2. 缴费 79 3. 选择班级 80 4. 退出 81 \033[0m''' 82 menu_dic = { 83 '1': student_enroll, 84 '2': student_pay, 85 '3': student_choose_class, 86 } 87 exit_flag = False 88 while not exit_flag: 89 print(menu) 90 user_option = input(">>:").strip() 91 if user_option in menu_dic: 92 menu_dic[user_option](school) 93 else: 94 print("\033[31;1mOption does not exist!\033[0m") 95 96 ''' 97 teacher interface 98 ''' 99 def teacher_teach(school): 100 ''' 101 handle the interaction of teacher choose class to teach 102 :param school: a school class 103 :return: 104 ''' 105 name = input("please input your name : ").strip() 106 print("------class info------") 107 for info in school.teachers: 108 if info.name == name : 109 for i,index in enumerate(info.Class): 110 print("%s. %s "%(i+1,index.name)) 111 112 user_option = int(input(">>:").strip())-1 113 print("%s has teach course[%s] in class[%s]"%(school.teachers[user_option].name,school.teachers[user_option].course,school.teachers[user_option].Class[user_option].name)) 114 def teacher_student_view(school): 115 ''' 116 a interface for teacher to view student 117 :param school: a school class 118 :return: 119 ''' 120 print("-----------info of classes----------") 121 for i,info in enumerate(school.classes): 122 print("%s. %s"%(i+1,info.name)) 123 user_option = int(input(">>:").strip())-1 124 for info in school.classes[user_option].student: 125 print(info.name) 126 def teacher_change_grade(school): 127 ''' 128 a interface for teacher to change student grade 129 :param school: a school class 130 :return: 131 ''' 132 print("-----------info of classes----------") 133 for i,info in enumerate(school.classes): 134 print("%s. %s"%(i+1,info.name)) 135 user_option = int(input(">>:").strip())-1 136 for i,info in enumerate(school.classes[user_option].student): 137 print("%s. %s %s "%(i+1,info.name,info.grade)) 138 stu_option = int(input(">>:").strip()) - 1 139 grade= int(input("pelase input the Student %s new grade : "%(school.classes[user_option].student[stu_option].name)).strip()) 140 school.classes[user_option].student[stu_option].choose_grade(grade) 141 write_school_info(school) 142 def teacher_view(school): 143 ''' 144 teacher interface 145 :param school: a school class 146 :return: 147 ''' 148 menu = u''' 149 ------- Bank --------- 150 \033[32;1m 151 1. 上课 152 2. 查看成员 153 3. 修改成绩 154 4. 退出 155 \033[0m''' 156 menu_dic = { 157 '1': teacher_teach, 158 '2': teacher_student_view, 159 '3': teacher_change_grade, 160 } 161 exit_flag = False 162 while not exit_flag: 163 print(menu) 164 user_option = input(">>:").strip() 165 if user_option in menu_dic: 166 menu_dic[user_option](school) 167 else: 168 print("\033[31;1mOption does not exist!\033[0m") 169 170 171 172 173 ''' 174 manage interface 175 ''' 176 def hire_teacher(school): 177 ''' 178 to hire a teacher by school 179 :param school: a school class 180 :return: true 181 ''' 182 name = input("please input the teacher name : ") 183 age = input("please input the teacher age : ") 184 sex = input("plesse input the teacher sex : ") 185 course = input("please input the teach course : ") 186 salary = input("please input the teacher salary : ") 187 teacher = school_class.Teacher(name,age,sex,course) 188 school.hire(teacher,salary) 189 write_school_info(school) 190 return True 191 def create_class(school): 192 ''' 193 to create a class(班级) by school 194 :param school: a school class 195 :return: true 196 ''' 197 classname = input("please input the class name : ") 198 print("----------info of course-----------") 199 for i,info in enumerate(school.courses): 200 print("%s. %s"%(i+1,info.name)) 201 course_num = int(input((">>:")).strip())-1 202 print("----------info of teacher----------") 203 for i,info in enumerate(school.teachers): 204 print("%s. teacher name:%s course:%s"%(i+1,info.name,info.course)) 205 teacher_num = int(input((">>:")).strip())-1 206 207 school.create_class(classname,school.courses[course_num],school.teachers[teacher_num]) 208 write_school_info(school) 209 def create_course(school): 210 ''' 211 to create a course by school 212 :param school: a school class 213 :return: true 214 ''' 215 name = input("please input the course name : ").strip() 216 price = input("please input the course price : ").strip() 217 time = input("please input the course time : ").strip() 218 school.create_course(name,price,time) 219 write_school_info(school) 220 def manage_view(school): 221 ''' 222 manage interface 223 :param school: 224 :return: 225 ''' 226 menu = u''' 227 ------- Bank --------- 228 \033[32;1m 229 1. 创建讲师 230 2. 创建班级 231 3. 创建课程 232 4. 退出 233 \033[0m''' 234 menu_dic = { 235 '1': hire_teacher, 236 '2': create_class, 237 '3': create_course, 238 } 239 exit_flag = False 240 while not exit_flag: 241 print(menu) 242 user_option = input(">>:").strip() 243 if user_option in menu_dic: 244 menu_dic[user_option](school) 245 else: 246 print("\033[31;1mOption does not exist!\033[0m") 247 248 249 def main(): 250 ''' 251 interact with user 252 :return: 253 ''' 254 255 choose = input("please input your school: \n1.北京电影学院\n2.上海戏剧学院\n>> :") 256 257 if choose == '1': 258 school = read_school_info("北京电影学院") 259 elif choose == '2': 260 school = read_school_info("上海戏剧学院") 261 262 menu = u''' 263 ------- Bank --------- 264 \033[32;1m 265 1. 学员视图 266 2. 讲师视图 267 3. 管理视图 268 4. 退出 269 \033[0m''' 270 menu_dic = { 271 '1': studnt_view, 272 '2': teacher_view, 273 '3': manage_view, 274 } 275 exit_flag = False 276 while not exit_flag: 277 print(menu) 278 user_option = input(">>:").strip() 279 if user_option in menu_dic: 280 menu_dic[user_option](school) 281 else: 282 print("\033[31;1mOption does not exist!\033[0m") 283 284 285 286 main()