-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlightManager.java
223 lines (203 loc) · 8.17 KB
/
FlightManager.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
import java.io.FileReader;
import java.util.*;
public class FlightManager {
//Creating the map
Map<String, Flight> flights = new TreeMap<>();
//Declaring and initializing the variables
String[] cities = {"Dallas", "New York", "London", "Paris", "Tokyo"};
final int DALLAS = 0;
final int NEWYORK = 1;
final int LONDON = 2;
final int PARIS = 3;
final int TOKYO = 4;
int[] flightTimes = {3, // Dallas
1, // New York
7, // London
8, // Paris
16,// Tokyo
};
//Creating the required arraylists
ArrayList<Aircraft> airplanes = new ArrayList<Aircraft>();
ArrayList<String> flightNumbers = new ArrayList<String>();
String errMsg = null;
Random random = new Random();
//FlightManager Constructor to read the text file and read the list of the flights from it and adds the information of the aircrafts
//Then the name of the airline is adjusted as per instruction
//Make the required LongHaulFlight and Flight objects and put them in the flight map
//FileNotFoundException if file is not found
public FlightManager() {
// Create some aircraft types with max seat capacities
airplanes.add(new Aircraft(112, "Boeing 737"));
airplanes.add(new Aircraft(116, "Airbus 320"));
airplanes.add(new Aircraft(48, "Dash-8 100"));
airplanes.add(new Aircraft(44, "Bombardier 5000"));
airplanes.add(new Aircraft(120, 20, "Boeing 747"));
try {
FileReader file = new FileReader("/Users/dibbyosaha/Assignment 2/src/flights.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNext()) {
String airlines = scanner.next();
String airlinesN = " ";
for (int i = 0; i < airlines.length(); i++) {
char current = airlines.charAt(i);
if (current == '_') {
airlinesN += " ";
} else {
airlinesN += airlines.charAt(i);
}
}
String destination = scanner.next();
String departure = scanner.next();
int passengerCapacity = scanner.nextInt();
String flightNum = generateFlightNumber(airlinesN);
int num = 0;
if (destination.equals("Dallas")) {
num = 0;
}
if (destination.equals("New York")) {
num = 1;
}
if (destination.equals("London")) {
num = 2;
}
if (destination.equals("Paris")) {
num = 3;
}
if (destination.equals("Tokyo")) {
num = 4;
}
Aircraft passenger1 = null;
for (int i = 0; i < airplanes.size(); i++) {
if (destination.equals("Tokyo")) {
passenger1 = airplanes.get(airplanes.size() - 1);
} else {
if (airplanes.get(i).getTotalSeats() >= passengerCapacity) {
passenger1 = airplanes.get(i);
}
}
}
if (destination.equals("Tokyo")) {
LongHaulFlight flight = new LongHaulFlight(flightNum, airlinesN, destination, departure, flightTimes[num], passenger1);
flights.put(flightNum, flight);
} else {
Flight flight = new Flight(flightNum, airlinesN, destination, departure, flightTimes[num], passenger1);
flights.put(flightNum, flight);
}
}
// throw new FileNotFoundException("File not found");
} catch (Exception e) {
System.out.println(e);
}
}
//Method that generates a flight number and ammends the flight name as per instructions
private String generateFlightNumber(String airline) {
String word1, word2;
Scanner scanner = new Scanner(airline);
word1 = scanner.next();
word2 = scanner.next();
String letter1 = word1.substring(0, 1);
String letter2 = word2.substring(0, 1);
letter1.toUpperCase();
letter2.toUpperCase();
// Generate random number between 101 and 300
boolean duplicate = false;
int flight = random.nextInt(200) + 101;
String flightNum = letter1 + letter2 + flight;
return flightNum;
}
public String getErrorMessage() {
return errMsg;
}
//Method that prints all the flights
public void printAllFlights() {
for (String key : flights.keySet())
System.out.println(flights.get(key));
}
//Method that checks if a flight is available
public boolean seatsAvailable(String flightNum, String seatType) {
boolean valid = false;
String a = null;
for (String key : flights.keySet()) {
Flight abc = flights.get(key);
String reqFlight = abc.getFlightNum();
if (flightNum.equals(reqFlight)) {
valid = true;
a = key;
}
}
if (!valid) {
errMsg = "Flight " + flightNum + " Not Found";
return false;
}
return flights.get(a).seatsAvailable(seatType);
}
// Method that is used as a part of reserving the flight
public Reservation reserveSeatOnFlight(String flightNum, String name, String passport, String seat) {
boolean valid = false;
String a = null;
for (String key : flights.keySet()) {
Flight abc = flights.get(key);
String reqFlight = abc.getFlightNum();
if (flightNum.equals(reqFlight)) {
valid = true;
a = key;
}
}
try{
if (!valid) {
// errMsg = "Flight " + flightNum + " Not Found";
// return null;
throw new FlightNotFoundException("Flight " + flightNum + " Not Found");
}
}catch(FlightNotFoundException e){
System.out.println(e);
}
Flight flight = flights.get(a);
String seatType = "ECO";
if (seat.indexOf("+") == seat.length() - 1) {
seatType = "FCL";
}
if (!flight.reserveSeat(name, passport, seatType, seat)) {
errMsg = flight.getErrorMessage();
return null;
}
// String seat1 = flight.getLastAssignedSeat();
return new Reservation(flightNum, flight.toString(), name, passport, seat, seatType);
}
//Method that is used to cancel a flight reservation
public void cancelReservation(Reservation res) {
boolean valid = false;
String a = null;
for (String key : flights.keySet()) {
Flight abc = flights.get(key);
String reqFlight = abc.getFlightNum();
if (res.getFlightNum() == reqFlight) {
valid = true;
a = key;
}
}
if (!valid) {
errMsg = "Flight " + res.getFlightNum() + " Not Found";
return;
}
Flight flight = flights.get(a);
flight.cancelSeat(res.name, res.passport, res.seatType);
}
//Method that compares two flight object by Departure
private class DepartureTimeComparator implements Comparator<Flight> {
public int compare(Flight a, Flight b) {
return a.getDepartureTime().compareTo(b.getDepartureTime());
}
}
//Method that compares two flight object by Duration
private class DurationComparator implements Comparator<Flight> {
public int compare(Flight a, Flight b) {
return a.getFlightDuration() - b.getFlightDuration();
}
}
//Method that prints all the aircrafts
public void printAllAircraft() {
for (Aircraft craft : airplanes)
craft.print();
}
}