main.py
# 1. 导入managerSystem模块
from managerSystem import *
# 2. 启动管理系统
# 保证是当前文件运行才启动管理系统: if --创建对象并调用run方法
if __name__ == "__main__":
student = StudentManager()
student.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}"
# aa = Student("aa", "nv", 1214)
# print(aa)
managerSystem.py
from student import *
class StudentManager(object):
def __init__(self):
# 存储学生对象 列表
self.student_list = []
# 一、程序入口函数
def run(self):
# 1. 加载文件学员数据
self.load_student()
while True:
# 2. 显示功能菜单
self.show_menu()
# 3. 用户输入功能序号
menu_num = int(input("请输入功能序号:"))
# 4. 根据序号执行不同的功能
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.退出系统")
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):
"""删除学员"""
name = input("请输入要删除的学员姓名:")
for i in self.student_list:
if i.name == name:
self.student_list.remove(i)
break
else:
print("查无此人")
print(self.student_list)
def modify_student(self):
"""修改学员"""
name = input("请输入要修改的学员姓名:")
for i in self.student_list:
if i.name == 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):
"""查询学员信息"""
name = input("请输入要查询的学员姓名:")
for i in self.student_list:
if i.name == name:
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):
"""保存所有学员信息"""
# 1. 打开文件
f = open("student.data", "w")
# 2. 写入数据
new_list = [i.__dict__ for i in self.student_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()