Python 程序:选课系统
Posted 水无
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 程序:选课系统相关的知识,希望对你有一定的参考价值。
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[Python开发程序:选课系统-改良版