Advertisement
CR7CR7

AirResrvationSystem

May 10th, 2023
925
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1. public static void main(String[] args) {
  2.  
  3.     FlightBookingSystem fbs = new FlightBookingSystem(); //create instance of the class
  4.  
  5.     Flight flight = fbs.getFlightInfo(); //Obtain flight information from user
  6.  
  7.     if (flight == null) {
  8.         return; //Exit the program if flight information is not provided
  9.     }
  10.  
  11.     Scanner scanS = new Scanner(System.in);
  12.     Scanner scanN = new Scanner(System.in);
  13.  
  14.     String bookMoreSeats;
  15.  
  16.     do {
  17.         //Ask user if they want to book seats for the flight
  18.         System.out.print("\nWould you like to book seats on the " +
  19.                 flight.getFlightName() + " flight? (yes/no): ");
  20.  
  21.         bookMoreSeats = scanS.nextLine();
  22.  
  23.         if (bookMoreSeats.equalsIgnoreCase("yes")) {
  24.             boolean validInput = false;
  25.             while (!validInput) {
  26.                 System.out.print("How many seats would you like to book?: ");
  27.                 try {
  28.                     int bs = scanN.nextInt();
  29.                     flight.bookSeats(bs); //Book the specified # of seats
  30.                     validInput = true;
  31.                 } catch (InputMismatchException e) {
  32.                     System.out.println("Invalid input. Please enter an integer value.");
  33.                     scanN.nextLine();
  34.                 }
  35.             }
  36.         }
  37.     } while (bookMoreSeats.equalsIgnoreCase("yes"));
  38.  
  39.     System.out.println(flight.toString()); //Display the flight information
  40. }
  41.  
  42. public Flight getFlightInfo() {
  43.     //Declare variables/scanners
  44.     Scanner scanS = new Scanner(System.in);
  45.     Scanner scanN = new Scanner(System.in);
  46.     int maxSeats;
  47.     String flightName;
  48.  
  49.     ArrayList<String> validFlightNames = new ArrayList<>(Arrays.asList("AA", "AC", "DL"));
  50.  
  51.     //Take user input for the name of the flight and maximum seating
  52.     System.out.println("Here are the available flights!: "
  53.             + "\n- American Airlines (AA)"
  54.             + "\n- Air Canada (AC)"
  55.             + "\n- Delta Air Lines (DL)");
  56.  
  57.     do {
  58.         System.out.print("\nWhat is the name of your flight: ");
  59.         flightName = scanS.nextLine().toUpperCase();
  60.  
  61.         if (!validFlightNames.contains(flightName)) {
  62.             System.out.println("Sorry, that is not an available flight. Please try again.");
  63.         }
  64.  
  65.     } while (!validFlightNames.contains(flightName));
  66.  
  67.     try {
  68.         System.out.print("What is the maximum amount of seating on this flight: ");
  69.         maxSeats = scanN.nextInt();
  70.  
  71.         //Validate that maxSeats is greater than 0
  72.         if (maxSeats <= 0) {
  73.             System.out.println("Invalid input. Maximum seating must be greater than 0.");
  74.             return null;
  75.         }
  76.     } catch (InputMismatchException e) {
  77.         System.out.println("Invalid input. Please enter an integer value. ");
  78.         return null;
  79.     }
  80.  
  81.     //Created and return Flight object with the provided information
  82.     return new Flight(flightName, maxSeats);
  83.  
  84. } //end of getFlightInfo() method
  85.  
  86. /*
  87. Project: Airline Reservation System
  88. Programmer: Quynh Nguyen
  89. Date: May 11th, 2023
  90. Program name: AirlineReservationSystem.java
  91. Description: A class for managing airline reservations
  92. */
  93.  
  94. public class AirlineReservationSystem {
  95.     private String airlineName;
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement