# 数据结构
class Student:
def __init__(self, name, age, score, id=0):
self.name = name
self.age = age
self.score = score
self.id = id
# 操作控制
class Manage:
# 学号
__init_id = 1000
def __init__(self):
self.__sut_list = []
@property
def sut_list(self):
return self.__sut_list
def add_student(self, sut):
self.__generate(sut)
self.__sut_list.append(sut)
def remove_student(self, id):
for item in self.sut_list:
if item.id == id:
self.sut_list.remove(item)
print('删除成功!')
return True
else:
print('未找到数据!')
def update_student(self, id):
for item in range(len(self.sut_list)):
if self.__sut_list[item].id == id:
self.__sut_list[item].name = input('输入修改姓名:')
self.__sut_list[item].age = int(input('输入修改年龄:'))
self.__sut_list[item].score = int(input('输入修改成绩:'))
print('更新成功!')
return True
else:
print('未找到数据!')
# 自动生成学号
def __generate(self, sut):
Manage.__init_id += 1
sut.id = Manage.__init_id
# 视图
class View():
def __init__(self):
self.__manage = Manage()
self.main()
def __viewmanage(self):
print('1) 添加学生')
print('2) 显示学生')
print('3) 删除学生')
print('4) 更新学生')
print('5) 学生按成绩排序')
print('6) 显示所有学生信息')
def __select(self):
choice = input('请输入您的选择:')
if choice == '1':
self.__input_student()
elif choice == '2':
self.__display_student()
elif choice == '3':
id = int(input('请输入要删除的ID:'))
self.__manage.remove_student(id)
elif choice == '4':
id = int(input('请输入要更新的ID:'))
self.__manage.update_student(id)
elif choice == '5':
self.__sort_student()
elif choice == '6':
self.__show_all()
else:
return False
def main(self):
while True:
self.__viewmanage()
self.__select()
def __input_student(self):
name = input('输入姓名:')
age = int(input('输入年龄:'))
score = int(input('输入分数:'))
self.__manage.add_student(Student(name, age, score))
def __display_student(self):
id = int(input('请输入查询ID:'))
for item in self.__manage.sut_list:
if item.id == id:
print(item.id, item.name, item.age, item.score)
else:
print('未找到数据!')
def __sort_student(self):
len1 = len(self.__manage.sut_list)-1
for i in range(len1):
for j in range(len1-i):
if self.__manage.sut_list[j].score > self.__manage.sut_list[j+1].score:
temp = self.__manage.sut_list[j]
self.__manage.sut_list[j] = self.__manage.sut_list[j+1]
self.__manage.sut_list[j + 1] = temp
def __show_all(self):
for item in self.__manage.sut_list:
print(item.id, item.name, item.age, item.score)
v01 = View()