面向对象版学员管理系统
项目根目录为:StudentManagerSystem
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}'
# aa = Student('aa', 'nv', 111)
# print(aa) # 测试内容
managerSystem.py代码为:
# 管理系统
from student import *
class StudentManager(object):
def __init__(self):
self.student_list = [] # 存储数据的列表
def run(self): # 入口函数
self.load_student() # 加载学员信息
while True:
self.show_menu()
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:
self.load_student() # 加载学员信息
elif menu_num == 8:
break # 退出系统
@staticmethod
def show_menu():
print("请选择如下功能----------------")
print("1,添加成员")
print("2,删除成员")
print("3,修改成员")
print("4,查询成员")
print("5,显示所有成员")
print("6,保存学员信息")
print("7,加载学员信息")
print("8,退出系统")
print("-" * 28)
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)
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)
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('查无所人!')
def search_student(self):
search_name = input('请输入要查找的学员的姓名:')
for i in self.student_list:
if i.name == search_name:
print(f'学员信息如下:')
print(f'姓名:{i.name},性别:{i.gender},手机号:{i.tel}')
break
else: # 正常结束执行的代码
print('查无所人!')
def show_student(self):
print('姓名\t性别\t手机号')
for i in self.student_list:
print(f'{i.name}\t{i.gender}\t{i.tel}')
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()
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:
# 主程序入口文件
from managerSystem import *
if __name__ == '__main__': # 当前文件
student_manager = StudentManager()
student_manager.run()