# 選課系統
# 角色:學校、學員、課程、講師
# 要求:
# 1. 創建北京、上海 2 所學校
# 2. 創建linux , python , go 3個課程 , linux\py 在北京開, go 在上海開
# 3. 課程包含,周期,價格,通過學校創建課程
# 4. 通過學校創建班級, 班級關聯課程、講師
# 5. 創建學員時,選擇學校,關聯班級
# 5. 創建講師角色時要關聯學校,
# 6. 提供兩個角色介面
# 6.1 學員視圖, 可以登錄,注冊, 選擇學校,選擇課程,查看成績
# 6.2 講師視圖, 講師登錄,選擇學校,選擇課程, 查看課程下學員串列 , 修改所管理的學員的成績
# 6.3 管理視圖,登錄,注冊,創建講師, 創建班級,創建課程,創建學校
# 7. 上面的操作產生的資料都通過pickle序列化保存到檔案里
分析:
角色:
管理員:
注冊
登錄
創建校區
創建老師
創建課程
老師:
登錄
選擇教授課程
查看課程下學生
修改學生的成績
學生:
注冊
登錄
選擇校區
選擇課程
查看成績
類:
抽出共有屬性方法:Base
學校:School
學員:Student
課程:Course
講師:Teacher
各個類屬性和方法:
Base:
公用方法:
存資料:save
取資料:select
學校:
屬性:
學校名字:school_name str
學校地址:school_addr str
開設的課程:course_list list
學員:
屬性:
學員名字:student_name str
學員密碼:student_pwd str
學員所屬校區:school str
學員課程: course_list list
學員成績: course_score dict
方法:
學員選擇校區 choose_school(school_name)
列出所有校區的資訊,學員選擇
學員選擇課程 choose_course(school_name)
學員必須先選擇校區,列出該校區包含的課程
選擇相應的課程,添加到學員課程串列中,并且將用戶系結給課程
講師:
屬性:
講師名字:teacher_name str
講師密碼:teacher_pwd str
講師課程: course_list list
方法:
講師選擇課程: choose_course(school_name, course_name)
先選擇校區,列出該校區所有課程,講師選擇課程,如果沒有選過,則添加
講師修改學生的成績:change_student_score(course, student_name)
通過自身物件中的課程串列,選擇相應課程,呼叫課程下的所有學生資訊,更改學生成績
管理員:
屬性:
管理員的名字:admin_name str
管理員的密碼:admin_pwd str
方法:
管理員創建學校: create_school(school_name,school_addr)
直接輸入學校的名字和地址,然后判斷學校是否存在,不存在,則創建
管理員創建講師: create_teacher(teacher_name)
直接創建講師,密碼為默認
管理員創建課程: create_course(school_name, course_name)
先選擇校區,再創建課程名字,如果不存在,則創建這個課程
那個角色登陸了就執行那個角色的功能
進入到角色選項里面了可以隨時退出
下面是程式說明程圖
-------------------------------------------------------------

目錄結構圖

