class StudentManagerView: def __init__(self): self.__manager = StudentManagerController() def __display_menu(self): print(""" 1) 添加学生信息 2) 显示学生信息 3) 删除学生信息 4) 修改学生信息 5) 按照成绩从高到低显示 """) def __select_menu(self): item = input("请输入选项:") if item == "1": self.__input_students() elif item == "2": self.__output_students() elif item == "3": self.__delete_student() elif item == "4": self.__modify_student() elif item == "5": self.__output_student_by_score() def main(self): while True: self.__display_menu() self.__select_menu() def __input_students(self): while True: name = input("请输入姓名:") # age = int(input("请输入年龄:")) age = self.__input_number("年龄") score = self.__input_number("成绩") stu = StudentModel(name, age, score) # StudentManagerController().add_student(....) self.__manager.add_student(stu) if input("输入e退出:") == "e": break def __input_number(self, message): while True: try: number = int(input("请输入%s:" % message)) break except: print("输入有误.") return number def __output_students(self): for item in self.__manager.stu_list: print(item.id, item.name, item.age, item.score) def __delete_student(self): # id = int(input("请输入需要删除的编号:")) id = self.__input_number("需要删除的编号") if self.__manager.remove_student(id): print("删除成功") else: print("删除失败") def __modify_student(self): stu = StudentModel() # stu.id = int(input("请输入需要修改的学生编号:")) stu.id = self.__input_number("需要修改的学生编号") stu.name = input("请输入需要修改的学生姓名:") # stu.age = int(input("请输入需要修改的学生年龄:")) stu.age = self.__input_number("需要修改的学生年龄") # stu.score = int(input("请输入需要修改的学生成绩:")) stu.score = self.__input_number("需要修改的学生成绩") if self.__manager.update_student(stu): print("修改成功") else: print("修改失败") def __output_student_by_score(self): self.__manager.order_by_score() self.__output_students()
class StudentManagerView: """ 学生管理器视图,负责处理界面逻辑. """ def __init__(self): self.__controller = StudentManagerController() def __display_menu(self): print("1)添加学生") print("2)显示学生") print("3)删除学生") print("4)修改学生") def __select_menu_item(self): item = input("请您输入选项:") if item == "1": self.__input_students() elif item == "2": self.__output_students() elif item == "3": self.__delete_student() elif item == "4": self.__modify_student() def main(self): while True: self.__display_menu() self.__select_menu_item() def get_int_info(self, str_info): while True: try: result = int(input("请输入%s:" % str_info)) return result except: print("%s输入有误" % str_info) def __input_students(self): # 调用逻辑控制类的add_student方法 while True: stu = StudentModel() stu.name = input("请输入姓名:") if stu.name == "": break stu.age = self.get_int_info("年龄") stu.score = self.get_int_info("成绩") # while True: # try: # stu.score = int(input("请输入成绩:")) # break # except: # print("年龄输入有误") self.__controller.add_student(stu) def __output_students(self): for item in self.__controller.stu_list: print(item.id, item.name, item.age, item.score) def __delete_student(self): while True: # id = int(input("请输入需要删除的学生编号:")) id = self.get_int_info("需要删除的学生编号") if self.__controller.remove_student(id): print("删除成功") break else: print("删除失败") def __modify_student(self): stu = StudentModel() stu.id = int(input("请输入需要修改的学生编号:")) stu.name = input("请输入新的姓名:") stu.age = int(input("请输入新的年龄:")) stu.score = int(input("请输入新的成绩:")) if self.__controller.update_student(stu): print("修改成功") else: print("修改失败")
class StudentManagerView: """ 界面视图类 """ def __init__(self): #创建逻辑控制器对象 self.__manager = StudentManagerController() def __input_int(self,str): while True: try: return int(input(str)) except: print("输入有误") def __input_student(self):#1 #1,在控制台中录入学生信息,存成学生对象StudentModel #2,调用逻辑控制器的add_student方法 stu = StudentModel() stu.name = input("请输入学生姓名") # stu.age = int(input("请输入学生年龄")) stu.age = self.__input_int("请输入学生年龄") # stu.score = float(input("请输入学生成绩")) stu.score = self.__input_int("请输入学生成绩") self.__manager.add_student(stu) # print(self.__manager) def __output_students(self,list_target):#2 """ 显示学生 :return: 学生信息 """ # for item in self.__manager.stu_list: for item in list_target: print(item.id, item.name, item.age, item.score) def __delete_student(self):#3 # value = int(input("请输入需要删除的学生id")) value = self.__input_int("请输入需要删除的学生id") if self.__manager.remove_student(value): print("删除成功") else: print("删除失败") def __modify_student(self):#4 """ 修改学生信息 :return: """ stu = StudentModel() # stu.id = int(input("请输入需要修改的学生id")) stu.id = self.__input_int("请输入需要删除的学生id") stu.name = input("请输入学生姓名") # stu.age = int(input("请输入学生年龄")) stu.age =self.__input_int("请输入学生年龄") # stu.score = float(input("请输入学生成绩")) stu.score = self.__input_int("请输入学生成绩") if self.__manager.update_student(stu): print("修改成功") else: print("修改失败") def __output_student_by_score(self):#5 """ 根据成绩排序 :return: """ list_target = self.__manager.sorting_stu_by_score() self.__output_students(list_target) def __display_menu(self): """ 显示菜单 :return: """ print("1)添加学生") print("2)显示学生") print("3)删除学生") print("4)修改学生") print("5)按照成绩降序排列") def __select_menu(self): number = input("请输入选项:") if number == "1": self.__input_student() elif number == "2": self.__output_students(self.__manager.stu_list) elif number == "3": self.__delete_student() elif number == "4": self.__modify_student() elif number == "5": self.__output_student_by_score() def main(self): """ 界面入口方法 :return: """ while True: self.__display_menu() self.__select_menu()
class StudentManagerView: """ 界面视图类 """ def __init__(self): # 创建逻辑控制器对象 self.__manager = StudentManagerController() def __input_int(self, msg): while True: try: return int(input(msg)) except: print("您的输入有误!!!") def __input_students(self): # 1. 在控制台中录入学生信息,存成学生对象StudentModel. stu = StudentModel() # stu.name = input("请输入学生姓名:") # stu.age = int(input("请输入学生年龄:")) # stu.score = int(input("请输入学生成绩:")) stu.name = input("请输入学生姓名:") stu.age = self.__input_int("请输入学生年龄:") stu.score = self.__input_int("请输入学生成绩:") # 2. 调用逻辑控制器的add_student方法 self.__manager.add_student(stu) def __output_student_by_score(self): """ 根据成绩显示学生所有信息 :return: """ list_target = self.__manager.order_by_score() self.__output_students(list_target) def __output_students(self, list_target): print("+------+---------------+----------+----------+") print("| id | name | age | score |") print("+------+---------------+----------+----------+") for item in list_target: id = str(item.id).center(6) name = item.name.center(15) age = str(item.age).center(10) score = str(item.score).center(10) print("|%s|%s|%s|%s|" % (id, name, age, score)) print("+------+---------------+----------+----------+") def __delete_student(self): # id = int(input("请输入要删除的学生ID:")) id = self.__input_int("请输入要删除的学生ID:") self.__manager.remove_student(id) def __modify_student(self): stu = StudentModel() # stu.id = int(input("请输入要修改的学生ID:")) # stu.name = input("请输入学生姓名:") # stu.age = int(input("请输入学生年龄:")) # stu.score = int(input("请输入学生成绩:")) stu.id = self.__input_int("请输入要修改的学生ID:") stu.name = input("请输入学生姓名:") stu.age = self.__input_int("请输入学生年龄:") stu.score = self.__input_int("请输入学生成绩:") self.__manager.update_student(stu) def __display_menu(self): """ 显示菜单 :return: """ print('+-------------------------+') print('| 1) 添加学生信息 |') print('| 2) 显示学生信息 |') print('| 3) 删除学生信息 |') print('| 4) 修改学生成绩 |') print('| 5) 按照成绩升序排列 |') print('+-------------------------+') def __select_menu(self): """ 选择菜单 :return: """ number = input("请输入选项:") if number == "1": self.__input_students() elif number == "2": self.__output_students(self.__manager.stu_list) elif number == "3": self.__delete_student() elif number == "4": self.__modify_student() elif number == "5": self.__output_student_by_score() def main(self): """ 界面入口方法 :return: """ while True: self.__display_menu() self.__select_menu()
class StudentManagerView: """ 学生管理器视图,负责处理界面逻辑. """ def __init__(self): # 将StudentManagerController类的对象作为一个私有变量 self.__controller = StudentManagerController() def __display_menu(self): print("1)添加学生") print("2)显示学生") print("3)删除学生") print("4)修改学生") print("5)成绩排序") def __select_menu_item(self): item = input("请您输入选项:") if item == "1": self.__input_students() elif item == "2": self.__output_students() elif item == "3": self.__delete_student() elif item == "4": self.__modify_student() elif item == "5": self.__order_student() def main(self): """ 主函数,先显示再选择 :return: """ while True: self.__display_menu() self.__select_menu_item() def __input_students(self): while True: # 创建学生对象 stu = StudentModel() # 添加信息 stu.name = input("请输入姓名:") if stu.name == "": break stu.age = int(input("请输入年龄:")) stu.score = int(input("请输入分数:")) # 得到学生信息后要调用add方法,但add是在StudentManagerController类里面的 # 必须通过StudentManagerController类的对象来访问。 # 所以常规的想法是先创建一个类,再调用方法,如下: # controller = StudentManagerController() # controller.add_student(stu) # 但是这样每次都要创建一个新的类,每次调用时都创建一个新列表存放学生对象 # 这样无法将学生都添加到一个列表中,不符合要求 # 所以我们采用下面的做法:把类对象作为私有变量,只有一个对象,存放在一个列表中 self.__controller.add_student(stu) def __output_students(self): for item in self.__controller.stu_list: print(item.id, item.name, item.age, item.score) def __delete_student(self): # 根据学生id删除,界面只做输入输出操作,不处理逻辑 student_id = int(input("请输入需要删除的学生id:")) if self.__controller.remove_student(student_id): print("删除成功") else: print("删除失败") def __modify_student(self): # 根据学生id修改 stu = StudentModel() stu.id = int(input("请输入要修改的学生id:")) stu.name = input("请输入修改后的姓名:") stu.age = int(input("请输入修改后的年龄:")) stu.score = int(input("请输入修改后的分数:")) if self.__controller.update_student(stu): print("修改成功") else: print("修改失败") def __order_student(self): self.__controller.order_by_score()
class StudentManagerView: """ 学生管理器视图 """ def __init__(self): # 将类实体化 self.__manager = StudentManagerController() def __display_menu(self): """ 显示菜单 """ print("1>增加学生信息") print("2>显示学生信息") print("3>删除学生信息") print("4>修改学生信息") print("5>按成绩低~高显示学生信息") print("0>退出管理") def __select_menu(self): """ 选择菜单项 """ i = int(input("please enter menu option:")) if i == 0: # 让程序中断 raise ValueError("退出循环") elif i == 1: self.__input_student() elif i == 2: self.__output_students(self.__manager.stu_list) elif i == 3: self.__delete_student() elif i == 4: self.__modify_student() elif i == 5: self.__output_student_by_score() def main(self): """ 入口逻辑 """ while True: self.__display_menu() self.__select_menu() def __input_student(self): """ 将用户输的信息增加到列表中 """ name = input("please enter name:") age = int(input("please enter age:")) score = int(input("please enter score:")) stu = StudentModel(name, age, score) self.__manager.add_student(stu) def __output_students(self, list_output): """ 显示所有学生信息 :param list_output:用户输入信息存储的列表 """ for i in list_output: StudentModel.output_student_info(i) def __delete_student(self): """ 删除学生信息,根据ID """ i = int(input("please enter delete ID:")) self.__manager.remove_student(i) def __modify_student(self): """ 修改学生信息 """ id = int(input("please enter delete ID:")) name = input("please enter name:") age = int(input("please enter age:")) score = int(input("please enter score:")) stu = StudentModel(name, age, score, id) self.__manager.update_student(stu) def __output_student_by_score(self): """ 升序成绩显示所有 """ list_target = self.__manager.order_by_score() self.__output_students(list_target)
class StudentManagerView: """ 界面视图类 """ def __init__(self): # 创建逻辑控制器对象 self.__manager = StudentManagerController() def __input_students(self): # 1. 在控制台中录入学生信息,存成学生对象StudentModel. stu = StudentModel() stu.name = input("请输入姓名:") stu.age = self.__input_int('年龄') stu.score = self.__input_int('分数') # 2. 调用逻辑控制器的add_student方法 self.__manager.add_student(stu) print(self.__manager) def __output_students(self, list_target): """ 显示学生列表信息 :return: """ # for stu in self.__manager.list_stu: for stu in list_target: print("%d -- %s -- %d -- %d" % (stu.id, stu.name, stu.age, stu.score)) def __output_student_by_score(self): """ 根据成绩显示所有学生信息 :return: """ list_target = self.__manager.order_by_score() self.__output_students(list_target) def __delete_student(self): id = self.__input_int('编号') if self.__manager.remove_student(id): print("删除成功") else: print("删除失败") def __modify_student(self): """ 修改学生信息 :return: """ stu = StudentModel() stu.id = self.__input_int('编号') stu.name = input("请输入姓名:") stu.age = self.__input_int('年龄') stu.score = self.__input_int('分数') if self.__manager.update_student(stu): print("修改成功") else: print("更新失败") def __display_menu(self): """ 显示菜单 :return: """ print("1) 添加学生") print("2) 显示学生") print("3) 删除学生") print("4) 修改学生") print("5) 按照成绩降序排列") def __select_menu(self): """ 选择菜单 :return: """ number = input("请输入选项:") if number == "1": self.__input_students() elif number == "2": self.__output_students(self.__manager.list_stu) elif number == "3": self.__delete_student() elif number == "4": self.__modify_student() elif number == "5": self.__output_student_by_score() def __input_int(self, value): while True: try: score = int(input('请输入你的%s' % value)) except Exception: continue if 0 < score < 100: return score def main(self): """ 界面入口方法 :return: """ while True: self.__display_menu() self.__select_menu()
class StudentManagerView: """ 学生管理器视图类 """ def __init__(self): # 创建学生管理控制器对象 self.__controller = StudentManagerController() def __display_menu(self): """ 显示菜单 :return: """ print("---------------------") print("1)添加学生") print("2)显示学生") print("3)删除学生") print("4)修改学生") print("5)按照成绩降序显示") print("---------------------") def __select_menu(self): """ 选择菜单 :return: """ number = input("请输入选项:") if number == "1": self.input_student() elif number =="2": self.__out_put_students(self.__controller.list_stu) elif number == "3": self.del_student() elif number == "4": self.xiugai_student() elif number == "5": self.order_score() def main(self): """ 学生管理器入口 :return: """ while True: self.__display_menu() self.__select_menu() def input_student(self): while True: stu = StudentModel() stu.name = input("请输入姓名") stu.age = int(input("请输入年龄")) stu.score = int(input("请输入成绩")) self.__controller.add_student(stu) if input("按Y继续") != "Y": break def __out_put_students(self,list_stu): for item in list_stu: print("%d|%s|%d|%d "%(item.id,item.name,item.age,item.score)) def del_student(self): while True: stu = StudentModel() Num = int(input("请输入ID:")) self.__controller.remove_student(Num) if input("按Y继续") != "Y": break def xiugai_student(self): stu_info = StudentModel() stu_info.id=int(input("请输入你要修改的Id:")) stu_info.name= str(input("请输入你要修改的名字:")) stu_info.age= int(input("请输入你要修改的年纪:")) stu_info.score=(int(input("请输入你要修改的分数:"))) if self.__controller.update_student(stu_info): print("修改成功") else: print("修改失败")
class StudentManagerView: """ 学生管理视图:主要负责界面逻辑 """ def __init__(self): self.__controller = StudentManagerController() def __display_menu(self): print("1)添加学生信息") print("2)显示学生信息") print("3)删除学生信息") print("4)修改学生信息") print("5)根据成绩升序排列") def __select_menu(self): item = input("请输入选项:") if item == "1": self.__input_students() elif item == "2": self.__output_students() elif item == "3": self.__delete_student() elif item == "4": self.__modify_student() elif item == "5": self.__output_students_order_by_score() def main(self): """ 程序入口方法 """ while True: self.__display_menu() self.__select_menu() def __input_students(self): name = input("请输入学生姓名:") score = self.__input_integer("请输入学生成绩:") age = self.__input_integer("请输入学生年龄:") stu = StudentModel(name, age, score) self.__controller.add_student(stu) def __input_integer(self, message): while True: try: return int(input(message)) except: print("输入有误") def __output_students(self): for item in self.__controller.stu_list: print("编号是:%d,姓名是%s,年龄是%d,成绩是%d." % (item.id, item.name, item.atk, item.score)) def __delete_student(self): # stu_id = int(input("请输入编号:")) stu_id = self.__input_integer("请输入编号:") if self.__controller.remove_student(stu_id): print("删除成功") else: print("删除失败") def __modify_student(self): stu = StudentModel() stu.id = self.__input_integer("请输入需要修改的学生编号:") stu.name = input("请输入需要修改的学生姓名:") stu.age = self.__input_integer("请输入需要修改的学生年龄:") stu.score = self.__input_integer("请输入需要修改的学生成绩:") if self.__controller.update_student(stu): print("修改成功") else: print("修改失败") def __output_students_order_by_score(self): self.__controller.order_by_score() self.__output_students()
class StudentManagerView: """ 学生管理视图:主要负责界面逻辑 """ def __init__(self): self.__controller = StudentManagerController() def __display_menu(self): print("1) 添加学生信息" + "\n" + "2) 显示学生信息" + "\n" + "3) 删除学生信息" + "\n" + "4) 修改学生信息" + "\n" + "5) 根据成绩升序排列" + "\n" + "6) 结束所有操作并清除数据") def __select_menu(self): """ 用户选择界面 :return: True表示结束所有操作并清除数据。 """ order_index = input("请输入您的指令选项: ") if order_index == "1": self.__input_students() return False elif order_index == "2": self.__output_students(self.__controller.stu_list) return False elif order_index == "3": self.__delete_student() return False elif order_index == "4": self.__modify_student() return False elif order_index == "5": self.__output_students_order_by_score() return False elif order_index == "6": print("感谢您的使用!") return True else: print("指令错误!请重新输入指令!") self.__select_menu() return False def __input_students(self): """ 添加新学生信息 """ name = input("请输入学生的姓名,输入\"end\"结束添加过程:") if name == "end": print("添加完成!") return age = self.__input_integer("请输入学生的年龄:") score = self.__input_integer("请输入学生的成绩:") new_stu = StudentModel(name, age, score) self.__controller.add_student(new_stu) self.__input_students() def __input_integer(self, message): while True: try: return int(input(message)) except: print("输入有误") def __output_students(self, stu_list): """ 显示已存学生信息。 """ if self.__controller.check_empty(): print("列表中无学生信息。") else: for student in stu_list: print("学生id:%d, 姓名:%s, 年龄:%d, 成绩:%d" % (student.id, student.name, student.age, student.score)) def __delete_student(self): """ 根据学生姓名删除学生信息。 """ stu_name = input("请输入需要删除学生的姓名:") stu_id = self.__controller.search_student_id(stu_name) if self.__controller.remove_student(stu_id) == True: print("成功删除%s的信息" % (stu_name)) def __modify_student(self): """ 根据学生id更新学生信息。 """ new_stu = StudentModel() new_stu.id = self.__input_integer("请输入学生的id:") new_stu.name = input("请输入学生的姓名:") new_stu.age = self.__input_integer("请输入学生的年龄:") new_stu.score = self.__input_integer("请输入学生的成绩:") if self.__controller.update_student(new_stu): print("修改成功!") else: print("修改失败!") def __output_students_order_by_score(self): """ 将学生成绩升序排序后显示所有学生信息。 """ temp_student_list = self.__controller.order_by_score() self.__output_students(temp_student_list) def main(self): while True: self.__display_menu() if self.__select_menu(): break
class StudentManagerView: """ 视图 """ def __init__(self): self.__controller = StudentManagerController() def main(self): while True: self.__display_menu() self.__select_menu() def __display_menu(self): print("1)添加学生") print("2)显示学生") print("3)删除学生") print("4)修改学生") print("5)根据成绩升序显示学生") def __select_menu(self): item = input("请输入选项:") if item == "1": self.__input_students() elif item == "2": self.__output_students() elif item == "3": self.__delete_student() elif item == "4": self.__modify_student() elif item == "5": self.__output_students_by_score() # 这里是为了处理异常的函数 def __input_number(self, message): while True: try: number = int(input(message)) return number except: print("输入有误,请重新输入") def __input_students(self): # 循环获取信息 while True: name = input("请输入姓名:") if name == "": break age = self.__input_number("请输入年龄:") score = self.__input_number("请输入成绩:") stu = StudentModel(name, age, score) # 将学生信息交给Controller中的add_student方法 return self.__controller.add_student(stu) # 显示学生__output_students def __output_students(self): for item in self.__controller.stu_list: print(item.id, item.name, item.age, item.score) # 删除学生__delete_student def __delete_student(self): stu_id = self.__input_number("请输入需要删除的学生编号:") if self.__controller.remove_student(stu_id): print("删除成功") else: print("删除失败") def __modify_student(self): stu = model.StudentModel() stu.id = self.__input_number("请输入编号:") stu.name = input("请输入姓名:") stu.age = self.__input_number("请输入年龄:") stu.score = self.__input_number("请输入成绩:") if self.__controller.update_student(stu): print("修改成功") else: print("修改失败") def __output_students_by_score(self): self.__controller.order_by_score() self.__output_students()
class StudentManagerView: def __init__(self): self.__manager = StudentManagerController() def __display_menu(self): print("1)添加学生") print("2)显示学生") print("3)删除学生") print("4)修改学生") print("5)按照成绩升序显示学生") def __select_menu(self): item = input("请输入:") if item == "1": self.__input_student() elif item == "2": self.__output_student(self.__manager.stu_list) elif item == "3": self.__delete_student() elif item == "4": self.__modify_student() else: self.__output_student_by_score() def __input_student(self): name = input("请输入姓名:") age = self.input_data("请输入年龄:") score = self.input_data("请输入成绩:") stu = StudentModel(name, age, score) self.__manager.add_student(stu) def input_data(self, message): while True: try: input_data = int(input(message)) return input_data except: print("输入有误,请重新输入!") continue def __output_student(self, list_output): for item in list_output: print(item.id, item.name, item.age, item.score) def __delete_student(self): id = self.input_data("请输入待删除的学生编号:") if self.__manager.remove_student(id): print("删除成功") else: print("删除失败") def __modify_student(self): stu = StudentModel() stu.id = self.input_data("请输入待修改学生编号:") stu.name = input("请输入姓名:") stu.age = self.input_data("请输入年龄:") stu.score = self.input_data("请输入成绩:") if self.__manager.update_student(stu): print("修改成功") else: print("修改失败") def __output_student_by_score(self): self.__manager.order_by_score() self.__output_student(self.__manager.stu_list) def main(self): while True: self.__display_menu() self.__select_menu()