9. Design Hotel Management
Difficulty: Medium Topics: Object-Oriented Design, Concurrency, Date Logic Key Concepts: Booking management, room allocation, avoiding double bookings.
Phase 1: Requirements Gathering
Goals
Design a system to manage hotel bookings.
Support room search, booking, and cancellation.
specific focus on concurrency (prevent double booking).
1. Who are the actors?
Guest: Searches for rooms, makes bookings.
Receptionist: Checks guests in/out, manages bookings.
System: Validates availability, stores data.
2. What are the must-have features? (Core)
Search: Find rooms by type (Standard, Deluxe, Suite) and date range.
Book: Reserve a room for a specific user.
Cancel: Refund and release room.
Check-in/Out: Update booking status.
3. What are the constraints?
Concurrency: Two users cannot book the same room for overlapping dates.
Invariant: Bookings must not overlap for the same room.
Phase 2: Use Cases
UC1: Search Rooms
Actor: User Flow:
User enters
StartDate,EndDate, andRoomType.System filters all rooms of
RoomType.System checks availability for each room against existing bookings.
System returns list of available rooms.
UC2: Book Room
Actor: User Flow:
User selects a specific
Room.System attempts to lock the room/date slots.
System verifies availability one last time (Double Check).
System creates
Bookingrecord.System confirms reservation.
Phase 3: Class Diagram
Step 1: Core Entities
Hotel: Singleton, manages Rooms.
Room: Has ID, Type, Price, and List of Bookings.
Booking: Links User, Room, DateRange.
User/Guest: Person making the booking.
UML Diagram
Phase 4: Design Patterns
1. Singleton Pattern
Description: Ensures a class has only one instance and provides a global point of access to it.
Why used: The
Hotelsystem acts as the centralized controller for all rooms and bookings. A single instance ensures consistent access to the inventory and prevents data inconsistency.
2. Lock / Synchronization (Concurrency Pattern)
Description: Mechanisms to control access to shared resources by multiple threads.
Why used: Two guests might try to book the same room at the same exact second. Locking (Optimistic or Pessimistic) ensures only one transaction succeeds, preventing double bookings.
Phase 5: Code Key Methods
Java Implementation
Phase 6: Discussion
Concurrency & Isolation (SDE-3 Concept)
Q: How to prevent double bookings in a distributed system where synchronized fails?
A: "In a real-world scenario with multiple application instances, local JVM locks are insufficient."
Database Level (PostgreSQL EXCLUDE Constraint): The most robust way to prevent overlapping date ranges in a DB is using an Exclusion Constraint with GiST indexes:
ALTER TABLE bookings ADD CONSTRAINT no_double_booking EXCLUDE USING gist (room_id WITH =, date_range WITH &&);. The DB guarantees no overlaps at the lowest level.Distributed Lock (Redis Redlock): If using Redis as a caching/locking layer before hitting the DB, acquire a distributed lock on
room:{id}with a timeout. If successful, check availability and book. If not, fail fast.Optimistic Locking: Add a
versioncolumn to theRoomtable.UPDATE Room SET version = 2 WHERE id = X AND version = 1. If it returns 0 rows updated, someone else booked it first.
Expiration
Q: How delay/expire unpaid bookings?
A: "Use a temporary status (
PENDING_PAYMENT) with a TTL (Time To Live). A Redis keybooking:{id}with expiry 10m can trigger a release if payment hook isn't received."
Dynamic Pricing
Q: How to handle pricing surges?
A: "Implement Strategy Pattern for pricing.
PricingStrategycan calculate price based on demand (occupancy %), seasonality, or user loyalty."
SOLID Principles Checklist
S (Single Responsibility): Room manages availability, Hotel manages Global Search.
O (Open/Closed): Add new
RoomTypesorPricingStrategieswithout modifying core logic.L (Liskov Substitution): N/A (Standard OOP).
I (Interface Segregation): Booking interfaces for Admin vs User could be split.
D (Dependency Inversion): Hotel could depend on
RoomRepositoryinstead of list.
Last updated