Skip to main content

Design a Airline Reservation System

Problem Statement

Design an Airline Reservation System that allows users to search flights, book seats, and cancel bookings. The system should ensure that seats are not double booked and should correctly update seat availability when a booking is cancelled. It should also support multiple flights and passengers per booking.


Functional Requirements

  • The system should support multiple flights between different cities
  • Each flight should have multiple seats
  • Users should be able to view available seats
  • Users should be able to book one or more seats on a flight
  • Once booked, seats should not be available to others
  • Users should be able to cancel a booking
  • Cancelled seats should become available again

Objects Required

  • Flight
  • Seat
  • Passenger
  • Booking
  • BookingStatus
  • AirlineSystem

BookingStatus Enum


public enum BookingStatus {
    CONFIRMED,
    CANCELLED
}

The BookingStatus enum tracks whether a booking is active or cancelled. This helps in maintaining booking history without deleting records.


Seat Class


public class Seat {

    private String seatId;
    private boolean isBooked;

    public Seat(String seatId) {
        this.seatId = seatId;
        this.isBooked = false;
    }

    public boolean isAvailable() {
        return !isBooked;
    }

    public void bookSeat() {
        isBooked = true;
    }

    public void cancelSeat() {
        isBooked = false;
    }

    public String getSeatId() {
        return seatId;
    }
}

The constructor initializes each seat as available by default.

isAvailable() simply checks whether the seat is free at the moment.

bookSeat() marks the seat as reserved once a booking goes through.

cancelSeat() resets the seat so it can be used again for future bookings.


Flight Class


import java.util.*;

public class Flight {

    private String flightId;
    private String source;
    private String destination;
    private List seats;

    public Flight(String flightId, String source, String destination, List seats) {
        this.flightId = flightId;
        this.source = source;
        this.destination = destination;
        this.seats = seats;
    }

    public List getAvailableSeats() {

        List available = new ArrayList<>();

        for (Seat seat : seats) {
            if (seat.isAvailable()) {
                available.add(seat);
            }
        }

        return available;
    }

    public boolean bookSeats(List selectedSeats) {

        for (Seat seat : selectedSeats) {
            if (!seat.isAvailable()) {
                return false;
            }
        }

        for (Seat seat : selectedSeats) {
            seat.bookSeat();
        }

        return true;
    }

    public void cancelSeats(List selectedSeats) {

        for (Seat seat : selectedSeats) {
            seat.cancelSeat();
        }
    }
}

The constructor sets up a flight with its route and available seats.

getAvailableSeats() filters out seats that are already booked and returns only free ones.

bookSeats() first checks all selected seats. If even one seat is already booked, the whole booking fails. This prevents partial bookings which could cause inconsistencies.

Only after validation are seats marked as booked.

cancelSeats() releases all seats in a booking when the user cancels.


Passenger Class


public class Passenger {

    private String passengerId;
    private String name;

