当前位置:   article > 正文

python学生管理器_完成数据模型类studentmodel

完成数据模型类studentmodel
#!/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)


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/261608
推荐阅读
相关标签
  

闽ICP备14008679号