def enroll(self): #注册 student_data = File( setting.student_file).file_load() # 读取student_file数据 if self.name not in student_data: student_data[self.name] = self.__dict__ # 进行注册 File(setting.student_file).file_dump( student_data) # 写入student_file文件 print("\033[32;1m学员【%s】在【%s】学校【%s】分校注册成功\033[0m" % (self.name, self.school, self.address))
def add_teacher(self): teacher = input("讲师:").strip() school_data = File( setting.school_file).file_load() # 读取 school_file 文件内容 if teacher not in school_data[self.school][self.address]["teacher"]: school_data[self.school][self.address]["teacher"].append(teacher) self.teacher = school_data[self.school][self.address]["teacher"] File(setting.school_file).file_dump(school_data) print("\033[32;1m讲师【%s】加入【%s】学校【%s】分校\033[0m" % (teacher, self.school, self.address)) else: print("\033[41;1m讲师【%s】已经存在【%s】学校【%s】分校\033[0m" % (teacher, self.school, self.address))
def set_score(self, grade): student_name = input("学员:").strip() score = input("成绩:").strip() if not 0 <= int(score) <= 100: print("\033[41;1mScore[%s]值超出范围\033[0m" % score) else: grade_data = File(setting.grade_file).file_load() # 读取班级数据 if student_name in grade_data[grade]["student"]: # 查看学员是否在班级内 grade_data[grade]["score"][student_name] = score # 修改学员成绩 File(setting.grade_file).file_dump(grade_data) # 写入文件 print("\033[32;1m【%s】班学员【%s】成绩修改为【%s】\033[0m" % (grade, student_name, score)) else: print("\033[41;1m【%s】班学员【%s】不存在\033[0m" % (self.name, student_name))
def add_course(self): course = input("课程:").strip() period = input("周期:").strip() price = input("价格:").strip() school_data = File(setting.school_file).file_load() # 读取school_file文件 course_data = school_data[self.school][self.address][ "course"] # 读取课程数据 if course not in course_data: # 判断course不存在 course_data[course] = Course(course, period, price).__dict__ # 添加cours信息 # self.course = course_data # 添加到self.course中 File(setting.school_file).file_dump(school_data) # 写入school_file文件 print("\033[32;1m课程【%s】在【%s】学校【%s】分校创建成功\033[0m" % (course, self.school, self.address)) else: print("\033[41;1m【%s】课程已经存在\033[0m" % course)
def pay(self): # 交费 amount = input("交费金额:").strip() student_data = File( setting.student_file).file_load() # 读取student_file数据 if not student_data.get(self.name): student_data[self.name] = self.__dict__ # 如果self.name没有注册,初始化数据 sum_amount = student_data[self.name]["amount"] # 以前交费总额 if amount.isdigit(): sum_amount += int(amount) # 加上这次交费,算总额 student_data[ self.name]["amount"] = sum_amount # 加入总额传入student_data中 File(setting.student_file).file_dump( student_data) # 写入student_file文件 self.amount = sum_amount # 总额数赋值给self.amount print("\033[32;1m【%s】总缴费【¥%s】\033[0m" % (self.name, self.amount)) else: print("\033[41;1m【%s】格式错误\033[0m" % amount)
def add_student(self, student_name, grade_dict): # 添加学生进入对应班级 if not grade_dict.get(self.grade): grade_dict[self.grade] = self.__dict__ # 不存在,初始化self.grade班级 if student_name not in grade_dict[ self.grade]["student"]: # 判断 student_name 不在没有加入过该班级 self.student = grade_dict[self.grade]["student"].append( student_name) # 添加student_name学生 File(setting.grade_file).file_dump(grade_dict) # 写入grade_file文件 print("\033[32;1m【%s】添加成功\033[0m" % student_name) else: print("\033[41;1m【%s】已存在\033[0m" % student_name)
def add_grade(self): grade = input("班级:").strip() teacher = input("讲师:").strip() course = input("课程:").strip() school_data = File(setting.school_file).file_load() #读取school_file文件内容 #判断grade不存在,并且course与teacher都存在 if grade not in school_data[self.school][self.address]["grade"] \ and course in school_data[self.school][self.address]["course"] \ and teacher in school_data[self.school][self.address]["teacher"]: school_data[self.school][self.address]["grade"].append( grade) # 新建grade班级 # self.grade = school_data[self.school][self.address]["grade"] # 添加到self.grade grade_data = File(setting.grade_file).file_load() # 读取grade_file文件 grade_data[grade] = Grade( grade, teacher, course).__dict__ # 初始化grade在grade_file中的值 File(setting.grade_file).file_dump(grade_data) # 写入grade_file文件 File(setting.school_file).file_dump(school_data) # 写入school_file文件 print("\033[32;1m【%s】学校【%s】分校成功创建【%s】班级\033[0m" % (self.school, self.address, grade)) else: print("\033[41;1m班级【%s】已经存在或课程【%s】不存在\033[0m" % (grade, course))
def select_grade(self): # 选择班级 grade = input("班级:").strip() grade_data = File(setting.grade_file).file_load() # 读取班级数据 if grade in grade_data: # 判断班级是否存在 course = grade_data[grade]["course"] # 班级对应的课程 teacher = grade_data[grade]["teacher"] # 班级对应的讲师 Grade(grade, teacher, course).add_student(self.name, grade_data) # 在班级文件内添加学生 school_data = File(setting.school_file).file_load() # 读取学校文件内容 price = school_data[self.school][self.address]["course"][course][ "price"] # 课程对应的价格 period = school_data[self.school][self.address]["course"][course][ "period"] # 课程对应的周期 Grade(grade, teacher, course).show_grade() # 展示班级信息 Course(course, period, price).show_course() # 展示课程信息 student_data = File(setting.student_file).file_load() # 读取学生文件内容 student_data[self.name]["grade"].append(grade) # 学生信息中加入所选班级 self.grade = student_data[self.name]["grade"] # 学生加入的班级列表 File(setting.student_file).file_dump(student_data) # 学生信息写入文件 print("\033[32;1m学员【%s】加入了【%s】班级" % (self.name, self.grade)) else: print("\033[41;1m班级【%s】不存在\033[0m" % grade)
def initializ(self): school_data = File(setting.school_file).file_load() if not school_data.get(self.school): school_data[self.school] = {} school_data[self.school][self.address] = self.__dict__ File(setting.school_file).file_dump(school_data)
def get_score(self, grade): grade_data = File(setting.grade_file).file_load() # 读取grade_file内容 print("\033[33;1m%s\033[0m" % grade_data[grade]["score"]) # 查看grade班级学员成绩
def view_student(self, grade): # 查看学员列表 data = File(setting.grade_file).file_load() # 读取grade_file内容 print("\033[32;1m%s\033[0m" % data[grade]["student"]) #查看grade_file中的学员
def manage_grade(self, grade): # 管理班级 grade_data = File(setting.grade_file).file_load() # 读取grade_file数据 grade_data[grade]["manager"] = self.name # 设置管班级manager File(setting.grade_file).file_dump(grade_data) # 写入grade_file print("\033[31;1m【%s】管理【%s】班\033[0m" % (self.name, grade))
print("""\033[32;1m ===== 管理视图 ===== \t招聘讲师 add_teacher \t创建课程 add_course \t创建班级 add_grade \t退出 b\033[0m""") choice2 = input(">>> ").strip() if choice2 == 'b': break func = S + '.' + ''.join(choice2) + '()' exec(func) elif choice == "2": name = input("学员:").strip() school = input("学校:").strip() addr = input("地址:").strip() school_data = File(setting.school_file).file_load() if school in school_data and addr in school_data[school]: print("\033[32;1m【%s】学员进入了【%s】学校【%s】分校\033[0m" % (name, school, addr)) else: print("\033[41;1m学校【%s】或【%s】地址不存在\033[0m" % (school, addr)) continue S = "Student(name,school,addr)" while True: print("""\033[32;1m ===== 学员视图 ===== \t注册 enroll \t交学费 pay \t选择班级 select_grade \t退出 b\033[0m""")