    public Passenger(String passengerId, String name) {
        this.passengerId = passengerId;
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

The Passenger class stores basic user details required during booking.


Booking Class


import java.util.*;

public class Booking {

    private String bookingId;
    private Passenger passenger;
    private Flight flight;
    private List seats;
    private BookingStatus status;

    public Booking(String bookingId,
                   Passenger passenger,
                   Flight flight,
                   List seats) {

        this.bookingId = bookingId;
        this.passenger = passenger;
        this.flight = flight;
        this.seats = seats;
        this.status = BookingStatus.CONFIRMED;
    }

    public void cancelBooking() {

        flight.cancelSeats(seats);
        status = BookingStatus.CANCELLED;
    }
}

The booking object stores all reservation details including passenger, flight, and selected seats.

The status starts as CONFIRMED once booking is successful.

cancelBooking() frees all seats through the flight object and updates status to cancelled.


AirlineSystem Class


import java.util.*;

public class AirlineSystem {

    private List flights;

    public AirlineSystem(List flights) {
        this.flights = flights;
    }

    public Booking createBooking(Passenger passenger,
                                 Flight flight,
                                 List seats) {

        boolean success = flight.bookSeats(seats);

        if (!success) {
            System.out.println("Seats not available");
            return null;
        }

        return new Booking(
                UUID.randomUUID().toString(),
                passenger,
                flight,
                seats
        );
    }
}

This class acts as the entry point of the system and manages booking flow.

It first tries to reserve seats from the flight. If even one seat is unavailable, booking fails immediately.

If successful, it creates a Booking object with a unique ID.


Main Class


import java.util.*;

public class Main {

    public static void main(String[] args) {

        List seats = new ArrayList<>();

        for (int i = 1; i <= 5; i++) {
            seats.add(new Seat("S" + i));
        }

        Flight flight = new Flight(
                "F1",
                "Seattle",
                "Dubai",
                seats
        );

        Passenger passenger =
                new Passenger("P1", "Ronaldo");

        AirlineSystem system =
                new AirlineSystem(Arrays.asList(flight));

        List selectedSeats =
                Arrays.asList(seats.get(0), seats.get(1));

        Booking booking =
                system.createBooking(passenger, flight, selectedSeats);

        if (booking != null) {
            System.out.println("Booking Successful");
        }

        booking.cancelBooking();

        System.out.println("Booking Cancelled");
    }
}

The main method simulates a full booking flow from seat creation to cancellation.

It creates a flight, adds seats, books them for a passenger, and finally cancels the booking to show seat recovery.


Class Diagram

FlightflightId : Stringsource : Stringdestination : Stringseats : List<Seat>getAvailableSeats() : List<Seat>bookSeats(seats : List<Seat>) : booleancancelSeats(seats : List<Seat>) : voidSeatseatId : StringisBooked : booleanisAvailable() : booleanbookSeat() : voidcancelSeat() : voidPassengerpassengerId : Stringname : StringgetName() : StringBookingbookingId : Stringpassenger : Passengerflight : Flightseats : List<Seat>status : BookingStatuscancelBooking() : voidBookingStatusCONFIRMEDCANCELLEDAirlineSystemflights : List<Flight> flight : Flight,createBooking(passenger : Passenger,seats : List<Seat>) : BookingMainmain(args : String[]) : void

Also See

Comments

Popular posts from this blog

Most Frequently Asked Low Level Design(LLD) Interview Questions

Below are the curated list of most commonly asked Low Level Design (LLD) interview problems. Each problem includes a short description and a link to the complete solution with code and class diagrams. Design Parking Lot System The system should handle parking for different vehicle types such as bikes, cars, and trucks. It should manage slot allocation, availability tracking, and entry/exit flow. The design also ensures efficient usage of parking space under varying load conditions. View Solution Design Elevator / Lift System The system should support multiple elevators operating across floors with request handling logic. It focuses on scheduling algorithms to minimize wait time and optimize movement. It also manages direction control and concurrent floor requests. View Solution Design Movie Ticket Booking System The system should allow users to browse movies, select shows, and book seats. It handles seat ...

Designing a Parking Lot - Low Level Design

Problem Statement Design a parking lot that can handle vehicles entering and leaving while managing parking across multiple floors. Each vehicle should be assigned a suitable parking spot based on its type, and the spot should be freed once the vehicle exits. The design should also support generating a ticket at entry and optionally calculating the parking fee based on the duration of stay. Asked In Companies Amazon Google Microsoft Uber Walmart Flipkart Meta PayPal Oracle Salesforce Adobe Apple Intuit LinkedIn Atlassian Functional Requirements The design should support multiple vehicle types such as bikes, cars, and trucks A vehicle must be assigned a parking spot compatible with its type A parking spot cannot be assigned to more than one vehicle at a time The parking lot should support multiple levels (floors) The design should search and allocate an availa...

Design an AI Agent Tool Execution Coordinator

Problem Statement Design an autonomous AI Agent Tool Execution Coordinator (similar to the orchestration cores of LangChain, Semantic Kernel, or Claude Desktop agent runtimes). The engine must coordinate agents by parsing structured tool call requests emitted by a Language Model (LLM), invoking corresponding local Java code methods dynamically by name using reflection, appending execution outputs back to the context history, and driving the agentic loop iteratively until a final synthesized text answer is generated. Additionally, to prevent token limit errors in long-running sessions, the engine must perform proactive context-window pruning, dropping the oldest historical messages while strictly preserving the system instructions template at the head of the context. Asked In Companies OpenAI Anthropic Google Microsoft Design Decisions & Patterns Used Modern agentic AI systems use dynamic tool execution to interact with external databas...