/* * IT205 Intro to Object Oriented Programming * Programmer: Ian Hagmann * Nov 26, 2024 * I made this program without help from anyone. * NetBeans Version: Apache NetBeans IDE 22 * * Test program for Passenger and Flight classes References: Deitel, Paul, and Harvey Deitel. Java How to Program (Early Objects), Tenth Edition. Prentice Hall, 2014. */ package final2; import java.util.ArrayList; import java.util.Scanner; public class Final2 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); //arraylist to hold flights ArrayList flights = new ArrayList<>(); //populating all flights for the 3 days for (int i = 7; i<=9; i++){ flights.add(new Flight("SA2017", "May "+i+", 2020", "8:00 AM", "7:30 AM", "NFK", "DC")); flights.add(new Flight("SA1218", "May "+i+", 2020", "11:00 AM", "10:30 AM", "DC", "NY")); flights.add(new Flight("SA1219", "May "+i+", 2020", "2:00 PM", "1:30 PM", "NY", "DC")); flights.add(new Flight("SA2020", "May "+i+", 2020", "5:00 PM", "4:30 PM", "DC", "NFK")); } //arraylist to hold valid passengers ArrayList passengers = new ArrayList<>(); //test passengers passengers.add(new Passenger("Tom Hanks", "804-555-6548")); passengers.add(new Passenger("Taylor Swift", "804-555-9356")); passengers.add(new Passenger("Bill Gates", "804-555-1520")); passengers.add(new Passenger("Kanye West", "804-555-4972")); //test reservations for above passengers for (Flight i : flights){ i.reserveSeat(1, 3, passengers.get(0)); i.reserveSeat(4, 2, passengers.get(1)); i.reserveSeat(7, 1, passengers.get(2)); i.reserveSeat(10, 4, passengers.get(3)); } while (true){//loop //getting user to choose 1 or 2 System.out.print("\nWhat would you like to do?:\n\t1) view your boarding pass" + "\n\t2) make a reservation\n> "); int option = Integer.parseInt(scan.nextLine()); if (option == 1){//searching for reservations System.out.print("What is your last name?\n> "); String lastName = scan.nextLine(); System.out.print("What is your phone number?\n(###-###-####)\n> "); String phoneNumber = scan.nextLine(); //looping through all passengers and displaying the boarding pass of matching phone number for (Passenger i : passengers){ if (i.getPhoneNumber().equals(phoneNumber) && i.getName().split(" ")[1].equals(lastName)){ i.getPass(); } } } else if (option == 2){//making new passenger reservation System.out.print("Where are you departing from?\n(NFK, DC or NY)\n> "); String from = scan.nextLine(); System.out.print("Where are you going to?\n(NFK, DC or NY)\n> "); String to = scan.nextLine(); //there is no direct flight connecting NFK and NY, so users must make 2 reservations if (from.equals("NFK") && to.equals("NY") || from.equals("NY") && to.equals("NFK")){ System.out.println("You will need to make two reservations\n" + "from "+from+" to DC first, then to "+to); continue; } System.out.print("What is your full name?\n> "); String name = scan.nextLine(); System.out.print("What is your phone number?\n(###-###-####)\n> "); String phoneNumber = scan.nextLine(); //making new passenger without seat yet passengers.add(new Passenger(name,phoneNumber)); System.out.print("Which day?\nMay (7 8 or 9), 2020\nPut just the number\n> "); String day = "May "+scan.nextLine()+", 2020"; //finding out which flight based on to and from and day int flightIndex = 0; if (from.equals("DC")){ if (to.equals("NY")){ flightIndex++; }else{ flightIndex += 3; } }else if (from.equals("NY")){ flightIndex += 2; } switch(day){ case "May 9, 2020": flightIndex += 4; case "May 8, 2020": flightIndex += 4; } //loop until valid seat choice while (passengers.getLast().getReservation() == null){ flights.get(flightIndex).displaySeats(); System.out.print("\nWhat row and seat number? (# #)\n> "); int row = scan.nextInt();//gets first number before space int seat = scan.nextInt();//get second number scan.nextLine();//removes extra newline //prints returned message, errors and successes System.out.println(flights.get(flightIndex).reserveSeat(row,seat,passengers.getLast())); } //displays the finished boarding pass passengers.getLast().getPass(); } else {//anything other than the two options are given System.out.println("Not an option, pick 1 or 2"); } }//end of loop } }