赞
踩
#!/usr/bin/python # -*- coding: UTF-8 -*- """ 学生管理系统 项目计划: 1.完成数据模型类StudentModel 2.完成逻辑控制类StudentManagerContrller 3.完成数据:学生列表__stu__list 4.行为:获取列表stu__list 5。添加学生 add_student """ class StudentModel: """ 学生模型 """ def __init__(self, name="", age=0, score=0, id=0): """ :param id: 学生对象唯一标识 :param name: 姓名 :param age: :param score: """ self.id = id self.name = name self.age = age self.score = score class StudentManagerContrller: # 类变量,用于初始化编号 __init = 0 def __init__(self): self.__stu__list = [] @property def stu__list(self): return self.__stu__list def add_student(self, stu_info): stu_info.id = self.__generate_id() self.__stu__list.append(stu_info) def __generate_id(self): """ id自增一方法 :return: """ StudentManagerContrller.__init += 1 return StudentManagerContrller.__init def remove_student(self, stu_id): """ 根据id删除学生 :param stu_id: :return: """ for item in self.__stu__list: if item.id == stu_id: self.__stu__list.remove(item) return True return False def update_student(self, stu_info): """ 根据stu_info.id修改学生信息 """ for item in self.__stu__list: if item.id == stu_info.id: item.name = stu_info.name item.age = stu_info.age item.sccore = stu_info.score return True return False def order_by_score(self): """ 对学生数据进行排序,前面的跟后面比在交换 :return: """ for r in range(len(self.__stu__list)-1): for c in range(r+1,len(self.__stu__list)): if self.__stu__list[r].score>self.__stu__list[c].score: self.__stu__list[r].score,self.__stu__list[c].score=self.__stu__list[c].score,self.__stu__list[r].score class StudentManagerView: """ 学生管理器视图 """ def __init__(self): self.__manager = StudentManagerContrller() def __display_memu(self): print("1)添加学生") print("2)显示学生") print("3)删除学生") print("4)修改学生") print("5)按照成绩升序学生") def __select_memu(self): item = input("请输入选择:") if item == "1": self.__input_stu() elif item == "2": self.__outprint(self.__manager.stu__list) elif item == "3": self.__del_stu() elif item == "4": self.__modify_student() elif item == "5": self.__ascend_score() def __input_stu(self): name = input("请输入姓名:") age = input("请输入年龄:") score = input("请输入分数:") stu = StudentModel(name, age, score) self.__manager.add_student(stu) def __outprint(self, stu_list): for item in stu_list: print(item.id, item.name, item.age, item.score) def __del_stu(self): id = int(input("请输入id号删除:")) self.__manager.remove_student(id) def __modify_student(self): id=int(input("请输入需要修改的学生的编号:")) name = input("请输入新的学生名称:") age = int(input("请输入新的学生年龄:")) score = int(input("请输入新的学生成绩")) stu = StudentModel(name,age,score,id) if self.__manager.update_student(stu): print("修改成功") else: print("修改失败") def __ascend_score(self): """ :param stu_list: :return: """ self.__manager.order_by_score() self.__outprint(self.__manager.stu__list) def main(self): while True: self.__display_memu() self.__select_memu() view = StudentManagerView() view.main() """ 测试代码 """ # stu01 = StudentModel("张三",12,99) # stu02 = StudentModel("张1",12,99) # manager = StudentManagerContrller() # manager.add_student(stu01) # manager.add_student(stu02) # # for item in manager.stu__list: # print(item.name,item.id) """ 测试修改代码 """ # stu01 = StudentModel("张三",12,99) # manager = StudentManagerContrller() # manager.add_student(stu01) # manager.update_student(StudentModel("张三",11,90,id=1)) # for item in manager.stu__list: # print(item.score)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。