设计一个学生管理系统,完成学生基本信息(包括学号、姓名和性别)的添加、输出、查询、显示、保存(将数据保存到文件)、读取(从文件读取数据)和退出系统功能
时间: 2023-07-10 10:21:29 浏览: 67
好的,这是一个比较完整的系统设计问题。你可以使用 Python 语言来实现。
以下是一个示例代码,用于设计学生管理系统:
```python
import os
# 学生类,用于保存学生基本信息
class Student:
def __init__(self, id, name, gender):
self.id = id
self.name = name
self.gender = gender
def __str__(self):
return f"学号:{self.id},姓名:{self.name},性别:{self.gender}"
# 学生管理类,用于实现学生管理系统的功能
class StudentManager:
def __init__(self):
self.students = []
# 添加学生
def add_student(self):
id = input("请输入学号:")
name = input("请输入姓名:")
gender = input("请输入性别:")
student = Student(id, name, gender)
self.students.append(student)
print("添加成功!")
# 输出所有学生信息
def show_all_students(self):
print("所有学生信息如下:")
for student in self.students:
print(student)
# 查询学生信息
def query_student(self):
id = input("请输入要查询的学生的学号:")
for student in self.students:
if student.id == id:
print("查询结果如下:")
print(student)
return
print("没有找到该学生!")
# 显示菜单
def show_menu(self):
print("学生管理系统菜单:")
print("1. 添加学生")
print("2. 输出所有学生信息")
print("3. 查询学生信息")
print("4. 保存数据")
print("5. 读取数据")
print("6. 退出系统")
# 保存数据
def save_data(self):
with open("students.txt", "w") as f:
for student in self.students:
f.write(f"{student.id},{student.name},{student.gender}\n")
print("保存成功!")
# 读取数据
def load_data(self):
if os.path.exists("students.txt"):
with open("students.txt", "r") as f:
lines = f.readlines()
for line in lines:
data = line.strip().split(",")
student = Student(data[0], data[1], data[2])
self.students.append(student)
print("读取成功!")
else:
print("数据文件不存在!")
# 运行系统
def run(self):
self.load_data()
while True:
self.show_menu()
choice = input("请输入菜单选项:")
if choice == "1":
self.add_student()
elif choice == "2":
self.show_all_students()
elif choice == "3":
self.query_student()
elif choice == "4":
self.save_data()
elif choice == "5":
self.load_data()
elif choice == "6":
print("退出系统,谢谢使用!")
break
else:
print("无效的菜单选项,请重新输入!")
# 创建学生管理对象并运行系统
manager = StudentManager()
manager.run()
```
这段代码首先定义了一个 Student 类,用于保存学生基本信息。然后定义了一个 StudentManager 类,用于实现学生管理系统的功能。
在 StudentManager 类中,代码实现了添加学生、输出所有学生信息、查询学生信息、保存数据、读取数据和退出系统等功能。其中,保存数据和读取数据使用了文件操作。
最后,代码创建了一个 StudentManager 对象,并调用了 run 方法来运行系统。
注意,这只是一个简单的示例代码,实际应用中还需要加上一些错误处理和异常处理的代码。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)