作业:选课系统
本章作业
题目:选课系统开发,要求有四种角色:学校、学员、课程、讲师
详细要求:
-
创建北京、上海 2 所学校
-
创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开
-
课程包含,周期,价格,通过学校创建课程
-
通过学校创建班级, 班级关联课程、讲师
-
创建学员时,选择学校,关联班级
-
创建讲师角色时要关联学校
-
提供两个角色接口
-
为学员、讲师、管理员分别提供用户界面,并提供对应功能: 1 学员视图, 可以注册, 交学费, 选择班级, 2 讲师视图, 讲师可管理自己的班级, 上课时选择班级, 查看班级学员列表 , 修改所管理的学员的成绩 3 管理视图,创建讲师, 创建班级,创建课程
注1:上面的操作产生的数据都通过pickle序列化保存到文件里 注
C:.
│ main_server.py
│ RAMDE
│ tree.txt
│ __init__.py
│ 选课系统.pdf
│
├─conf
│ │ setting.py --->配置文件
│ │ __init__.py
│ │
│ └─__pycache__
│ setting.cpython-36.pyc
│ __init__.cpython-36.pyc
│
├─core
│ │ admin.py --> 管理员类
│ │ db_handler.py ---> 文件读、存操作
│ │ main.py --> 入口
│ │ school.py --> 学校类
│ │ Student.py --》 学生类
│ │ Teachar.py --》 讲师类
│ │ __init__.py
│ │
│ └─__pycache__
│ admin.cpython-36.pyc
│ db_handler.cpython-36.pyc
│ main.cpython-36.pyc
│ school.cpython-36.pyc
│ Student.cpython-36.pyc
│ Teachar.cpython-36.pyc
│ __init__.cpython-36.pyc
│
├─db
│ │ __init__.py
│ │
│ └─accounts --》 账号文件
│ │ class_info ---》班级信息文件
│ │ course_info---》课程文件
│ │ school_info---》学校文件
│ │ student---》学生文件
│ │ teacher_info---》讲师文件
│ │ userinfo---》用户登陆文件
│ │ __init__.py
│ │
│ └─studentinfo --》 班级文件夹
│ linuxs1
│ pythons2
main_server.py
1 import os,sys 2 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 3 sys.path.append(BASE_DIR) 4 5 if __name__ == '__main__': 6 from core import main 7 main.entrance()
main.py
1 import sys,os 2 from conf.setting import userinfo 3 from .school import Course 4 from .admin import admin 5 from .Teachar import Teacher 6 from .Student import Student 7 def login(): 8 user = input('请输入账号:').strip() 9 pwd = input('请输入密码:').strip() 10 with open(userinfo) as f: 11 for line in f: 12 username,passwd,role = line.strip().split('|') 13 if username == user and passwd == pwd: 14 print('登陆成功') 15 return {'username':user,'role':role} 16 17 else: 18 print('登陆失败!') 19 def entrance(): 20 print('欢迎登陆选课系统') 21 ret = login() 22 flag_exit = False 23 if ret: 24 role_class = getattr(sys.modules[__name__],ret['role']) 25 obj = role_class(ret['username']) 26 while not flag_exit: 27 for i,j in enumerate(role_class.menu,1): 28 print(i,j[0]) 29 choice = input('请输入您的选择:').strip() 30 if choice.isdigit(): 31 choice = int(choice) 32 getattr(obj,role_class.menu[choice-1][1])() 33 else: 34 print('输入错误请重新输入')
setting.py
1 import os,sys 2 import logging 3 4 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 5 6 userinfo = r"%s\db\accounts\userinfo"%BASE_DIR 7 teacher_obj = r"%s\db\accounts\teacher_info"%BASE_DIR 8 school_obj = r"%s\db\accounts\school_info"%BASE_DIR 9 class_obj = r"%s\db\accounts\class_info"%BASE_DIR 10 course_obj = r"%s\db\accounts\course_info"%BASE_DIR 11 student_obj = r"%s\db\accounts\student"%BASE_DIR 12 studentinfo = r"%s\db\accounts\studentinfo"%BASE_DIR
admin.py
1 from conf import setting 2 import os,pickle 3 from .db_handler import My_pickle 4 from .Teachar import myTeacher 5 from .school import Class 6 from .Student import myStudent 7 class admin: 8 menu = [ ('创建讲师账号','creatTeacher'), 9 ('查看讲师信息','showTeacher'), 10 ('查看课程信息','showCourse'), 11 ('查看班级信息','showClass'), 12 ('查看学校','showSchool'), 13 ('创建班级','creatClass'), 14 ('创建学生账号','creatStudent'), 15 ('指定班级老师','boundClass') 16 ] 17 def __init__(self,name,): 18 self.name = name 19 self.teacher_pickle_obj = My_pickle(setting.teacher_obj) 20 self.course_pickle_obj = My_pickle(setting.course_obj) 21 self.class_pickle_obj = My_pickle(setting.class_obj) 22 self.school_pickle_obj = My_pickle(setting.school_obj) 23 24 25 @staticmethod 26 def user_info(obj): 27 with open(setting.userinfo,'a') as f: 28 f.write('\n%s'%obj) 29 30 31 32 def creatTeacher(self): 33 teacher_name = input('请输入姓名:').strip() 34 teacher_pwd = input('请输入密码:').strip() 35 self.showSchool() 36 teacher_school = input('请输入讲师所在学校').strip() 37 teacher_salary = input("请输入讲师工资:").strip() 38 teacher_file = '%s|%s|Teacher'%(teacher_name,teacher_pwd) 39 self.user_info(teacher_file) 40 teacher = myTeacher(teacher_name,teacher_school,teacher_salary) 41 self.teacher_pickle_obj.dump_pickle(teacher) 42 print('创建成功') 43 44 def creatStudent(self): 45 student_name = input('请输入学生名字:').strip() 46 student_pwd = input('请输入学生密码:').strip() 47 student_age = input('请输入学生年龄:').strip() 48 student_sex = input('请输入学生性别:').strip() 49 self.showSchool() 50 student_school = input('请输入学生所在的学校:').strip() 51 self.showClass() 52 student_class = input('请输入学生所在的班级:').strip() 53 class_g = self.class_pickle_obj.load_pickle() 54 for clas in class_g: 55 # print(clas.name) 56 if clas.name == student_class: 57 content = '%s|%s|Student'%(student_name,student_pwd) 58 admin.user_info(content) 59 stu_obj = myStudent(student_name,student_school,student_age,student_sex) 60 My_pickle(setting.student_obj).dump_pickle(stu_obj) 61 print('创建成功!') 62 break 63 else: 64 print('输入的内容有误!') 65 66 def boundClass(self): 67 self.showClass() 68 class_name = input('请输入为指定的班级:').strip() 69 self.showTeacher() 70 class_teacher = input('请输入为指定的老师:').strip() 71 teach_g = self.teacher_pickle_obj.load_pickle() 72 for teacher_obj in teach_g: 73 # print(teacher_obj.__dict__) 74 if teacher_obj.name == class_teacher: 75 teacher_obj.classes.append(class_name) 76 self.teacher_pickle_obj.edit_pickle(teacher_obj) 77 print('绑定成功') 78 return 79 80 def creatClass(self): 81 class_name = input('请输入班级的名称:').strip() 82 self.showSchool() 83 school_name = input('请输入学校的名字:').strip() 84 self.showCourse() 85 course = input('请输入学科的名称:').strip() 86 student_path = os.path.join(setting.studentinfo,class_name) 87 open(student_path,'w').close() 88 class_obj = Class(school_name,class_name,course,student_path) 89 self.class_pickle_obj.dump_pickle(class_obj) 90 print('创建成功!') 91 92 def show(self,pickle_obj): 93 94 pickle_obj = getattr(self,pickle_obj) 95 load_g = pickle_obj.load_pickle() 96 for coures_obj in load_g: 97 print(coures_obj.__dict__) 98 for i in coures_obj.__dict__: 99 print(i, coures_obj.__dict__[i]) 100 print(''.center(50,'-')) 101 102 def showTeacher(self): 103 self.show('teacher_pickle_obj') 104 def showCourse(self): 105 self.show('course_pickle_obj') 106 def showClass(self): 107 self.show('class_pickle_obj') 108 def showSchool(self): 109 self.show('school_pickle_obj')
db_handler.py
1 from conf import setting 2 from .school import Course 3 import pickle 4 import os 5 6 class My_pickle: 7 8 def __init__(self,filename): 9 self.filename = filename 10 11 def dump_pickle(self,obj): 12 with open(self.filename,'ab') as f: 13 pickle.dump(obj,f) 14 15 def load_pickle(self): 16 with open(self.filename,'rb') as f: 17 while True: 18 try: 19 obj = pickle.load(f) 20 yield obj 21 except: 22 break 23 24 25 def edit_pickle(self,obj): 26 # f2 = My_pickle(self.filename+'.bak') 27 with open(self.filename,'rb') as f: 28 with open(self.filename+'.new','wb') as f1: 29 item = pickle.load(f) 30 if item.name == obj.name: 31 pickle.dump(obj,f1) 32 else: 33 pickle.dump(item,f1) 34 35 os.remove(self.filename) 36 os.rename(self.filename+'.new',self.filename)
school.py
1 import os,sys 2 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 3 sys.path.append(BASE_DIR) 4 class Class: 5 def __init__(self,school,name,course,student_path): 6 self.school = school 7 self.name = name 8 self.course = course 9 self.student_path = student_path 10 11 12 class Course: 13 def __init__(self,name,period,price,school): 14 self.name = name 15 self.period = period 16 self.price = price 17 self.school = school 18 def __repr__(self): 19 return self.name 20 21 class School: 22 def __init__(self,name,course): 23 self.name = name 24 self.course = course 25 26 27 28 # if __name__ == '__main__': 29 30 # from core import school 31 # from conf import setting 32 # from core import db_handler 33 # school_pickle = db_handler.My_pickle(setting.school_obj) 34 # python = school.Course('python','6个月',19800,'上海校区') 35 # liunx = school.Course('liunx','7个月',20000,'上海校区') 36 # go = school.Course('go','4个月',6000,'北京校区') 37 # bejing = school.School('北京校区',[go]) 38 # shanghai = school.School('上海校区',[python,liunx]) 39 # school_pickle.dump_pickle(bejing) 40 # school_pickle.dump_pickle(shanghai) 41 42 43 # import pickle 44 # from core import school 45 # from conf.setting import course_obj 46 # from core.db_handler import My_pickle 47 # coures_pickle1 = My_pickle(course_obj) 48 # python = Course('python','6个月',19800,'上海校区') 49 # liunx = Course('liunx','7个月',20000,'上海校区') 50 # go = Course('go','4个月',6000,'北京校区') 51 # coures_pickle1.dump_pickle(python) 52 # coures_pickle1.dump_pickle(liunx) 53 # coures_pickle1.dump_pickle(go) 54 # with open(course_obj, 'rb') as f: 55 # data = coures_pickle1.load_pickle() 56 # print(data.__dict__) 57 # #
Student.py
1 import os 2 from .db_handler import My_pickle 3 from conf import setting 4 5 class myStudent: 6 def __init__(self,name,school,age,sex): 7 self.name = name 8 self.school =school 9 self.age = age 10 self.sex = sex 11 self.course = [] 12 self.classes = [] 13 self.performance = '' 14 15 class Student: 16 menu = [ 17 ('查看课程','showCourse'), 18 ('注册账号','registeredAccount'), 19 ('选择班级','creatClass'), 20 21 ] 22 def __init__(self,name): 23 self.name = name 24 self.teacher_pickle_obj = My_pickle(setting.teacher_obj) 25 self.course_pickle_obj = My_pickle(setting.course_obj) 26 self.class_pickle_obj = My_pickle(setting.class_obj) 27 self.school_pickle_obj = My_pickle(setting.school_obj) 28 self.student_pickle_obj = My_pickle(setting.student_obj) 29 30 @staticmethod 31 def user_info(obj): 32 with open(setting.userinfo,'a') as f: 33 f.write('\n%s'%obj) 34 35 def show(self,pickle_obj): 36 pickle_obj = getattr(self,pickle_obj) 37 load_g = pickle_obj.load_pickle() 38 # print(load_g.__dict__) 39 for coures_obj in load_g: 40 for i in coures_obj.__dict__: 41 print(i, coures_obj.__dict__[i]) 42 print(''.center(50,'-')) 43 44 def creatClass(self): 45 self.show('class_pickle_obj') 46 choice_class = input('请输入选择的班级:').strip() 47 student_clas = self.student_pickle_obj.load_pickle() 48 for student_obj in student_clas: 49 if student_obj.name == choice_class: 50 student_obj.classes.append(choice_class) 51 self.student_pickle_obj.edit_pickle(student_obj) 52 print('已成功加入班级!') 53 else: 54 print("输入的信息有误!") 55 56 def registeredAccount(self): 57 student_name = input('请输入名字:').strip() 58 student_pwd = input('请输入密码:').strip() 59 student_age = input('请输入年龄:').strip() 60 student_sex = input('请输入性别:').strip() 61 self.showSchool() 62 student_school = input('请选择学校:').strip() 63 self.showClass() 64 student_class = input('请选择班级:').strip() 65 class_g = self.class_pickle_obj.load_pickle() 66 for clas in class_g: 67 # print(clas.name) 68 if clas.name == student_class: 69 content = '%s|%s|Student'%(student_name,student_pwd) 70 Student.user_info(content) 71 stu_obj = myStudent(student_name,student_school,student_age,student_sex) 72 My_pickle(setting.student_obj).dump_pickle(stu_obj) 73 print('创建成功!') 74 75 def showSchool(self): 76 self.show('school_pickle_obj') 77 def showClass(self): 78 self.show('class_pickle_obj') 79 80 def showCourse(self): 81 self.show('course_pickle_obj')
Teachar.py
1 import os 2 from .db_handler import My_pickle 3 from .school import Class 4 from .Student import Student 5 from conf import setting 6 7 class myTeacher: 8 def __init__(self,name,school,salary): 9 self.name = name 10 self.school = school 11 self.salary = salary 12 self.classes = [] 13 self.student = [] 14 15 16 class Teacher: 17 menu =[ 18 ('查看班级','showclass'), 19 ('选择班级','showStudent'), 20 ('选择上课班级','creatClass'), 21 ('查看班级学员信息','showStudent'), 22 ('修改学员成绩','creatPerformance'), 23 ] 24 def __init__(self,name): 25 self.name = name 26 self.teacher_pickle_obj = My_pickle(setting.teacher_obj) 27 self.course_pickle_obj = My_pickle(setting.course_obj) 28 self.class_pickle_obj = My_pickle(setting.class_obj) 29 self.school_pickle_obj = My_pickle(setting.school_obj) 30 self.student_pickle_obj = My_pickle(setting.student_obj) 31 32 33 def show(self,pickle_obj): 34 pickle_obj = getattr(self,pickle_obj) 35 load_g = pickle_obj.load_pickle() 36 # print(load_g.__dict__) 37 for coures_obj in load_g: 38 for i in coures_obj.__dict__: 39 print(i, coures_obj.__dict__[i]) 40 print(''.center(50,'-')) 41 42 def showclass(self): 43 self.show('class_pickle_obj') 44 def showStudent(self): 45 self.show('student_pickle_obj') 46 def creatClass(self): 47 self.show('class_pickle_obj') 48 choice = input('请选择要上课的班级:').strip() 49 print('已选择%s班,请准时上课'%choice) 50 def creatPerformance(self): 51 self.show('student_pickle_obj') 52 choice = input('请输入需修改成绩的名字:').strip() 53 choice_p = input('请输入修改成绩:').strip() 54 student_g = self.student_pickle_obj.load_pickle() 55 for student_obj in student_g: 56 if student_obj.name == choice: 57 student_obj.performance = choice_p 58 self.student_pickle_obj.edit_pickle(student_obj)