Python 程序:选课系统

Python 程序:选课系统


角色:学校、学员、课程、讲师
要求:
1. 创建北京、上海 2 所学校
2. 创建linux , python , go 3个课程 , linuxpy 在北京开, go 在上海开
3. 课程包含,周期,价格,通过学校创建课程
4. 通过学校创建班级, 班级关联课程、讲师
5. 创建学员时,选择学校,关联班级
5. 创建讲师角色时要关联学校,
6. 提供两个角色接口
6.1 学员视图, 可以注册, 交学费, 选择班级,
6.2 讲师视图, 讲师可管理自己的班级, 上课时选择班级, 查看班级学员列表 , 修改所管理的学员的成绩
6.3 学校视图,创建讲师, 创建班级,创建课程
7. 上面的操作产生的数据都通过jison序列化保存到文件里


一、需求分析

程序最终目的是展示三个视图,每个视图有自己的功能:

需要定义的类:

二、目录结构

 

 三、程序

1 import sys,os
2 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) #程序主目录:/选课系统/
3 sys.path.append(BASE_DIR)  #添加环境变量
4 from core import main
5 if __name__ == '__main__':
6     a = main.run()
7     a.interactive()
run
  1 #!/usr/bin/env python
  2 # -*- coding:utf-8 -*-
  3 
  4 import sys,os,json
  5 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) #程序主目录:/选课系统/
  6 sys.path.append(BASE_DIR)  #添加环境变量
  7 db_DIR = BASE_DIR + r"db"
  8 
  9 class base_class(object):
 10     '''基础类,用来进行文件的读写操作'''
 11     def write(self,dir_name,type,dict):
 12         filename = dir_name
 13         file_path = "%s\%s" %(db_DIR,type)
 14         ab_file = "%s\%s" %(file_path,filename)
 15         with open(ab_file, "w", encoding="UTF-8") as f:
 16             json.dump(dict, f)
 17             if True:
 18                 print("-------",type,"创建成功","-------")
 19                 for key in dict:
 20                     print(key,":	",dict[key])
 21     def extend(self,dir_name,type,list):
 22         filename = dir_name
 23         file_path = "%s\%s" %(db_DIR,type)
 24         ab_file = "%s\%s" %(file_path,filename)
 25         with open(ab_file, "r", encoding="UTF-8") as f:
 26             file_list = json.load(f)
 27         file_list.extend(list)
 28         with open(ab_file, "w", encoding="UTF-8") as f:
 29             # json.dump(list, f)
 30             json.dump(file_list, f)
 31             if True:
 32                 print("-------",type,"创建成功","-------")
 33                 for i in list:
 34                     for key in i:
 35                         print(key,i[key])
 36                     print("
")
 37         return True
 38     def read(self,type):
 39         data = []
 40         db_path = "%s\%s" %(db_DIR,type)
 41         for i in os.listdir(db_path):
 42             db_file = os.path.join(db_path,i)
 43             with open(db_file, "r", encoding="UTF-8") as f:
 44                 file_dict = json.load(f)
 45                 data.append(file_dict)
 46                 pass
 47         return data
 48 
 49 class admin(base_class):
 50     '''管理类:创建学校、招聘讲师、招收学员、创建班级、创建课程'''
 51     def __init__(self):
 52         pass
 53     def create_school(self):
 54         school_dict = {}
 55         school_name = input("校名:")
 56         school_address = input("地址:")
 57         school_dict["校名"] = school_name
 58         school_dict["地址"] = school_address
 59         print(school_dict)
 60         base_class.write(self,school_name, "school", school_dict)
 61     def read_school(self):
 62         list = base_class.read(self,"school")
 63         return list
 64     def create_teacher(self):
 65         teacher_dict = {}
 66         teacher_name = input("教师名字:")
 67         teacher_salary = input("工资:")
 68         teacher_course = input("教授课程:")
 69         teacher_school = input("所属学校:")
 70         teacher_dict["教师名字"] = teacher_name
 71         teacher_dict["工资"] = teacher_salary
 72         teacher_dict["教授课程"] = teacher_course
 73         teacher_dict["所属学校"] = teacher_school
 74         print(teacher_dict)
 75         base_class.write(self,teacher_name, "teacher", teacher_dict)
 76     def read_teacher(self):
 77         list = base_class.read(self,"teacher")
 78         return list
 79     def create_student(self):
 80         student_dict = {}
 81         student_name = input("学员姓名:")
 82         student_id = input("学员学号:")
 83         student_school = input("所属学校:")
 84         student_classes = input("学员班级:")
 85         student_course  = input("课程名:")
 86         student_dict["姓名"] = student_name
 87         student_dict["学号"] = student_id
 88         student_dict["学校"] = student_school
 89         student_dict["班级"] = student_classes
 90         student_dict["课程名"] = student_course
 91         base_class.write(self,student_name, "student", student_dict)
 92     def read_student(self):
 93         list = base_class.read(self,"student")
 94         return list
 95     def create_classes(self):
 96         classes_dict = {}
 97         classes_name = input("班级号:")
 98         classes_course = input("所学课程:")
 99         classes_student = input("学习的学员:")
100         classes_teacher = input("负责的讲师:")
101         classes_dict["班级名"] = classes_name
102         classes_dict["课程"] = classes_course
103         classes_dict["学习的学员"] = classes_student
104         classes_dict["负责讲师"] = classes_teacher
105         base_class.write(self,classes_name, "classes", classes_dict)
106     def read_classes(self):
107         list = base_class.read(self,"classes")
108         return list
109     def create_course(self):
110         course_dict = {}
111         course_name = input("课程名:")
112         course_price = input("价格:")
113         course_period = input("周期:")
114         course_dict["课程名"] = course_name
115         course_dict["价格"] = course_price
116         course_dict["周期"] = course_period
117         base_class.write(self,course_name, "course", course_dict)
118     def read_course(self):
119         list = base_class.read(self,"course")
120         return list
121 
122 class school(base_class):
123     '''学校类:名称、地址'''
124     def __init__(self,school_name,school_address):
125         self.school_name = school_name
126         self.school_address = school_address
127 
128 class teacher(base_class):
129     '''教师类:名称、工资、课程、创建学生成绩、创建上课记录、查看学生成绩、查看学生上课记录、查个人信息'''
130     def __init__(self,teacher_name,teacher_salary,teacher_course,teacher_school):
131         self.teacher_name =teacher_name
132         self.teacher_salary = teacher_salary
133         self.teacher_course = teacher_course
134         self.teacher_school = teacher_school
135     def create_class_record(self): #上课记录
136         class_record = []
137         student_school = input("选择学校:")
138         student_classes = input("选择班级:")
139         student_times = input("课次:")
140         student_list = base_class.read(self,"student")
141         for i in student_list:
142             if i["学校"] == student_school and i["班级"] == student_classes:
143                 student_name = i["姓名"]
144                 student_status = input("%s 上课情况:" % student_name)
145                 i["上课情况"] = student_status
146                 i["课次"] = student_times
147                 class_record.append(i)
148         base_class.extend(self,"class_record.json","class_record",class_record)
149     def create_student_results(self): #创建学生成绩
150         student_results = []
151         student_school = input("选择学校:")
152         student_classes = input("选择班级:")
153         student_times = input("课次:")
154         student_list = base_class.read(self,"student")
155         for i in student_list:
156             if i["学校"] == student_school and i["班级"] == student_classes:
157                 student_name = i["姓名"]
158                 student_grade = input("%s 成绩:" % student_name)
159                 i["成绩"] = student_grade
160                 i["课次"] = student_times
161                 student_results.append(i)
162         base_class.extend(self,"student_results.json","student_results",student_results)
163     def check_class_record(self): #查看上课记录
164         record_list = []
165         student_school = input("校名:")
166         student_class = input("班级:")
167         student_times = input("课次:")
168         class_record_list = base_class.read(self, "class_record")
169         for i in class_record_list:
170             for j in i:
171                 if j["学校"] == student_school and j["班级"] == student_class and j["课次"] == student_times:
172                     record_list.append(j)
173         for i in record_list:
174             for key in i:
175                 print(key,i[key])
176             print("
")
177     def check_student_results(self): #查看学生成绩
178         grade_list = []
179         student_school = input("校名:")
180         student_class = input("班级:")
181         student_times = input("课次:")
182         student_results_list = base_class.read(self, "student_results")
183         for i in student_results_list:
184             for j in i:
185                 if j["学校"] == student_school and j["班级"] == student_class and j["课次"] == student_times:
186                     grade_list.append(j)
187         for i in grade_list:
188             for key in i:
189                 print(key,i[key])
190             print("
")
191     def check_self_info(self): #查看个人信息
192         print('''----info of Teacher:%s ----
193         Name:%s
194         Salary:%s
195         Course:%s
196         School:%s
197         '''%(self.teacher_name,self.teacher_name,self.teacher_salary,self.teacher_course,self.teacher_school)
198         )
199 
200 class student(base_class):
201     '''学生类:名称,id,注册,交学费、查成绩、查上课记录、查个人信息'''
202     def __init__(self,student_name,student_id,student_school, student_classes,student_course):
203         self.student_name =student_name
204         self.student_id = student_id
205         self.student_school = student_school
206         self.student_classes = student_classes
207         self.student_course = student_course
208 
209     def student_registered(self): #注册
210         student_dict = {}
211         student_dict["姓名"] = self.student_name
212         student_dict["学号"] = self.student_id
213         student_dict["学校"] = self.student_school
214         student_dict["班级"] = self.student_classes
215         student_dict["课程名"] = self.student_course
216         base_class.write(self,self.student_name, "student", student_dict)
217 
218     def pay_tuition(self): #交学费
219         course_list = base_class.read(self,"course")
220         print(course_list)
221         for i in course_list:
222             if i["课程名"] == self.student_course:
223                 print("课程价格为:¥%s"%i["价格"])
224                 money = input("提交金额:")
225                 if i["价格"] == money:
226                     print("%s has paid tuition for ¥%s"%(self.student_name,i["价格"]))
227                 else:
228                     print("金额错误!")
229 
230     def check_student_record(self): #查看学生上课记录
231         student_school = input("校名:")
232         student_class = input("班级:")
233         student_times = input("课次:")
234         student_name = input("姓名:")
235         class_record_list = base_class.read(self,"class_record")
236         for i in class_record_list:
237             for j in i:
238                 if j["学校"] == student_school and j["班级"] == student_class and j["课次"] == student_times 
239                     and j["姓名"] == student_name:
240                     for key in j:
241                         print(key,j[key])
242                     print("
")
243 
244     def check_student_results(self): #查看学生成绩
245         student_school = input("校名:")
246         student_class = input("班级:")
247         student_times = input("课次:")
248         student_name = input("姓名:")
249         student_results_list = base_class.read(self,"student_results")
250         for i in student_results_list:
251             for j in i:
252                 if j["学校"] == student_school and j["班级"] == student_class and j["课次"] == student_times 
253                     and j["姓名"] == student_name:
254                     for key in j:
255                         print(key,j[key])
256                     print("
")
257 
258 
259     def check_self_info(self): #查看个人信息
260         pass
261 
262 class classes(base_class):
263     '''班级类:名称,课程,学生,老师'''
264     def __init__(self,classes_name,classes_course,classes_student,classes_teacher):
265         self.classes_name = classes_name
266         self.classes_course = classes_course
267         self.classes_student = classes_student
268         self.classes_teacher = classes_teacher
269 
270 class course(base_class):
271     '''课程类:名称、价格、周期'''
272     def __init__(self,course_name,course_price,course_period):
273         self.course_name = course_name
274         self.course_price = course_price
275         self.course_period = course_period
276 
277 class school_view(admin):
278     '''学校管理视图'''
279     def auth(self,username,password):
280         if username == "admin" and password == "admin":
281             return True
282         else:
283             print("用户名或密码错误!")
284     def login(self):
285         menu = u'''
286         ------- 欢迎进入学校视图 ---------
287             33[32;1m 1.  校区管理
288             2.  讲师管理
289             3.  学员管理
290             4.  课程管理
291             5.  返回
292             33[0m'''
293         menu_dic = {
294             '1': school_view.school_manager,
295             '2': school_view.teacher_manager,
296             '3': school_view.student_manager,
297             '4': school_view.course_manager,
298             '5': "logout",
299         }
300         username = input("输入用户名:").strip()
301         password = input("输入密码:").strip()
302         auth = school_view.auth(self,username,password)
303         if auth:
304             exit_flag = False
305             while not exit_flag:
306                 print(menu)
307                 option = input("请选择:").strip()
308                 if option in menu_dic:
309                     if int(option) == 5:
310                         exit_flag = True
311                     else:
312                         menu_dic[option](self)
313                 else:
314                     print("33[31;1m输入错误,重新输入33[0m")
315     def school_manager(self):
316         exit_flag = False
317         while not exit_flag:
318             print("""
319                 ------- 欢迎进入校区管理 ---------
320                 33[32;1m1、  创建校区
321                 2、  创建班级
322                 3、 查看学校
323                 4、 查看班级
324                 5、  返回
325                 33[0m
326             """)
327             option = input("请选择:").strip()
328             if int(option) == 1:
329                 admin.create_school(self)
330             elif int(option) == 2:
331                 admin.create_classes(self)
332             elif int(option) == 3:
333                 list = admin.read_school(self)
334                 for i in list:
335                     print(i)
336             elif int(option) == 4:
337                 list = admin.read_classes(self)
338                 for i in list:
339                     print(i)
340             else:
341                 exit_flag = True
342     def teacher_manager(self):
343         exit_flag = False
344         while not exit_flag:
345             print("""
346                 ------- 欢迎进入讲师管理 ---------
347                 33[32;1m 1.  创建讲师
348                 2.  查看讲师信息
349                 3.  返回
350                 33[0m
351             """)
352             option = input("请选择:").strip()
353             if int(option) == 1:
354                 admin.create_teacher(self)
355             elif int(option) == 2:
356                 list = admin.read_teacher(self)
357                 for i in list:
358                     print(i)
359             else:
360                 exit_flag = True
361     def student_manager(self):
362         exit_flag = False
363         while not exit_flag:
364             print("""
365                 ------- 欢迎进入学员管理 ---------
366                 33[32;1m 1.  创建学员
367                 2.  查看学员
368                 3.  返回
369                 33[0m
370             """)
371             option = input("请选择:").strip()
372             if int(option) == 1:
373                 admin.create_student(self)
374             elif int(option) == 2:
375                 list = admin.read_student(self)
376                 for i in list:
377                     print(i)
378             else:
379                 exit_flag = True
380     def course_manager(self):
381         exit_flag = False
382         while not exit_flag:
383             print("""
384                 ------- 欢迎进入课程管理 ---------
385                 33[32;1m 1.  创建课程
386                 2.  查看课程
387                 3.  返回
388                 33[0m
389             """)
390             option = input("请选择:").strip()
391             if int(option) == 1:
392                 admin.create_course(self)
393             elif int(option) == 2:
394                 list = admin.read_course(self)
395                 for i in list:
396                     print(i)
397             else:
398                 exit_flag = True
399 
400 class teacher_view(object):
401     '''讲师视图'''
402     def auth(self,name):
403         list = admin.read_teacher(self)
404         for i in list:
405             if i["教师名字"] == name:
406                 return i
407     def login(self):
408         menu = u'''
409         ------- 欢迎进入讲师视图 ---------
410             33[32;1m  1.  创建上课记录
411             2.  创建学员成绩
412             3.  查看学员上课记录
413             4.  查看学员成绩
414             5.  返回
415             33[0m'''
416         name = input("教师名称:")
417         auth = teacher_view.auth(self,name)
418         if auth is not None:
419             teacher_name = auth["教师名字"]
420             teacher_salary = auth["工资"]
421             teacher_course = auth["教授课程"]
422             teacher_school = auth["所属学校"]
423             t1 = teacher(teacher_name,teacher_salary,teacher_course,teacher_school)
424             t1.check_self_info()
425             menu_dic = {
426             '1': t1.create_class_record,
427             '2': t1.create_student_results,
428             '3': t1.check_class_record,
429             '4': t1.check_student_results,
430             '5': "logout",
431             }
432             if True:
433                 exit_flag = False
434                 while not exit_flag:
435                     print(menu)
436                     option = input("请选择:").strip()
437                     if option in menu_dic:
438                         if int(option) == 5:
439                             exit_flag = True
440                         else:
441                             menu_dic[option]()
442                     else:
443                         print("33[31;1m输入错误,重新输入33[0m")
444         else:
445             print("不存在此教师")
446 
447 class student_view(object):
448     '''学生视图'''
449     def auth(self,name):
450         list = admin.read_student(self)
451         for i in list:
452             if i["姓名"] == name:
453                 return i
454     def login(self):
455         menu = u'''
456         ------- 欢迎进入学生管理视图 ---------
457         33[32;1m 1.  交学费
458         2.  查看上课记录
459         3.  查看作业成绩
460         4.  返回
461         33[0m'''
462         name = input("姓名:")
463         auth = student_view.auth(self,name)
464         if auth is not None:
465             print(auth)
466             student_name = auth["姓名"]
467             student_id = auth["学号"]
468             student_school = auth["学校"]
469             student_classes = auth["班级"]
470             student_course = auth["课程名"]
471             st1 = student(student_name,student_id,student_school,student_classes,student_course)
472             st1.check_self_info()
473             menu_dic = {
474             '1': st1.pay_tuition,
475             '2': st1.check_student_record,
476             '3': st1.check_student_results,
477             '4': "logout"}
478             if True:
479                 exit_flag = False
480                 while not exit_flag:
481                     print(menu)
482                     option = input("请选择:").strip()
483                     if option in menu_dic:
484                         if int(option) == 4:
485                             exit_flag = True
486                         else:
487                             menu_dic[option]()
488                     else:
489                         print("33[31;1m输入错误,重新输入33[0m")
490         else:
491             choice = input("33[32;1m不存在此学生是否注册Y/N:33[0m")
492             if choice == "Y":
493                 admin.create_student(self)
494             else:
495                 print("33[31;1m谢谢使用!33[0m")
496 
497 class run(object):
498     '''运行'''
499     def interactive(self):
500         menu = u'''
501 ------- 欢迎进入选课系统 ---------
502     33[32;1m 1.  学生视图
503     2.  讲师视图
504     3.  学校视图
505     4.  退出
506         33[0m'''
507         menu_dic = {
508             '1': student_view,
509             '2': teacher_view,
510             '3': school_view,
511             '4': "logout"}
512         exit_flag = False
513         while not exit_flag:
514             print(menu)
515             option_view = input("请选择视图:").strip()
516             if option_view in menu_dic:
517                 if int(option_view) == 4:
518                     exit_flag = True
519                 else:
520                     menu_dic[option_view].login(self)
521             else:
522                 print("33[31;1m输入错误,重新输入33[0m")
main
原文地址:https://www.cnblogs.com/hy0822/p/9195556.html