Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Model a Hospital with Patients Doctors and Appointments

Design a small hospital scheduling system using composition. Model Patients, Doctors and Appointments and implement scheduling rules.

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

Problem statement

You will model a simplified hospital scheduling system using composition (objects containing other objects) rather than inheritance. Implement the following classes and top-level helper to create a demo hospital: - Patient: stores name and age. - Doctor: stores name and specialty. - Appointment: represents an appointment between a patient and a doctor at a specific time and has a unique integer id. - Hospital: manages patients, doctors and appointments. It should provide methods to add patients and doctors, schedule appointments, cancel appointments and query appointments by doctor or for a patient. Scheduling rules and behavior: - Appointment ids start at 1 and increment by 1 for each scheduled appointment. - schedule_appointment(patient_name, doctor_name, time) should only succeed if both patient and doctor exist and if the doctor has no other appointment at the exact same time. On success, create and store an Appointment and return its id (int). On failure (missing patient/doctor or conflict) return None. - cancel_appointment(appointment_id) removes an appointment if it exists and returns True; otherwise return False. - get_appointments_by_doctor(doctor_name) returns a list of tuples (patient_name, time) for that doctor's appointments in insertion order. If doctor does not exist or has no appointments, return an empty list. - get_next_appointment_for_patient(patient_name) returns a tuple (doctor_name, time) for the earliest appointment of that patient by insertion order, or None if none exists. Follow composition: Hospital should contain collections of Patient, Doctor, and Appointment objects. Do not use inheritance to solve the problem.

Task

Implement a Hospital system that composes Patient, Doctor and Appointment objects. Support adding patients/doctors, scheduling, cancelling and querying appointments while preventing doctor double-booking.

Examples

Schedule and query

Input

h = Hospital(); h.add_patient(Patient('Alice', 30)); h.add_doctor(Doctor('Dr. Smith', 'Cardiology')); id = h.schedule_appointment('Alice','Dr. Smith','09:00'); h.get_appointments_by_doctor('Dr. Smith')

Output

[('Alice', '09:00')]

After adding a patient and doctor, scheduling an appointment returns an id and querying the doctor's appointments returns a list with one tuple for Alice at 09:00.

Input format

All functions are invoked via direct Python expressions. Use the provided classes and functions in the global scope.

Output format

Functions return Python values. The test harness compares str(return_value) (or '' for None) to expected output strings.

Constraints

Use composition: Hospital must store Patient, Doctor and Appointment objects. Appointment ids must be consecutive integers starting at 1. Scheduling must prevent a doctor from having two appointments at the same exact time. No external libraries.

Samples

Sample 1

Input

h = Hospital(); h.add_patient(Patient('Alice', 30)); h.add_doctor(Doctor('Dr. Smith', 'Cardiology')); h.schedule_appointment('Alice','Dr. Smith','09:00')

Output

1

First scheduled appointment receives id 1.