1、admin.py 主要是系統管理初始化,用於管理員註冊、登錄系統、創建授課老師、創建課程等操作。 ...
1、admin.py
主要是系統管理初始化,用於管理員註冊、登錄系統、創建授課老師、創建課程等操作。
#!/usr/bin/env python # -*- coding:utf-8 -*- """ 模擬學校的選課系統 """ import random import time import pickle 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 = teacher_obj self.create_time = time.strftime('%Y-%m-%d %H:%M:%S') self.create_admin = admin #定義管理員類 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 = self.username pickle.dump(self, open(path, 'xb')) #定義創建老師的方法 def create_teacher(admin_obj): teacher_list = [] while True: teacher_name = input("請輸入老師的姓名:(q退出)") if teacher_name == "q": break teacher_age = input("請輸入老師的年齡:") obj = Teacher(teacher_name, teacher_age, admin_obj) teacher_list.append(obj) if os.path.exists("teacher_list"): #判斷老師列表是否存在 exists_list = pickle.load(open('teacher_list', 'rb')) teacher_list.extend(exists_list) pickle.dump(teacher_list, open('teacher_list', 'wb')) #定義創建課程的方法 def create_course(admin_obj): teacher_list = pickle.load(open('teacher_list', '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 = Course(name, cost, teacher_list[int(num) - 1], admin_obj) course_list.append(obj) if os.path.exists("course_list"): #判斷課程列表是否存在 exists_list = pickle.load(open('course_list', 'rb')) course_list.extend(exists_list) pickle.dump(course_list, open('course_list', 'wb')) #定義查看課程表的方法 def look_course(): course_list = pickle.load(open('course_list', 'rb')) for num, item in enumerate(course_list, 1): print(num, item.course_name, item.cost, item.teacher.name, item.create_time, item.create_admin.username) #定義管理員登錄方法 def to_login(user, pwd): if os.path.exists(user): # 從文件中將管理員對象讀取出來(裡面封裝了用戶信息以及提供了登錄訪求) admin_obj = pickle.load(open(user, 'rb')) if admin_obj.login(user, pwd): print('登錄成功!') while True: sel = input("1、創建老師;2、創建課程; 3、查看課程表(q退出程式) \n >>>>") if sel == '1': #創建老師 create_teacher(admin_obj) elif sel == '2': #創建課程 create_course(admin_obj) elif sel == '3': look_course() else: break else: return 1 else: return 0 #定義管理員註冊方法 def to_register(user, pwd): admin_obj = Admin() admin_obj.register(user, pwd) def main(): inp = input("1、管理員登錄; 2、管理員註冊;\n >>>>>") user = input('請輸入用戶名:') pwd = input('請輸入密碼:') if inp == '1': ret = to_login(user, pwd) if ret == 1: print("密碼錯誤!") elif ret == 0: print("用戶不存在!") elif inp == '2': to_register(user, pwd) if __name__ == "__main__": main()