-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookingAppUI.java
More file actions
425 lines (362 loc) · 14.4 KB
/
Copy pathBookingAppUI.java
File metadata and controls
425 lines (362 loc) · 14.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
/**
* The UI for the BookingApp
*/
public class BookingAppUI extends BookingAppUIConstants {
private Scanner scanner;
private BookingApp bookingApp;
private boolean isGuest;
BookingAppUI() {
scanner = new Scanner(System.in);
bookingApp = BookingApp.getInstance();
isGuest = true;
}
public static void main(String[] args) {
BookingAppUI main = new BookingAppUI();
main.run();
}
public void run() {
clear();
System.out.println(GREETING);
//Let's go until the user is done
while(true) {
displayMainMenu();
int userCommand = getUserCommand(mainMenuText.length);
if(userCommand == -1) {
System.out.println("Not a valid command, please try again.");
sleep(1500);
continue;
}
if (userCommand == mainMenuText.length-1) break;
switch(userCommand) {
case 0:
if(isGuest) {
isGuest = loginUser();
} else {
accessAccount();
}
break;
case 1:
searchForFlight();
break;
case 2:
searchForHotel();
break;
case 3:
searchForCar();
break;
}
}
clear();
System.out.println("See ya later!");
bookingApp.logout();
}
private void displayMainMenu() {
System.out.println("\n=------------ Main Menu ------------=");
if (isGuest) {
for (int i = 0; i < mainMenuText.length; i++) {
System.out.println((i+1) + ". " + mainMenuText[i]);
}
} else {
for (int i = 0; i < rUserMainMenuText.length; i++) {
System.out.println((i+1) + ". " + rUserMainMenuText[i]);
}
}
System.out.println("\n");
}
private int getUserCommand(int numOfCommands) {
System.out.println("What would you like to do?: ");
int command = scanner.nextInt() - 1;
scanner.nextLine();
if (command >= 0 && command <= numOfCommands-1) return command;
return -1;
}
private boolean loginUser() {
clear();
System.out.println("\n=------------ Log In ------------=");
String username = getUsername();
if (username == null) return false;
String password = getPassword();
if (password == null) return false;
if(!bookingApp.tryLogin(username, password)) {
System.out.println("There doesn't seem to be an account with that username and password.\n");
return true;
}
System.out.println("Successfully logged in. Hello there " + username +"!\n");
return false;
}
private String getUsername() {
// clear();
System.out.println("Please enter your username: ");
String username = "";
username = checkString(username);
return username;
}
private String getPassword() {
System.out.println("Please enter your password: ");
String password = "";
password = checkString(password);
return password;
}
private void accessAccount() {
clear();
System.out.println("\n=------------ Bookings ------------=");
bookingApp.printUserBookings();
}
private void searchForFlight() {
while(true) {
// clear();
displayFlightSearchOptions();
int userChoice = getUserCommand(flightSearchText.length);
if (userChoice == -1) {
System.out.println("Not a valid command, please try again.");
sleep(1500);
continue;
}
if (userChoice == flightSearchText.length-1) break;
switch(userChoice) {
case 0:
searchOnewayFlight();
break;
case 1:
break;
}
}
}
private void displayFlightSearchOptions() {
System.out.println("\n=-------- Search for a Flight --------=");
for (int i = 0; i < flightSearchText.length; i++) {
System.out.println((i+1) + ". " + flightSearchText[i]);
}
System.out.println("\n");
}
private void searchOnewayFlight() {
if (!isGuest) {
bookingApp.prepPartyMembers();
if(getYesOrNo("Would you like to add a party member?"))
addPartyMembers();
userOneWayFlightSearch();
} else {
guestOneWayFlightSearch();
}
}
private void addPartyMembers() {
while(true) {
bookingApp.addPartyMember(getPartyMember());
if(!getYesOrNo("Would you like to add another party member?")) return;
}
}
private PartyMember getPartyMember() {
clear();
System.out.println("=-------- Adding a Party Member --------=");
String firstName = getString("Please enter their First Name: ");
String lastName = getString("Please enter their Last Name: ");
int age = getInt("Please enter their age: ");
return new PartyMember(firstName, lastName, age);
}
/*---------------- Book a Oneway Flight ----------------------------------------------*/
private void userOneWayFlightSearch() {
// Get User's flight specs
System.out.println("We'll need to get some information from you to begin.");
System.out.println("For these first two, please enter IATA airport codes to get results.\n");
String deptAirportCode = getString("Which airport would you like to depart from?: ");
if (deptAirportCode == null) return;
String arrivAirportCode = getString("Which airport would you like to arrive at?: ");
if (arrivAirportCode == null) return;
Date deptDate = getDate("When would you like to depart?");
if (deptDate == null) return;
// Search for flights
ArrayList<Object> searchResults = bookingApp.searchForAOneWayFlight(deptAirportCode, arrivAirportCode, deptDate);
String fOrFG = printSearchResults(searchResults);
if (getYesOrNo("Would you like to book a flight?")) {
bookAOneWayFlight(fOrFG, searchResults);
}
}
private void guestOneWayFlightSearch() {
System.out.println("We'll need to get some information from you to begin.");
System.out.println("For these first two, please enter IATA airport codes to get results.\n");
String deptAirportCode = getString("Which airport would you like to depart from?: ");
if (deptAirportCode == null) return;
String arrivAirportCode = getString("Which airport would you like to arrive at?: ");
if (arrivAirportCode == null) return;
Date deptDate = getDate("When would you like to depart?");
if (deptDate == null) return;
ArrayList<Object> searchResults = bookingApp.searchForAOneWayFlight(deptAirportCode, arrivAirportCode, deptDate);
printSearchResults(searchResults);
sleep(3000);
}
private void bookAOneWayFlight(String fOrFG, ArrayList<Object> searchResults) {
int userChoice = chooseFlight(searchResults);
if (fOrFG.charAt(userChoice) == 'f') {
Flight userFlight = bookingApp.getFlight((Flight)searchResults.get(userChoice));
chooseFlightSeats(userFlight);
bookingApp.addUserFlight(userFlight);
} else if (fOrFG.charAt(userChoice) == 'G') {
FlightGroup userFG = bookingApp.getFGroup((FlightGroup)searchResults.get(userChoice));
for (Flight f : userFG.getAllFlights()) {
chooseFlightSeats(f);
}
bookingApp.addUserFGroup(userFG);
}
}
/*------------------------------------------------------------------------------------*/
private void chooseFlightSeats(Flight userFlight) {
for (int i = 0; i < bookingApp.getTotalTravelers(); i++) {
System.out.println(userFlight.getSeatChart());
Seat userSeat = chooseASeat();
userFlight.addUserSeat(userSeat);
bookingApp.updateFlightChart(userFlight, userSeat);
}
return;
}
private Seat chooseASeat() {
System.out.println("Please enter the row and column to choose a seat (The format should be \"1A\")):");
return new Seat(scanner.nextLine(), false);
}
private int chooseFlight(ArrayList<Object> searchResults) {
clear();
printSearchResults(searchResults);
System.out.println("Choose a Flight: ");
int userChoice = scanner.nextInt()-1;
scanner.nextLine();
return userChoice;
}
private String printSearchResults(ArrayList<Object> searchResults) {
for (int i = 0; i < searchResults.size()-1; i++) {
System.out.println("Flight #" + (i+1) + "\n" + searchResults.get(i).toString());
}
return (String)searchResults.get(searchResults.size()-1);
}
private Date getDate(String datePrompt) {
System.out.println(datePrompt);
System.out.println("(Please enter your date in the format \"mm/dd/yy\"): ");
String dateString = "";
dateString = checkString(dateString);
if (dateString != null) return new Date(dateString);
return null;
}
private String getString(String prompt) {
System.out.println(prompt);
String info = "";
info = checkString(info);
return info;
}
private int getInt(String prompt) {
System.out.print(prompt + "Age: ");
int info = scanner.nextInt();
scanner.nextLine();
return info;
}
private int getIntRating(String prompt) {
System.out.print(prompt + "Rating: ");
int info = scanner.nextInt();
scanner.nextLine();
return info;
}
private int getPrice(String prompt) {
System.out.print(prompt + "Price: ");
int info = scanner.nextInt();
scanner.nextLine();
return info;
}
private String checkString(String userInput) {
while (true) {
userInput = scanner.nextLine().trim();
if(!userInput.contentEquals("")) return userInput;
System.out.println("You can't leave this empty you know.");
System.out.println("Would you like to try again (y) or go back to the main menu (n)?: ");
String userChoice = scanner.nextLine().trim().toLowerCase();
if (userChoice == "n") return null;
}
}
private boolean getYesOrNo(String prompt) {
System.out.println(prompt + " (y/n):");
String input = scanner.nextLine().trim().toLowerCase();
if (input.equals("y")) return true;
return false;
}
/**
* Basic logic for searching for hotel.
* Asking user for information.
* Uses other classes to search.
*/
private void searchForHotel() {
System.out.println("\n=-------- Search for a Hotel --------=");
System.out.println("First we will need your preferences.\n");
String address = getString("What city (address) is the hotel in?: ");
if (address == null) return;
int rating = getIntRating("What is the rating of the hotel you would like (x/5 stars)?: ");
int price = getPrice("What is the price you are searching for?: ");
printHotelAmenityQs();
ArrayList<String> hotelAmenities = new ArrayList<>();
while(true) {
String tempHotelAmenity = getString("-");
if (tempHotelAmenity.equalsIgnoreCase("q")) {
System.out.println("Saved your entered preferences (if any), moving on.\n");
break;
}
hotelAmenities.add(tempHotelAmenity);
}
printRoomAmenityQs();
ArrayList<String> roomAmenities = new ArrayList<>();
while(true) {
String tempRoomAmenity = getString("-");
if (tempRoomAmenity.equalsIgnoreCase("q")) {
System.out.println("Saved your entered preferences (if any), Searching . . .\n");
break;
}
roomAmenities.add(tempRoomAmenity);
}
ArrayList<Object> searchResults = bookingApp.searchForHotel(address, rating, price, hotelAmenities, roomAmenities);
printSearchResults(searchResults);
sleep(3000);
}
/**
* Print hotel amenities
*/
private void printHotelAmenityQs() {
System.out.println("Now we will need you to input all (if any) hotel amenities you want.");
System.out.println("Here are your options:\n");
System.out.println("Pet-Friendly");
System.out.println("Stair-Free Path-to-Entrance");
System.out.println("Parking");
System.out.println("Free Wifi");
System.out.println("Pool");
System.out.println("Air Conditioning\n");
System.out.println("Enter each amenity one at a time seperated by pressing the return key.");
System.out.println("Enter q or Q to quit entering amenities.");
}
/**
* Print room amenities
*/
private void printRoomAmenityQs() {
System.out.println("Now we will need you to input all (if any) room amenities you want.");
System.out.println("Here are your options:\n");
System.out.println("Smoking");
System.out.println("Accessible Bathroom");
System.out.println("Roll-In-Shower");
System.out.println("Washer and Dryer");
System.out.println("Queen Bed");
System.out.println("King Bed");
System.out.println("Twin Bed");
System.out.println("Double Bed");
System.out.println("Studio Bed\n");
System.out.println("Enter each amenity one at a time seperated by pressing the return key.");
System.out.println("Enter q or Q to quit entering amenities.");
}
private void searchForCar() {
clear();
}
private void clear() {
System.out.print("\033[H\033[2J");
}
private void sleep(int num) {
try {
TimeUnit.MILLISECONDS.sleep(num);
} catch (Exception e) {
System.out.println("Timmer error");
}
}
}