Python类的使用案例----设计一个学生管理系统

设计一个学生管理系统

设计学生类(Student)

属性:姓名(name)、学号(student_id)、年龄(age)、成绩(grades)

设计学生管理系统类(StudentManagementSystem)

属性:学生列表(students)

class Student:
    def __init__(self, name, id, age, grades):
        self.name = name
        self.id = id
        self.age = age
        self.grades = grades

    def display_info(self):
        print(f"Name: {self.name}, Student ID: {self.id}, Age: {self.age}, Grades: {self.grades}")
class StudentManagementSystem:
    def __init__(self):
        self.students = []  # 存储Student对象的列表

    def add_student(self, name, id, age, grades ):
        student = Student(name, id, age, grades)     # 创建Student类的对象
        self.students.append(student)

    def delete_student(self, id):
        for stu in self.students:
            if stu.id == id:
                self.students.remove(stu)
                print(f"Student {id} deleted successfully.")
                break
            else:
                print(f"Student {id} not found.")

    def display_all_students(self):
        print("-------------  All students ------------- ")
        for stu in self.students:
            stu.display_info()
            # print("-" * 50)
            # print("\n")
***Main Test***
if __name__ == '__main__':
    # s = Student("James", "s598", 24, { "Math": 90, "English": 88, "Physics": 95 })
    # s.display_info()
    sms = StudentManagementSystem()     # 创建学生管理系统对象
    sms.add_student("Alice", "S001", 20, {"Math": 85, "English": 90, "Physics": 92})
    sms.add_student("Bob", "S002", 21, {"Math": 90, "English": 88, "Physics": 95})
    sms.add_student("James", "S001", 20, {"Math": 85, "English": 90, "Physics": 92})
    sms.add_student("Aj", "S002", 21, {"Math": 90, "English": 88, "Physics": 95})
    sms.add_student("Will", "S001", 20, {"Math": 85, "English": 90, "Physics": 92})
    sms.add_student("Cherry", "S002", 21, {"Math": 90, "English": 88, "Physics": 95})
    sms.add_student("Ming", "S001", 20, {"Math": 85, "English": 90, "Physics": 92})
    sms.add_student("Ice", "S002", 21, {"Math": 90, "English": 88, "Physics": 95})

    sms.display_all_students()

posted @ 2024-02-03 16:25  阿大当家  阅读(86)  评论(0)    收藏  举报