Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Build a School Management System with Classes Students and Teachers

Model a small school using composition: Student and Teacher objects managed by a School. Practice composing objects, managing relationships, and designing a clean API.

Python practice30 minComposition & Real-World ModelingAdvancedLast updated April 11, 2026

Problem statement

Design a small School Management System that demonstrates composition (a School contains Student and Teacher objects). Implement the following responsibilities: - Student and Teacher classes hold basic attributes (name, id) and the classes they are associated with. - A School class manages collections of students, teachers, and classes. Classes are identified by string names. - The School should allow adding students and teachers, assigning teachers to classes, enrolling students in classes, and querying: - students enrolled in a class (as a list of names, sorted alphabetically) - teachers assigned to a class (as a list of names, sorted alphabetically) - detailed info for a student or teacher (name and sorted list of class names). The School API should allow method chaining for modifications (so mutating methods return the School instance). Query methods should return concrete Python objects (lists or tuples). Ensure duplicate enrollments/assignments are idempotent (no duplicates). Keep all state inside objects (no globals).

Task

Implement Student and Teacher classes and a School manager that composes them. Support adding people, assigning teachers to classes, enrolling students, and querying class rosters and person info.

Examples

Basic workflow

Input

School().add_student('Alice', 1).add_teacher('Mr Lee', 10).assign_teacher_to_class(10, 'Math').enroll_student_in_class(1, 'Math').get_students_in_class('Math')

Output

['Alice']

Create a School, add one student and one teacher, assign the teacher to 'Math', enroll the student in 'Math', and list students in 'Math'.

Input format

You will use the provided classes and methods. Tests will call expressions like: School().add_student('Name', id).enroll_student_in_class(id, 'ClassName').get_students_in_class('ClassName')

Output format

Query methods return Python objects (lists or tuples). For example get_students_in_class returns a list of student names; student_info returns a tuple (name, [sorted class names]).

Constraints

- Use composition: School contains Student and Teacher instances. - Mutating methods (add/assign/enroll) should return the School instance to allow chaining. - Avoid duplicate students/teachers in a class; operations should be idempotent. - All class name lists returned should be sorted alphabetically. - No input/output functions; the API is object-oriented method calls.

Samples

Sample 1

Input

School().add_student('Bob', 2).enroll_student_in_class(2, 'Science').student_info(2)

Output

('Bob', ['Science'])

Add Bob and enroll him in Science. student_info returns his name and a list of classes.