You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import json
class StudentManagementSystem:
def __init__(self):
# 存储学员信息的列表
self.students = []
# 记录当前操作,用于提示用户
self.current_operation = None
def display_menu(self):
# 显示系统菜单
print("\n学生信息管理系统")
print("1. 添加学员信息")
print("2. 删除学员信息")
print("3. 修改学员信息")
print("4. 保存学员信息")
print("5. 查询学员信息")
print("6. 显示学员信息")
print("7. 退出系统")
def add_student(self, name, gender, phone_number):
# 添加学员信息
student = {
"姓名": name,
"性别": gender,
"手机号": phone_number
}
self.students.append(student)
self.current_operation = "添加学员信息"
def delete_student(self, name):
# 删除学员信息
for student in self.students:
if student["姓名"] == name:
self.students.remove(student)
self.current_operation = "删除学员信息"
return True
return False
def modify_student(self, name, new_data):
# 修改学员信息
for i, student in enumerate(self.students):
if student["姓名"] == name:
self.students[i] = new_data
self.current_operation = "修改学员信息"
return True
return False
def save_data(self, filename="student_data.json"):
# 保存学员信息到文件JSON格式
with open(filename, "w", encoding="utf-8") as file:
json.dump(self.students, file, ensure_ascii=False, indent=4)
print("数据保存成功!")
def query_student(self, name):
# 查询学员信息
for student in self.students:
if student["姓名"] == name:
return student
return None
def display_students(self):
# 显示所有学员信息
if not self.students:
print("暂无学员信息")
else:
for student in self.students:
print(student)
def run(self):
while True:
# 运行系统主循环
self.display_menu()
choice = input("请输入您的选择:")
if choice == "1":
# 添加学员信息
name = input("请输入姓名:")
gender = input("请输入性别:")
phone_number = input("请输入手机号:")
self.add_student(name, gender, phone_number)
elif choice == "2":
# 删除学员信息
name = input("请输入要删除的学员姓名:")
if self.delete_student(name):
print("学员信息删除成功!")
else:
print("未找到学员信息,删除失败!")
elif choice == "3":
# 修改学员信息
name = input("请输入要修改的学员姓名:")
new_data = {
"姓名": input("请输入新的姓名:"),
"性别": input("请输入新的性别:"),
"手机号": input("请输入新的手机号:")
}
if self.modify_student(name, new_data):
print("学员信息修改成功!")
else:
print("未找到学员信息,修改失败!")
elif choice == "4":
# 保存学员信息到文件
self.save_data()
elif choice == "5":
# 查询学员信息
name = input("请输入要查询的学员姓名:")
student = self.query_student(name)
if student:
print("学员信息查询成功:", student)
else:
print("未找到学员信息!")
elif choice == "6":
# 显示所有学员信息
self.display_students()
elif choice == "7":
# 退出系统
print("感谢使用学生信息管理系统,再见!")
break
else:
print("无效的选择,请重新输入。")
if self.current_operation:
# 提示用户操作完成
print(f"{self.current_operation}操作完成,数据已更新。")
self.current_operation = None
if __name__ == "__main__":
# 实例化学生信息管理系统并运行
sms = StudentManagementSystem()
sms.run()