|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.List; |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +class Passenger { |
| 6 | + private String name; |
| 7 | + private int age; |
| 8 | + private String passportNumber; |
| 9 | + |
| 10 | + public Passenger(String name, int age, String passportNumber) { |
| 11 | + this.name = name; |
| 12 | + this.age = age; |
| 13 | + this.passportNumber = passportNumber; |
| 14 | + } |
| 15 | + |
| 16 | + public String getName() { |
| 17 | + return name; |
| 18 | + } |
| 19 | + |
| 20 | + @Override |
| 21 | + public String toString() { |
| 22 | + return "Name: " + name + ", Age: " + age + ", Passport Number: " + passportNumber; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +class Flight { |
| 27 | + private String flightNumber; |
| 28 | + private String destination; |
| 29 | + private int availableSeats; |
| 30 | + private List<Passenger> passengers; |
| 31 | + |
| 32 | + public Flight(String flightNumber, String destination, int availableSeats) { |
| 33 | + this.flightNumber = flightNumber; |
| 34 | + this.destination = destination; |
| 35 | + this.availableSeats = availableSeats; |
| 36 | + this.passengers = new ArrayList<>(); |
| 37 | + } |
| 38 | + |
| 39 | + public String getFlightNumber() { |
| 40 | + return flightNumber; |
| 41 | + } |
| 42 | + |
| 43 | + public String getDestination() { |
| 44 | + return destination; |
| 45 | + } |
| 46 | + |
| 47 | + public int getAvailableSeats() { |
| 48 | + return availableSeats; |
| 49 | + } |
| 50 | + |
| 51 | + public List<Passenger> getPassengers() { |
| 52 | + return passengers; |
| 53 | + } |
| 54 | + |
| 55 | + public boolean bookSeat(Passenger passenger) { |
| 56 | + if (availableSeats > 0) { |
| 57 | + passengers.add(passenger); |
| 58 | + availableSeats--; |
| 59 | + return true; |
| 60 | + } |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public String toString() { |
| 66 | + return "Flight Number: " + flightNumber + ", Destination: " + destination + ", Available Seats: " + availableSeats; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +public class AirlineReservationSystem { |
| 71 | + private static List<Flight> flights = new ArrayList<>(); |
| 72 | + |
| 73 | + public static void main(String[] args) { |
| 74 | + initializeFlights(); |
| 75 | + Scanner scanner = new Scanner(System.in); |
| 76 | + |
| 77 | + while (true) { |
| 78 | + System.out.println("Airline Reservation System Menu:"); |
| 79 | + System.out.println("1. List Available Flights"); |
| 80 | + System.out.println("2. Book a Flight"); |
| 81 | + System.out.println("3. Exit"); |
| 82 | + System.out.print("Enter your choice: "); |
| 83 | + |
| 84 | + int choice = scanner.nextInt(); |
| 85 | + scanner.nextLine(); // Consume the newline character |
| 86 | + |
| 87 | + switch (choice) { |
| 88 | + case 1: |
| 89 | + listAvailableFlights(); |
| 90 | + break; |
| 91 | + case 2: |
| 92 | + bookAFlight(scanner); |
| 93 | + break; |
| 94 | + case 3: |
| 95 | + System.out.println("Exiting the program. Thank you!"); |
| 96 | + scanner.close(); |
| 97 | + System.exit(0); |
| 98 | + default: |
| 99 | + System.out.println("Invalid choice. Please enter a valid option."); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private static void initializeFlights() { |
| 105 | + flights.add(new Flight("F101", "New York", 100)); |
| 106 | + flights.add(new Flight("F102", "Los Angeles", 120)); |
| 107 | + flights.add(new Flight("F103", "Chicago", 90)); |
| 108 | + } |
| 109 | + |
| 110 | + private static void listAvailableFlights() { |
| 111 | + System.out.println("Available Flights:"); |
| 112 | + for (Flight flight : flights) { |
| 113 | + System.out.println(flight); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private static void bookAFlight(Scanner scanner) { |
| 118 | + System.out.print("Enter your name: "); |
| 119 | + String name = scanner.nextLine(); |
| 120 | + System.out.print("Enter your age: "); |
| 121 | + int age = scanner.nextInt(); |
| 122 | + scanner.nextLine(); // Consume the newline character |
| 123 | + System.out.print("Enter your passport number: "); |
| 124 | + String passportNumber = scanner.nextLine(); |
| 125 | + System.out.print("Enter the flight number you want to book: "); |
| 126 | + String flightNumber = scanner.nextLine(); |
| 127 | + |
| 128 | + Flight selectedFlight = null; |
| 129 | + for (Flight flight : flights) { |
| 130 | + if (flight.getFlightNumber().equalsIgnoreCase(flightNumber)) { |
| 131 | + selectedFlight = flight; |
| 132 | + break; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + if (selectedFlight != null) { |
| 137 | + Passenger passenger = new Passenger(name, age, passportNumber); |
| 138 | + if (selectedFlight.bookSeat(passenger)) { |
| 139 | + System.out.println("Seat booked successfully!"); |
| 140 | + } else { |
| 141 | + System.out.println("Sorry, the flight is fully booked."); |
| 142 | + } |
| 143 | + } else { |
| 144 | + System.out.println("Invalid flight number. Please enter a valid flight number."); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments