自己琢磨的学生选课系统
import hashlib import pickle import os class Iden: # 用户的父类 def __init__(self, user, pwd, ): self.user = user self.pwd = pwd # 学生类 class Normal(Iden): def __init__(self, user, pwd, course='无'): super().__init__(user, pwd) self.groupwork = 'student' # 普通用户的注册 def normal_regedist(user, pwd, ): pwd = hashli(user, pwd) person = Normal(user, pwd) picke_write(person, f'./user/{user}') return True # 学生选择课程 def use_course(self, course): self.course = course picke_write(self, f'./user_course/{self.user}_course') return '添加成功' # 读取自己的课程 def read_course(self): student_user_course = os.listdir('./user_course/') if f'{self.user}_course' in student_user_course: return picke_read(f'./user_course/{self.user}_course') return False # 管理员类 class Admin(Iden): def __init__(self, user, pwd, ): super().__init__(user, pwd) self.groupwork = 'administrator' # 管理员的注册, def __admin_regedist(user, pwd): pwd = hashli(user, pwd) person = Admin(user, pwd, ) picke_write(person, f'./user/{user}') return True # 查看所有的学生 def see_all_student(self): student_user = os.listdir('./user/') for us in student_user: if us != self.user: print(f"账号:{picke_read(f'./user/{us}').user}") # 查看所有的学生的课程 def see_all_student_course(self): student_user_course = os.listdir('./user_course/') for us in student_user_course: course_read(picke_read(f'./user_course/{us}')) # 使用picke读取文件 def picke_read(path): with open(path, 'rb')as f: a = pickle.load(f) return a # 使用picke写入文件 def picke_write(self, path): with open(path, 'wb')as f: pickle.dump(self, f) # 打印课程信息,格式化 def course_read(a): print(f'账号:{a.user},课程名称:{a.course.name},课程价格:{a.course.price},' f'课程周期:{a.course.month},课程讲师:{a.course.teacher}') # 课程类 class Course: def __init__(self, name, price, month, teacher): self.name = name self.price = price self.month = month self.teacher = teacher @staticmethod # 创建课程 def create_course(name, price, month, teacher): course = Course(name, price, month, teacher) print(course.name) with open('./course/course', 'ab') as f: pickle.dump(course, f) return True @staticmethod # 读取所有的课程,并将结果以列表的形式返回 def read_all_course(): li = [] with open('./course/course', 'rb') as f: while True: try: li.append(pickle.load(f).__dict__) except: break return li # 查看所有的课程 def see_course(): course = Course.read_all_course() for i in range(len(course)): print(f'序号{i + 1} 课程名称{course[i]["name"]},课程价格{course[i]["price"]},\ 课程周期{course[i]["month"]},课程讲师{course[i]["teacher"]}') return course # 数据加密 def hashli(user, pwd): md5 = hashlib.sha256(user.encode('utf-8')) md5.update(pwd.encode('utf-8')) return md5.hexdigest() # 登录验证,正确就返回账户的信息(包含类中的方法) def login(user, pwd): lisdis = os.listdir('./user/') if user in lisdis: with open(f'./user/{user}', 'rb') as f: user_pwd_dic = pickle.load(f) if user == user_pwd_dic.user and hashli(user, pwd) == user_pwd_dic.pwd: return user_pwd_dic else: return else: return # 用户登录输入 def login_input(): while 1: user = input('请输入用户名>>>').replace(' ', '') pwd = input('请输入密码>>>').replace(' ', '') if user and pwd: break else: print('输入有误,请重新输入') return user, pwd # 创建课程输入 def courese_input(): name = input('请输入课程名称>>>').replace(' ', '') price = int(input('请输入课程价格>>>').replace(' ', '')) month = input('请输入课程周期>>>').replace(' ', '') teacher = input('请输入授课老师>>>').replace(' ', '') Course.create_course(name, price, month, teacher) return flag = True while flag: user, pwd = login_input() # 输入用户密码 a = login(user, pwd) # 登录验证 if a: if a.groupwork == 'administrator': # 如果身份是管理员身份 print(f'欢迎管理员{a.user}登录') while flag: print('1,创建用户。 2,创造课程 3,查看课程 4,查看学生 5,查看学生选课 6,退出程序 ') choose = input('请输入你要选择的操作').replace(' ', '') if choose and choose.isnumeric() and int(choose) <= 6 and int(choose) > 0: if choose == '1': user, pwd = login_input() if Normal.normal_regedist(user, pwd): print(f'创建学生用户{user}成功') elif choose == '2': b = courese_input() elif choose == '3': look_course = see_course() elif choose == '4': a.see_all_student() elif choose == '5': a.see_all_student_course() elif choose == '6': print('欢迎下次使用') flag = False break else: print('输入不正确,请重新输入') elif a.groupwork == 'student': print(f'欢迎学生{a.user}') while True: print('1,查看课程 2,选择课程 3,查看所选课程 4,退出程序 ') choose = input('请输入你要选择的操作').replace(' ', '') if choose and choose.isnumeric() and int(choose) <= 4 and int(choose) > 0: if choose == '1': look_course = see_course() elif choose == '2': while flag: course = see_course() n = input('请输入课程编号(输入q退出)>>>').replace(' ', '') if n and n.isnumeric() and int(n) <= len(course) and int(n) > 0: n = int(n) - 1 choose_course = Course(course[n]['name'], course[n]['price'], course[n]['month'], course[n]['teacher']) a_course = a.use_course(choose_course) print(a_course) elif n.lower() == 'q': break else: print('输入有误,请重新输入') elif choose == '3': is_course = (a.read_course()) if is_course == False: print('你现在还没有选择课程') else: course_read(is_course) elif choose == '4': print('欢迎下次使用') flag = False break else: print('输入有误,请重新输入') else: print('用户不存在,或者密码错误')