栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

管理信息系统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)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/294055.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号