面向对象版学员管理系统程序已经成功跑通
大家可以按需学习
目录结构(小白请严格按照此目录进行,大佬随意)
main.py文件
# 1. 导入managerSystem模块
from managerSystem import *
# 2. 启动学员管理系统
if __name__ == '__main__':
student_manager = StudentManager()
student_manager.run()
student.py
# 在这个类中包含了这个学生的相关的信息
class Student(object):
def __init__(self, name, gender, tel):
self.name = name
self.gender = gender
self.tel = tel
def __str__(self):
return f'{self.name},{self.gender},{self.tel}'
class A(object):
a = 0
def __init__(self):
self.b = 1
aa = A()
print(A.__dict__)
print(aa.__dict__)
managerSystem.py
# from StudentManagerSystem.student import Student
from student import *
class StudentManager(object):
def __init__(self):
# 存储数据所需要的列表
self.student_list = []
# 程序函数的入口,启动程序后开始去执行
def run(self):
# load the information of the students
self.load_student()
while True:
# express the function of menu
self.show_menu()
# the user input the function of id
menu_num = int(input('请输入你需要的功能序号:'))
if menu_num == 1:
self.add_student()
elif menu_num == 2:
self.del_student()
elif menu_num == 3:
# 修改学员信息
self.modify_student()
elif menu_num == 4:
# 查询学员信息
self.search_student()
elif menu_num == 5:
# 显示所有学员信息
self.show_student()
elif menu_num == 6:
# 保存学员信息
self.save_student()
elif menu_num == 7:
# 退出系统
break
@staticmethod
def show_menu():
print('请选择如下功能-----------------')
print('1:添加学员')
print('2:删除学员')
print('3:修改学员信息')
print('4:查询学员信息')
print('5:显示所有学员信息')
print('6:保存学员信息')
print('7:退出系统')
# 2.2 添加学员
def add_student(self):
name = input('请输入您的姓名:')
gender = input('请输入您的性别:')
tel = input('请输入您的手机号:')
student = Student(name, gender, tel)
self.student_list.append(student)
print(self.student_list)
print(student)
# 2.3 删除学员
def del_student(self):
del_name = input('请输入要删除学生的姓名')
for i in self.student_list:
if i.name == del_name:
self.student_list.remove(i)
break
else:
print('查无此人')
print(self.student_list)
# 2.4 修改学员信息
def modify_student(self):
modify_name = input('请输入要修改学员的姓名:')
for i in self.student_list:
if i.name == modify_name:
i.name = input('请输入学员的姓名:')
i.gender = input('请输入学员的性别:')
i.tel = input('请输入学员的的手机号:')
print(f'修改该学员的信息成功,姓名{i.name},性别{i.gender},电话{i.tel}')
break
else:
print('查无此人')
# 2.5 查询学员信息
def search_student(self):
search_name = input('请输入要查询学生的个人信息')
for i in self.student_list:
if i.name == search_name:
print('查到了')
break
else:
print('查无此人')
# 2.6 显示所有学员信息
def show_student(self):
print('姓名\t性别\t手机号')
for i in self.student_list:
print(f'{i.name}\t{i.gender}\t{i.tel}')
# 2.7 保存学员信息
def save_student(self):
f = open('student.data', 'w')
new_list = [i.__dict__ for i in self.student_list]
print(new_list)
f.write(str(new_list))
f.close()
# 2.8 加载学员信息
def load_student(self):
try:
f = open('student.data', 'r')
except:
f = open('student.data', 'w')
else:
data = f.read()
new_list = eval(data)
self.student_list = [Student(i['name'], i['gender'], i['tel']) for i in new_list]
finally:
f.close()
完成代码的编写之后
直接在main。py中运行程序即可