Bird
Raised Fist0
LLDsystem_design~15 mins

Hotel, Room, Booking classes in LLD - Deep Dive

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Overview - Hotel, Room, Booking classes
What is it?
Hotel, Room, and Booking classes are a way to organize information about hotels, their rooms, and reservations in software. Each class represents a real-world entity: a hotel, a room inside the hotel, and a booking made by a guest. These classes help manage data and actions related to hotel management in a clear and structured way.
Why it matters
Without these classes, managing hotel data would be chaotic and error-prone. They solve the problem of keeping track of which rooms are available, who booked them, and when. This organization makes it easier to build systems like hotel websites or apps that customers and staff can use reliably.
Where it fits
Before learning this, you should understand basic programming concepts like classes and objects. After this, you can learn about more complex systems like payment processing, user authentication, or scalable booking platforms.
Mental Model
Core Idea
Hotel, Room, and Booking classes model real-world hotel operations by representing hotels, their rooms, and reservations as connected objects with clear responsibilities.
Think of it like...
Think of a hotel as a building, rooms as individual apartments inside it, and bookings as rental agreements for those apartments during specific dates.
┌───────────┐      ┌───────────┐      ┌────────────┐
│  Hotel    │──────│   Room    │──────│  Booking   │
│ - name    │      │ - number  │      │ - guest    │
│ - address │      │ - type    │      │ - dates    │
│ - rooms[] │      │ - status  │      │ - status   │
└───────────┘      └───────────┘      └────────────┘
Build-Up - 7 Steps
1
FoundationDefine Hotel Class Basics
🤔
Concept: Introduce the Hotel class to represent a hotel with basic details and a collection of rooms.
Create a Hotel class with properties like name, address, and a list to hold Room objects. This class acts as the container for all rooms in the hotel.
Result
A Hotel object can store its name, address, and keep track of all its rooms.
Understanding that a hotel is a container for rooms helps organize data logically and mirrors real-world structure.
2
FoundationDefine Room Class Basics
🤔
Concept: Introduce the Room class to represent individual rooms with attributes like room number and type.
Create a Room class with properties such as room number, type (single, double), and availability status. Rooms belong to a hotel.
Result
Room objects can represent each room's details and current availability.
Modeling rooms separately allows detailed management of each room's state and characteristics.
3
IntermediateIntroduce Booking Class
🤔Before reading on: do you think a Booking should store only guest info or also room and date details? Commit to your answer.
Concept: Create a Booking class that links a guest to a specific room for a date range.
The Booking class holds guest information, the room booked, and the start and end dates of the stay. It also tracks booking status (confirmed, cancelled).
Result
Booking objects represent reservations connecting guests to rooms over time.
Linking bookings to rooms and dates models real reservations and enables availability checks.
4
IntermediateConnect Rooms to Hotel
🤔Before reading on: do you think rooms should know their hotel or only hotels know their rooms? Commit to your answer.
Concept: Establish a two-way relationship where rooms know their hotel and hotels know their rooms.
Add a reference in Room to its Hotel and ensure Hotel maintains a list of its Rooms. This helps navigation and data integrity.
Result
Rooms and hotels are connected, enabling queries like 'which hotel does this room belong to?'
Two-way links improve data consistency and make traversing relationships easier.
5
IntermediateManage Room Availability
🤔Before reading on: do you think room availability should be a simple flag or computed from bookings? Commit to your answer.
Concept: Room availability is determined by checking existing bookings for date conflicts.
Instead of a simple flag, implement a method in Room that checks if any Booking overlaps with requested dates to decide availability.
Result
Room availability reflects actual bookings, preventing double-booking.
Computing availability dynamically ensures accuracy and prevents booking conflicts.
6
AdvancedImplement Booking Lifecycle
🤔Before reading on: do you think cancelling a booking should free the room immediately or wait until the booking date? Commit to your answer.
Concept: Manage booking states and their effects on room availability over time.
Add methods to confirm, cancel, or modify bookings. Cancelling a booking frees the room for those dates immediately. Confirmed bookings block availability.
Result
Booking lifecycle management keeps room availability up-to-date and consistent.
Handling booking states properly prevents errors and improves user experience.
7
ExpertDesign for Scalability and Concurrency
🤔Before reading on: do you think a single system can handle all bookings instantly or do we need special techniques? Commit to your answer.
Concept: Address challenges of multiple users booking rooms simultaneously and scaling the system.
Implement locking or transactional checks to prevent race conditions during booking. Use caching and database indexing for performance. Consider distributed systems for large hotels.
Result
The system can handle many users booking rooms at once without errors or slowdowns.
Understanding concurrency and scalability is crucial for real-world booking systems to avoid conflicts and maintain speed.
Under the Hood
Each class is an object with properties and methods. Hotel holds a collection of Room objects. Room objects track their own details and check bookings to determine availability. Booking objects link guests to rooms and dates. When a booking is created or cancelled, the system updates availability by checking overlapping dates. Internally, data structures like lists or maps store rooms and bookings. Concurrency control mechanisms ensure no two bookings overlap for the same room.
Why designed this way?
This design mirrors real-world relationships, making it intuitive and maintainable. Separating concerns into classes allows independent updates and testing. Dynamic availability checks prevent stale data. Concurrency controls are necessary because multiple users can book simultaneously, which could cause conflicts if not handled.
┌───────────┐          ┌───────────┐          ┌────────────┐
│  Hotel    │◄─────────│   Room    │◄─────────│  Booking   │
│ - rooms[] │          │ - hotel   │          │ - room     │
│           │          │ - bookings│          │ - guest    │
└───────────┘          └───────────┘          └────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Do you think room availability can be stored as a simple true/false flag without issues? Commit to yes or no.
Common Belief:Room availability is just a true or false flag updated when bookings happen.
Tap to reveal reality
Reality:Availability must be computed by checking all bookings for overlapping dates, not a simple flag.
Why it matters:Using a flag can cause double bookings if multiple bookings overlap but the flag isn't updated correctly.
Quick: Do you think a booking only needs guest info and no link to a room? Commit to yes or no.
Common Belief:Booking only stores guest details; room info is separate and unrelated.
Tap to reveal reality
Reality:Booking must link to a specific room to track which room is reserved and when.
Why it matters:Without linking, the system can't prevent double bookings or know which room is booked.
Quick: Do you think concurrency issues are rare and can be ignored in booking systems? Commit to yes or no.
Common Belief:Booking systems rarely face concurrency problems because users book at different times.
Tap to reveal reality
Reality:Concurrency is common; multiple users can try to book the same room simultaneously, causing conflicts.
Why it matters:Ignoring concurrency leads to double bookings and unhappy customers.
Quick: Do you think rooms should only know their hotel or also know their bookings? Commit to yes or no.
Common Belief:Rooms only need to know their hotel; bookings are managed separately.
Tap to reveal reality
Reality:Rooms should track their bookings to check availability efficiently.
Why it matters:Without this, availability checks become inefficient and error-prone.
Expert Zone
1
Booking date ranges must consider time zones and check-in/check-out times to avoid overlaps.
2
Implementing optimistic locking can improve performance but requires careful conflict resolution.
3
Caching room availability can speed up queries but must be invalidated correctly on booking changes.
When NOT to use
This class design is less suitable for extremely large hotel chains with millions of rooms and bookings where microservices and event-driven architectures are better. For simple apps, a flat data structure or third-party booking APIs might be easier.
Production Patterns
Real systems use layered architecture: UI calls service layer managing Hotel, Room, Booking classes. Databases store persistent data with indexes on room and date. Concurrency handled via transactions or distributed locks. APIs expose booking functions with validation and error handling.
Connections
Database Normalization
Builds-on
Understanding how Hotel, Room, and Booking classes relate helps grasp database normalization principles like separating entities and avoiding data duplication.
Concurrency Control in Databases
Same pattern
Booking conflicts illustrate concurrency control concepts such as locking and transactions used in databases to maintain data integrity.
Project Management Scheduling
Analogy in scheduling conflicts
Managing room bookings over time is similar to scheduling tasks in project management, where overlapping assignments must be avoided.
Common Pitfalls
#1Treating room availability as a static flag updated on booking creation.
Wrong approach:class Room { bool isAvailable = true; void book() { isAvailable = false; } void cancel() { isAvailable = true; } }
Correct approach:class Room { List bookings; bool isAvailable(DateRange requested) { return bookings.noneOverlap(requested); } }
Root cause:Misunderstanding that availability depends on date ranges, not just a simple flag.
#2Not linking bookings to specific rooms, only storing guest info.
Wrong approach:class Booking { String guestName; DateRange dates; // No room reference }
Correct approach:class Booking { String guestName; DateRange dates; Room room; }
Root cause:Ignoring the need to track which room is reserved.
#3Ignoring concurrency, allowing multiple bookings for the same room at the same time.
Wrong approach:void createBooking(Room room, DateRange dates) { if (room.isAvailable(dates)) { room.bookings.add(new Booking(dates)); } }
Correct approach:void createBooking(Room room, DateRange dates) { lock(room) { if (room.isAvailable(dates)) { room.bookings.add(new Booking(dates)); } } }
Root cause:Not handling simultaneous access leading to race conditions.
Key Takeaways
Hotel, Room, and Booking classes model real-world hotel operations by representing entities and their relationships clearly.
Room availability must be computed dynamically by checking bookings to avoid conflicts and double bookings.
Booking objects link guests to specific rooms and dates, enabling accurate reservation management.
Concurrency control is essential in booking systems to prevent race conditions and maintain data integrity.
Designing these classes with clear responsibilities and relationships lays a strong foundation for scalable hotel management systems.

Practice

(1/5)
1. Which class is primarily responsible for storing information about individual rooms in a hotel system?
easy
A. Room
B. Hotel
C. Booking
D. Guest

Solution

  1. 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.
  2. Step 2: Identify which class holds room details

    Since Room is designed to represent individual rooms, it stores room number, type, and availability.
  3. Final Answer:

    Room -> Option A
  4. Quick Check:

    Room class = stores room info [OK]
Hint: Room class holds room details, not Hotel or Booking [OK]
Common Mistakes:
  • Confusing Hotel with Room class
  • Thinking Booking stores room details
  • Assuming Guest class stores room info
2. Which of the following is the correct way to represent a Booking class constructor in Python that takes room, guest, and date as parameters?
easy
A. def __init__(self, room, guest, date):
B. def Booking(room, guest, date):
C. def __booking__(self, room, guest, date):
D. def init(self, room, guest, date):

Solution

  1. Step 1: Recall Python constructor syntax

    Python constructors use the special method __init__ with self as the first parameter.
  2. 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.
  3. Final Answer:

    def __init__(self, room, guest, date): -> Option A
  4. Quick Check:

    Constructor = __init__ method [OK]
Hint: Python constructors always use __init__(self, ...) [OK]
Common Mistakes:
  • Using method name other than __init__
  • Omitting self parameter
  • Using class name as method name
3. Given the following code snippet, what will be the output?
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)
medium
A. True\nTrue
B. False\nTrue
C. False\nFalse
D. True\nFalse

