python選課系統完整版
阿新 • • 發佈:2018-08-31
都沒有 自己的 data inpu new one image __file__ imp
一、需求
角色:學校、學員、課程、講師
要求:
1. 創建北京、上海 2 所學校
2. 創建linux , python , go 3個課程 , linux\py 在北京開, go 在上海開
3. 課程包含,周期,價格,通過學校創建課程
4. 通過學校創建班級, 班級關聯課程、講師
5. 創建學員時,選擇學校,關聯班級
5. 創建講師角色時要關聯學校,
6. 提供兩個角色接口
7. 學員視圖, 可以註冊, 交學費, 選擇班級,
8. 講師視圖, 講師可管理自己的班級, 上課時選擇班級, 查看班級學員列表 , 修改所管理的學員的成績
9. 管理視圖,創建講師, 創建班級,創建課程
10. 上面的操作產生的數據都通過pickle序列化保存到文件裏
二、代碼
import os, pickle BASR_DIR = os.path.dirname(os.path.abspath(__file__)) __school_db = os.path.join(BASR_DIR, ‘school_db‘) __teacher_db = os.path.join(BASR_DIR, ‘teacher_db‘) def init_database(): if not os.path.exists(__school_db): bj = School(‘北京‘, ‘北京沙河‘) sh = School(‘View Code上海‘, ‘上海市‘) dic = {bj: [], sh: []} file_opration(__school_db, ‘wb‘, dic) if not os.path.exists(__teacher_db): dic = {} file_opration(__teacher_db, ‘wb‘, dic) def file_opration(file, mode, *args): if mode == ‘wb‘: with open(file, mode)as f: f.write(pickle.dumps(args[0]))if mode == ‘rb‘: with open(file, mode) as f: return pickle.loads(f.read()) class School: def __init__(self, name, addr): self.name = name self.addr = addr def msg_display(self): print(‘校區名稱:\033[1;32m%s\033[0m 校區位置:\033[1;32m %s\033[0m‘ % (self.name, self.addr)) def create_course(self, course_obj, school_dict, file): school_dict[self].append(course_obj) file_opration(file, ‘wb‘, school_dict) class Course: def __init__(self, name, price, time): self.name = name self.price = price self.time = time self.classes = {} def msg_display(self): print(‘【課程】:\033[1;32m%s\033[0m 【價格】:\033[1;32m%s\033[0m 【周期】:\033[1;32m%s\033[0m‘ % ( self.name, self.price, self.time)) def create_class(self, class_obj): self.classes[class_obj.name] = class_obj class Class: def __init__(self, name): self.name = name self.students = {} self.teachers = {} class Teacher: def __init__(self, name): self.name = name class Student: def __init__(self, name, age): self.name = name self.age = age def main_center(): flag = True while flag: print(‘********\033[1;35m歡迎來到管理員系統\033[0m!**********‘) choice = input(‘輸入1:增加課程\n輸入2:增加班級\n輸入3:增加教師\n輸入4:返回\n‘).strip() school_dict = file_opration(__school_db, ‘rb‘) if choice == ‘1‘: for i in school_dict: i.msg_display() while 1: school_choice = input("請輸入需要創建課程的校區名稱:\n") if school_choice == ‘北京‘ or school_choice == ‘上海‘: for i in school_dict: if i.name == school_choice: school_obj = i course_name = input("請輸入課程名稱\n") price = input("請輸入課程價格\n") time = input("請輸入課程周期\n") course_obj = Course(course_name, price, time) school_obj.create_course(course_obj, school_dict, __school_db) print("\033[1;35m增加課程成功\033[0m") break else: print("\033[1;35m別瞎幾把亂輸!輸入學校名稱,懂??\033[0m") elif choice == ‘2‘: for i in school_dict: i.msg_display() while flag: school_choice = input("請輸入需要創建班級的校區名稱:\n") if school_choice == ‘北京‘ or school_choice == ‘上海‘: for i in school_dict: if i.name == school_choice: school_obj = i if not school_dict[school_obj]: print("\033[1;35m你腦袋在想啥?沒建課程,就想創班級??\033[0m") break print(‘******\033[1;32m%s校區已有的課程如下:\033[0m******‘ % (school_choice)) course_name_list = [] for i in school_dict[school_obj]: course_name_list.append(i.name) i.msg_display() while 1: course_name = input("請輸入課程名稱\n") if course_name in course_name_list: for i in school_dict[school_obj]: if i.name == course_name: if i.classes: print("%s課程已有的班級如下:" % (i.name)) for j in i.classes: print(j) else: print("該課程該沒有班級,請創建。") class_name = input("請輸入要新建的班級名稱:\n") new_class_obj = Class(class_name) i.create_class(new_class_obj) file_opration(__school_db, ‘wb‘, school_dict) print("\033[1;32m創建成功!\033[0m") flag = False break break else: print("\033[1;35m輸入已有課程的名字!understand?\033[0m") else: print("\033[1;35m別瞎幾把亂輸!輸入學校名稱,懂??\033[0m") elif choice == ‘3‘: flag = True for i in school_dict: i.msg_display() while flag: school_choice = input("請輸入需要招聘教師的校區名稱:\n") if school_choice == ‘北京‘ or school_choice == ‘上海‘: for i in school_dict: if i.name == school_choice: school_obj = i if not school_dict[school_obj]: print("\033[1;35m你腦袋在想啥?課程都沒有,招老師去幹嗎??\033[0m") break print(‘******\033[1;32m%s校區已有的課程如下:\033[0m******‘ % (school_choice)) course_name_list = [] for i in school_dict[school_obj]: course_name_list.append(i.name) i.msg_display() flag2 = True while flag2: course_name = input("請輸入需要招聘教師的課程名稱\n") if course_name in course_name_list: for i in school_dict[school_obj]: if i.name == course_name: if i.classes: print("%s課程已有的班級如下:" % (i.name)) for j in i.classes: print(j) else: print("該課程該沒有班級,請創建後再進行教師招聘。") flag2 = False flag = False break while 1: class_name = input("請輸入要招聘教師的班級名稱:\n").strip() teacher_name = input("請輸入教師名字:\n") teacher_obj = Teacher(teacher_name) if class_name in i.classes: i.classes[class_name].teachers[teacher_name] = teacher_obj file_opration(__school_db, ‘wb‘, school_dict) print("\033[1;32m招聘成功!\033[0m") else: print("\033[1;35m輸入班級名稱!!!\033[0m") flag = False break break else: print("\033[1;35m輸入已有課程的名字!understand?\033[0m") else: print("\033[1;35m別瞎幾把亂輸!輸入學校名稱,懂??\033[0m") elif choice == ‘4‘: break else: print("\033[1;35m別瞎幾把亂輸!輸入1、2、3,懂??\033[0m") def studet_center(): print(‘********\033[1;35m歡迎來到學生系統\033[0m!**********‘) school_dict = file_opration(__school_db, ‘rb‘) while 1: choice = input(‘輸入1:報名\n輸入2:返回\n‘).strip() if choice == ‘1‘: student_name = input("請輸入您的姓名:\n").strip() student_age = input("請輸入您的年齡:\n").strip() student_obj = Student(student_name, student_age) flag = True for i in school_dict: i.msg_display() while flag: school_choice = input("請輸入需要要報名的校區名稱:\n") if school_choice == ‘北京‘ or school_choice == ‘上海‘: for i in school_dict: if i.name == school_choice: school_obj = i if not school_dict[school_obj]: print("\033[1;35m你腦袋在想啥?課程都沒有,報啥名??\033[0m") break print(‘******\033[1;32m%s校區已有的課程如下:\033[0m******‘ % (school_choice)) course_name_list = [] for i in school_dict[school_obj]: course_name_list.append(i.name) i.msg_display() flag2 = True while flag2: course_name = input("請輸入需要加入的課程名稱\n") if course_name in course_name_list: for i in school_dict[school_obj]: if i.name == course_name: if i.classes: print("%s課程已有的班級如下:" % (i.name)) for j in i.classes: print(j) else: print("該課程班級都沒有,報啥子名,去哪上課??") flag2 = False flag = False break while 1: class_name = input("請輸入加入的班級名稱:\n").strip() if class_name in i.classes: print("這門課要%s元,請確認你有錢嗎?" % (i.price)) stu_choice = input("這麽貴你還要報名嗎?Y/N\n").strip() if stu_choice.lower() == ‘y‘: i.classes[class_name].students[student_name] = student_obj file_opration(__school_db, ‘wb‘, school_dict) print("\033[1;32m報名成功!\033[0m") flag = False break else: print("\033[1;32m窮鬼!!\033[0m\n") flag = False break else: print("\033[1;35m輸入班級名稱!!!\033[0m") break else: print("\033[1;35m輸入已有課程的名字!understand?\033[0m") else: print("\033[1;35m別瞎幾把亂輸!輸入學校名稱,懂??\033[0m") elif choice == ‘2‘: break else: print("\033[1;35m別瞎幾把亂輸!輸入1、2,懂??\033[0m") def teacher_center(): print(‘********\033[1;35m歡迎來到教師管理系統\033[0m!**********‘) teacher_name = input("請輸入您的姓名").strip() school_dict = file_opration(__school_db, ‘rb‘) while 1: choice = input(‘輸入1:查看學生信息 輸入2:選擇班級上課 輸入3:返回\n‘).strip() teacher_exit = False if choice == ‘1‘: for i in school_dict: for course_obj in school_dict[i]: for class_obj in course_obj.classes.values(): if teacher_name in class_obj.teachers: teacher_exit = True print("***您的班級:\033[1;35m%s\033[0m,當前學生如下:" % (class_obj.name)) for i in class_obj.students: print(i, end=‘ ‘) print() if teacher_exit == False: print("沒有這個老師,請重新檢查後再輸入") break elif choice == ‘2‘: class_dic = {} for i in school_dict: for course_obj in school_dict[i]: for class_obj in course_obj.classes.values(): if teacher_name in class_obj.teachers: teacher_exit = True class_dic[class_obj.name] = class_obj if teacher_exit == False: print("沒有這個老師,請重新檢查後再輸入") break print("您當前授課的班級如下請輸出班級名稱:") for j in class_dic: print(j, end=‘‘) print() cls_choice = input(">>>:").strip() if cls_choice in class_dic: print("成功在\033[1;32m%s\033[0m上課" % (cls_choice)) elif choice == ‘3‘: break else: print("\033[1;35m別瞎幾把亂輸!輸入1、2、3,懂??\033[0m") def main(): print(‘********\033[1;35m歡迎來到老男孩學生管理系統\033[0m!**********‘) while 1: choice = input(‘輸入1:學生登錄\n輸入2:教師登錄\n輸入3:管理員登錄\n輸入q:退出\n‘).strip() if choice == ‘1‘: studet_center() elif choice == ‘2‘: teacher_center() elif choice == ‘3‘: main_center() elif choice.upper() == ‘Q‘: break else: print("\033[1;35m別瞎幾把亂輸!輸入學校名稱,懂??\033[0m") if __name__ == ‘__main__‘: init_database() main()
三、關於程序的相關說明
1、這個系統支持一個課程有多個班級,一個同學選擇多門課程,一個老師任教多個班級。
2、系統功能基本實現,但是代碼還很low,而且還沒拆分程序,做文件目錄。
三、測試
登錄界面
增加課程
增加班級(選擇學校會顯示已有的課程,班級):
增加教師:
學生報名:
老師方法:
註:這個系統支持一個課程有多個班級,一個同學選擇多門課程,一個老師任教多個班級。以上代碼僅是部分功能測試。
python選課系統完整版