Railway Ticket Booking System

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-12-23

Problem Statement

Design the Low-Level Design (LLD) for a Railway Ticket Booking System (similar to IRCTC) that allows passengers to search trains, check seat availability, book tickets, cancel bookings, and view booking history.


The system should efficiently handle concurrent bookings, seat allocation, and waiting list management, ensuring data consistency and fair allocation of seats.


🎯 Core Requirements


1. Train Management


•Each train has:
•A unique train number and name.
•Source and destination stations.
•Multiple intermediate stops with arrival/departure times.
•Different classes (Sleeper, AC, 2S, etc.).
•A fixed number of seats in each class.


2. Search and Availability


•Users can search trains by:
•Source, destination, and date of travel.
•The system should display:
•Available trains.
•Number of available seats and fare for each class.


3. Booking


•Users can:
•Select a train and class.
•Enter passenger details.
•Book seats if available.
•If seats are full, add booking to a waiting list.
•Generate a PNR (Passenger Name Record) for each confirmed or waiting booking.


4. Cancellation


•A user can cancel a confirmed or waiting ticket.
•On cancellation:
•Freed seats are reallocated to passengers in the waiting list (FIFO order).


5. Booking History


•Users can view:
•All their current and past bookings.
•Each booking’s status (Confirmed / Waiting / Cancelled).


⚙️ Functional APIs


OperationDescription


searchTrains(source, destination, date)


Returns list of trains for a route.


checkAvailability(trainId, date, classType)


Shows available seat count.


bookTicket(trainId, userId, passengerList, classType)


Books ticket and returns PNR.


cancelTicket(pnr)


Cancels the booking and reallocates seats.


getBookingHistory(userId)


Lists all bookings made by a user.


🧱 Suggested Class Design


ClassResponsibility


Train

Represents a train, route, and classes of seats.


Coach/ClassType

Represents a specific coach type (Sleeper, AC).


Seat

Represents a single seat with number and status (Available/Booked).


Passenger

Represents passenger details (name, age, gender).


Booking

Represents a booking record (PNR, train, user, passengers, status).


User

Represents user account and booking history.


BookingService

Handles booking, cancellation, and seat allocation logic.


TrainService

Handles train search, schedule, and seat availability.


PNRGenerator

Generates unique PNRs.


WaitingListManager

Manages queue for waiting passengers.


🧩 Entity Example


Train

1 2Train ID: 12951 3Name: Mumbai Rajdhani 4Route: Mumbai -> Delhi 5Classes: [1AC: 20 seats, 2AC: 50 seats, 3AC: 100 seats, Sleeper: 200 seats]


Booking

1 2PNR: AB1234 3Train: 12951 4Date: 2025-10-24 5Passengers: [Sahil (28, M), Priya (25, F)] 6Status: Confirmed 7Coach: 3AC 8Seat Numbers: [B4-21, B4-22]


🧠 Design Considerations


Concurrency Handling:

•Use synchronization or locks to prevent overbooking when multiple users book simultaneously.
•Data Consistency:
•Ensure atomic booking — seat allocation, PNR generation, and payment should either all succeed or all fail.


Waiting List Handling:

•Maintain queue structure (FIFO) for unconfirmed bookings.


Cancellation Flow:

•When a confirmed ticket is canceled, reassign seat to next waiting passenger automatically.


Extensibility:

•Add payment module.
•Add dynamic pricing.
•Add notifications on confirmation.


🔄 Example Flow


Booking Flow

1.User searches train: Mumbai → Delhi, 24 Oct
2.System shows available trains with seat count.
3.User selects Mumbai Rajdhani (3AC).
4.Books 2 tickets → 2 seats assigned.
5.System generates:
1 2PNR: AB1234 3Status: Confirmed 4Seats: B4-21, B4-22


When Seats are Full

•User books in 3AC → Waiting list entry created:
1 2PNR: XY9876 3Status: Waiting (Position 3)


Cancellation Flow

•A confirmed booking is canceled → First waiting passenger’s ticket becomes Confirmed.


🧱 Sample Class Diagram (Conceptual)

1 2+-------------------+ 3| Train | 4+-------------------+ 5| trainId | 6| name | 7| route | 8| coaches | 9+-------------------+ 10 11+-------------------+ 12| Seat | 13+-------------------+ 14| seatNumber | 15| status | 16| classType | 17+-------------------+ 18 19+-------------------+ 20| Booking | 21+-------------------+ 22| pnr | 23| trainId | 24| passengers | 25| status | 26| seats | 27+-------------------+ 28 29+-------------------+ 30| BookingService | 31+-------------------+ 32| +bookTicket() | 33| +cancelTicket() | 34+-------------------+ 35 36+-------------------+ 37| WaitingListManager| 38+-------------------+ 39| +addToWaiting() | 40| +promoteToConfirm()| 41+-------------------+


🚀 Extensions (Optional)


•Add payment integration (PaymentService).
•Add notifications for booking confirmation.
•Add real-time seat map visualization.
•Support group bookings and special quotas (Ladies, Senior Citizen, Tatkal).
•Allow dynamic pricing during peak demand.


✅ Goal:


Design an object-oriented Railway Ticket Booking System that supports train search, seat availability, booking, cancellation, and waiting list management — ensuring extensibility, concurrency safety, and clean modular design.

Solution

Comments