Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Implement a Hotel Composed of Rooms and Reservations

Build a small hotel booking system using composition: Room and Reservation objects managed by a Hotel. Enforce non-overlapping bookings and compute revenue.

Python practice15 minComposition & Real-World ModelingIntermediateLast updated April 11, 2026

Problem statement

You will implement a simple hotel booking system using composition (Room and Reservation objects inside a Hotel). Implement three classes: - Room: represents a single room with a room number, capacity, and nightly rate. - Reservation: represents a booking for a room with a guest, a start date (check-in) and end date (check-out). - Hotel: manages rooms and reservations. It should allow adding rooms, making reservations (if the room is available), checking available rooms for a date range, and computing total revenue from all reservations. Business rules: - Dates are given as strings in ISO format: 'YYYY-MM-DD'. Use Python's datetime.date to handle them. - The end date is the checkout date and is considered exclusive. A reservation from 2026-04-10 to 2026-04-12 occupies nights 10th and 11th and frees the room on the 12th. - Reservations must not overlap for the same room. Back-to-back reservations are allowed (one reservation's end date can equal another's start date). - Hotel.reserve(room_number, start_date, end_date, guest) should attempt to create a reservation and return True if successful or False if the room is not available or the room does not exist. - Hotel.available_rooms(start_date, end_date) returns a sorted list of room numbers that are free for the entire date range. - Hotel.total_revenue() should return the total revenue across all reservations computed as nightly_rate * nights for each reservation. You must implement the core class logic (Room, Reservation, Hotel). The provided starter code includes helper scenario functions that will be used in tests; those should remain as-is and will exercise your implementation. Edge cases to consider: back-to-back bookings, invalid room numbers when reserving, and zero-night or negative ranges (reserve should fail if end <= start).

Task

Practice composition by implementing Room, Reservation, and Hotel classes. Manage bookings, check availability, and compute total revenue using date logic.

Examples

Basic availability example

Input

h = Hotel() h.add_room(101, 2, 100) h.add_room(102, 2, 120) h.reserve(101, '2026-04-10', '2026-04-12', 'Alice') h.available_rooms('2026-04-10', '2026-04-12')

Output

[102]

Room 101 is booked for the nights of 10th and 11th; room 102 remains available so the function returns [102].

Input format

This is a class-implementation task. Your code will be exercised by helper scenario functions that create a Hotel, add rooms, and make reservations. Tests call those scenario functions as single expressions (e.g. scenario_basic()).

Output format

Each test expression should evaluate to a value (list, tuple, bool, int, etc.). The test harness compares the string representation of that value to the expected output.

Constraints

- Use only Python standard library (datetime allowed). - Date strings are 'YYYY-MM-DD'. - end_date is exclusive; reservations require end_date > start_date. - Keep class and method names exactly as specified in the starter code so the test helpers can call them.

Samples

Sample 1

Input

h = Hotel() h.add_room(201, 1, 80) # succeed h.reserve(201, '2026-05-01', '2026-05-04', 'Bob') # cannot overlap ok_second = h.reserve(201, '2026-05-03', '2026-05-05', 'Carol') (h.available_rooms('2026-05-01','2026-05-04'), ok_second, h.total_revenue())

Output

([], False, 240)

Room 201 is booked for nights 1-3 (3 nights at $80 = 240). The second reservation overlaps and fails, and there are no available rooms for those nights.