创建一个学生管理系统,满足1.添加学生2.删除学生3.修改学生4.查询学生5.获取所有学生信息6.保存信息7.退出系统,要求实现备份。
时间: 2024-02-26 10:58:05 浏览: 83
好的,我可以帮您创建一个学生管理系统。这个系统将使用 Python 语言编写,使用文件进行数据存储,并实现了备份功能。以下是代码实现:
```python
import os
import shutil
class Student:
def __init__(self, id, name, age, gender, grade):
self.id = id
self.name = name
self.age = age
self.gender = gender
self.grade = grade
class StudentManager:
def __init__(self):
self.students = []
self.data_file = "student_data.txt"
self.backup_dir = "backup"
def load_data(self):
if not os.path.exists(self.data_file):
return
with open(self.data_file, "r") as f:
lines = f.readlines()
for line in lines:
id, name, age, gender, grade = line.strip().split(",")
student = Student(id, name, age, gender, grade)
self.students.append(student)
def save_data(self):
with open(self.data_file, "w") as f:
for student in self.students:
line = f"{student.id},{student.name},{student.age},{student.gender},{student.grade}\n"
f.write(line)
def backup_data(self):
if not os.path.exists(self.backup_dir):
os.mkdir(self.backup_dir)
backup_file = os.path.join(self.backup_dir, self.data_file)
shutil.copyfile(self.data_file, backup_file)
def add_student(self):
id = input("请输入学生ID:")
name = input("请输入学生姓名:")
age = input("请输入学生年龄:")
gender = input("请输入学生性别(男/女):")
grade = input("请输入学生成绩:")
student = Student(id, name, age, gender, grade)
self.students.append(student)
def delete_student(self):
id = input("请输入要删除的学生ID:")
for student in self.students:
if student.id == id:
self.students.remove(student)
print("删除成功!")
return
print("未找到对应学生!")
def modify_student(self):
id = input("请输入要修改的学生ID:")
for student in self.students:
if student.id == id:
name = input(f"请输入学生姓名(原值为{student.name}):")
age = input(f"请输入学生年龄(原值为{student.age}):")
gender = input(f"请输入学生性别(原值为{student.gender}):")
grade = input(f"请输入学生成绩(原值为{student.grade}):")
student.name = name
student.age = age
student.gender = gender
student.grade = grade
print("修改成功!")
return
print("未找到对应学生!")
def query_student(self):
id = input("请输入要查询的学生ID:")
for student in self.students:
if student.id == id:
print(f"学生ID:{student.id},姓名:{student.name},年龄:{student.age},性别:{student.gender},成绩:{student.grade}")
return
print("未找到对应学生!")
def get_all_students(self):
for student in self.students:
print(f"学生ID:{student.id},姓名:{student.name},年龄:{student.age},性别:{student.gender},成绩:{student.grade}")
def run(self):
self.load_data()
while True:
print("请输入操作编号:")
print("1.添加学生")
print("2.删除学生")
print("3.修改学生")
print("4.查询学生")
print("5.获取所有学生信息")
print("6.保存信息")
print("7.退出系统")
choice = input()
if choice == "1":
self.add_student()
elif choice == "2":
self.delete_student()
elif choice == "3":
self.modify_student()
elif choice == "4":
self.query_student()
elif choice == "5":
self.get_all_students()
elif choice == "6":
self.save_data()
self.backup_data()
print("保存成功!")
elif choice == "7":
break
else:
print("输入有误,请重新输入!")
if __name__ == "__main__":
manager = StudentManager()
manager.run()
```
在此系统中,我们定义了一个 `Student` 类,表示一个学生对象。然后,我们定义了一个 `StudentManager` 类,表示学生管理器,它包含了上述所有功能。在 `load_data` 函数中,我们从文件中读取数据,然后在 `save_data` 函数中,我们将数据写入文件中。在 `backup_data` 函数中,我们将文件备份到一个备份目录中。
在 `run` 函数中,我们循环接收用户输入,然后根据用户的选择调用对应的函数。注意,在用户选择保存信息时,我们同时备份了数据。
这样,一个简单的学生管理系统就实现了!
阅读全文