-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlightSchedulingTester.java
More file actions
114 lines (107 loc) · 5.22 KB
/
FlightSchedulingTester.java
File metadata and controls
114 lines (107 loc) · 5.22 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
import java.io.File;
import java.util.Scanner;
public class FlightSchedulingTester {
// Display the menu options
public static void menu() {
System.out.println("\nFlight Schedule Menu:");
System.out.println("1. Load the flight schedule from a file");
System.out.println("2. Add a flight to the schedule");
System.out.println("3. Remove a flight from the schedule");
System.out.println("4. Search for flights");
System.out.println("5. Get flight details");
System.out.println("6. Save the updated flight schedule as a text file");
System.out.println("7. Print the flight summary");
System.out.println("8. Exit");
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
FlightScheduler flightScheduler = new FlightScheduler();
int choice;
boolean exit = false;
// Main loop for user interaction
while (!exit) {
menu(); // Display the menu
// Get user choice
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
try {
switch (choice) {
case 1:
// Load flight schedule from a file
System.out.print("Enter filename: ");
String filename = scanner.next();
File file = new File(filename);
flightScheduler = new FlightScheduler(file);
System.out.println("Flight schedule loaded successfully.");
flightScheduler.getFlightsSummary();
break;
case 2:
// Add a flight to the schedule
System.out.print("Add flight: ");
String[] flightData1 = scanner.nextLine().split(", ");
flightScheduler.addFlight(flightData1[0], flightData1[1], flightData1[2], flightData1[3], flightData1[4]);
System.out.println("Flight added successfully.");
flightScheduler.getFlightsSummary();
break;
case 3:
// Remove a flight from the schedule
System.out.print("Remove flight: ");
flightScheduler.removeFlight(scanner.next());
System.out.println("Flight removed successfully.");
flightScheduler.getFlightsSummary();
break;
case 4:
// Search for flights
System.out.print("Search flights: ");
String[] flightData2 = scanner.nextLine().split(", ");
Flight[] flights = flightScheduler.searchFlights(flightData2[0], flightData2[1]);
System.out.print("Flights: ");
for(int i = 0; i < flights.length - 1; i++)
System.out.print(flights[i].getFlightNumber() + ", ");
System.out.println(flights[flights.length - 1].getFlightNumber());
flightScheduler.getFlightsSummary();
break;
case 5:
// Get details of a specific flight
System.out.print("Flight details: ");
Flight flight = flightScheduler.getFlightDetails(scanner.next());
System.out.println(flight);
flightScheduler.getFlightsSummary();
break;
case 6:
// Save the updated flight schedule to a file
System.out.print("Save Updated Schedule (Y/N): ");
if(scanner.next().equals("Y")) {
System.out.print("Enter filename: ");
flightScheduler.saveFlightSchedule(scanner.next());
System.out.println("Flight schedule saved successfully.");
}
else {
System.out.println("Flight schedule is not saved.");
}
break;
case 7:
// Print the flight summary
flightScheduler.getFlightsSummary();
break;
case 8:
// Exit the program
exit = true;
break;
default:
System.out.println("Invalid choice! Please try again.");
break;
}
} catch(FlightAlreadyExistsException e) {
System.out.println(e.getMessage());
} catch(FlightNotFoundException e) {
System.out.println(e.getMessage());
} catch(NoFlightsFoundException e) {
System.out.println(e.getMessage());
}
}
// Close the scanner
scanner.close();
}
}