用Python写一个学员信息管理系统以文件方式进行增删查改,并且能够根据学号进行排序
时间: 2023-12-14 11:37:35 浏览: 64
好的,我可以给您提供一个基础版本的代码,您可以在此基础上进行修改和完善。
```python
import os
class Student:
def __init__(self, sid, name, age, gender):
self.sid = sid
self.name = name
self.age = age
self.gender = gender
def __str__(self):
return f"{self.sid}\t{self.name}\t{self.age}\t{self.gender}"
def to_dict(self):
return {
"sid": self.sid,
"name": self.name,
"age": self.age,
"gender": self.gender
}
@staticmethod
def from_dict(data):
return Student(data["sid"], data["name"], data["age"], data["gender"])
class StudentManager:
def __init__(self, data_file):
self.data_file = data_file
self.students = []
if os.path.exists(data_file):
with open(data_file, "r") as f:
for line in f:
sid, name, age, gender = line.strip().split("\t")
student = Student(sid, name, age, gender)
self.students.append(student)
def add_student(self, sid, name, age, gender):
student = Student(sid, name, age, gender)
self.students.append(student)
self.save_data()
def delete_student(self, sid):
for i, student in enumerate(self.students):
if student.sid == sid:
del self.students[i]
self.save_data()
return True
return False
def find_student(self, sid):
for student in self.students:
if student.sid == sid:
return student
return None
def update_student(self, sid, name=None, age=None, gender=None):
student = self.find_student(sid)
if student is None:
return False
if name is not None:
student.name = name
if age is not None:
student.age = age
if gender is not None:
student.gender = gender
self.save_data()
return True
def sort_students(self):
self.students = sorted(self.students, key=lambda x: int(x.sid))
self.save_data()
def save_data(self):
with open(self.data_file, "w") as f:
for student in self.students:
f.write(str(student) + "\n")
def main():
manager = StudentManager("students.txt")
while True:
print("1. Add student")
print("2. Delete student")
print("3. Find student")
print("4. Update student")
print("5. Sort students")
print("6. Exit")
choice = input("Please select an option: ")
if choice == "1":
sid = input("Please enter student ID: ")
name = input("Please enter student name: ")
age = input("Please enter student age: ")
gender = input("Please enter student gender: ")
manager.add_student(sid, name, age, gender)
print("Student added successfully.")
elif choice == "2":
sid = input("Please enter student ID: ")
if manager.delete_student(sid):
print("Student deleted successfully.")
else:
print("Student not found.")
elif choice == "3":
sid = input("Please enter student ID: ")
student = manager.find_student(sid)
if student is not None:
print(student)
else:
print("Student not found.")
elif choice == "4":
sid = input("Please enter student ID: ")
name = input("Please enter new name (enter to skip): ")
age = input("Please enter new age (enter to skip): ")
gender = input("Please enter new gender (enter to skip): ")
if manager.update_student(sid, name, age, gender):
print("Student updated successfully.")
else:
print("Student not found.")
elif choice == "5":
manager.sort_students()
print("Students sorted by ID.")
elif choice == "6":
print("Goodbye!")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
```
这个学员信息管理系统使用了 `Student` 和 `StudentManager` 两个类来实现。
其中,`Student` 类表示一个学员,包含学号、姓名、年龄和性别等基本信息。
`StudentManager` 类用于管理学员信息。它可以添加、删除、查找、修改和排序学员信息,并且将信息保存到文件中。在每次启动程序时,它会从文件中读取已有的学员信息。
在 `main` 函数中,我们定义了一个循环,不断提示用户选择操作,然后调用 `StudentManager` 中对应的方法来实现该操作。
您可以根据自己的需求进行修改和完善。
阅读全文