这几天学完面向对象,然后找了一个练习做(题目如下):因为刚刚接触编程,可能有很多方面考虑得不周到
目录如下:
import os import sys BASEDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(BASEDIR) from core.admin_view import admin_main from lib.public_func import instanc from core.student_view import student_main from core.teacher_view import teacher_main def main(): msg = ''' --------------------选课系统--------------------- 1 管理员 2 讲师 3 学生 ''' msg_main = { '1' : admin_main, '2' : teacher_main, '3' : student_main } instanc(msg,msg_main) if __name__ == '__main__': main()
import os BASEDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) DATABASE_DIR = os.path.join(BASEDIR,'db') ADMIN_DIR = os.path.join(DATABASE_DIR,'admin') COURSE_DIR = os.path.join(DATABASE_DIR,'course') COURSE_TO_TEACHER_DIR = os.path.join(DATABASE_DIR,'course_to_teacher') SCHOOL_DIR = os.path.join(DATABASE_DIR,'school') STUDENT_DIR = os.path.join(DATABASE_DIR,'student') TEACHER_DIR = os.path.join(DATABASE_DIR,'teacher') CLASSES_DIR = os.path.join(DATABASE_DIR,'classes') USER = None
from core.modu import * from lib.public_func import * @is_login('admin') def adm_index(): msg = ''' -------------------------------------------- 欢迎来到oldboy选课管理系统 1 创建校区 2 查看学校 3 添加老师 4 查看老师 5 添加班级 6 查看班级 7 添加课程 8 查看课程 ''' main_func = { '1' : setting.USER.create_school, '2' : School.get_all, '3': setting.USER.add_teacher, '4': Teacher.get_all, '5': setting.USER.add_classes, '6': Classes.get_all, '7': setting.USER.add_course, '8': Course.get_all } instanc(msg,main_func) def initialization(): user = input('用户名:') pwd = input('密码:') admin = Admin(user,pwd) admin.save() def admin_view(): msg = """ ---------------------------------------- 1 初始化 2 登录 ---------------------------------------- """ main_func = { '1' : initialization, '2' : Admin.login } instanc(msg,main_func) def admin_main(): adm_index() if __name__ == '__main__': adm_index()
from lib.Base import Base,Uid from conf.setting import * import pickle class Admin(Base): ''' 管理员类 主要字段: path 管理员对象存储地址 user 管理员账号 pwd 管理员密码 login 登录函数 ''' path = ADMIN_DIR def __init__(self,user,pwd): super().__init__() self.user = user self.pwd = pwd def create_school(self): ''' 创建学校,并且保存 :return: ''' sch_name = input('请输入校区地址:') s1 = School(sch_name) s1.save() def add_teacher(self): ''' 创建老师,并且保存 :return: ''' t_name = input("请输入老师的姓名:") t_age = input("请输入老师的年龄:") school_all = School.get_all() school = input("请输入学校对应的下标,从0开始") user = input("请输入对应的用户名:") pwd = input("请输入密码:") teacher = Teacher(t_name,t_age,user,pwd,Uid(school_all[int(school)].uuid)) teacher.save() def add_classes(self): ''' 创建班级,并且保存 :return: ''' name = input('请输入班级名称:') school_all = School.get_all() school_id = input("请输入所在校区:(下标从0开始)") course_all = Course.get_all() course_id = input("请输入此班级课程:(下标从0开始)") classes = Classes(name,Uid(school_all[int(school_id)].uuid),Uid(course_all[int(course_id)].uuid)) classes.save() tea = course_all[int(course_id)].to_teacher for i in tea: t = i.get_obj(TEACHER_DIR) t.add_classes(Uid(classes.uuid)) t.save() def add_course(self): ''' 添加课程,并且保存,这里的课程和老师是一对多的关系 :return: ''' c_name = input("请输入课程名:") c_price = input("请输入课程价格:") c_period = input("课程周期:") teacher_all = Teacher.get_all() x = True;teacher_list = [] while x : x = input("请选择授课讲师:(下标从0 开始,按b结束)") if x == 'b': break teacher_list.append(teacher_all[int(x)].uuid) course = Course(c_name,c_price,c_period,teacher_list) course.save() coursetoteacher = CourseToTeacher(Uid(course.uuid),Uid(teacher_list)) coursetoteacher.save() class School(Base): ''' 学校类 ''' path = SCHOOL_DIR name = 'oldboy' def __init__(self,addr): super().__init__() self.addr = addr def __repr__(self): return '学校:%s(%s)' % (self.name,self.addr) class Course(Base): path = COURSE_DIR def __init__(self,name,price,period,to_teacher): super().__init__() self.name = name self.price = price self.period = period self.to_teacher = [Uid(i) for i in to_teacher] def __repr__(self): return '课程名: %s 授课讲师:%s ' % (self.name,[i.get_obj(TEACHER_DIR) for i in self.to_teacher]) class Teacher(Base): path = TEACHER_DIR def __init__(self,name,age,user,pwd,school_uuid): super().__init__() self.name = name self.age = age self.user = user self.pwd = pwd self.classes_uuid = [] self.class_uuid = '' self.school_uuid = Uid(school_uuid) def add_classes(self,val): self.classes_uuid.append(val) def get_cla_stu(self): if self.class_uuid == "": print("请先选择班级!") return False print("当前所在班级:%s" % self.class_uuid) result = [] for i in os.listdir(STUDENT_DIR): s = pickle.load(open(os.path.join(STUDENT_DIR, i), 'rb')) if s.classes_id.uuid == self.class_uuid.uuid: result.append(s) print("当前班级学员有:(姓名,成绩)\n %s" % [(i.name,i.get_exploit()) for i in result]) return result def show_class(self): t_classes = [i.get_obj(CLASSES_DIR) for i in self.classes_uuid] u_choice = input("请选择要管理的班级 %s(下标从0开始):" %t_classes) print("进入班级----> %s" % t_classes[int(u_choice)]) self.class_uuid = t_classes[int(u_choice)] def set_stu_exploit(self): if self.class_uuid == "": print("请先选择班级!") return False print("当前所在班级:%s" % self.class_uuid) stu = self.get_cla_stu() choice = input("请选择需要修改成绩的学员下标(从0开始):") num = input("请输入该学员成绩:") stu[int(choice)].set_exploit(int(num)) stu[int(choice)].save() def __repr__(self): return self.name class CourseToTeacher(Base): path = COURSE_TO_TEACHER_DIR def __init__(self,course_uuid,teacher_uuid): super().__init__() self.course_uuid = course_uuid self.teacher_uuid = teacher_uuid class Student(Base): path = STUDENT_DIR def __init__(self,name,age,sex,user,pwd,classes_id,price=0,exploit=0): super().__init__() self.name = name self.age = age self.sex = sex self.user = user self.pwd = pwd self.__exploit = exploit self.price = price self.classes_id = classes_id def get_exploit(self): return self.__exploit def set_exploit(self,val): self.__exploit = val @staticmethod def regiest(): name = input('请输入姓名:') age = input("请输入年龄:") sex = input("请输入性别:") user= input("用户名:") pwd = input("密码:") classes_all = Classes.get_all() class_id = input("选择班级:(下边从 0 开始)") stu = Student(name,age,sex,user,pwd,Uid(classes_all[int(class_id)].uuid)) stu.save() def show_all(self): msg = ''' 姓名:{0} 年龄:{1} 性别:{2} 所在班级:{3} 已交学费:{4} ''' print(msg.format(self.name,self.age,self.sex,self.classes_id.get_obj(CLASSES_DIR).name,self.price)) def update_cla(self): cla_all = Classes.get_all() class_choice = input('请选择班级名称,下标从 0 开始:') self.classes_id = Uid(cla_all[int(class_choice)].uuid) self.save() def get_price(self): print('已交学费 %s 元' % self.price) def update_price(self): price = input("输入需要缴费的金额:") self.price = price self.save() def __repr__(self): return self.name class Classes(Base): path = CLASSES_DIR def __init__(self,name,school_uuid,course_uuid): super().__init__() self.name = name self.school_uuid = school_uuid self.course_uuid = course_uuid def __repr__(self): return self.name if __name__ == '__main__': print(Classes.get_all())
from lib.public_func import * from core.modu import * @is_login('student') def student_index(): msg = ''' -------------------------------------------- 欢迎来到oldboy选课管理系统 1 查看资料 2 修改班级 3 查看已交学费 4 交学费 ''' main_func = { '1' : setting.USER.show_all, '2' : setting.USER.update_cla, '3': setting.USER.get_price, '4': setting.USER.update_price } instanc(msg,main_func) def student_main(): student_index()
from lib.public_func import * from core.modu import * @is_login('teacher') def teacher_index(): msg = ''' -------------------------------------------- 欢迎来到oldboy选课管理系统 1 选择班级 2 查看班级学员 3 修改所在班级学员成绩 ''' main_func = { '1' : setting.USER.show_class, '2' : setting.USER.get_cla_stu, '3': setting.USER.set_stu_exploit, # '4': setting.USER.update_price } instanc(msg,main_func) def teacher_main(): teacher_index()
import pickle import os import time import hashlib class Base: ''' 所有人类的基类, 拥有属性uuid, 拥有方法: save 保存到数据库 get_all 获取所有元素 create_uuid 通过时间获取hash值 login 登录 ''' def __init__(self): self.uuid = self.create_uuid() def save(self): pickle.dump(self,open(os.path.join(self.path,self.uuid),'wb')) @classmethod def get_all(cls): result = [] for i in os.listdir(cls.path): result.append(pickle.load(open(os.path.join(cls.path,i),'rb'))) print(result) return result @staticmethod def create_uuid(): uuid = hashlib.md5(str(time.time()).encode('utf-8')) return uuid.hexdigest() @classmethod def login(cls): while True: if cls.__name__ is "Student": uchoice = input('如需要注册,请输入 c :') if uchoice == 'c': cls.regiest() print("注册成功") user = input("用户名:") pwd = input("密码:") for i in os.listdir(cls.path): f = pickle.load(open(os.path.join(cls.path,i),'rb')) if f.user == user and f.pwd == pwd: return f print("账号密码错误!") class Uid: ''' 关联类,当类与类关联时,如果直接关联对象会出现修改不同步的问题,所以就值关联uuid即可,这里是用一个中间对象来取代uuid字符串 get_obj方法就是用来通过uuid获取相对应的对象 ''' def __init__(self,uuid): self.uuid = uuid def get_obj(self,path): for i in os.listdir(path): if i == self.uuid: return pickle.load(open(os.path.join(path,i),'rb')) return None
from core.modu import * from conf import setting ''' 把输出消息和功能结合到一起 ''' def instanc(main_msg,main): u_choice = True while u_choice: u_choice = input(main_msg) if u_choice == 'b': break main[u_choice]() setting.USER = None ''' 判断是否登录的装饰器 ''' def is_login(types): def warrp(func): def deco(*args,**kwargs): if setting.USER: return func(*args,**kwargs) else: if types == 'admin': setting.USER = Admin.login() return func(*args,**kwargs) elif types == 'teacher': setting.USER = Teacher.login() return func(*args, **kwargs) elif types == 'student': setting.USER = Student.login() return func(*args,**kwargs) return deco return warrp