Solution

  1. Step 1: Check initial availability of room101

    When room101 is created, is_available is set to True, so first print outputs True.
  2. Step 2: Booking changes room availability

    Booking constructor sets room101.is_available to False, so second print outputs False.
  3. Final Answer:

    True\nFalse -> Option D
  4. Quick Check:

    Initial True, then set False by Booking [OK]
Hint: Booking sets room availability to False immediately [OK]
Common Mistakes:
  • Assuming availability stays True after booking
  • Confusing order of prints
  • Ignoring side effect on room object
4. Identify the error in the following Booking class code snippet:
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()
medium
A. is_available should be a method, not attribute
B. Missing guest argument when creating Booking instance
C. book method should return a value
D. Room class is not defined

Solution

  1. Step 1: Check Booking constructor parameters

    Booking __init__ requires room and guest, but only room is passed when creating booking instance.
  2. Step 2: Identify missing argument error

    Omitting guest argument causes a TypeError at runtime.
  3. Final Answer:

    Missing guest argument when creating Booking instance -> Option B
  4. Quick Check:

    Constructor args mismatch = missing guest [OK]
Hint: Match all constructor parameters when creating objects [OK]
Common Mistakes:
  • Ignoring missing guest argument
  • Assuming book method must return value
  • Thinking is_available must be a method
5. You want to design a system where a Hotel manages multiple Rooms and allows Bookings only if rooms are available. Which design approach best supports scalability and maintainability?
hard
A. Make Booking class manage all Rooms and Guests directly, without Hotel involvement.
B. Store all booking data inside Room class only, without separate Booking class.
C. Have Hotel class contain a list of Room objects, and Booking class references Room and Guest; Hotel checks availability before booking.
D. Use a single class combining Hotel, Room, and Booking functionalities.

Solution

  1. Step 1: Analyze class responsibilities

    Hotel should manage Rooms, Booking should link Rooms and Guests, keeping clear separation.
  2. 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.
  3. Final Answer:

    Hotel manages Rooms; Booking references Room and Guest; Hotel checks availability -> Option C
  4. Quick 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]
Hint: Separate Hotel, Room, Booking roles for clean design [OK]
Common Mistakes:
  • Combining all logic in one class
  • Booking managing Rooms directly
  • Ignoring availability checks in Hotel