class StudentView: def __init__(self): """ 将控制器设置为属性 """ self.__controller = StudentController() def main(self): while True: self.__display_menu() self.__select_menu() def __display_menu(self): print("1. 添加学生信息") print("2. 显示学生信息") print("3. 删除学生信息") print("4. 修改学生信息") def __select_menu(self): item = input("请输入执行选项序号: ") if item == "1": self.__input_student() elif item == "2": self.__display_students() elif item == "3": self.__delete_student() elif item == "4": self.__update_student() def __input_student(self): stu = StudentModel() stu.name = input("请输入学生的名字: ") stu.sex = input("请输入学生的性别: ") stu.score = input("请输入学生的成绩: ") stu.age = input("请输入学生的年龄: ") self.__controller.add_student(stu) def __display_students(self): for stu in self.__controller.list_students: print(stu) def __delete_student(self): sid = int(input("请输入需要删除的学生编号: ")) if self.__controller.remove_student(sid): print("删除成功") else: print("删除失败") def __update_student(self): stu = StudentModel() stu.sid = int(input("请输入需要修改的学生的编号: ")) stu.name = input("请输入需要修改的学生的姓名: ") stu.sex = input("请输入需要修改的学生的性别: ") stu.score = int(input("请输入需要修改的学生的成绩: ")) stu.age = int(input("请输入需要修改的学生的年龄: ")) if self.__controller.update_student(stu): print("修改成功") else: print("修改失败")
class StudentView: """ # 学生视图:负责处理界面逻辑(输入/输出/界面跳转) """ def __init__(self): self.__controller = StudentController() def __display_menu(self): print("1) 添加学生信息") print("2) 显示所有学生信息") print("3) 删除学生信息") def __select_menu(self): item = input("请输入选项:") if item == "1": self.__input_student() elif item == "2": # 先写方法调用,通过快捷键生成方法. # alt + 回车 + 回车 self.__display_students() elif item == "3": self.__delete_student() def __input_student(self): stu = StudentModel() stu.name = input("请输入姓名:") stu.age = self.get_int("请输入年龄:") stu.sex = input("请输入性别:") stu.score = self.get_int("请输入成绩:") self.__controller.add_student(stu) def get_int(self, message): while True: try: data = int(input(message)) return data except: print("重新输入") def main(self): while True: self.__display_menu() self.__select_menu() def __display_students(self): # list_students for stu in self.__controller.list_students: # print("我是xx,今年xx,性别xx,成绩xx,学号xx.") print( f"我是{stu.name},今年{stu.age},性别{stu.sex},成绩{stu.score},学号{stu.sid}." ) def __delete_student(self): # sid = int(input("请输入学生编号:")) sid = self.get_int("请输入学生编号:") if self.__controller.remove_student(sid): print("删除成功") else: print("删除失败")
class StudentView: """ # 学生视图:负责处理界面逻辑(输入/输出/界面跳转) """ def __init__(self): self.__controller = StudentController() def exception_handling(self, message): while True: try: data = eval(message) # data = int(input(message)) return data except: print("输入有误") def __display_menu(self): print("1) 添加学生信息") print("2) 显示所有学生信息") print("3) 删除学生信息") def __select_menu(self): item = input("请输入选项:") if item == "1": self.__input_student() elif item == "2": self.__display_students() elif item == "3": self.__delete_student() def __input_student(self): stu = StudentModel() stu.name = input("请输入姓名:") stu.age = self.exception_handling("int(input('请输入年龄:'))") stu.sex = input("请输入性别:") stu.score = self.exception_handling("int(input('请输入请输入学生年龄:'))") self.__controller.add_student(stu) def main(self): while True: self.__display_menu() self.__select_menu() def __display_students(self): for stu in self.__controller.list_students: print( f"我是{stu.name},今年{stu.age},性别{stu.sex},成绩{stu.score},学号{stu.sid}." ) def __delete_student(self): sid = self.exception_handling(int(input("请输入学生编号:"))) if self.__controller.remove_student(sid): print("删除成功") else: print("删除失败")
class StudentView: """ 学生视图:负责处理界面逻辑 """ def __init__(self): self.__controller = StudentController() def __display_menu(self): print("1) 添加学生信息") print("2) 显示学生信息") print("3) 删除学生信息") # ... def __select_menu(self): item = input("请输入选项:") if item == "1": # 先写调用,再快捷键生成定义函数代码 # atl + 回车 self.__input_student() elif item == "2": self.__show_students() elif item == "3": pass def main(self): """ 入口函数 """ while True: self.__display_menu() self.__select_menu() def __input_student(self): stu = StudentModel() stu.name = input("请输入学生姓名:") stu.age = self.get_student_age() stu.score = self.get_student_score() self.__controller.add_student(stu) def get_student_age(self): while True: try: age = int(input("请输入学生年龄:")) return age except: print("输入有误请重新输入") def get_student_score(self): while True: try: score = int(input("请输入学生成绩:")) return score except: print("输入有误请重新输入") def __show_students(self): for stu in self.__controller.list_students: print(f"{stu.name}的编号是{stu.sid}年龄是{stu.age}成绩是{stu.score}")
class StudentView: """ 界面视图:负责界面逻辑,输入/输出.. """ def __init__(self): self.__controller = StudentController() def main(self): while True: self.__display_menu() self.__select_item() def __display_menu(self): print("按1键录入学生信息") print("按2键显示学生信息") print("按3键删除学生信息") print("按4键修改学生信息") def __select_item(self): item = input("请输入选项:") if item == "1": self.__input_student() elif item == "2": self.__display_students() elif item == "3": self.__delete_student() elif item == "4": self.__modify_student() def __input_student(self): stu = StudentModel() stu.name = input("请输入学生姓名:") stu.age = int(input("请输入学生年龄:")) stu.score = int(input("请输入学生成绩:")) self.__controller.add_student(stu) def __display_students(self): for item in self.__controller.list_students: print(f"{item.name}的编号是{item.sid},年龄是{item.age},成绩是{item.score}") def __delete_student(self): sid = int(input("请输入学生编号:")) if self.__controller.remove_student(sid): print("删除成功") else: print("删除失败") def __modify_student(self): stu = StudentModel() stu.sid = int(input("请输入学生编号:")) stu.name = int(input("请输入学生姓名:")) stu.age = int(input("请输入学生年龄:")) stu.score = int(input("请输入学生成绩:")) if self.__controller.update_student(stu): print("修改成功") else: print("修改失败")
class StudentView: """ 学生视图:负责处理界面逻辑 """ def __init__(self): self.__controller = StudentController() def __display_menu(self): print("1) 添加学生信息") print("2) 显示学生信息") print("3) 删除学生信息") # ... def __select_menu(self): item = input("请输入选项:") if item == "1": # 先写调用,再快捷键生成定义函数代码 # atl + 回车 self.__input_student() elif item == "2": self.__show_students() elif item == "3": self.__delete_student() def main(self): """ 入口函数 """ while True: self.__display_menu() self.__select_menu() def get_number(self, message): while True: try: number = int(input(message)) return number # 退出函数 except: print("输入有误!") def __input_student(self): stu = StudentModel() stu.name = input("请输入学生姓名:") stu.age = self.get_number("请输入学生年龄:") stu.score = self.get_number("请输入学生成绩:") self.__controller.add_student(stu) def __show_students(self): for stu in self.__controller.list_students: print(f"{stu.name}的编号是{stu.sid}年龄是{stu.age}成绩是{stu.score}") def __delete_student(self): sid = self.get_number("请输入需要删除的学生编号:") if self.__controller.remove_student(sid): print("删除成功") else: print("删除失败")