What if a simple class design could save your hotel from booking chaos?
Why Hotel, Room, Booking classes in LLD? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine managing a hotel by writing down every room and booking on paper or in separate spreadsheets.
Every time a guest arrives, you manually check availability, update records, and track bookings.
This manual method is slow and prone to mistakes.
You might double-book rooms or lose track of cancellations.
It becomes hard to scale when the hotel grows or when many guests book at once.
Using Hotel, Room, and Booking classes organizes this information clearly in code.
Each class handles its own data and actions, making the system easy to manage and update.
This approach reduces errors and speeds up booking processes.
rooms = ['101', '102'] bookings = [] # Check availability manually if '101' not in bookings: bookings.append('101')
class Room: def __init__(self, number): self.number = number self.is_booked = False class Booking: def __init__(self, room): self.room = room self.room.is_booked = True
This design lets you easily add features like checking availability, cancelling bookings, and scaling to many rooms.
Online hotel booking websites use similar classes to manage thousands of rooms and bookings instantly and accurately.
Manual tracking is slow and error-prone.
Classes organize data and behavior clearly.
Design enables scalable, reliable booking systems.
Practice
Solution
Step 1: Understand the role of each class
The Hotel class manages the overall hotel, Booking handles reservations, and Room stores details about each room.Step 2: Identify which class holds room details
Since Room is designed to represent individual rooms, it stores room number, type, and availability.Final Answer:
Room -> Option AQuick Check:
Room class = stores room info [OK]
- Confusing Hotel with Room class
- Thinking Booking stores room details
- Assuming Guest class stores room info
Solution
Step 1: Recall Python constructor syntax
Python constructors use the special method __init__ with self as the first parameter.Step 2: Match the correct method signature
def __init__(self, room, guest, date): correctly uses def __init__(self, room, guest, date): which is the standard constructor format.Final Answer:
def __init__(self, room, guest, date): -> Option AQuick Check:
Constructor = __init__ method [OK]
- Using method name other than __init__
- Omitting self parameter
- Using class name as method name
class Room:
def __init__(self, number):
self.number = number
self.is_available = True
class Booking:
def __init__(self, room):
self.room = room
self.room.is_available = False
room101 = Room(101)
print(room101.is_available)
booking1 = Booking(room101)
print(room101.is_available)Solution
Step 1: Check initial availability of room101
When room101 is created, is_available is set to True, so first print outputs True.Step 2: Booking changes room availability
Booking constructor sets room101.is_available to False, so second print outputs False.Final Answer:
True\nFalse -> Option DQuick Check:
Initial True, then set False by Booking [OK]
- Assuming availability stays True after booking
- Confusing order of prints
- Ignoring side effect on room object
class Room:
def __init__(self, number):
self.number = number
self.is_available = True
class Booking:
def __init__(self, room, guest):
self.room = room
self.guest = guest
def book(self):
if self.room.is_available:
self.room.is_available = False
print("Booking successful")
else:
print("Room not available")
room = Room(201)
booking = Booking(room)
booking.book()Solution
Step 1: Check Booking constructor parameters
Booking __init__ requires room and guest, but only room is passed when creating booking instance.Step 2: Identify missing argument error
Omitting guest argument causes a TypeError at runtime.Final Answer:
Missing guest argument when creating Booking instance -> Option BQuick Check:
Constructor args mismatch = missing guest [OK]
- Ignoring missing guest argument
- Assuming book method must return value
- Thinking is_available must be a method
Solution
Step 1: Analyze class responsibilities
Hotel should manage Rooms, Booking should link Rooms and Guests, keeping clear separation.Step 2: Evaluate design for scalability
Have Hotel class contain a list of Room objects, and Booking class references Room and Guest; Hotel checks availability before booking. cleanly separates concerns, allowing Hotel to check availability and Booking to handle reservations, supporting easy maintenance and scaling.Final Answer:
Hotel manages Rooms; Booking references Room and Guest; Hotel checks availability -> Option CQuick Check:
Separation of concerns = Have Hotel class contain a list of Room objects, and Booking class references Room and Guest; Hotel checks availability before booking. [OK]
- Combining all logic in one class
- Booking managing Rooms directly
- Ignoring availability checks in Hotel
