-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlightReservationSystem.java
More file actions
180 lines (153 loc) · 5.53 KB
/
FlightReservationSystem.java
File metadata and controls
180 lines (153 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//Sina Pahlavan
//501 034 271
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
// Flight System for one single day at YYZ (Print this in title) Departing flights!!
public class FlightReservationSystem {
public static void main(String[] args) throws FileNotFoundException {
// Create a FlightManager object
FlightManager manager = new FlightManager();
// List of reservations that have been made
ArrayList<Reservation> myReservations = new ArrayList<Reservation>(); // my flight reservations
Scanner scanner = new Scanner(System.in);
System.out.print(">");
while (scanner.hasNextLine()) {
String inputLine = scanner.nextLine();
if (inputLine == null || inputLine.equals("")) continue;
// The command line is a scanner that scans the inputLine string
// For example: list AC201
Scanner commandLine = new Scanner(inputLine);
// The action string is the command to be performed (e.g. list, cancel etc)
String action = commandLine.next();
String flightNum;
String name;
String passportNum;
String seatNum;
if (action == null || action.equals("")) {
continue;
}
// List all flights
else if (action.equalsIgnoreCase("LIST")) {
manager.printAllFlights();
}
// Reserve a flight based on Flight number string (example input: res AC220)
else if (action.equalsIgnoreCase("RES")) {
// Get the flight number string from the commndLine scanner (use hasNext() to check if there is a
// flight number string entered
// call reserveSeatOnFlight() method in manager passing in the flight number string
// A reference to a Reservation object is returned. Check to make sure it is not == null
// If it is null, then call manager.getErrorMessage() and print the message
// If it is not null, add the reservation to the myReservations array list and print the reservation (see class Reservation)
if (commandLine.hasNext()) {
flightNum = commandLine.next();
if (commandLine.hasNext()) {
name = commandLine.next();
if (commandLine.hasNext()) {
passportNum = commandLine.next();
if (commandLine.hasNext()) {
seatNum = commandLine.next();
try{
Reservation res = manager.reserveSeatOnFlight(flightNum,new Passenger(name,passportNum,seatNum));
if (res!=null){
if (seatNum.charAt(seatNum.length()-1)=='+'){
res.setFirstClass();
}
myReservations.add(res);
}
}
catch(FlightNotFoundException fLNF){
System.out.println(fLNF.getMessage());
}
catch(SeatOccupiedException soe){
System.out.println(soe.getMessage());
}
catch(SeatDoesNotExistException sdne){
System.out.println(sdne.getMessage());
}
catch(PassengerAlreadyOnFlightException paoe){
System.out.println(paoe.getMessage());
}
catch(FlightHasNoFirstClassSeatsException e){
System.out.println(e.getMessage());
}
}
}
}
}
} else if (action.equalsIgnoreCase("SEATS")) {
if (commandLine.hasNext()) {
try{
flightNum = commandLine.next();//getting flight number
manager.printSeat(flightNum);
}
catch(FlightNotFoundException fnfe){
System.out.println(fnfe.getMessage());
}
}
}
// Cancel an existing reservation (example input: cancel AC220)
else if (action.equalsIgnoreCase("PASMAN")) {
if (commandLine.hasNext()){
try{
flightNum = commandLine.next();
manager.printPassengersForAFlight(flightNum);
}
catch(FlightNotFoundException fnfe){
System.out.println(fnfe.getMessage());
}catch (FlightEmptyException fee){
System.out.println(fee.getMessage());
}
}
}
// These commands can be left until we study Java interfaces
// Feel free to implement the code in class Manager if you already understand interface Comparable
// and interface Comparator
else if (action.equalsIgnoreCase("CANCEL")) {
/**
* cancels a passenger on a flight
* needs flight number, passenger name, and passport number
* for example, cnclpsngr AC123 Sina 123
* cancels the seat of a passenger names Sina with a passport number of 123 on flight AC123
*/
if (commandLine.hasNext()) {
String flightnum = commandLine.next();//get flight num
if (commandLine.hasNext()) {
name = commandLine.next();// get name
if (commandLine.hasNext()) {
String passportnum = commandLine.next();// get passport number
try{
Passenger apassenger = new Passenger(name, passportnum);//create passenger object with name and pass num
manager.cancelReservation(flightnum,apassenger);
Reservation toRemove = new Reservation(flightnum,apassenger);
for (int i=0;i<myReservations.size();i++){
if (myReservations.get(i).equals(toRemove)){
myReservations.remove(i);
break;
}
}
}
catch(PassengerNotOnFlightException pnof){
System.out.println(pnof.getMessage());
}
catch(FlightNotFoundException e){
System.out.println(e.getMessage());
}
catch (FlightEmptyException e){
System.out.println(e.getMessage());
}
//with flightNum and passenger object
}
}
}
}
else if(action.equalsIgnoreCase("MYRES")){
for (Reservation res:myReservations){
System.out.println(res.toString());
}
}
System.out.print("\n>");
}
}
}