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.
Comments
Post a Comment