python之路之课后作业
以下代码只包含管理员代码,用户代码和管理员相似
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys import os sys.path.append(os.path.dirname(os.path.dirname(__file__))) import random import time import pickle import os from lib import modles from lib.modles import Admin from config import setting def create_course(admin_obj): teacher_list = pickle.load(open(setting.TEACHER_db_dir, 'rb')) for num, item in enumerate(teacher_list, 1): print(num, item.name, item.age, item.create_time, item.create_admin.username) course_list = [] while True: name = input('请输入课程(q退出):') if name == 'q': break cost = input('请输入课时费:') num = input('选择老师(序号):') obj = modles.Course(name, cost, teacher_list[int(num)-1], admin_obj) course_list.append(obj) if os.path.exists(setting.COURSE_db_dir): exists_list = pickle.load(open(setting.COURSE_db_dir, 'rb')) course_list.extend(exists_list) pickle.dump(course_list, open(setting.COURSE_db_dir, 'wb')) def create_teacher(admin_obj): teacher_list = [] while True: teacher_name = input('请输入老师姓名(q表示退出)') if teacher_name == 'q': break teacher_age = input('请输入老师年龄') obj = modles.Teacher(teacher_name, teacher_age, admin_obj) teacher_list.append(obj) if os.path.exists(setting.TEACHER_db_dir): exists_list = pickle.load(open(setting.TEACHER_db_dir, 'rb')) teacher_list.extend(exists_list) pickle.dump(teacher_list, open(setting.TEACHER_db_dir, 'wb')) def login(user, pwd): if os.path.exists(os.path.join(setting.BASE_ADMIN_DIR, user)): # 这有bug,已解决,导入Admin和添加文件路径os.path.join(setting.BASE_ADMIN_DIR admin_obj = pickle.load(open(os.path.join(setting.BASE_ADMIN_DIR, user), 'rb')) if admin_obj.login(user, pwd): while True: sel = input("1、创建老师: 2、创建课程:q、退出") if sel == '1': create_teacher(admin_obj) elif sel == '2': create_course(admin_obj) else: break else: return 1 else: return 0 def register(user, pwd): admin_obj = modles.Admin() admin_obj.register(user, pwd) def main(): inp = input("1、管理员登录:2、管理员注册:\n >>>>>") user = input('请输入用户名:') pwd = input('请输入密码:') if inp == '1': ret = login(user, pwd) if ret == 1: print('密码错误') elif ret == 0: print('用户不存在') elif inp == '2': register(user, pwd) if __name__ == "__main__": main()
#!/usr/bin/env python # -*- coding: utf-8 -*- import os BASE_DIR = os.path.dirname(os.path.dirname(__file__)) BASE_ADMIN_DIR = os.path.join(BASE_DIR, "db", "adminall") TEACHER_db_dir = os.path.join(BASE_DIR, "db", "teacher_list") COURSE_db_dir = os.path.join(BASE_DIR, "db", "course_list")
#!/usr/bin/env python # -*- coding: utf-8 -*- import random import time import pickle from config import setting import os class Teacher: """ 封装老师的相关信息 """ def __init__(self, name, age, admin): self.name = name self.age = age self.__assets = 0 self.create_time = time.strftime('%Y-%m-%d %H:%M:%S') self.create_admin = admin def gain(self, cost): """ :param cost: 资产的增加 :return: """ self.__assets += cost def decrease(self, cost): """ :param cost: 资产的减少 :return:资产 """ self.__assets -= cost class Course: """ 课程相关信息 """ def __init__(self,course_name, cost, teacher_obj, admin): self.course_name = course_name self.cost = cost self.teacher_obj = teacher_obj self.create_time = time.strftime('%Y-%m-%d %H:%M:%S') self.create_admin = admin def have_lesson(self): """ 课程上课,自动给相关联的任课老师增加课时费 :return:课程内容返回上课者 """ self.teacher.gain(self.cost) content = random.randrange(10, 100) r = time.strftime('%Y-%m-%d %H:%M:%S') temp = "课程:%s: 老师:%s: 内容: %d: 时间:" %(self.course_name, self.teacher, content, r) return temp def absence(self): """ 教学事故 """ class Admin: def __init__(self): self.username = None self.password = None def login(self, user, pwd): """ 管理员登录 :param user:用户名 :param pwd: 密码 :return: """ if self.username == user and self.password == pwd: return True else: return False def register(self, user, pwd): """ 管理员注册 :param user: 用户名 :param pwd: 密码 :return: """ self.username = user self.password = pwd path = os.path.join(setting.BASE_ADMIN_DIR,self.username) pickle.dump(self, open(path, 'xb'))