# 选课系统# 角色:学校、学员、课程、讲师# 要求:# 1. 创建北京、上海 2 所学校# 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开# 3. 课程包含,周期,价格,通过学校创建课程# 4. 通过学校创建班级, 班级关联课程、讲师# 5. 创建学员时,选择学校,关联班级# 5. 创建讲师角色时要关联学校,# 6. 提供两个角色接口 # 6.1 学员视图, 可以登录,注册, 选择学校,选择课程,查看成绩 # 6.2 讲师视图, 讲师登录,选择学校,选择课程, 查看课程下学员列表 , 修改所管理的学员的成绩 # 6.3 管理视图,登录,注册,创建讲师, 创建班级,创建课程,创建学校# 7. 上面的操作产生的数据都通过pickle序列化保存到文件里分析:角色: 管理员: 注册 登录 创建校区 创建老师 创建课程 老师: 登录 选择教授课程 查看课程下学生 修改学生的成绩 学生: 注册 登录 选择校区 选择课程 查看成绩类: 抽出共有属性方法:Base 学校:School 学员:Student 课程:Course 讲师:Teacher各个类属性和方法:Base: 公用方法: 存数据:save 取数据:select学校: 属性: 学校名字:school_name str 学校地址:school_addr str 开设的课程:course_list list学员: 属性: 学员名字:student_name str 学员密码:student_pwd str 学员所属校区:school str 学员课程: course_list list 学员成绩: course_score dict 方法: 学员选择校区 choose_school(school_name) 列出所有校区的信息,学员选择 学员选择课程 choose_course(school_name) 学员必须先选择校区,列出该校区包含的课程 选择相应的课程,添加到学员课程列表中,并且将用户绑定给课程讲师: 属性: 讲师名字:teacher_name str 讲师密码:teacher_pwd str 讲师课程: course_list list 方法: 讲师选择课程: choose_course(school_name, course_name) 先选择校区,列出该校区所有课程,讲师选择课程,如果没有选过,则添加 讲师修改学生的成绩:change_student_score(course, student_name) 通过自身对象中的课程列表,选择相应课程,调用课程下的所有学生信息,更改学生成绩管理员: 属性: 管理员的名字:admin_name str 管理员的密码:admin_pwd str 方法: 管理员创建学校: create_school(school_name,school_addr) 直接输入学校的名字和地址,然后判断学校是否存在,不存在,则创建 管理员创建讲师: create_teacher(teacher_name) 直接创建讲师,密码为默认 管理员创建课程: create_course(school_name, course_name) 先选择校区,再创建课程名字,如果不存在,则创建这个课程那个角色登陆了就执行那个角色的功能进入到角色选项里面了可以随时退出下面是程序说明程图-------------------------------------------------------------
目录结构图
一 ,下面是业务逻辑代码
1.conf下settings.py下代码
1 import os 2 3 BASE_DIR = os.path.dirname(os.path.dirname(__file__)) 4 BASE_DB = os.path.join(BASE_DIR, 'db')
2.core下admin.py代码
1 from interface import student_interface, teacher_interface, admin_interface, common_interface 2 from lib import common 3 4 admin_info = { 5 'name': None 6 } 7 8 9 def admin_register(): 10 print('管理员注册') 11 if admin_info['name']: 12 print('管理员已经登陆了不能注册') 13 return 14 while True: 15 name = input('请输入名字:').strip() 16 password = input('请输入密码:').strip() 17 conf_password = input('请确认密码:').strip() 18 if password == conf_password: 19 flag, msg = admin_interface.admin_register_interface(name, password) 20 if flag: 21 print(msg) 22 break 23 else: 24 print(msg) 25 else: 26 print('两次密码不一致') 27 28 29 def admin_login(): 30 print('管理员登陆') 31 if admin_info['name']: 32 print('管理员已经登陆了不能重复登陆') 33 return 34 while True: 35 name = input('请输入名字: ').strip() 36 password = input('请输入密码: ').strip() 37 flag, msg = common_interface.login_interface(name, password, 'admin') 38 if flag: 39 admin_info['name'] = name 40 print(msg) 41 break 42 else: 43 print(msg) 44 45 46 @common.login_auth(user_type='admin') 47 def create_school(): 48 print('创建学校') 49 school_name = input('请输入学校名字: ').strip() 50 address = input('请输入学校地址: ').strip() 51 flag, msg = admin_interface.create_school_interface(admin_info['name'], school_name,address) 52 if flag: 53 print(msg) 54 else: 55 print(msg) 56 57 58 59 60 @common.login_auth(user_type='admin') 61 def create_teacher(): 62 print('创建老师') 63 name = input('请输入老师名字: ').strip() 64 flag, msg = admin_interface.create_teacher_interface(admin_info['name'], name) 65 if flag: 66 print(msg) 67 else: 68 print(msg) 69 70 71 72 73 @common.login_auth(user_type='admin') 74 def create_course(): 75 print('创建课程') 76 while True: 77 school_list = common_interface.check_all_schools() 78 if school_list: 79 for i, school in enumerate(school_list): 80 print('%s: %s' % (i, school)) 81 choose = input('请选择学校: ').strip() 82 if choose.isdigit(): 83 choose = int(choose) 84 if choose >= len(school_list): 85 continue 86 name = input('请输入课程名字:').strip() 87 flag, msg = admin_interface.create_course_interface(admin_info['name'], name, school_list[choose]) 88 if flag: 89 print(msg) 90 break 91 else: 92 print(msg) 93 94 95 96 97 func_dic = { 98 '1': admin_register, 99 '2': admin_login, 100 '3': create_school, 101 '4': create_teacher, 102 '5': create_course, 103 } 104 105 106 def admin_view(): 107 while True: 108 print(""" 109 1 注册 110 2 登陆 111 3 创建学校 112 4 创建老师 113 5 创建课程 114 """) 115 116 choice = input('请选择功能:').strip() 117 if choice == 'q' and 'Q': 118 break 119 120 if choice not in func_dic: 121 continue 122 123 func_dic[choice]()
3.core下student.py代码
1 from interface import student_interface, common_interface 2 from lib import common 3 4 student_info = { 5 'name': None 6 } 7 8 9 def student_register(): 10 print('学生注册') 11 if student_info['name']: 12 print('已经登陆了不能在注册了') 13 return 14 while True: 15 name = input('请输入学生名字: ').strip() 16 password = input('请输入学生密码').strip() 17 conf_password = input('请确认密码: ').strip() 18 if password == conf_password: 19 flag, msg = student_interface.student_register_interface(name, password) 20 if flag: 21 print(msg) 22 break 23 else: 24 print(msg) 25 else: 26 print('两次密码不一致') 27 28 29 def student_login(): 30 print('学生登陆') 31 if student_info['name']: 32 print('已经登陆了不能重复登陆') 33 return 34 while True: 35 name = input('请输入学生名字: ').strip() 36 password = input('请输入学生密码: ').strip() 37 flag, msg = common_interface.login_interface(name, password, 'student') 38 if flag: 39 print(msg) 40 student_info['name'] = name 41 break 42 else: 43 print(msg) 44 45 46 @common.login_auth('student') 47 def choose_school(): 48 print('选择学校') 49 school_list = common_interface.check_all_schools() 50 if not school_list: 51 print('请联系管理员创建学校') 52 return 53 for i, school in enumerate(school_list): 54 print('%s; %s' % (i, school)) 55 choice = input('请选择学校: ').strip() 56 if choice.isdigit(): 57 choice = int(choice) 58 if choice < len(school_list): 59 flag, msg = student_interface.choice_school_interface(student_info['name'], 60 school_list[choice]) 61 print(msg) 62 63 64 @common.login_auth('student') 65 def choose_course(): 66 print('选择课程') 67 flag, course_list = student_interface.get_can_choose_course_interface(student_info['name']) 68 if not flag: 69 print(course_list) 70 return 71 for i, course in enumerate(course_list): 72 print('%s: %s' % (i, course)) 73 choice = input('请选择课程').strip() 74 if choice.isdigit(): 75 choice = int(choice) 76 if choice > len(course_list): 77 flag, msg = student_interface.choose_course_interface(student_info['name'], course_list[choice]) 78 print(msg) 79 else: 80 print('请选择已经存在的课程') 81 else: 82 print('必须要输入数字') 83 84 85 @common.login_auth('student') 86 def check_score(): 87 print('查看分数') 88 course_score_dict = student_interface.check_score_interface(student_info['name']) 89 print(course_score_dict) 90 91 92 93 func_dic = { 94 '1': student_register, 95 '2': student_login, 96 '3': choose_school, 97 '4': choose_course, 98 '5': check_score, 99 } 100 101 102 def student_view(): 103 while True: 104 print(""" 105 1 注册 106 2 登陆 107 3 选择学校 108 4 选择课程 109 5 查看成绩 110 """) 111 112 choice = input('请选择功能:').strip() 113 if choice == 'q' and 'Q': 114 break 115 if choice not in func_dic: 116 continue 117 func_dic[choice]()
4.core下teacher.py代码
1 from interface import common_interface, teacher_interface 2 from lib import common 3 4 5 teacher_info = { 6 'name': None 7 } 8 9 10 def teacher_login(): 11 print('老师登陆') 12 if teacher_info['name']: 13 print('老师已经登陆了不能重复登陆') 14 return 15 while True: 16 name = input('请输入老师名字: ').strip() 17 password = input('请输入老师密码: ').strip() 18 flag, msg = common_interface.login_interface(name, password, 'teacher') 19 if flag: 20 print(msg) 21 teacher_info['name'] = name 22 break 23 else: 24 print(msg) 25 26 27 @common.login_auth('teacher') 28 def choose_teach_course(): 29 print('选择要教授的课程') 30 while True: 31 course_list = teacher_interface.get_all_course() 32 if not course_list: 33 print('现在还没有课程,请联系管理员创建课程') 34 break 35 else: 36 for i, course_list in enumerate(course_list): 37 print('%s; %s' % (i, course_list)) 38 choice = input('请选择课程: ').strip() 39 if choice.isdigit(): 40 choice = int(choice) 41 if choice >= len(course_list): continue 42 flag, msg = teacher_interface.choose_teacher_course_interface(teacher_info['name'], course_list[choice]) 43 if flag: 44 print(msg) 45 break 46 else: 47 print(msg) 48 else: 49 print('必须输入数字') 50 51 52 @common.login_auth('teacher') 53 def check_course(): 54 print('查看教授的课程') 55 course_list = teacher_interface.check_all_teacher_course(teacher_info['name']) 56 if course_list: 57 for course in course_list: 58 print(course) 59 else: 60 print('你还没有选择课程, 先去选择课程吧') 61 62 63 @common.login_auth('teacher') 64 def check_student(): 65 count = 0 66 print('查看学生') 67 course_list = teacher_interface.check_all_teacher_course(teacher_info['name']) 68 if course_list: 69 for i, course in enumerate(course_list): 70 print(i, course) 71 choice = input('选择要查看课程下的学生: ').strip() 72 if choice.isdigit(): 73 choice = int(choice) 74 if choice < len(course_list): 75 student_list = teacher_interface.check_student_in_course(course_list[choice]) 76 for j, student in enumerate(student_list): 77 count += j 78 print('%s : %s 一共有%s个学生' % (j, student, count)) 79 else: 80 print('请选择的课程') 81 else: 82 print('请输入数字') 83 84 85 @common.login_auth('teacher') 86 def modify_score(): 87 count = 0 88 print('查看学生') 89 course_list = teacher_interface.check_all_teacher_course(teacher_info['name']) 90 if course_list: 91 for i, course in enumerate(course_list): 92 print(i, course) 93 choice = input('选择要查看课程下的学生: ').strip() 94 if choice.isdigit(): 95 choice = int(choice) 96 if choice < len(course_list): 97 student_list = teacher_interface.check_student_in_course(course_list[choice]) 98 for j, student in enumerate(student_list): 99 count += j 100 print('%s : %s 一共有%s个学生' % (j, student, count)) 101 choose = input('请选择学生: ').strip() 102 if choose.isdigit(): 103 choose = int(choose) 104 if choose < len(student_list): 105 score = input('请输入学生要修改的的分数: ').strip() 106 if score.isdigit(): 107 score = int(score) 108 flag, msg = teacher_interface.modify_score(teacher_info['name'], 109 course_list[choice], 110 student_list[choose], score) 111 print(msg) 112 else: 113 print('分数必须输入数字') 114 else: 115 print('请选择存在的学生') 116 else: 117 print('请选择存在的课程') 118 else: 119 print('请输入数字') 120 else: 121 print('请选择课程') 122 123 124 func_dic = { 125 '1': teacher_login, 126 '2': choose_teach_course, 127 '3': check_course, 128 '4': check_student, 129 '5': modify_score, 130 } 131 132 133 def teacher_view(): 134 while True: 135 print(""" 136 1 登陆 137 2 选择课程 138 3 查看课程 139 4 查看学生 140 5 修改学生成绩 141 """) 142 143 choice = input('请选择功能:').strip() 144 if choice == 'q' and 'Q': 145 break 146 if choice not in func_dic: 147 continue 148 func_dic[choice]()
5.core下src.py代码
1 from core import admin, student, teacher 2 3 func_dic = { 4 '1': admin.admin_view, 5 '2': student.student_view, 6 '3': teacher.teacher_view 7 8 } 9 10 11 def run(): 12 while True: 13 print(""" 14 1 管理员视图 15 2 学生视图 16 3 老师视图 17 """) 18 choice = input('请选择:').strip() 19 if choice not in func_dic: 20 continue 21 func_dic[choice]()
二 ,下面是db文件代码
1.db_handler.py代码
1 from conf import settings 2 import os 3 import pickle 4 5 6 def save(obj): 7 path_dir = os.path.join(settings.BASE_DB, obj.__class__.__name__.lower()) 8 if not os.path.isdir(path_dir): 9 os.mkdir(path_dir) 10 path_obj = os.path.join(path_dir, obj.name) 11 with open(path_obj, 'wb') as f: 12 pickle.dump(obj, f) 13 14 15 def select(name, dir_type): 16 path_dir = os.path.join(settings.BASE_DB, dir_type) 17 if not os.path.isdir(path_dir): 18 os.mkdir(path_dir) 19 path_obj = os.path.join(path_dir, name) 20 if os.path.exists(path_obj): 21 with open(path_obj, 'rb')as f: 22 return pickle.load(f) 23 else: 24 return None
2.modles.py
1 from db import db_handler 2 3 4 class BaseClass: 5 def save(self): 6 db_handler.save(self) 7 8 @classmethod 9 def get_obj_by_name(cls, name): 10 return db_handler.select(name, cls.__name__.lower()) 11 12 13 class Admin(BaseClass): 14 def __init__(self, name, password): 15 self.name = name 16 self.password = password 17 self.save() 18 19 20 def create_school(self, school_name, addr): 21 School(school_name, addr) 22 23 24 def create_course(self, name): 25 Course(name) 26 27 28 def create_teacher(self, name, password): 29 Teacher(name, password) 30 31 32 class Teacher(BaseClass): 33 def __init__(self, name, password): 34 self.name = name 35 self.password = password 36 self.course_list = [] 37 self.save() 38 39 def add_course(self, course_name): 40 self.course_list.append(course_name) 41 self.save() 42 43 def modify_score(self, student, course_name, score): 44 student.score[course_name] = score 45 student.save() 46 47 48 class Student(BaseClass): 49 def __init__(self, name, password): 50 self.name = name 51 self.password = password 52 self.score = {} 53 self.school = None 54 self.course_list = [] 55 self.save() 56 57 def get_school(self): 58 return self.school 59 60 def choose_school(self, school_name): 61 self.school = school_name 62 self.save() 63 64 def add_course(self, course_name): 65 self.course_list.append(course_name) 66 self.score[course_name] = 0 67 self.save() 68 69 70 71 72 class School(BaseClass): 73 def __init__(self, name, addr): 74 self.name = name 75 self.addr = addr 76 self.course_list = [] 77 self.save() 78 79 def add_course(self, course_name): 80 self.course_list.append(course_name) 81 self.save() 82 83 84 85 class Course(BaseClass): 86 def __init__(self, name): 87 self.name = name 88 self.student_list = [] 89 self.save()
三 ,下面是接口api文件 interface
1.admin_interface
1 from db import modles 2 3 4 def admin_register_interface(name, password): 5 admin_obj = modles.Admin.get_obj_by_name(name) 6 if admin_obj: 7 return False, '管理员已经存在' 8 else: 9 modles.Admin(name, password) 10 return True, '注册成功' 11 12 13 def create_school_interface(admin_name, school_name, addr): 14 school_obj = modles.School.get_obj_by_name(school_name) 15 if school_obj: 16 return False, '学校已经存在' 17 else: 18 admin_obj = modles.Admin.get_obj_by_name(admin_name) 19 admin_obj.create_school(school_name, addr) 20 return True, '学校创建成功' 21 22 23 def create_teacher_interface(admin_name, name, password = '666'): 24 obj = modles.Teacher.get_obj_by_name(name) 25 if obj: 26 return False, '老师已经存在' 27 else: 28 admin_obj = modles.Admin.get_obj_by_name(admin_name) 29 admin_obj.create_teacher(name, password) 30 return True, '老师创建成功' 31 32 33 def create_course_interface(admin_name, course_name, school_name): 34 obj = modles.Course.get_obj_by_name(course_name) 35 if obj: 36 return False, '课程已经存在' 37 else: 38 admin_obj = modles.Admin.get_obj_by_name(admin_name) 39 admin_obj.create_course(course_name) 40 school_obj = modles.School.get_obj_by_name(school_name) 41 school_obj.add_course(course_name) 42 return True, '课程已经创建成功'
2.common_interface
1 from db import modles 2 import os 3 from conf import settings 4 from lib import common 5 6 7 def login_interface(name, password, user_type): 8 if user_type == 'admin': 9 obj = modles.Admin.get_obj_by_name(name) 10 elif user_type == 'teacher': 11 obj = modles.Teacher.get_obj_by_name(name) 12 elif user_type == 'student': 13 obj = modles.Student.get_obj_by_name(name) 14 else: 15 return False, '没有这个用户类型' 16 if obj: 17 if obj.name == name and obj.password == password: 18 return True, '%s: %s 登陆成功' % (user_type, name) 19 else: 20 return False, '密码不对' 21 22 else: 23 return False, '用户不存在' 24 25 26 def check_all_schools(): 27 path = os.path.join(settings.BASE_DB, 'school') 28 return common.get_all_dir_obj(path) 29 30 31 def choose_course_interface(student_name, course_name): 32 obj = modles.Student.get_obj_by_name(student_name) 33 obj.add_course(course_name) 34 return True, '选课成功'
3.student_interface
1 from db import modles 2 3 4 def student_register_interface(name, password): 5 student_obj = modles.Student.get_obj_by_name(name) 6 if student_obj: 7 return False, '学生已经存在' 8 else: 9 modles.Student(name, password) 10 return True, '注册成功' 11 12 13 def choice_school_interface(student_name, school_name): 14 student_obj = modles.Student.get_obj_by_name(student_name) 15 school = student_obj.get_school() 16 if not school: 17 18 student_obj.choose_school(school_name) 19 return True, '%s: 选择学校成功' % (student_name) 20 else: 21 return False, '已经选择学校了, 不能重复选择学校' 22 23 24 def get_can_choose_course_interface(student_name): 25 obj = modles.Student.get_obj_by_name(student_name) 26 if not obj.school: 27 return False, '你没有选择学校,请先选择学校' 28 school_obj = modles.School.get_obj_by_name(student_name) 29 if school_obj.course_list: 30 return True, school_obj.course_list 31 else: 32 return False, '该学校下没有课程' 33 34 35 def choose_course_interface(student_name, course_name): 36 obj = modles.Student.get_obj_by_name(student_name) 37 obj.add_course(course_name) 38 return True, '选课成功' 39 40 41 def check_score_interface(student_name): 42 obj = modles.Student.get_obj_by_name(student_name) 43 return obj.score
4.teacher_nterface
1 from db import modles 2 import os 3 from conf import settings 4 from lib import common 5 6 7 def get_all_course(): 8 path = os.path.join(settings.BASE_DB, 'course') 9 return common.get_all_dir_obj(path) 10 11 12 def choose_teacher_course_interface(teacher_name, course_name): 13 teacher_obj = modles.Teacher.get_obj_by_name(teacher_name) 14 if course_name not in teacher_obj.course_list: 15 teacher_obj.add_course(course_name) 16 return True, '选择课程成功' 17 else: 18 return False, '你已经选择了本门课程' 19 20 21 def check_all_teacher_course(teacher_name): 22 teacher_obj = modles.Teacher.get_obj_by_name(teacher_name) 23 return teacher_obj.course_list 24 25 26 def check_student_in_course(course_name): 27 course_obj = modles.Course.get_obj_by_name(course_name) 28 return course_obj.student_list 29 30 31 def modify_score(teacher_name, course_name, student_name, score): 32 teacher_obj = modles.Teacher.get_obj_by_name(teacher_name) 33 student_obj = modles.Student.get_obj_by_name(student_name) 34 teacher_obj.modify_score(student_obj, course_name, score) 35 return True, '修改分数成功'
四 , 下面是lib代码
1.common下代码
1 import os 2 3 4 def login_auth(user_type): 5 from core import admin, student, teacher 6 7 def auth(func): 8 9 def wrapper(*args, **kwargs): 10 if user_type == 'admin': 11 if not admin.admin_info['name']: 12 admin.admin_login() 13 else: 14 return func(*args, **kwargs) 15 if user_type == 'teacher': 16 if not teacher.teacher_info['name']: 17 teacher.teacher_login() 18 else: 19 return func(*args, **kwargs) 20 if user_type == 'student': 21 if not student.student_info['name']: 22 student.student_login() 23 else: 24 return func(*args, **kwargs) 25 26 return wrapper 27 28 return auth 29 30 31 def get_all_dir_obj(path): 32 if os.path.exists(path): 33 obj_list = os.listdir(path) 34 return obj_list 35 else: 36 return None
来源:https://www.cnblogs.com/xiaolang666/p/12358158.html