Python 程序:選課系統
角色:學校、學員、課程、講師
要求:
1. 創建北京、上海 2 所學校
2. 創建linux , python , go 3個課程 , linux\py 在北京開, go 在上海開
3. 課程包含,周期,價格,通過學校創建課程
4. 通過學校創建班級, 班級關聯課程、講師
5. 創建學員時,選擇學校,關聯班級
5. 創建講師角色時要關聯學校,
6. 提供兩個角色接口
6.1 學員視圖, 可以注冊, 交學費, 選擇班級,
6.2 講師視圖, 講師可管理自己的班級, 上課時選擇班級, 查看班級學員列表 , 修改所管理的學員的成績
6.3 學校視圖,創建講師, 創建班級,創建課程
7. 上面的操作產生的數據都通過jison序列化保存到文件里
一、需求分析
程序最終目的是展示三個視圖,每個視圖有自己的功能:
需要定義的類:
二、目錄結構
三、程序

1 import sys,os 2 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) #程序主目錄:/選課系統/ 3 sys.path.append(BASE_DIR) #添加環境變量 4 from core import main 5 if __name__ == '__main__': 6 a = main.run() 7 a.interactive()

1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import sys,os,json 5 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) #程序主目錄:/選課系統/ 6 sys.path.append(BASE_DIR) #添加環境變量 7 db_DIR = BASE_DIR + r"\db" 8 9 class base_class(object): 10 '''基礎類,用來進行文件的讀寫操作''' 11 def write(self,dir_name,type,dict): 12 filename = dir_name 13 file_path = "%s\%s" %(db_DIR,type) 14 ab_file = "%s\%s" %(file_path,filename) 15 with open(ab_file, "w", encoding="UTF-8") as f: 16 json.dump(dict, f) 17 if True: 18 print("-------",type,"創建成功","-------") 19 for key in dict: 20 print(key,":\t",dict[key]) 21 def extend(self,dir_name,type,list): 22 filename = dir_name 23 file_path = "%s\%s" %(db_DIR,type) 24 ab_file = "%s\%s" %(file_path,filename) 25 with open(ab_file, "r", encoding="UTF-8") as f: 26 file_list = json.load(f) 27 file_list.extend(list) 28 with open(ab_file, "w", encoding="UTF-8") as f: 29 # json.dump(list, f) 30 json.dump(file_list, f) 31 if True: 32 print("-------",type,"創建成功","-------") 33 for i in list: 34 for key in i: 35 print(key,i[key]) 36 print("\n") 37 return True 38 def read(self,type): 39 data = [] 40 db_path = "%s\%s" %(db_DIR,type) 41 for i in os.listdir(db_path): 42 db_file = os.path.join(db_path,i) 43 with open(db_file, "r", encoding="UTF-8") as f: 44 file_dict = json.load(f) 45 data.append(file_dict) 46 pass 47 return data 48 49 class admin(base_class): 50 '''管理類:創建學校、招聘講師、招收學員、創建班級、創建課程''' 51 def __init__(self): 52 pass 53 def create_school(self): 54 school_dict = {} 55 school_name = input("校名:") 56 school_address = input("地址:") 57 school_dict["校名"] = school_name 58 school_dict["地址"] = school_address 59 print(school_dict) 60 base_class.write(self,school_name, "school", school_dict) 61 def read_school(self): 62 list = base_class.read(self,"school") 63 return list 64 def create_teacher(self): 65 teacher_dict = {} 66 teacher_name = input("教師名字:") 67 teacher_salary = input("工資:") 68 teacher_course = input("教授課程:") 69 teacher_school = input("所屬學校:") 70 teacher_dict["教師名字"] = teacher_name 71 teacher_dict["工資"] = teacher_salary 72 teacher_dict["教授課程"] = teacher_course 73 teacher_dict["所屬學校"] = teacher_school 74 print(teacher_dict) 75 base_class.write(self,teacher_name, "teacher", teacher_dict) 76 def read_teacher(self): 77 list = base_class.read(self,"teacher") 78 return list 79 def create_student(self): 80 student_dict = {} 81 student_name = input("學員姓名:") 82 student_id = input("學員學號:") 83 student_school = input("所屬學校:") 84 student_classes = input("學員班級:") 85 student_course = input("課程名:") 86 student_dict["姓名"] = student_name 87 student_dict["學號"] = student_id 88 student_dict["學校"] = student_school 89 student_dict["班級"] = student_classes 90 student_dict["課程名"] = student_course 91 base_class.write(self,student_name, "student", student_dict) 92 def read_student(self): 93 list = base_class.read(self,"student") 94 return list 95 def create_classes(self): 96 classes_dict = {} 97 classes_name = input("班級號:") 98 classes_course = input("所學課程:") 99 classes_student = input("學習的學員:") 100 classes_teacher = input("負責的講師:") 101 classes_dict["班級名"] = classes_name 102 classes_dict["課程"] = classes_course 103 classes_dict["學習的學員"] = classes_student 104 classes_dict["負責講師"] = classes_teacher 105 base_class.write(self,classes_name, "classes", classes_dict) 106 def read_classes(self): 107 list = base_class.read(self,"classes") 108 return list 109 def create_course(self): 110 course_dict = {} 111 course_name = input("課程名:") 112 course_price = input("價格:") 113 course_period = input("周期:") 114 course_dict["課程名"] = course_name 115 course_dict["價格"] = course_price 116 course_dict["周期"] = course_period 117 base_class.write(self,course_name, "course", course_dict) 118 def read_course(self): 119 list = base_class.read(self,"course") 120 return list 121 122 class school(base_class): 123 '''學校類:名稱、地址''' 124 def __init__(self,school_name,school_address): 125 self.school_name = school_name 126 self.school_address = school_address 127 128 class teacher(base_class): 129 '''教師類:名稱、工資、課程、創建學生成績、創建上課記錄、查看學生成績、查看學生上課記錄、查個人信息''' 130 def __init__(self,teacher_name,teacher_salary,teacher_course,teacher_school): 131 self.teacher_name =teacher_name 132 self.teacher_salary = teacher_salary 133 self.teacher_course = teacher_course 134 self.teacher_school = teacher_school 135 def create_class_record(self): #上課記錄 136 class_record = [] 137 student_school = input("選擇學校:") 138 student_classes = input("選擇班級:") 139 student_times = input("課次:") 140 student_list = base_class.read(self,"student") 141 for i in student_list: 142 if i["學校"] == student_school and i["班級"] == student_classes: 143 student_name = i["姓名"] 144 student_status = input("%s 上課情況:" % student_name) 145 i["上課情況"] = student_status 146 i["課次"] = student_times 147 class_record.append(i) 148 base_class.extend(self,"class_record.json","class_record",class_record) 149 def create_student_results(self): #創建學生成績 150 student_results = [] 151 student_school = input("選擇學校:") 152 student_classes = input("選擇班級:") 153 student_times = input("課次:") 154 student_list = base_class.read(self,"student") 155 for i in student_list: 156 if i["學校"] == student_school and i["班級"] == student_classes: 157 student_name = i["姓名"] 158 student_grade = input("%s 成績:" % student_name) 159 i["成績"] = student_grade 160 i["課次"] = student_times 161 student_results.append(i) 162 base_class.extend(self,"student_results.json","student_results",student_results) 163 def check_class_record(self): #查看上課記錄 164 record_list = [] 165 student_school = input("校名:") 166 student_class = input("班級:") 167 student_times = input("課次:") 168 class_record_list = base_class.read(self, "class_record") 169 for i in class_record_list: 170 for j in i: 171 if j["學校"] == student_school and j["班級"] == student_class and j["課次"] == student_times: 172 record_list.append(j) 173 for i in record_list: 174 for key in i: 175 print(key,i[key]) 176 print("\n") 177 def check_student_results(self): #查看學生成績 178 grade_list = [] 179 student_school = input("校名:") 180 student_class = input("班級:") 181 student_times = input("課次:") 182 student_results_list = base_class.read(self, "student_results") 183 for i in student_results_list: 184 for j in i: 185 if j["學校"] == student_school and j["班級"] == student_class and j["課次"] == student_times: 186 grade_list.append(j) 187 for i in grade_list: 188 for key in i: 189 print(key,i[key]) 190 print("\n") 191 def check_self_info(self): #查看個人信息 192 print('''----info of Teacher:%s ---- 193 Name:%s 194 Salary:%s 195 Course:%s 196 School:%s 197 '''%(self.teacher_name,self.teacher_name,self.teacher_salary,self.teacher_course,self.teacher_school) 198 ) 199 200 class student(base_class): 201 '''學生類:名稱,id,注冊,交學費、查成績、查上課記錄、查個人信息''' 202 def __init__(self,student_name,student_id,student_school, student_classes,student_course): 203 self.student_name =student_name 204 self.student_id = student_id 205 self.student_school = student_school 206 self.student_classes = student_classes 207 self.student_course = student_course 208 209 def student_registered(self): #注冊 210 student_dict = {} 211 student_dict["姓名"] = self.student_name 212 student_dict["學號"] = self.student_id 213 student_dict["學校"] = self.student_school 214 student_dict["班級"] = self.student_classes 215 student_dict["課程名"] = self.student_course 216 base_class.write(self,self.student_name, "student", student_dict) 217 218 def pay_tuition(self): #交學費 219 course_list = base_class.read(self,"course") 220 print(course_list) 221 for i in course_list: 222 if i["課程名"] == self.student_course: 223 print("課程價格為:¥%s"%i["價格"]) 224 money = input("提交金額:") 225 if i["價格"] == money: 226 print("%s has paid tuition for ¥%s"%(self.student_name,i["價格"])) 227 else: 228 print("金額錯誤!") 229 230 def check_student_record(self): #查看學生上課記錄 231 student_school = input("校名:") 232 student_class = input("班級:") 233 student_times = input("課次:") 234 student_name = input("姓名:") 235 class_record_list = base_class.read(self,"class_record") 236 for i in class_record_list: 237 for j in i: 238 if j["學校"] == student_school and j["班級"] == student_class and j["課次"] == student_times \ 239 and j["姓名"] == student_name: 240 for key in j: 241 print(key,j[key]) 242 print("\n") 243 244 def check_student_results(self): #查看學生成績 245 student_school = input("校名:") 246 student_class = input("班級:") 247 student_times = input("課次:") 248 student_name = input("姓名:") 249 student_results_list = base_class.read(self,"student_results") 250 for i in student_results_list: 251 for j in i: 252 if j["學校"] == student_school and j["班級"] == student_class and j["課次"] == student_times \ 253 and j["姓名"] == student_name: 254 for key in j: 255 print(key,j[key]) 256 print("\n") 257 258 259 def check_self_info(self): #查看個人信息 260 pass 261 262 class classes(base_class): 263 '''班級類:名稱,課程,學生,老師''' 264 def __init__(self,classes_name,classes_course,classes_student,classes_teacher): 265 self.classes_name = classes_name 266 self.classes_course = classes_course 267 self.classes_student = classes_student 268 self.classes_teacher = classes_teacher 269 270 class course(base_class): 271 '''課程類:名稱、價格、周期''' 272 def __init__(self,course_name,course_price,course_period): 273 self.course_name = course_name 274 self.course_price = course_price 275 self.course_period = course_period 276 277 class school_view(admin): 278 '''學校管理視圖''' 279 def auth(self,username,password): 280 if username == "admin" and password == "admin": 281 return True 282 else: 283 print("用戶名或密碼錯誤!") 284 def login(self): 285 menu = u''' 286 ------- 歡迎進入學校視圖 --------- 287 \033[32;1m 1. 校區管理 288 2. 講師管理 289 3. 學員管理 290 4. 課程管理 291 5. 返回 292 \033[0m''' 293 menu_dic = { 294 '1': school_view.school_manager, 295 '2': school_view.teacher_manager, 296 '3': school_view.student_manager, 297 '4': school_view.course_manager, 298 '5': "logout", 299 } 300 username = input("輸入用戶名:").strip() 301 password = input("輸入密碼:").strip() 302 auth = school_view.auth(self,username,password) 303 if auth: 304 exit_flag = False 305 while not exit_flag: 306 print(menu) 307 option = input("請選擇:").strip() 308 if option in menu_dic: 309 if int(option) == 5: 310 exit_flag = True 311 else: 312 menu_dic[option](self) 313 else: 314 print("\033[31;1m輸入錯誤,重新輸入\033[0m") 315 def school_manager(self): 316 exit_flag = False 317 while not exit_flag: 318 print(""" 319 ------- 歡迎進入校區管理 --------- 320 \033[32;1m1、 創建校區 321 2、 創建班級 322 3、 查看學校 323 4、 查看班級 324 5、 返回 325 \033[0m 326 """) 327 option = input("請選擇:").strip() 328 if int(option) == 1: 329 admin.create_school(self) 330 elif int(option) == 2: 331 admin.create_classes(self) 332 elif int(option) == 3: 333 list = admin.read_school(self) 334 for i in list: 335 print(i) 336 elif int(option) == 4: 337 list = admin.read_classes(self) 338 for i in list: 339 print(i) 340 else: 341 exit_flag = True 342 def teacher_manager(self): 343 exit_flag = False 344 while not exit_flag: 345 print(""" 346 ------- 歡迎進入講師管理 --------- 347 \033[32;1m 1. 創建講師 348 2. 查看講師信息 349 3. 返回 350 \033[0m 351 """) 352 option = input("請選擇:").strip() 353 if int(option) == 1: 354 admin.create_teacher(self) 355 elif int(option) == 2: 356 list = admin.read_teacher(self) 357 for i in list: 358 print(i) 359 else: 360 exit_flag = True 361 def student_manager(self): 362 exit_flag = False 363 while not exit_flag: 364 print(""" 365 ------- 歡迎進入學員管理 --------- 366 \033[32;1m 1. 創建學員 367 2. 查看學員 368 3. 返回 369 \033[0m 370 """) 371 option = input("請選擇:").strip() 372 if int(option) == 1: 373 admin.create_student(self) 374 elif int(option) == 2: 375 list = admin.read_student(self) 376 for i in list: 377 print(i) 378 else: 379 exit_flag = True 380 def course_manager(self): 381 exit_flag = False 382 while not exit_flag: 383 print(""" 384 ------- 歡迎進入課程管理 --------- 385 \033[32;1m 1. 創建課程 386 2. 查看課程 387 3. 返回 388 \033[0m 389 """) 390 option = input("請選擇:").strip() 391 if int(option) == 1: 392 admin.create_course(self) 393 elif int(option) == 2: 394 list = admin.read_course(self) 395 for i in list: 396 print(i) 397 else: 398 exit_flag = True 399 400 class teacher_view(object): 401 '''講師視圖''' 402 def auth(self,name): 403 list = admin.read_teacher(self) 404 for i in list: 405 if i["教師名字"] == name: 406 return i 407 def login(self): 408 menu = u''' 409 ------- 歡迎進入講師視圖 --------- 410 \033[32;1m 1. 創建上課記錄 411 2. 創建學員成績 412 3. 查看學員上課記錄 413 4. 查看學員成績 414 5. 返回 415 \033[0m''' 416 name = input("教師名稱:") 417 auth = teacher_view.auth(self,name) 418 if auth is not None: 419 teacher_name = auth["教師名字"] 420 teacher_salary = auth["工資"] 421 teacher_course = auth["教授課程"] 422 teacher_school = auth["所屬學校"] 423 t1 = teacher(teacher_name,teacher_salary,teacher_course,teacher_school) 424 t1.check_self_info() 425 menu_dic = { 426 '1': t1.create_class_record, 427 '2': t1.create_student_results, 428 '3': t1.check_class_record, 429 '4': t1.check_student_results, 430 '5': "logout", 431 } 432 if True: 433 exit_flag = False 434 while not exit_flag: 435 print(menu) 436 option = input("請選擇:").strip() 437 if option in menu_dic: 438 if int(option) == 5: 439 exit_flag = True 440 else: 441 menu_dic[option]() 442 else: 443 print("\033[31;1m輸入錯誤,重新輸入\033[0m") 444 else: 445 print("不存在此教師") 446 447 class student_view(object): 448 '''學生視圖''' 449 def auth(self,name): 450 list = admin.read_student(self) 451 for i in list: 452 if i["姓名"] == name: 453 return i 454 def login(self): 455 menu = u''' 456 ------- 歡迎進入學生管理視圖 --------- 457 \033[32;1m 1. 交學費 458 2. 查看上課記錄 459 3. 查看作業成績 460 4. 返回 461 \033[0m''' 462 name = input("姓名:") 463 auth = student_view.auth(self,name) 464 if auth is not None: 465 print(auth) 466 student_name = auth["姓名"] 467 student_id = auth["學號"] 468 student_school = auth["學校"] 469 student_classes = auth["班級"] 470 student_course = auth["課程名"] 471 st1 = student(student_name,student_id,student_school,student_classes,student_course) 472 st1.check_self_info() 473 menu_dic = { 474 '1': st1.pay_tuition, 475 '2': st1.check_student_record, 476 '3': st1.check_student_results, 477 '4': "logout"} 478 if True: 479 exit_flag = False 480 while not exit_flag: 481 print(menu) 482 option = input("請選擇:").strip() 483 if option in menu_dic: 484 if int(option) == 4: 485 exit_flag = True 486 else: 487 menu_dic[option]() 488 else: 489 print("\033[31;1m輸入錯誤,重新輸入\033[0m") 490 else: 491 choice = input("\033[32;1m不存在此學生是否注冊Y/N:\033[0m") 492 if choice == "Y": 493 admin.create_student(self) 494 else: 495 print("\033[31;1m謝謝使用!\033[0m") 496 497 class run(object): 498 '''運行''' 499 def interactive(self): 500 menu = u''' 501 ------- 歡迎進入選課系統 --------- 502 \033[32;1m 1. 學生視圖 503 2. 講師視圖 504 3. 學校視圖 505 4. 退出 506 \033[0m''' 507 menu_dic = { 508 '1': student_view, 509 '2': teacher_view, 510 '3': school_view, 511 '4': "logout"} 512 exit_flag = False 513 while not exit_flag: 514 print(menu) 515 option_view = input("請選擇視圖:").strip() 516 if option_view in menu_dic: 517 if int(option_view) == 4: 518 exit_flag = True 519 else: 520 menu_dic[option_view].login(self) 521 else: 522 print("\033[31;1m輸入錯誤,重新輸入\033[0m")