设计一个学生管理系统,包括功能:1.添加学生《需要包含如学号,姓名性别,籍贯等信息)、2.删除学生、3.修改学生信息、4.查询单个学生信息5.查询所有学生的信息、6.退出系统。“
时间: 2024-03-10 12:48:09 浏览: 56
下面是一个简单的学生管理系统的设计:
1. 创建一个学生类,包含学号、姓名、性别、籍贯等信息,并提供相应的构造方法和属性访问方法。
2. 创建一个学生列表类,包含添加、删除、修改、查询单个和查询所有学生信息的方法。
3. 创建一个学生管理系统类,包含一个学生列表对象和一个菜单方法,提供添加、删除、修改、查询和退出系统的功能。
代码如下:
```python
class Student:
def __init__(self, id, name, gender, hometown):
self.id = id
self.name = name
self.gender = gender
self.hometown = hometown
def get_id(self):
return self.id
def get_name(self):
return self.name
def get_gender(self):
return self.gender
def get_hometown(self):
return self.hometown
class StudentList:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, id):
for student in self.students:
if student.get_id() == id:
self.students.remove(student)
def update_student(self, id, name, gender, hometown):
for student in self.students:
if student.get_id() == id:
student.name = name
student.gender = gender
student.hometown = hometown
def get_student(self, id):
for student in self.students:
if student.get_id() == id:
return student
return None
def get_all_students(self):
return self.students
class StudentManagementSystem:
def __init__(self):
self.student_list = StudentList()
def menu(self):
print("1. 添加学生")
print("2. 删除学生")
print("3. 修改学生信息")
print("4. 查询单个学生信息")
print("5. 查询所有学生信息")
print("6. 退出系统")
choice = input("请选择操作:")
if choice == "1":
id = input("请输入学号:")
name = input("请输入姓名:")
gender = input("请输入性别:")
hometown = input("请输入籍贯:")
student = Student(id, name, gender, hometown)
self.student_list.add_student(student)
elif choice == "2":
id = input("请输入要删除的学生学号:")
self.student_list.remove_student(id)
elif choice == "3":
id = input("请输入要修改的学生学号:")
name = input("请输入新姓名:")
gender = input("请输入新性别:")
hometown = input("请输入新籍贯:")
self.student_list.update_student(id, name, gender, hometown)
elif choice == "4":
id = input("请输入要查询的学生学号:")
student = self.student_list.get_student(id)
if student:
print("学号:", student.get_id())
print("姓名:", student.get_name())
print("性别:", student.get_gender())
print("籍贯:", student.get_hometown())
else:
print("找不到该学生!")
elif choice == "5":
students = self.student_list.get_all_students()
for student in students:
print("学号:", student.get_id())
print("姓名:", student.get_name())
print("性别:", student.get_gender())
print("籍贯:", student.get_hometown())
elif choice == "6":
print("退出系统!")
return
else:
print("输入有误,请重新输入!")
self.menu()
if __name__ == '__main__':
system = StudentManagementSystem()
system.menu()
```
运行程序后,会显示一个菜单,根据需要选择相应的操作即可。
阅读全文