设计一学生信息管理系统,使之能提供以下功能:1、系统以菜单方式工作2、能够完成对学生信息录入(学生信息用文件保存),可不定时地增加学生信息。3、学生信息浏览功能4、学生信息查询功能(1)按学号查询(2)按姓名查询5、学生信息删除与修改(1)输入学号,若该学生存在,删除该学生信息。6、图书信息的修改输入学号,查询该学生,如果存在,则修改该学生信息(不允许修改学号的程序代码
时间: 2024-02-18 08:04:52 浏览: 59
好的,以下是设计的学生信息管理系统:
```python
import os
class Student:
def __init__(self, name, age, id_num):
self.name = name
self.age = age
self.id_num = id_num
def __str__(self):
return f"Name:{self.name}, Age:{self.age}, ID:{self.id_num}"
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def delete_student(self, id_num):
for student in self.students:
if student.id_num == id_num:
self.students.remove(student)
def modify_student(self, id_num, name, age):
for student in self.students:
if student.id_num == id_num:
student.name = name
student.age = age
def search_by_id(self, id_num):
for student in self.students:
if student.id_num == id_num:
return student
def search_by_name(self, name):
results = []
for student in self.students:
if student.name == name:
results.append(student)
return results
def display_all(self):
for student in self.students:
print(student)
def save_to_file(self, file_path):
with open(file_path, "w") as f:
for student in self.students:
f.write(f"{student.name},{student.age},{student.id_num}\n")
def load_from_file(self, file_path):
if not os.path.exists(file_path):
return
with open(file_path, "r") as f:
lines = f.readlines()
for line in lines:
name, age, id_num = line.strip().split(",")
student = Student(name, int(age), id_num)
self.add_student(student)
def main():
manager = StudentManager()
file_path = "students.txt"
manager.load_from_file(file_path)
while True:
print("1. Add student")
print("2. Delete student")
print("3. Modify student")
print("4. Search by ID")
print("5. Search by name")
print("6. Display all")
print("7. Save to file")
print("8. Exit")
choice = input("Please enter your choice: ")
if choice == "1":
name = input("Enter name: ")
age = input("Enter age: ")
id_num = input("Enter ID number: ")
student = Student(name, int(age), id_num)
manager.add_student(student)
elif choice == "2":
id_num = input("Enter ID number: ")
manager.delete_student(id_num)
elif choice == "3":
id_num = input("Enter ID number: ")
name = input("Enter name: ")
age = input("Enter age: ")
manager.modify_student(id_num, name, int(age))
elif choice == "4":
id_num = input("Enter ID number: ")
student = manager.search_by_id(id_num)
if student:
print(student)
else:
print("Student not found")
elif choice == "5":
name = input("Enter name: ")
students = manager.search_by_name(name)
if students:
for student in students:
print(student)
else:
print("Student not found")
elif choice == "6":
manager.display_all()
elif choice == "7":
manager.save_to_file(file_path)
print("Data saved to file")
elif choice == "8":
print("Goodbye!")
break
else:
print("Invalid choice")
if __name__ == "__main__":
main()
```
这个系统使用了面向对象的编程方式,其中Student类表示一个学生,StudentManager类表示学生信息管理器。系统使用菜单方式工作,可以完成对学生信息的录入、浏览、查询、删除、修改和保存到文件的功能。学生信息用文件保存,可以不定时地增加学生信息。按学号查询和按姓名查询都是支持的。不允许修改学号的程序代码已经在修改学生信息的函数中实现。
阅读全文