管理信息系统02 增删改,排序

"""
  步骤一:
       数据模型类:StudentModel
          数据:姓名 name,年龄 age,成绩 score,编号 id
       逻辑控制类:StudentManagerController
          数据:学生列表 _stu_list
          行为:获取列表stu_List
               添加学生 add_student
               修改学生 update_student(new_stu)
               根据成绩升序排列 order_by_score
"""


class StudentModel:
    def __init__(self, name, age, score, id=0):
        self.name = name
        self.age = age
        self.score = score
        self.id = id


class StudentManagerController:
    init_id = 1000

    @classmethod
    def __generate__id(cls, stu):
        stu.id = cls.init_id
        cls.init_id += 1

    def __init__(self):
        self.__stu_list = []

    @property
    def stu_list(self):
        return self.__stu_list

    def add_student(self, stu):
        StudentManagerController.__generate__id(stu)
        self.__stu_list.append(stu)

    def remove_student(self, stu_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, new_stu):
        for item in self.__stu_list:
            if item.id == new_stu.id:
                item.name = new_stu.name
                item.age = new_stu.age
                item.score = new_stu.score
                return True
        return False

    def order_by_score(self):
        for r in range(len(self.__stu_list) - 1):
            for c in range(r + 1, len(self.__stu_list)):
                if self.__stu_list[r] > self.__stu_list[c]:
                    self.__stu_list[r], self.__stu_list[c] = self.__stu_list[c], self.__stu_list[r]


# 测试
controller = StudentManagerController()
data01 = StudentModel('悟空', 23, 96)
controller.add_student(data01)
controller.remove_student(1006)
controller.update_student(StudentModel('孙悟空', 24, 97, 1000))
for i in controller.stu_list:
    print(i.id, i.name)

猜你喜欢

转载自blog.csdn.net/jtpython666/article/details/120619994