Flight Booking system
import java.util.HashMap; import java.util.LinkedList; import java.util.Map; import java.util.Queue; // Flight Interface public interface Flight { String getFlightNumber(); String getDepartureLocation(); String getArrivalLocation(); int getAvailableSeats(); void bookSeat(); void cancelSeat(); // Added method for cancelling a seat } // FlightImpl Class public class FlightImpl implements Flight { private final String flightNumber; private final String departureLocation; private final String arrivalLocation; private int availableSeats; public FlightImpl(String flightNumber, String departureLocation, String arrivalLocation, int availableSeats) { this.flightNumber = flightNumber; this.departureLocation = departureLocation; this.arrivalLocation = arrivalLocation; this.availableSeats = availableSeats; } @Override public String getFlightNumber() { return flightNumber; } @Override public String getDepartureLocation() { return departureLocation; } @Override public String getArrivalLocation() { return arrivalLocation; } @Override public int getAvailableSeats() { return availableSeats; } @Override public synchronized void bookSeat() { if (availableSeats > 0) { availableSeats--; } } @Override public synchronized void cancelSeat() { availableSeats++; } } // Passenger Interface public interface Passenger { String getPassengerId(); String getName(); String getEmail(); } // PassengerImpl Class public class PassengerImpl implements Passenger { private final String passengerId; private final String name; private final String email; public PassengerImpl(String passengerId, String name, String email) { this.passengerId = passengerId; this.name = name; this.email = email; } @Override public String getPassengerId() { return passengerId; } @Override public String getName() { return name; } @Override public String getEmail() { return email; } } // Booking Interface public interface Booking { String getBookingId(); Flight getFlight(); Passenger getPassenger(); boolean isConfirmed(); boolean isCancelled(); // New method to check if the booking is cancelled void cancel(); // New method to cancel the booking } // BookingImpl Class public class BookingImpl implements Booking { private final String bookingId; private final Flight flight; private final Passenger passenger; private boolean confirmed; private boolean cancelled; // New field to track cancellation public BookingImpl(String bookingId, Flight flight, Passenger passenger, boolean confirmed) { this.bookingId = bookingId; this.flight = flight; this.passenger = passenger; this.confirmed = confirmed; this.cancelled = false; // Initial status is not cancelled } @Override public String getBookingId() { return bookingId; } @Override public Flight getFlight() { return flight; } @Override public Passenger getPassenger() { return passenger; } @Override public boolean isConfirmed() { return confirmed; } @Override public boolean isCancelled() { return cancelled; } @Override public synchronized void cancel() { if (!cancelled && confirmed) { cancelled = true; flight.cancelSeat(); // Release the seat back to the flight } } } // AirlineReservationSystem Class public class AirlineReservationSystem { private final Map<String, Flight> flights = new HashMap<>(); private final Map<String, Passenger> passengers = new HashMap<>(); private final Map<String, Booking> bookings = new HashMap<>(); private final RateLimiter rateLimiter; // Add rate limiter for the system public AirlineReservationSystem(RateLimiter rateLimiter) { this.rateLimiter = rateLimiter; } public void addFlight(Flight flight) { flights.put(flight.getFlightNumber(), flight); } public void addPassenger(Passenger passenger) { passengers.put(passenger.getPassengerId(), passenger); } public Booking bookFlight(String bookingId, String flightNumber, String passengerId) { if (!rateLimiter.acquire()) { throw new RuntimeException("Rate limit exceeded"); } Flight flight = flights.get(flightNumber); Passenger passenger = passengers.get(passengerId); if (flight == null || passenger == null) { throw new IllegalArgumentException("Flight or Passenger not found"); } synchronized (flight) { if (flight.getAvailableSeats() > 0) { flight.bookSeat(); Booking booking = new BookingImpl(bookingId, flight, passenger, true); bookings.put(bookingId, booking); return booking; } else { throw new RuntimeException("No available seats on the flight"); } } } public void cancelBooking(String bookingId) { Booking booking = bookings.get(bookingId); if (booking == null) { throw new IllegalArgumentException("Booking not found"); } synchronized (booking) { if (!booking.isCancelled()) { booking.cancel(); bookings.remove(bookingId); // Optionally remove the booking from the system } else { throw new RuntimeException("Booking already cancelled"); } } } public Flight getFlight(String flightNumber) { return flights.get(flightNumber); } public Passenger getPassenger(String passengerId) { return passengers.get(passengerId); } public Booking getBooking(String bookingId) { return bookings.get(bookingId); } } // Main Class for Demonstration public class Main { public static void main(String[] args) throws InterruptedException { // Create a rate limiter with a maximum of 5 requests per 10 seconds RateLimiter rateLimiter = new SlidingWindowRateLimiter(5, 10000); AirlineReservationSystem system = new AirlineReservationSystem(rateLimiter); // Create and add flights Flight flight1 = new FlightImpl("FL123", "New York", "Los Angeles", 100); system.addFlight(flight1); // Create and add passengers Passenger passenger1 = new PassengerImpl("P1", "Alice", "alice@example.com"); system.addPassenger(passenger1); // Book a flight try { Booking booking = system.bookFlight("B1", "FL123", "P1"); System.out.println("Booking successful: " + booking.getBookingId()); // Cancel the booking system.cancelBooking("B1"); System.out.println("Booking cancelled successfully"); // Try to cancel the booking again system.cancelBooking("B1"); } catch (Exception e) { System.out.println("Operation failed: " + e.getMessage()); } } }
Leave a Comment