System Design
Object-Oriented Design
Hotel Management

Design Example: Hotel Management System

A Hotel Management System manages room inventory, guest bookings, and billing for a chain of hotels.


Step 1: Requirements Gathering

Core Use Cases:

  • Search: Guests can search for available rooms by date and hotel location.
  • Booking: Guests can reserve a room (Single, Double, Suite).
  • Check-in/Check-out: Manage the guest's stay lifecycle.
  • Housekeeping: Assign rooms to housekeepers for cleaning.
  • Billing: Calculate the final bill including taxes and room service.

Entity States:

  • Room Status: Available, Occupied, NotAvailable, BeingServiced.
  • Booking Status: Requested, Confirmed, CheckedIn, CheckedOut, Canceled.

Step 2: Identify Core Objects

  • Hotel: Contains information about the location and rooms.
  • Room: Abstract base for different room types (Single, Deluxe, Suite).
  • Guest: Person staying at the hotel.
  • Booking: Represents a reservation with dates and room assignments.
  • Bill: Handles payment calculations.
  • Housekeeper: Employee responsible for room maintenance.

Step 3: Design Class Diagram


Step 4: Implementation in TypeScript

enum RoomStatus { AVAILABLE, OCCUPIED, SERVICING }
 
abstract class Room {
  constructor(
    public roomNumber: string,
    public price: number,
    public status: RoomStatus = RoomStatus.AVAILABLE
  ) {}
}
 
class Suite extends Room {
  constructor(roomNumber: string, price: number) {
    super(roomNumber, price);
  }
}
 
class Booking {
  constructor(
    public id: string,
    public guest: string,
    public room: Room,
    public startDate: Date,
    public endDate: Date
  ) {}
 
  public calculateTotal(): number {
    const days = (this.endDate.getTime() - this.startDate.getTime()) / (1000 * 3600 * 24);
    return days * this.room.price;
  }
}

Deep Dive: Factory Pattern for Rooms

Using a Factory Pattern allows the hotel to create different room types based on a configuration or database entry without hardcoding classes everywhere.

class RoomFactory {
  public static createRoom(type: string, number: string): Room {
    if (type === "SUITE") return new Suite(number, 500);
    // ...
    throw new Error("Invalid room type");
  }
}

Wrap Up

The Hotel Management System demonstrates the importance of Status Management and Type Hierarchies. By decoupling the booking logic from the physical room implementation, the system can scale to handle thousands of hotels.

[!TIP] Use the Strategy Pattern for dynamic pricing (e.g., higher prices during peak season).