Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- FlightBookingSystem fbs = new FlightBookingSystem(); //create instance of the class
- Flight flight = fbs.getFlightInfo(); //Obtain flight information from user
- if (flight == null) {
- return; //Exit the program if flight information is not provided
- }
- Scanner scanS = new Scanner(System.in);
- Scanner scanN = new Scanner(System.in);
- String bookMoreSeats;
- do {
- //Ask user if they want to book seats for the flight
- System.out.print("\nWould you like to book seats on the " +
- flight.getFlightName() + " flight? (yes/no): ");
- bookMoreSeats = scanS.nextLine();
- if (bookMoreSeats.equalsIgnoreCase("yes")) {
- boolean validInput = false;
- while (!validInput) {
- System.out.print("How many seats would you like to book?: ");
- try {
- int bs = scanN.nextInt();
- flight.bookSeats(bs); //Book the specified # of seats
- validInput = true;
- } catch (InputMismatchException e) {
- System.out.println("Invalid input. Please enter an integer value.");
- scanN.nextLine();
- }
- }
- }
- } while (bookMoreSeats.equalsIgnoreCase("yes"));
- System.out.println(flight.toString()); //Display the flight information
- }
- public Flight getFlightInfo() {
- //Declare variables/scanners
- Scanner scanS = new Scanner(System.in);
- Scanner scanN = new Scanner(System.in);
- int maxSeats;
- String flightName;
- ArrayList<String> validFlightNames = new ArrayList<>(Arrays.asList("AA", "AC", "DL"));
- //Take user input for the name of the flight and maximum seating
- System.out.println("Here are the available flights!: "
- + "\n- American Airlines (AA)"
- + "\n- Air Canada (AC)"
- + "\n- Delta Air Lines (DL)");
- do {
- System.out.print("\nWhat is the name of your flight: ");
- flightName = scanS.nextLine().toUpperCase();
- if (!validFlightNames.contains(flightName)) {
- System.out.println("Sorry, that is not an available flight. Please try again.");
- }
- } while (!validFlightNames.contains(flightName));
- try {
- System.out.print("What is the maximum amount of seating on this flight: ");
- maxSeats = scanN.nextInt();
- //Validate that maxSeats is greater than 0
- if (maxSeats <= 0) {
- System.out.println("Invalid input. Maximum seating must be greater than 0.");
- return null;
- }
- } catch (InputMismatchException e) {
- System.out.println("Invalid input. Please enter an integer value. ");
- return null;
- }
- //Created and return Flight object with the provided information
- return new Flight(flightName, maxSeats);
- } //end of getFlightInfo() method
- /*
- Project: Airline Reservation System
- Programmer: Quynh Nguyen
- Date: May 11th, 2023
- Program name: AirlineReservationSystem.java
- Description: A class for managing airline reservations
- */
- public class AirlineReservationSystem {
- private String airlineName;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement