problemmediumood

Object-Oriented Design for an Online Hotel Booking System (OYO-like)

MediumUpdated: Jan 28, 2018

Problem

Design an online hotel booking system (OYO-like) that allows users to search hotels in a city and book one or more rooms for a duration. Provide an object-oriented design that models hotels, rooms, bookings, users, and related enums/attributes.

Solution

1. Requirements Analysis

Functional requirements:

  • Search hotels by city and filters (room type, facilities, rating).
  • Book one or more rooms at a specific hotel for a date range.
  • Track booking status and payment state.
  • Represent hotel metadata (address, facilities) and room attributes (type, status).

Non-functional requirements:

  • Data consistency for bookings (prevent double-booking of the same room/time).
  • Reasonable performance for search and booking operations.
  • Extensible model to support extra facilities, pricing models, and promo codes.

Assumptions (derived from source):

  • Each booking is for rooms in a single hotel. Payments and billing are simplified to a PaymentStatus enum.

2. Use Case Diagram

Actors: User, System Administrator

Use case summary: User searches hotels, selects rooms, and completes a booking (including payment). Admin may manage hotels and rooms.

graph TD
    subgraph Hotel Booking System
        UC1(Search Hotels)
        UC2(View Hotel Details)
        UC3(Book Rooms)
        UC4(View Bookings)
    end
    User --> UC1
    User --> UC2
    User --> UC3
    User --> UC4
    Admin --> UC2

3. Class Diagram

Core classes and responsibilities:

  • User: Holds user profile and contact details.
  • Hotel: Contains hotel metadata and list of rooms.
  • Room: Represents a single room with type, status, and identifier.
  • Booking: Captures booking details, booked rooms, amount, payment status and duration.
  • Address: Simple value object for hotel location.
  • Facilities: Value object wrapping a list of Facility enums.
classDiagram
    class User {
        +int userId
        +String name
        +String emailId
    }

    class Hotel {
        +int hotelId
        +String hotelName
        +Address address
        +List~Room~ rooms
        +float rating
        +Facilities facilities
    }

    class Room {
        +int roomId
        +int hotelId
        +RoomType roomType
        +RoomStatus roomStatus
    }

    class Booking {
        +int bookingId
        +int userId
        +int hotelId
        +List~Room~ bookedRooms
        +int amount
        +PaymentStatus paymentStatus
        +Duration duration
    }

    class Address {
        +String city
        +String pinCode
    }

    User "1" -- "0..*" Booking : makes
    Hotel "1" -- "1..*" Room : has
    Booking "1" -- "1..*" Room : reserves

4. Activity Diagrams

Activity: Search and Book

graph TD
    U[User searches hotels] --> S[System returns list of hotels]
    U --> V[User views hotel details]
    V --> B[User selects rooms and date range]
    B --> P[System checks availability]
    P --> C{Available?}
    C -- Yes --> R[Create Booking and collect payment]
    C -- No --> E[Show unavailable message]

Activity: Cancel Booking (simplified)

graph TD
    U[User requests cancel] --> S[System validates cancel policy]
    S --> A{Allowed?}
    A -- Yes --> R[Mark booking cancelled and refund if applicable]
    A -- No --> E[Return error]

5. High-Level Code Implementation

Below are cleaned Java skeletons (from the original note) and a Python equivalent. These are skeletons: attributes and method signatures only.

Java

public enum RoomStatus { EMPTY, NOT_EMPTY }
public enum RoomType { SINGLE, DOUBLE, TRIPLE }
public enum PaymentStatus { PAID, UNPAID }
public enum Facility { LIFT, POWER_BACKUP, HOT_WATER, BREAKFAST_FREE, SWIMMING_POOL }

public class User {
        int userId;
        String name;
        Date dateOfBirth;
        String mobile;
        String emailId;
}

public class Room {
        int roomId; // room number
        int hotelId;
        RoomType roomType;
        RoomStatus roomStatus;
}

public class Hotel {
        int hotelId;
        String hotelName;
        Address address;
        List<Room> rooms;
        float rating;
        Facilities facilities;
}

public class Booking {
        int bookingId;
        int userId;
        int hotelId;
        List<Room> bookedRooms;
        int amount;
        PaymentStatus paymentStatus;
        Date bookingTime;
        Duration duration;
}

public class Address { String city; String pinCode; String state; String streetNo; String landmark; }
public class Duration { Date from; Date to; }
public class Facilities { List<Facility> facilitiesList; }

Python

from __future__ import annotations
from dataclasses import dataclass, field
from enum import Enum
from typing import List
from datetime import datetime

class RoomStatus(Enum):
        EMPTY = 1
        NOT_EMPTY = 2

class RoomType(Enum):
        SINGLE = 1
        DOUBLE = 2
        TRIPLE = 3

class PaymentStatus(Enum):
        PAID = 1
        UNPAID = 2

class Facility(Enum):
        LIFT = 1
        POWER_BACKUP = 2
        HOT_WATER = 3
        BREAKFAST_FREE = 4
        SWIMMING_POOL = 5

@dataclass
class User:
        user_id: int
        name: str
        email: str

@dataclass
class Room:
        room_id: int
        hotel_id: int
        room_type: RoomType
        room_status: RoomStatus

@dataclass
class Booking:
        booking_id: int
        user_id: int
        hotel_id: int
        booked_rooms: List[Room]
        amount: int
        payment_status: PaymentStatus
        booking_time: datetime
        duration_from: datetime
        duration_to: datetime

References

Comments