-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlight.java
More file actions
399 lines (362 loc) · 15.4 KB
/
Flight.java
File metadata and controls
399 lines (362 loc) · 15.4 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/**
* @file Flight.java
*
* @author Malia Cherry
*
* @date Nov. 2022
**/
import java.util.*;
/**
* The Flight class contains the methods used to create and book flights.
*/
public class Flight extends Ticket {
Scanner input = new Scanner(System.in);
Random rand = new Random();
String flightNum, ticketNum = "";
String[] seatArray;
boolean[][] G100;
boolean[][] G400;
/**
* Constructor for the Flight class. Initializes the array of seats.
*
* @param none
*/
public Flight() {
G100 = new boolean[7][3];
G400 = new boolean[20][3];
}
/**
* Prompt the user to select an option from the main menu and
* calls to the function that corresponds to the user's selection.
*
* @param none
*/
public void mainMenu() {
//testFull(G100);
// testFull(G400);
String selection = "";
String number = "";
String returned = "";
String repeat = "Y";
System.out.println("\n\t\tMain Menu \n\"1\" - Purchase a new ticket \n\"2\" - View existing ticket");
System.out.print("Your selection: ");
while (!selection.equals("1") && !selection.equals("2")) {
selection = input.nextLine();
switch (selection) {
case "1":
while (repeat.equalsIgnoreCase("Y")) {
System.out.println("\n***********************************************");
flightInfo();
System.out.println("***********************************************\n");
do {
System.out.print("Please enter the flight number you would like to purchase: ");
number = buyTicket(input.nextLine());
if (number.equals("FULL")) {
System.out.println("Please select another flight.");
number = "";
}
} while (number.equals(""));
if (number.equals("FULL")) {
break;
} else {
confirmation(number);
}
System.out.println("***********************************************");
System.out.print("Would you like to purchase another ticket? (Y/N): ");
repeat = input.nextLine();
System.out.println("***********************************************");
while (!repeat.equalsIgnoreCase("Y")) {
if (repeat.equalsIgnoreCase("N")) {
break;
} else {
System.out.println("Invalid input. Please try again.\n");
repeat = "";
}
System.out.println("***********************************************");
System.out.print("Would you like to purchase another ticket? (Y/N): ");
repeat = input.nextLine();
System.out.println("***********************************************");
}
}
break;
case "2":
if (tickets.size() == 0) {
System.out.println("There are no tickets to view.");
repeat = "";
}
while (repeat.equalsIgnoreCase("Y")) {
do {
System.out.print("\nPlease enter your ticket number: ");
number = getTicket(input.nextLine());
} while (number.equals(""));
if (number.equals("not found")) {
repeat = "Y";
} else {
System.out.println("***********************************************");
System.out.print("Would you like to cancel your ticket? (Y/N): ");
repeat = input.nextLine();
System.out.println("***********************************************");
if (repeat.equalsIgnoreCase("Y")) {
returned = returnTicket(number.toUpperCase());
} else {
while (!repeat.equalsIgnoreCase("Y")) {
if (repeat.equalsIgnoreCase("N")) {
break;
} else {
System.out.println("Invalid input. Please try again.\n");
repeat = "";
}
System.out.println("***********************************************");
System.out.print("Would you like to cancel your ticket? (Y/N): ");
repeat = input.nextLine();
System.out.println("***********************************************");
}
returned = returnTicket(number.toUpperCase());
}
String flight = number.toUpperCase().substring(0, 4);
if (flight.equals("G100")) {
returnSeat(returned, G100);
} else {
returnSeat(returned, G400);
}
if (tickets.size() == 0) {
System.out.println("\nThere are no tickets to view.");
break;
}
System.out.println("***********************************************");
System.out.print("Would you like to view another ticket? (Y/N): ");
repeat = input.nextLine();
System.out.println("***********************************************");
if (repeat.equalsIgnoreCase("Y") && tickets.size() == 0) {
System.out.println("\nThere are no tickets to view.");
break;
}
while (!repeat.equalsIgnoreCase("Y")) {
if (tickets.size() == 0) {
System.out.println("\nThere are no tickets to view.");
break;
}
if (repeat.equalsIgnoreCase("N")) {
break;
} else {
System.out.println("Invalid input. Please try again.\n");
repeat = "";
}
System.out.println("***********************************************");
System.out.print("Would you like to view another ticket? (Y/N): ");
repeat = input.nextLine();
System.out.println("***********************************************");
}
}
}
break;
default:
System.out.println("\n\nCommand not recognized. Please try again.");
System.out.println("\n\"1\" - Purchase a new ticket \n\"2\" - View existing ticket");
System.out.print("Your selection: ");
break;
}
}
}
/**
* Changes all seats on the plane to true(booked) to test the full flight.
*
* @param flight the flight to be tested
*/
public void testFull(boolean[][] flight) {
for (int i = 0; i < flight.length; i++) {
for (int j = 0; j < flight[i].length; j++) {
flight[i][j] = true;
}
}
}
/**
* Gathers information from user to purchase a ticket for the given flight.
* Generates a ticket with entered information.
*
* @param flightNum the flight the user wants to buy a ticket for
*/
public String buyTicket(String flightNum) {
Ticket ticket;
String name, ticketNum, seat = "";
switch (flightNum) {
case "100":
try {
AirplaneFull(G100);
} catch (AirplaneFullException e) {
System.out.println(e.getMessage());
return "FULL";
}
System.out.print("\nEnter your first and last name (ex: J D or John Doe): ");
name = input.nextLine();
while (!name.matches("^[a-zA-Z']+\s[a-zA-Z-']+")) {
System.out.println("Invalid input. Please try again.");
System.out.print("Enter your first and last name: ");
name = input.nextLine();
}
displaySeats(G100);
do {
seat = seatSelector(G100);
} while (seat == "");
ticketNum = "G100" + String.format("%04d", rand.nextInt(9998));
ticket = new Ticket(name, flightNum, ticketNum, seat);
generateTicket(ticket);
return seat;
case "400":
try {
AirplaneFull(G400);
} catch (AirplaneFullException e) {
System.out.println(e.getMessage());
return "FULL";
}
System.out.print("\nEnter your first and last name: ");
name = input.nextLine();
while (!name.matches("^[a-zA-Z']+\s[a-zA-Z-']+")) {
System.out.println("Invalid input. Please try again.");
System.out.print("Enter your first and last name: ");
name = input.nextLine();
}
displaySeats(G400);
do {
seat = seatSelector(G400);
} while (seat == "");
ticketNum = "G400" + String.format("%04d", rand.nextInt(9998));
ticket = new Ticket(name, flightNum, ticketNum, seat);
generateTicket(ticket);
return seat;
default:
System.out.println("Flight number not recognized. Please try again.");
return "";
}
}
/**
* Throws an exception if the flight is full
*
* @param flight the flight to be checked
*/
public boolean AirplaneFull(boolean[][] flight) throws AirplaneFullException {
int totalSeats = (flight.length * flight[0].length);
int unavailableSeats = 0;
for (int i = 0; i < flight.length; i++) {
for (int j = 0; j < flight[i].length; j++) {
if (flight[i][j] == true) {
unavailableSeats++;
}
}
}
if (unavailableSeats == totalSeats) {
throw new AirplaneFullException();
} else {
return false;
}
}
/**
* Changes the seat on the given flight from true(booked) to false(unbooked).
*
* @param seat the seat to be returned
* @param flight the flight the seat is in
*/
public void returnSeat(String seat, boolean[][] flight) {
int row, column = 0;
String columnLabel = "";
if (seat.length() == 2) {
row = Integer.parseInt(seat.substring(0, 1)) - 1;
columnLabel = seat.substring(1, 2);
} else {
row = Integer.parseInt(seat.substring(0, 2)) - 1;
columnLabel = seat.substring(2, 3);
}
switch (columnLabel) {
case "A":
column = 0;
break;
case "B":
column = 1;
break;
case "C":
column = 2;
break;
}
for (int i = 0; i < flight.length; i++) {
for (int j = 0; j < flight[i].length; j++) {
if (i == row && j == column) {
flight[i][j] = false;
}
}
}
}
/**
* Displays the seats on the given flight.
*
* @param flightNum the flight to be displayed
*/
public void displaySeats(boolean[][] flightNum) {
System.out.println("\n\n A B C\n -------------");
for (int i = 0; i < flightNum.length; i++) {
System.out.print(" " + (i + 1) + " |");
for (int j = 0; j < flightNum[i].length; j++) {
if (flightNum[i][j] == false) {
System.out.print(" |");
} else {
System.out.print(" X |");
}
}
System.out.println("\n -------------");
}
System.out.println("\nX = Unavailable\n");
}
/**
* Changes a seat on the given flight from false(unbooked) to true(booked).
*
* @param flightNum the flight to the seat will be booked on
*/
public String seatSelector(boolean[][] flightNum) {
// display seats for flight and prompt for seat selection
// check input for valid seat (prompt for seat row[1-#], then seat
// column[A,B,C])
// if seat is taken, prompt for another seat
// if seat is available, assign seat number and return seat number
System.out.print("Please enter the seat you would like to purchase (row, column): ");
String seat = input.nextLine();
seat = seat.toUpperCase();
seat = seat.replaceAll(" ", "");
if (!seat.contains(",")) {
System.out.println("Invalid seat selection. Please try again.");
return "";
} else {
seatArray = seat.split(",");
if (!seatArray[0].matches("\\d+")) {
System.out.println("Invalid seat selection. Please try again.");
return "";
}
int row = Integer.parseInt(seatArray[0]) - 1;
int col = 0;
if (row < 0 || row > flightNum.length) {
System.out.println("Invalid seat selection. Please try again.");
return "";
} else {
switch (seatArray[1].toUpperCase()) {
case "A":
col = 0;
break;
case "B":
col = 1;
break;
case "C":
col = 2;
break;
default:
System.out.println("Invalid seat selection. Please try again.");
return "";
}
if (flightNum[row][col] == false) {
flightNum[row][col] = true;
return seatArray[0] + seatArray[1];
} else {
System.out.println("Seat is unavailable. Please try again.");
return "";
}
}
}
}
}