(4)学生管理:包括添加学生信息,删除学生信息,修改学生信息,查询学生信息(按学号查询学生信息、按姓名查询学生信息、查询某个班级的学生信息、查询某个学院的学生信息);学生属于某个班级。帮我写代码
时间: 2024-02-11 22:08:57 浏览: 86
学生信息管理系统,实现学生信息包括:学号、姓名、性别、年龄、班级等信息。
5星 · 资源好评率100%
好的,以下是一个简单的学生管理系统的代码示例,其中包含添加、删除、修改和查询学生信息的功能,以及学生属于班级的关联:
```python
class Student:
def __init__(self, id, name, gender, age, class_id):
self.id = id
self.name = name
self.gender = gender
self.age = age
self.class_id = class_id
class Class:
def __init__(self, id, name):
self.id = id
self.name = name
self.students = []
class College:
def __init__(self, id, name):
self.id = id
self.name = name
self.classes = []
class StudentManager:
def __init__(self):
self.colleges = []
def add_student(self, id, name, gender, age, class_id):
# find the class that the student belongs to
class_obj = None
for college in self.colleges:
for class_obj in college.classes:
if class_obj.id == class_id:
break
if class_obj:
break
if not class_obj:
print(f"Error: class {class_id} not found")
return
# add the student to the class
student = Student(id, name, gender, age, class_id)
class_obj.students.append(student)
def delete_student(self, id):
# find the student to delete
for college in self.colleges:
for class_obj in college.classes:
for student in class_obj.students:
if student.id == id:
class_obj.students.remove(student)
return
print(f"Error: student {id} not found")
def update_student(self, id, name, gender, age, class_id):
# find the student to update
for college in self.colleges:
for class_obj in college.classes:
for student in class_obj.students:
if student.id == id:
student.name = name
student.gender = gender
student.age = age
student.class_id = class_id
return
print(f"Error: student {id} not found")
def search_student_by_id(self, id):
# find the student by id
for college in self.colleges:
for class_obj in college.classes:
for student in class_obj.students:
if student.id == id:
return student
print(f"Error: student {id} not found")
return None
def search_student_by_name(self, name):
# find the student by name
result = []
for college in self.colleges:
for class_obj in college.classes:
for student in class_obj.students:
if student.name == name:
result.append(student)
if len(result) == 0:
print(f"Error: student {name} not found")
return result
def search_student_by_class(self, class_id):
# find the students in a class
result = []
for college in self.colleges:
for class_obj in college.classes:
if class_obj.id == class_id:
result.extend(class_obj.students)
if len(result) == 0:
print(f"Error: class {class_id} not found")
return result
def search_student_by_college(self, college_id):
# find the students in a college
result = []
for college in self.colleges:
if college.id == college_id:
for class_obj in college.classes:
result.extend(class_obj.students)
if len(result) == 0:
print(f"Error: college {college_id} not found")
return result
def add_class(self, id, name, college_id):
# find the college to add the class to
college = None
for c in self.colleges:
if c.id == college_id:
college = c
break
if not college:
print(f"Error: college {college_id} not found")
return
# add the class to the college
class_obj = Class(id, name)
college.classes.append(class_obj)
def delete_class(self, id):
# find the class to delete
for college in self.colleges:
for class_obj in college.classes:
if class_obj.id == id:
college.classes.remove(class_obj)
return
print(f"Error: class {id} not found")
def update_class(self, id, name):
# find the class to update
for college in self.colleges:
for class_obj in college.classes:
if class_obj.id == id:
class_obj.name = name
return
print(f"Error: class {id} not found")
def search_class_by_id(self, id):
# find the class by id
for college in self.colleges:
for class_obj in college.classes:
if class_obj.id == id:
return class_obj
print(f"Error: class {id} not found")
return None
def search_class_by_name(self, name):
# find the class by name
result = []
for college in self.colleges:
for class_obj in college.classes:
if class_obj.name == name:
result.append(class_obj)
if len(result) == 0:
print(f"Error: class {name} not found")
return result
def add_college(self, id, name):
# add a new college
college = College(id, name)
self.colleges.append(college)
def delete_college(self, id):
# find the college to delete
for college in self.colleges:
if college.id == id:
self.colleges.remove(college)
return
print(f"Error: college {id} not found")
def update_college(self, id, name):
# find the college to update
for college in self.colleges:
if college.id == id:
college.name = name
return
print(f"Error: college {id} not found")
def search_college_by_id(self, id):
# find the college by id
for college in self.colleges:
if college.id == id:
return college
print(f"Error: college {id} not found")
return None
def search_college_by_name(self, name):
# find the college by name
result = []
for college in self.colleges:
if college.name == name:
result.append(college)
if len(result) == 0:
print(f"Error: college {name} not found")
return result
```
使用示例:
```python
manager = StudentManager()
# add some colleges
manager.add_college(1, "College of Engineering")
manager.add_college(2, "College of Science")
# add some classes
manager.add_class(101, "Class 1", 1)
manager.add_class(102, "Class 2", 1)
manager.add_class(201, "Class 3", 2)
manager.add_class(202, "Class 4", 2)
# add some students
manager.add_student(1, "Alice", "F", 20, 101)
manager.add_student(2, "Bob", "M", 21, 101)
manager.add_student(3, "Charlie", "M", 19, 102)
manager.add_student(4, "David", "M", 22, 201)
manager.add_student(5, "Eve", "F", 20, 201)
# update a student's information
manager.update_student(1, "Alicia", "F", 20, 102)
# search for a student by id
student = manager.search_student_by_id(1)
print(student.name) # should print "Alicia"
# search for students by name
students = manager.search_student_by_name("Bob")
for student in students:
print(student.id, student.name) # should print "2 Bob"
# search for students in a class
students = manager.search_student_by_class(201)
for student in students:
print(student.id, student.name) # should print "4 David" and "5 Eve"
# search for students in a college
students = manager.search_student_by_college(1)
for student in students:
print(student.id, student.name) # should print "1 Alicia", "2 Bob", and "3 Charlie"
# delete a student
manager.delete_student(3)
# add a new college
manager.add_college(3, "College of Arts")
# delete a class
manager.delete_class(202)
# update a class's name
manager.update_class(201, "New Class Name")
# search for a class by id
class_obj = manager.search_class_by_id(101)
print(class_obj.name) # should print "Class 1"
# search for classes by name
classes = manager.search_class_by_name("Class 2")
for class_obj in classes:
print(class_obj.id, class_obj.name) # should print "102 Class 2"
# search for a college by id
college = manager.search_college_by_id(2)
print(college.name) # should print "College of Science"
# search for colleges by name
colleges = manager.search_college_by_name("College of Engineering")
for college in colleges:
print(college.id, college.name) # should print "1 College of Engineering"
```
阅读全文