一、系統目錄結構python
文件夾註解:app
bin--系統管理員和學生的主程序代碼less
config--系統的配置文件ide
db--系統的數據文件spa
admin--管理員的數據文件
3d
student--學生的數據文件code
lib--系統的公共類文件對象
一、admin.pyblog
主要用於管理員註冊、登陸系統、建立授課老師、建立課程等操做。utf-8
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 """ 4 管理員可操做的方法 5 """ 6 import os 7 import sys 8 sys.path.append(os.path.dirname(os.path.dirname(__file__))) 9 import pickle 10 import os 11 from lib import models 12 from config import settings 13 from lib.models import Course 14 from lib.models import Teacher 15 from lib.models import Admin 16 17 18 def create_teacher(admin_obj): 19 """ 20 管理員建立老師 21 :param admin_obj: 22 :return: 23 """ 24 teacher_list = [] 25 while True: 26 teacher_name = input("請輸入老師的姓名:(q退出)") 27 if teacher_name == "q": 28 break 29 teacher_age = input("請輸入老師的年齡:") 30 obj = models.Teacher(teacher_name, teacher_age, admin_obj) 31 teacher_list.append(obj) 32 if os.path.exists("teacher_list"): #判斷老師列表是否存在 33 exists_list = pickle.load(open(settings.TEACHER_DB_DIR, 'rb')) 34 teacher_list.extend(exists_list) 35 pickle.dump(teacher_list, open(settings.TEACHER_DB_DIR, 'wb')) 36 37 def create_course(admin_obj): 38 """ 39 管理員建立課程 40 :param admin_obj: 41 :return: 42 """ 43 teacher_list = pickle.load(open(settings.TEACHER_DB_DIR, 'rb')) 44 for num, item in enumerate(teacher_list, 1): 45 print(num, item.name, item.age, item.create_time, item.create_admin.username) 46 course_list = [] 47 while True: 48 name = input("請輸入課程名(q退出):") 49 if name == "q": 50 break 51 cost = input("請輸入課時費:") 52 num = input('請選擇老師(序號):') 53 obj = models.Course(name, cost, teacher_list[int(num) - 1], admin_obj) 54 course_list.append(obj) 55 if os.path.exists(settings.COURSE_DB_DIR): #判斷課程列表是否存在 56 exists_list = pickle.load(open(settings.COURSE_DB_DIR, 'rb')) 57 course_list.extend(exists_list) 58 pickle.dump(course_list, open(settings.COURSE_DB_DIR, 'wb')) 59 60 def look_course(): 61 """ 62 管理員查看課程表 63 :return: 64 """ 65 course_list = pickle.load(open(settings.COURSE_DB_DIR, 'rb')) 66 for num, item in enumerate(course_list, 1): 67 print(num, item.course_name, item.cost, item.teacher.name, item.create_time, item.create_admin.username) 68 69 def to_login(user, pwd): 70 """ 71 管理員登陸的方法 72 :param user: 73 :param pwd: 74 :return: 75 """ 76 if os.path.exists(os.path.join(settings.BASE_ADMIN_DIR, user)): 77 # 從文件中將管理員對象讀取出來(裏面封裝了用戶信息以及提供了登陸訪求) 78 admin_obj = pickle.load(open(os.path.join(settings.BASE_ADMIN_DIR, user), 'rb')) 79 if admin_obj.login(user, pwd): 80 print('登陸成功!') 81 while True: 82 sel = input("一、建立老師;二、建立課程; 三、查看課程表(q退出程序) \n >>>>") 83 if sel == '1': #建立老師 84 create_teacher(admin_obj) 85 elif sel == '2': #建立課程 86 create_course(admin_obj) 87 elif sel == '3': 88 look_course() 89 else: 90 break 91 else: 92 return 1 93 else: 94 return 0 95 96 def to_register(user, pwd): 97 """ 98 管理員註冊的方法 99 :param user: 100 :param pwd: 101 :return: 102 """ 103 admin_obj = models.Admin() 104 admin_obj.register(user, pwd) 105 106 def main(): 107 inp = input("一、管理員登陸; 二、管理員註冊;\n >>>>>") 108 user = input('請輸入用戶名:') 109 pwd = input('請輸入密碼:') 110 if inp == '1': 111 ret = to_login(user, pwd) 112 if ret == 1: 113 print("密碼錯誤!") 114 elif ret == 0: 115 print("用戶不存在!") 116 117 elif inp == '2': 118 to_register(user, pwd) 119 120 121 if __name__ == "__main__": 122 main()
二、students.py
主要是學生註冊、登陸系統,選課等操做。
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 """ 4 學生可操做的方法 5 """ 6 import os 7 import sys 8 import pickle 9 sys.path.append(os.path.dirname(os.path.dirname(__file__))) 10 11 from lib.models import Course 12 from lib.models import Admin 13 from lib.models import Teacher 14 from config import settings 15 from lib import models 16 17 def to_select_course(student_obj): 18 """ 19 學生選課的方法 20 :param student_obj: 21 :return: 22 """ 23 course_list = pickle.load(open(settings.COURSE_DB_DIR, 'rb')) 24 for num, item in enumerate(course_list, 1): 25 print(num, item.course_name, item.cost, item.teacher.name) 26 while True: 27 num = input("請選擇課程(q退出):") 28 if num == 'q': 29 break 30 select_course_obj = course_list[int(num) - 1] 31 if select_course_obj not in student_obj.course_list: 32 student_obj.course_list.append(select_course_obj) 33 pickle.dump(student_obj, open(os.path.join(settings.BASE_STUDENTS_DIR, student_obj.username), 'wb')) 34 35 def course_info(student_obj): 36 """ 37 查看學生已選擇的課程 38 :return: 39 """ 40 for item in student_obj.course_list: 41 print(item.course_name, item.teacher.name) 42 43 def to_register(user, pwd): 44 """ 45 學生註冊 46 :param user: 47 :param pwd: 48 :return: 49 """ 50 obj = models.Student() 51 obj.register(user, pwd) 52 53 def to_login(user, pwd): 54 """ 55 學生登陸 56 :param user: 57 :param pwd: 58 :return: 59 """ 60 if os.path.exists(os.path.join(settings.BASE_STUDENTS_DIR, user)): 61 student_obj = pickle.load(open(os.path.join(settings.BASE_STUDENTS_DIR, user), 'rb')) 62 if student_obj.login(user, pwd): 63 inp = input('一、選課;二、上課;三、查看已選課程 \n >>>') 64 if inp == '1': 65 to_select_course(student_obj) 66 elif inp == '3': 67 course_info(student_obj) 68 elif inp == '2': 69 pass 70 else: 71 print('密碼錯誤') 72 else: 73 print("學生用戶不存在") 74 75 def main(): 76 inp = input("一、學生登陸;二、學生註冊;\n >>>") 77 user = input("請輸入學生用戶名:") 78 pwd = input("請輸入登陸密碼:") 79 if inp == '1': 80 to_login(user, pwd) 81 elif inp == '2': 82 to_register(user, pwd) 83 84 85 if __name__ == "__main__": 86 main()
三、settings.py
系統主要路徑參數的設置文件
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import os 4 #定義路徑變量 5 BASE_DIR = os.path.dirname(os.path.dirname(__file__)) 6 BASE_ADMIN_DIR = os.path.join(BASE_DIR, "db", "admin") 7 BASE_STUDENTS_DIR = os.path.join(BASE_DIR, "db", "students") 8 TEACHER_DB_DIR = os.path.join(BASE_DIR, "db", "teacher_list") 9 COURSE_DB_DIR = os.path.join(BASE_DIR, "db", "course_list")
四、models.py
系統公共類的代碼實現
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 """ 4 用於存儲老師、管理員、學生類 5 """ 6 7 import time 8 import pickle 9 from config import settings 10 import os 11 12 13 #定義老師類 14 class Teacher: 15 """ 16 封裝老師的相關信息 17 """ 18 def __init__(self, name, age, admin): 19 self.name = name 20 self.age = age 21 self.__assets = 0 22 self.create_time = time.strftime('%Y-%m-%d %H:%M:%S') 23 self.create_admin = admin 24 25 def gain(self, cost): 26 """ 27 增長資產 28 :param cost: 增長的數量 29 :return: 30 """ 31 self.__assets += cost 32 33 def decrease(self, cost): 34 """ 35 減小資產 36 :param cost: 減小的數量 37 :return: 38 """ 39 self.__assets -= cost 40 41 #定義課程類 42 class Course: 43 """ 44 課程相關信息 45 """ 46 def __init__(self, course_name, cost, teacher_obj, admin): 47 self.course_name = course_name 48 self.cost = cost 49 self.teacher = teacher_obj 50 self.create_time = time.strftime('%Y-%m-%d %H:%M:%S') 51 self.create_admin = admin 52 53 #定義管理員類 54 class Admin: 55 """ 56 封裝管理員 57 """ 58 def __init__(self): 59 self.username = None 60 self.password = None 61 62 def login(self, user, pwd): 63 """ 64 管理員登陸 65 :param user: 66 :param pwd: 67 :return: 68 """ 69 if self.username == user and self.password == pwd: 70 return True 71 else: 72 return False 73 74 def register(self, user, pwd): 75 """ 76 管理員註冊 77 :param user: 78 :param pwd: 79 :return: 80 """ 81 self.username = user 82 self.password= pwd 83 84 path = os.path.join(settings.BASE_ADMIN_DIR, self.username) 85 pickle.dump(self, open(path, 'xb')) 86 87 #定義學生類 88 class Student: 89 """ 90 學生相關信息 91 """ 92 def __init__(self): 93 self.username = None 94 self.password = None 95 96 self.course_list = [] 97 self.study_dict = {} 98 99 def select_course(self, course_obj): 100 """ 101 學生選課 102 :param course_obj: 103 :return: 104 """ 105 self.course_list.append(course_obj) 106 107 def register(self, user, pwd): 108 """ 109 學生註冊 110 :param user: 111 :param pwd: 112 :return: 113 """ 114 self.username = user 115 self.password = pwd 116 path = os.path.join(settings.BASE_STUDENTS_DIR, self.username) 117 pickle.dump(self, open(path, 'xb')) 118 119 def login(self, user, pwd): 120 """ 121 學生登陸 122 :param user: 123 :param pwd: 124 :return: 125 """ 126 if self.username == user and self.password == pwd: 127 return True 128 else: 129 return False 130 131 def study(self, course_obj): 132 """ 133 學生上課 134 :param course_obj: 135 :return: 136 """ 137 class_result = course_obj.have_lesson() 138 if course_obj in self.study_dict.keys(): 139 self.study_dict[course_obj].append(class_result) 140 else: 141 self.study_dict[course_obj] = [class_result,]