Booker.java (3320B)
1 import java.io.FileNotFoundException; 2 import java.util.ArrayList; 3 import java.util.List; 4 import java.util.Scanner; 5 6 public class Booker { 7 public TicketDB Database; 8 Scanner sc; 9 Flight fl; 10 11 Booker() throws FileNotFoundException { 12 sc = new Scanner(System.in); 13 Database = new TicketDB(); 14 } 15 16 public ClientInfo GetClientInfo() { 17 System.out.print("name: "); 18 String name = sc.nextLine(); 19 20 System.out.print("tel: "); 21 String tel = sc.nextLine(); 22 23 System.out.print("birth date: "); 24 String bdate = sc.nextLine(); 25 26 System.out.print("credit card: "); 27 String card = sc.nextLine(); 28 29 System.out.print("address: "); 30 String addr = sc.nextLine(); 31 32 return new ClientInfo(name, tel, bdate, card, addr); 33 } 34 35 public String[] SearchFlight() { 36 System.out.println("Please enter details of your desired flight"); 37 System.out.println("starting point: "); 38 String start = sc.nextLine(); 39 40 System.out.println("destination: "); 41 String dest = sc.nextLine(); 42 43 ArrayList<Flight> ExistingFlights = new ArrayList<>(); 44 ArrayList<Flight> AvailableFlights = new ArrayList<>(); 45 for (Flight f : Database.AllFlights) 46 { 47 if (f.StartingPoint.equals(start) && f.Destination.equals(dest)) 48 ExistingFlights.add(f); 49 } 50 51 for (Flight f : ExistingFlights) 52 { 53 if (Database.CheckFlight(f) != null) 54 AvailableFlights.add(f); 55 } 56 57 if (AvailableFlights.size() == 0) 58 return null; 59 60 System.out.println("Available flights are : "); 61 62 for (Flight f : AvailableFlights) 63 { 64 System.out.println(f.toString()); 65 System.out.println("--------------"); 66 } 67 68 System.out.println("Enter desired flight ID : "); 69 String fID = sc.nextLine(); 70 71 boolean found = false; 72 for (Flight f : AvailableFlights) { 73 if (f.ID.equals(fID)) { 74 found = true; 75 fl = f; 76 break; 77 } 78 } 79 while(!found) 80 { 81 System.out.println("Invalid Flight ID, Please try again : "); 82 fID = sc.nextLine(); 83 for (Flight f : AvailableFlights) { 84 if (f.ID.equals(fID)) { 85 found = true; 86 fl = f; 87 break; 88 } 89 } 90 } 91 92 ArrayList<Seat> seats = Database.CheckFlight(fl); 93 94 System.out.println("Available seats are : "); 95 96 for (Seat s : seats) 97 System.out.println(s.toString()); 98 99 System.out.println("Enter ID of desired seat : "); 100 int sID = sc.nextInt(); 101 while (!ValidSeat(sID)) 102 { 103 System.out.println("Invalid selection! Please try again :"); 104 sID = sc.nextInt(); 105 } 106 107 return new String[]{Integer.toString(sID),fID}; 108 } 109 110 public void PrintTicket(Ticket T) { 111 System.out.println(T.toString()); 112 } 113 114 public boolean ValidSeat(int SeatInd) { 115 if (SeatInd > fl.Seats.length || SeatInd < 1) 116 return false; 117 return !fl.Seats[SeatInd-1].isOccupied; 118 } 119 120 }