一 ,下面是業務邏輯代碼
1.conf下settings.py下代碼
1 import os 2 3 BASE_DIR = os.path.dirname(os.path.dirname(__file__)) 4 BASE_DB = os.path.join(BASE_DIR, 'db')View Code
2.core下admin.py代碼
1 from interface import student_interface, teacher_interface, admin_interface, common_interface 2 from lib import common 3 4 admin_info = { 5 'name': None 6 } 7 8 9 def admin_register(): 10 print('管理員注冊') 11 if admin_info['name']: 12 print('管理員已經登陸了不能注冊') 13 return 14 while True: 15 name = input('請輸入名字:').strip() 16 password = input('請輸入密碼:').strip() 17 conf_password = input('請確認密碼:').strip() 18 if password == conf_password: 19 flag, msg = admin_interface.admin_register_interface(name, password) 20 if flag: 21 print(msg) 22 break 23 else: 24 print(msg) 25 else: 26 print('兩次密碼不一致') 27 28 29 def admin_login(): 30 print('管理員登陸') 31 if admin_info['name']: 32 print('管理員已經登陸了不能重復登陸') 33 return 34 while True: 35 name = input('請輸入名字: ').strip() 36 password = input('請輸入密碼: ').strip() 37 flag, msg = common_interface.login_interface(name, password, 'admin') 38 if flag: 39 admin_info['name'] = name 40 print(msg) 41 break 42 else: 43 print(msg) 44 45 46 @common.login_auth(user_type='admin') 47 def create_school(): 48 print('創建學校') 49 school_name = input('請輸入學校名字: ').strip() 50 address = input('請輸入學校地址: ').strip() 51 flag, msg = admin_interface.create_school_interface(admin_info['name'], school_name,address) 52 if flag: 53 print(msg) 54 else: 55 print(msg) 56 57 58 59 60 @common.login_auth(user_type='admin') 61 def create_teacher(): 62 print('創建老師') 63 name = input('請輸入老師名字: ').strip() 64 flag, msg = admin_interface.create_teacher_interface(admin_info['name'], name) 65 if flag: 66 print(msg) 67 else: 68 print(msg) 69 70 71 72 73 @common.login_auth(user_type='admin') 74 def create_course(): 75 print('創建課程') 76 while True: 77 school_list = common_interface.check_all_schools() 78 if school_list: 79 for i, school in enumerate(school_list): 80 print('%s: %s' % (i, school)) 81 choose = input('請選擇學校: ').strip() 82 if choose.isdigit(): 83 choose = int(choose) 84 if choose >= len(school_list): 85 continue 86 name = input('請輸入課程名字:').strip() 87 flag, msg = admin_interface.create_course_interface(admin_info['name'], name, school_list[choose]) 88 if flag: 89 print(msg) 90 break 91 else: 92 print(msg) 93 94 95 96 97 func_dic = { 98 '1': admin_register, 99 '2': admin_login, 100 '3': create_school, 101 '4': create_teacher, 102 '5': create_course, 103 } 104 105 106 def admin_view(): 107 while True: 108 print(""" 109 1 注冊 110 2 登陸 111 3 創建學校 112 4 創建老師 113 5 創建課程 114 """) 115 116 choice = input('請選擇功能:').strip() 117 if choice == 'q' and 'Q': 118 break 119 120 if choice not in func_dic: 121 continue 122 123 func_dic[choice]()View Code
3.core下student.py代碼
1 from interface import student_interface, common_interface 2 from lib import common 3 4 student_info = { 5 'name': None 6 } 7 8 9 def student_register(): 10 print('學生注冊') 11 if student_info['name']: 12 print('已經登陸了不能在注冊了') 13 return 14 while True: 15 name = input('請輸入學生名字: ').strip() 16 password = input('請輸入學生密碼').strip() 17 conf_password = input('請確認密碼: ').strip() 18 if password == conf_password: 19 flag, msg = student_interface.student_register_interface(name, password) 20 if flag: 21 print(msg) 22 break 23 else: 24 print(msg) 25 else: 26 print('兩次密碼不一致') 27 28 29 def student_login(): 30 print('學生登陸') 31 if student_info['name']: 32 print('已經登陸了不能重復登陸') 33 return 34 while True: 35 name = input('請輸入學生名字: ').strip() 36 password = input('請輸入學生密碼: ').strip() 37 flag, msg = common_interface.login_interface(name, password, 'student') 38 if flag: 39 print(msg) 40 student_info['name'] = name 41 break 42 else: 43 print(msg) 44 45 46 @common.login_auth('student') 47 def choose_school(): 48 print('選擇學校') 49 school_list = common_interface.check_all_schools() 50 if not school_list: 51 print('請聯系管理員創建學校') 52 return 53 for i, school in enumerate(school_list): 54 print('%s; %s' % (i, school)) 55 choice = input('請選擇學校: ').strip() 56 if choice.isdigit(): 57 choice = int(choice) 58 if choice < len(school_list): 59 flag, msg = student_interface.choice_school_interface(student_info['name'], school_list[choice]) 60 print(msg) 61 62 63 @common.login_auth('student') 64 def choose_course(): 65 print('選擇課程') 66 flag, course_list = student_interface.get_can_choose_course_interface(student_info['name']) 67 if not flag: 68 print(course_list) 69 return 70 for i, course in enumerate(course_list): 71 print('%s: %s' % (i, course)) 72 choice = input('請選擇課程').strip() 73 if choice.isdigit(): 74 choice = int(choice) 75 if choice < len(course_list): 76 _, msg = student_interface.choose_course_interface(student_info['name'], course_list[choice]) 77 print(msg) 78 else: 79 print('請選擇已經存在的課程') 80 else: 81 print('必須要輸入數字') 82 83 84 @common.login_auth('student') 85 def check_score(): 86 print('查看分數') 87 course_score_dict = student_interface.check_score_interface(student_info['name']) 88 print(course_score_dict) 89 90 91 92 func_dic = { 93 '1': student_register, 94 '2': student_login, 95 '3': choose_school, 96 '4': choose_course, 97 '5': check_score, 98 } 99 100 101 def student_view(): 102 while True: 103 print(""" 104 1 注冊 105 2 登陸 106 3 選擇學校 107 4 選擇課程 108 5 查看成績 109 """) 110 111 choice = input('請選擇功能:').strip() 112 if choice == 'q' and 'Q': 113 break 114 if choice not in func_dic: 115 continue 116 func_dic[choice]()View Code
4.core下teacher.py代碼
1 from interface import common_interface, teacher_interface 2 from lib import common 3 4 5 teacher_info = { 6 'name': None 7 } 8 9 10 def teacher_login(): 11 print('老師登陸') 12 if teacher_info['name']: 13 print('老師已經登陸了不能重復登陸') 14 return 15 while True: 16 name = input('請輸入老師名字: ').strip() 17 password = input('請輸入老師密碼: ').strip() 18 flag, msg = common_interface.login_interface(name, password, 'teacher') 19 if flag: 20 print(msg) 21 teacher_info['name'] = name 22 break 23 else: 24 print(msg) 25 26 27 @common.login_auth('teacher') 28 def choose_teach_course(): 29 print('選擇要教授的課程') 30 while True: 31 course_list = teacher_interface.get_all_course() 32 if not course_list: 33 print('現在還沒有課程,請聯系管理員創建課程') 34 break 35 else: 36 for i, course_list in enumerate(course_list): 37 print('%s; %s' % (i, course_list)) 38 choice = input('請選擇課程: ').strip() 39 if choice.isdigit(): 40 choice = int(choice) 41 if choice >= len(course_list): continue 42 flag, msg = teacher_interface.choose_teacher_course_interface(teacher_info['name'], course_list[choice]) 43 if flag: 44 print(msg) 45 break 46 else: 47 print(msg) 48 else: 49 print('必須輸入數字') 50 51 52 @common.login_auth('teacher') 53 def check_course(): 54 print('查看教授的課程') 55 course_list = teacher_interface.check_all_teacher_course(teacher_info['name']) 56 if course_list: 57 for course in course_list: 58 print(course) 59 else: 60 print('你還沒有選擇課程, 先去選擇課程吧') 61 62 63 @common.login_auth('teacher') 64 def check_student(): 65 count = 0 66 print('查看學生') 67 course_list = teacher_interface.check_all_teacher_course(teacher_info['name']) 68 if course_list: 69 for i, course in enumerate(course_list): 70 print(i, course) 71 choice = input('選擇要查看課程下的學生: ').strip() 72 if choice.isdigit(): 73 choice = int(choice) 74 if choice < len(course_list): 75 student_list = teacher_interface.check_student_in_course(course_list[choice]) 76 for j, student in enumerate(student_list): 77 count += j 78 print('%s : %s 一共有%s個學生' % (j, student, count)) 79 else: 80 print('請選擇的課程') 81 else: 82 print('請輸入數字') 83 84 85 @common.login_auth('teacher') 86 def modify_score(): 87 count = 0 88 print('查看學生') 89 course_list = teacher_interface.check_all_teacher_course(teacher_info['name']) 90 if course_list: 91 for i, course in enumerate(course_list): 92 print(i, course) 93 choice = input('選擇要查看課程下的學生: ').strip() 94 if choice.isdigit(): 95 choice = int(choice) 96 if choice < len(course_list): 97 student_list = teacher_interface.check_student_in_course(course_list[choice]) 98 for j, student in enumerate(student_list): 99 count += j 100 print('%s : %s 一共有%s個學生' % (j, student, count)) 101 choose = input('請選擇學生: ').strip() 102 if choose.isdigit(): 103 choose = int(choose) 104 if choose < len(student_list): 105 score = input('請輸入學生要修改的的分數: ').strip() 106 if score.isdigit(): 107 score = int(score) 108 flag, msg = teacher_interface.modify_score(teacher_info['name'], 109 course_list[choice], 110 student_list[choose], score) 111 print(msg) 112 else: 113 print('分數必須輸入數字') 114 else: 115 print('請選擇存在的學生') 116 else: 117 print('請選擇存在的課程') 118 else: 119 print('請輸入數字') 120 else: 121 print('請選擇課程') 122 123 124 func_dic = { 125 '1': teacher_login, 126 '2': choose_teach_course, 127 '3': check_course, 128 '4': check_student, 129 '5': modify_score, 130 } 131 132 133 def teacher_view(): 134 while True: 135 print(""" 136 1 登陸 137 2 選擇課程 138 3 查看課程 139 4 查看學生 140 5 修改學生成績 141 """) 142 143 choice = input('請選擇功能:').strip() 144 if choice == 'q' and 'Q': 145 break 146 if choice not in func_dic: 147 continue 148 func_dic[choice]()View Code
5.core下src.py代碼
1 from core import admin, student, teacher 2 3 func_dic = { 4 '1': admin.admin_view, 5 '2': student.student_view, 6 '3': teacher.teacher_view 7 8 } 9 10 11 def run(): 12 while True: 13 print(""" 14 1 管理員視圖 15 2 學生視圖 16 3 老師視圖 17 """) 18 choice = input('請選擇:').strip() 19 if choice not in func_dic: 20 continue 21 func_dic[choice]()View Code
二 ,下面是db檔案代碼
1.db_handler.py代碼
1 from conf import settings 2 import os 3 import pickle 4 5 6 def save(obj): 7 path_dir = os.path.join(settings.BASE_DB, obj.__class__.__name__.lower()) 8 if not os.path.isdir(path_dir): 9 os.mkdir(path_dir) 10 path_obj = os.path.join(path_dir, obj.name) 11 with open(path_obj, 'wb') as f: 12 pickle.dump(obj, f) 13 14 15 def select(name, dir_type): 16 path_dir = os.path.join(settings.BASE_DB, dir_type) 17 if not os.path.isdir(path_dir): 18 os.mkdir(path_dir) 19 path_obj = os.path.join(path_dir, name) 20 if os.path.exists(path_obj): 21 with open(path_obj, 'rb')as f: 22 return pickle.load(f) 23 else: 24 return NoneView Code
2.modles.py
1 from db import db_handler 2 3 4 class BaseClass: 5 def save(self): 6 db_handler.save(self) 7 8 @classmethod 9 def get_obj_by_name(cls, name): 10 return db_handler.select(name, cls.__name__.lower()) 11 12 13 class Admin(BaseClass): 14 def __init__(self, name, password): 15 self.name = name 16 self.password = password 17 self.save() 18 19 20 def create_school(self, school_name, addr): 21 School(school_name, addr) 22 23 24 def create_course(self, name): 25 Course(name) 26 27 28 def create_teacher(self, name, password): 29 Teacher(name, password) 30 31 32 class Teacher(BaseClass): 33 def __init__(self, name, password): 34 self.name = name 35 self.password = password 36 self.course_list = [] 37 self.save() 38 39 def add_course(self, course_name): 40 self.course_list.append(course_name) 41 self.save() 42 43 def modify_score(self, student, course_name, score): 44 student.score[course_name] = score 45 student.save() 46 47 48 class Student(BaseClass): 49 def __init__(self, name, password): 50 self.name = name 51 self.password = password 52 self.score = {} 53 self.school = None 54 self.course_list = [] 55 self.save() 56 57 def get_school(self): 58 return self.school 59 60 def choose_school(self, school_name): 61 self.school = school_name 62 self.save() 63 64 def add_course(self, course_name): 65 self.course_list.append(course_name) 66 self.score[course_name] = 0 67 self.save() 68 69 70 71 72 class School(BaseClass): 73 def __init__(self, name, addr): 74 self.name = name 75 self.addr = addr 76 self.course_list = [] 77 self.save() 78 79 def add_course(self, course_name): 80 self.course_list.append(course_name) 81 self.save() 82 83 84 85 class Course(BaseClass): 86 def __init__(self, name): 87 self.name = name 88 self.student_list = [] 89 self.save()View Code
三 ,下面是介面api檔案 interface
1.admin_interface
1 from db import modles 2 3 4 def admin_register_interface(name, password): 5 admin_obj = modles.Admin.get_obj_by_name(name) 6 if admin_obj: 7 return False, '管理員已經存在' 8 else: 9 modles.Admin(name, password) 10 return True, '注冊成功' 11 12 13 def create_school_interface(admin_name, school_name, addr): 14 school_obj = modles.School.get_obj_by_name(school_name) 15 if school_obj: 16 return False, '學校已經存在' 17 else: 18 admin_obj = modles.Admin.get_obj_by_name(admin_name) 19 admin_obj.create_school(school_name, addr) 20 return True, '學校創建成功' 21 22 23 def create_teacher_interface(admin_name, name, password = '666'): 24 obj = modles.Teacher.get_obj_by_name(name) 25 if obj: 26 return False, '老師已經存在' 27 else: 28 admin_obj = modles.Admin.get_obj_by_name(admin_name) 29 admin_obj.create_teacher(name, password) 30 return True, '老師創建成功' 31 32 33 def create_course_interface(admin_name, course_name, school_name): 34 obj = modles.Course.get_obj_by_name(course_name) 35 if obj: 36 return False, '課程已經存在' 37 else: 38 admin_obj = modles.Admin.get_obj_by_name(admin_name) 39 admin_obj.create_course(course_name) 40 school_obj = modles.School.get_obj_by_name(school_name) 41 school_obj.add_course(course_name) 42 return True, '課程已經創建成功'View Code
2.common_interface
1 from db import modles 2 import os 3 from conf import settings 4 from lib import common 5 6 7 def login_interface(name, password, user_type): 8 if user_type == 'admin': 9 obj = modles.Admin.get_obj_by_name(name) 10 elif user_type == 'teacher': 11 obj = modles.Teacher.get_obj_by_name(name) 12 elif user_type == 'student': 13 obj = modles.Student.get_obj_by_name(name) 14 else: 15 return False, '沒有這個用戶型別' 16 if obj: 17 if obj.name == name and obj.password == password: 18 return True, '%s: %s 登陸成功' % (user_type, name) 19 else: 20 return False, '密碼不對' 21 22 else: 23 return False, '用戶不存在' 24 25 26 def check_all_schools(): 27 path = os.path.join(settings.BASE_DB, 'school') 28 return common.get_all_dir_obj(path) 29 30 31 def choose_course_interface(student_name, course_name): 32 obj = modles.Student.get_obj_by_name(student_name) 33 obj.add_course(course_name) 34 return True, '選課成功'View Code
3.student_interface
1 from db import modles 2 3 4 def student_register_interface(name, password): 5 student_obj = modles.Student.get_obj_by_name(name) 6 if student_obj: 7 return False, '學生已經存在' 8 else: 9 modles.Student(name, password) 10 return True, '注冊成功' 11 12 13 def choice_school_interface(student_name, school_name): 14 student_obj = modles.Student.get_obj_by_name(student_name) 15 school = student_obj.get_school() 16 if not school: 17 student_obj.choose_school(school_name) 18 return True, '%s: 選擇學校成功' % (student_name) 19 else: 20 return False, '已經選擇學校了, 不能重復選擇學校' 21 22 23 def get_can_choose_course_interface(student_name): 24 student_obj = modles.Student.get_obj_by_name(student_name) 25 if not student_obj.school: 26 return False, '你沒有選擇學校,請先選擇學校' 27 school_obj = modles.School.get_obj_by_name(student_obj.school) 28 if school_obj.course_list: 29 return True, school_obj.course_list 30 else: 31 return False, "該學校下沒有課程" 32 33 34 def choose_course_interface(student_name, course_name): 35 obj = modles.Student.get_obj_by_name(student_name) 36 obj.add_course(course_name) 37 return True, '選課成功' 38 39 40 def check_score_interface(student_name): 41 obj = modles.Student.get_obj_by_name(student_name) 42 return obj.scoreView Code
4.teacher_nterface
1 from db import modles 2 import os 3 from conf import settings 4 from lib import common 5 6 7 def get_all_course(): 8 path = os.path.join(settings.BASE_DB, 'course') 9 return common.get_all_dir_obj(path) 10 11 12 def choose_teacher_course_interface(teacher_name, course_name): 13 teacher_obj = modles.Teacher.get_obj_by_name(teacher_name) 14 if course_name not in teacher_obj.course_list: 15 teacher_obj.add_course(course_name) 16 return True, '選擇課程成功' 17 else: 18 return False, '你已經選擇了本門課程' 19 20 21 def check_all_teacher_course(teacher_name): 22 teacher_obj = modles.Teacher.get_obj_by_name(teacher_name) 23 return teacher_obj.course_list 24 25 26 def check_student_in_course(course_name): 27 course_obj = modles.Course.get_obj_by_name(course_name) 28 return course_obj.student_list 29 30 31 def modify_score(teacher_name, course_name, student_name, score): 32 teacher_obj = modles.Teacher.get_obj_by_name(teacher_name) 33 student_obj = modles.Student.get_obj_by_name(student_name) 34 teacher_obj.modify_score(student_obj, course_name, score) 35 return True, '修改分數成功'View Code
四 , 下面是lib代碼
1.common下代碼
1 import os 2 3 4 def login_auth(user_type): 5 from core import admin, student, teacher 6 7 def auth(func): 8 9 def wrapper(*args, **kwargs): 10 if user_type == 'admin': 11 if not admin.admin_info['name']: 12 admin.admin_login() 13 else: 14 return func(*args, **kwargs) 15 if user_type == 'teacher': 16 if not teacher.teacher_info['name']: 17 teacher.teacher_login() 18 else: 19 return func(*args, **kwargs) 20 if user_type == 'student': 21 if not student.student_info['name']: 22 student.student_login() 23 else: 24 return func(*args, **kwargs) 25 26 return wrapper 27 28 return auth 29 30 31 def get_all_dir_obj(path): 32 if os.path.exists(path): 33 obj_list = os.listdir(path) 34 return obj_list 35 else: 36 return NoneView Code
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/189233.html
標籤:Python
上一篇:win下python腳本以unix風格換行保存將會報錯為編碼問題 SyntaxError: encoding problem:gbk
