-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
526 lines (458 loc) · 16.9 KB
/
Main.java
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
import java.time.LocalDateTime;
import java.util.*;
class Main {
public static void main(String[] args) {
// Create a flight reservation system
FlightReservationSystem reservationSystem = new FlightReservationSystem();
// Add some flights to the system
Flight flight1 = new Flight("A123", "NAG", "DEL", LocalDateTime.of(2023, 10, 15, 10, 00),
LocalDateTime.of(2023, 10, 15, 14, 00), "Indigo Airlines",
new HashMap<>(Map.of("Economy", 100, "Business", 20)));
reservationSystem.addFlight(flight1);
Flight flight2 = new Flight("P456", "PNQ", "HYD", LocalDateTime.of(2023, 10, 20, 12, 00),
LocalDateTime.of(2023, 10, 20, 16, 00), "Vistara Airlines",
new HashMap<>(Map.of("Economy", 50, "Business", 10)));
reservationSystem.addFlight(flight2);
// Create a Scanner for user input
Scanner scanner = new Scanner(System.in);
// Display a menu to the user
while (true) {
System.out.println("\nFlight Reservation System Menu:");
System.out.println("1. Search for Flights");
System.out.println("2. Make a Reservation");
System.out.println("3. Exit");
System.out.print("Enter your choice (1/2/3): ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
switch (choice) {
case 1:
// Search for flights
System.out.print("Enter departure airport code: ");
String departureAirport = scanner.nextLine();
System.out.print("Enter arrival airport code: ");
String arrivalAirport = scanner.nextLine();
System.out.print("Enter departure date (YYYY-MM-DD): ");
String departureDate = scanner.nextLine();
System.out.print("Enter number of passengers: ");
int numPassengers = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
System.out.print("Enter travel class (Economy/Business): ");
String travelClass = scanner.nextLine();
// Search for flights based on user input
List<Flight> flights = reservationSystem.searchFlights(
departureAirport, arrivalAirport, departureDate,
null, numPassengers, travelClass);
// Display the search results
System.out.println();
System.out.println();
if (flights.isEmpty()) {
System.out.println("No flights matching the criteria were found.");
} else {
System.out.println("Found flights:");
for (Flight flight : flights) {
System.out.println(flight);
}
}
break;
case 2:
// Make a reservation
System.out.print("Enter flight ID for reservation: ");
String flightId = scanner.nextLine();
System.out.print("Enter number of passengers: ");
numPassengers = scanner.nextInt();
scanner.nextLine();
// Check if the flight exists
Flight selectedFlight = reservationSystem.findFlightById(flightId);
if (selectedFlight == null) {
System.out.println("Flight not found.");
} else {
// Collect passenger information
List<Passenger> passengers = new ArrayList<>();
for (int i = 0; i <= numPassengers; i++) {
System.out.println("Passenger " + i + " details:");
System.out.print("Name: ");
String name = scanner.nextLine();
System.out.print("Email: ");
String email = scanner.nextLine();
System.out.print("Phone: ");
String phone = scanner.nextLine();
passengers.add(new Passenger(name, email, phone));
}
// Collect payment information
System.out.print("Enter credit card number: ");
String creditCardNumber = scanner.nextLine();
System.out.print("Enter expiration date (MM/YY): ");
String expirationDate = scanner.nextLine();
// Make the reservation
Reservation reservation = reservationSystem.makeReservation(
flightId, passengers, new PaymentInfo(creditCardNumber, expirationDate));
// Display the reservation details
System.out.println();
System.out.println();
System.out.println("Reservation created:");
System.out.println(reservation);
}
break;
case 3:
// Exit the program
System.out.println("Exiting Flight Reservation System.");
scanner.close();
System.exit(0);
break;
default:
System.out.println("Invalid choice. Please enter 1, 2, or 3.");
}
}
}
}
/**
* a flight reservation system that allows users to search for
* flights and make reservations.
*/
class FlightReservationSystem {
private List<Flight> availableFlights;
private List<Reservation> reservations;
FlightReservationSystem() {
availableFlights = new ArrayList<>();
reservations = new ArrayList<>();
}
void addFlight(Flight flight) {
availableFlights.add(flight);
}
/**
* Searches for available flights based on the specified criteria.
*
* @param origin The departure airport code.
* @param destination The arrival airport code.
* @param departureDate The date of departure (YYYY-MM-DD).
* @param returnDate The date of return (YYYY-MM-DD, optional).
* @param passengers The number of passengers (adults, children, infants).
* @param travelClass The class of service (e.g., Economy, Business).
* @return A list of available flight options.
*/
List<Flight> searchFlights(String origin, String destination, String departureDate,
String returnDate, int passengers, String travelClass) {
// Implement flight search logic here
// Return a list of Flight objects matching the criteria
List<Flight> matchingFlights = new ArrayList<>();
for (Flight flight : availableFlights) {
if (flight.getDepartureAirportCode().equals(origin) &&
flight.getArrivalAirportCode().equals(destination) &&
flight.getAvailableSeatsByClass().containsKey(travelClass) &&
flight.getAvailableSeatsByClass().get(travelClass) >= passengers) {
matchingFlights.add(flight);
}
}
return matchingFlights;
}
/**
* Makes a flight reservation for the specified flight and passengers.
*
* @param flightId The unique identifier for the selected flight.
* @param passengers The list of passenger details.
* @param paymentInfo The payment information (credit card details).
* @return A reservation confirmation.
*/
Reservation makeReservation(String flightId, List<Passenger> passengers, PaymentInfo paymentInfo) {
// Implement reservation logic here
// Return a reservation confirmation
Flight selectedFlight = findFlightById(flightId);
if (selectedFlight != null) {
// Check if there are enough available seats
int totalPassengers = passengers.size();
int availableSeats = selectedFlight.getAvailableSeatsByClass().get("Economy"); // Assuming Economy class
if (availableSeats >= totalPassengers) {
String reservationId = generateUniqueReservationId();
Reservation reservation = new Reservation(reservationId, selectedFlight, passengers, paymentInfo);
reservations.add(reservation);
// Update available seats
selectedFlight.getAvailableSeatsByClass().put("Economy", availableSeats - totalPassengers);
return reservation;
} else {
System.out.println("Not enough available seats for the selected flight.");
}
} else {
System.out.println("Flight not found.");
}
return null;
}
/**
* Finds a flight by its unique identifier.
*
* @param flightId The unique identifier for the flight.
* @return The matching Flight object, or null if not found.
*/
Flight findFlightById(String flightId) {
for (Flight flight : availableFlights) {
if (flight.getFlightId().equals(flightId)) {
return flight;
}
}
return null; // Flight not found
}
/**
* Generates a unique reservation identifier (e.g., using UUID).
*
* @return A unique reservation identifier.
*/
private String generateUniqueReservationId() {
// You can use java.util.UUID.randomUUID() to generate a unique ID
return UUID.randomUUID().toString();
}
}
/**
* Represents a flight available for booking.
*/
class Flight {
private String flightId;
private String departureAirportCode;
private String arrivalAirportCode;
private LocalDateTime departureDateTime;
private LocalDateTime arrivalDateTime;
private String airline;
private Map<String, Integer> availableSeatsByClass;
Flight(String flightId, String departureAirportCode, String arrivalAirportCode,
LocalDateTime departureDateTime, LocalDateTime arrivalDateTime, String airline,
Map<String, Integer> availableSeatsByClass) {
this.flightId = flightId;
this.departureAirportCode = departureAirportCode;
this.arrivalAirportCode = arrivalAirportCode;
this.departureDateTime = departureDateTime;
this.arrivalDateTime = arrivalDateTime;
this.airline = airline;
this.availableSeatsByClass = availableSeatsByClass;
}
/**
* Gets the unique identifier for the flight.
*
* @return The flight's unique identifier.
*/
String getFlightId() {
return flightId;
}
/**
* Gets the departure airport code for the flight.
*
* @return The departure airport code.
*/
String getDepartureAirportCode() {
return departureAirportCode;
}
/**
* Gets the arrival airport code for the flight.
*
* @return The arrival airport code.
*/
String getArrivalAirportCode() {
return arrivalAirportCode;
}
/**
* Gets the departure date and time for the flight.
*
* @return The departure date and time.
*/
LocalDateTime getDepartureDateTime() {
return departureDateTime;
}
/**
* Gets the arrival date and time for the flight.
*
* @return The arrival date and time.
*/
LocalDateTime getArrivalDateTime() {
return arrivalDateTime;
}
/**
* Gets the airline name for the flight.
*
* @return The airline name.
*/
String getAirline() {
return airline;
}
/**
* Gets the flight's available seats in different travel classes.
*
* @return A map where keys are travel class names (e.g., "Economy") and values
* are available seat counts.
*/
Map<String, Integer> getAvailableSeatsByClass() {
return availableSeatsByClass;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(flightId).append(" - ").append(airline).append(" - ").append(departureAirportCode)
.append(" - ").append(arrivalAirportCode).append(" - ").append(departureDateTime)
.append(" - ").append(arrivalDateTime).append("\n");
for (Map.Entry<String, Integer> entry : availableSeatsByClass.entrySet()) {
sb.append(entry.getKey()).append(": ").append(entry.getValue()).append(" seats available\n");
}
return sb.toString();
}
// Other flight-related properties and methods
}
/**
* Represents a passenger with personal information.
*/
class Passenger {
private String name;
private String email;
private String phone;
Passenger(String name, String email, String phone) {
this.name = name;
this.email = email;
this.phone = phone;
}
/**
* Gets the name of the passenger.
*
* @return The passenger's name.
*/
String getName() {
// Implement getter method
return name;
}
/**
* Sets the name of the passenger.
*
* @param name The passenger's name.
*/
void setName(String name) {
this.name = name;
}
/**
* Gets the email address of the passenger.
*
* @return The passenger's email address.
*/
String getEmail() {
return email;
}
/**
* Sets the email address of the passenger.
*
* @param email The passenger's email address.
*/
void setEmail(String email) {
this.email = email;
}
/**
* Gets the phone number of the passenger.
*
* @return The passenger's phone number.
*/
String getPhone() {
return phone;
}
/**
* Sets the phone number of the passenger.
*
* @param phone The passenger's phone number.
*/
void setPhone(String phone) {
this.phone = phone;
}
// Other passenger-related properties and methods
}
/**
* Represents payment information for a reservation.
*/
class PaymentInfo {
private String creditCardNumber;
private String expirationDate;
PaymentInfo(String creditCardNumber, String expirationDate) {
this.creditCardNumber = creditCardNumber;
this.expirationDate = expirationDate;
}
/**
* Gets the credit card number used for payment.
*
* @return The credit card number.
*/
String getCreditCardNumber() {
return creditCardNumber;
}
/**
* Sets the credit card number used for payment.
*
* @param creditCardNumber The credit card number.
*/
void setCreditCardNumber(String creditCardNumber) {
this.creditCardNumber = creditCardNumber;
}
/**
* Gets the credit card expiration date.
*
* @return The credit card expiration date.
*/
String getExpirationDate() {
return expirationDate;
}
/**
* Sets the credit card expiration date.
*
* @param expirationDate The credit card expiration date.
*/
void setExpirationDate(String expirationDate) {
this.expirationDate = expirationDate;
}
// Other payment-related properties and methods
}
/**
* Represents a flight reservation confirmation.
*/
class Reservation {
private String reservationId;
private Flight flight;
private List<Passenger> passengers;
private PaymentInfo paymentInfo;
Reservation(String reservationId, Flight flight, List<Passenger> passengers, PaymentInfo paymentInfo) {
this.reservationId = reservationId;
this.flight = flight;
this.passengers = passengers;
this.paymentInfo = paymentInfo;
}
/**
* Gets the unique identifier for the reservation.
*
* @return The reservation's unique identifier.
*/
String getReservationId() {
return reservationId;
}
/**
* Gets the flight that is being reserved.
*
* @return The flight that is being reserved.
*/
Flight getFlight() {
return flight;
}
/**
* Gets the passengers on the reservation.
*
* @return The passengers on the reservation.
*/
List<Passenger> getPassengers() {
return passengers;
}
/**
* Gets the payment information for the reservation.
*
* @return The payment information for the reservation.
*/
PaymentInfo getPaymentInfo() {
return paymentInfo;
}
@Override
public String toString() {
StringBuilder passengerNames = new StringBuilder();
for (Passenger passenger : passengers) {
passengerNames.append(passenger.getName()).append("\n");
}
return String.format("Reservation ID: %s\nFlight ID: %s\nPassengers:\n%s",
reservationId, flight.getFlightId(), passengerNames.toString());
}
// Other reservation-related properties and methods
}