-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlightManager.java
More file actions
204 lines (170 loc) · 8.64 KB
/
FlightManager.java
File metadata and controls
204 lines (170 loc) · 8.64 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
//Sina Pahlavan
//501 034 271
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class FlightManager
{
TreeMap<String,Flight> flightsMap = new TreeMap<String,Flight>();//arraylist to hold flights
// Contains list of available airplane types and their seat capacity
ArrayList<Aircraft> airplanes = new ArrayList<Aircraft>();
Random random = new Random(); // random number generator - google "Java class Random". Use this in generateFlightNumber
public FlightManager() throws FileNotFoundException {
airplanes.add(new Aircraft(4,0,"Aircraft with 4 seats"));
airplanes.add(new Aircraft(16,0,"Aircraft with 16 seats"));
airplanes.add(new Aircraft(20, 0,"Boeing 737"));
airplanes.add(new Aircraft(40,0,"Airbus 320"));
airplanes.add(new Aircraft(60, 0,"Dash-8 100"));
airplanes.add(new Aircraft(80, 0,"Bombardier 5000"));
airplanes.add(new Aircraft(100, 12, "Boeing 747"));//this is the aircraft for the Tokyo flight
//that's why it has 12 first class seats
// Create some aircraft types with max seat capacities
flightsMap = new TreeMap<String,Flight>();
try{//getting the flights from the file
File file = new File("flights.txt");
Scanner sc = new Scanner(file);
while (sc.hasNextLine()){
String line = sc.nextLine();
String[] words = line.split("\\W+");
Flight flightToAdd;
String airline = words[0];//the airline is always the first string
String flightnum = generateFlightNumber(airline);//this method will generate a random flightnumber
String dest = words[1];//destination is always the second string
String departure = words[2];//departure is the third string
int minSeats = Integer.parseInt(words[3]);//converting the minimum seats to an integer
int duration = Integer.parseInt(words[4]);//converting the duration to an integer
Aircraft aircraft = new Aircraft(0,0,"");
for (Aircraft plane:airplanes){
if (plane.getTotalSeats()>=minSeats){//if a plane has more seats or equal seats than the minimum seats
//we will give that flight that airplane
aircraft = plane;
break;
}
}
//creating the flightsMap
if (dest.equals("Tokyo")){//the flight going to tokyo requires to be a long haul
flightsMap.put(flightnum,new LongHaulFlight(flightnum,airline,dest,departure,duration,aircraft));
}
else{//if the flight is not going to Tokyo, it can be a regular flight
flightsMap.put(flightnum,new Flight(flightnum,airline,dest,departure,duration,aircraft));
}
}
}
catch(FileNotFoundException e){
System.out.println(e.getMessage());
}
}
/*
* This private helper method generates and returns a flight number string from the airline name parameter
* For example, if parameter string airline is "Air Canada" the flight number should be "ACxxx" where xxx is
* a random 3 digit number between 101 and 300 (Hint: use class Random - see variable random at top of class)
* you can assume every airline name is always 2 words.
*
*/
private String generateFlightNumber(String airline)
{
/**
* @ param : String airline
* Given an airline, we generate a random number beween 101 and 300
* Then, we will add that number to the two letter short version of the airline and return a string as the flight number
*/
// Your code here
int randomNum = 101 + (int)(Math.random() * 201);//generating a random flight number between 101 and 300
//we have to add 101 to a random number from 0 to 200
String flightnumrand = Integer.toString(randomNum);//we turn the random number to a string so that we can
//concacenate the string
if (airline.equals("Air_Canada")){
return "AC" + flightnumrand;
}
else if (airline.equals("United_Airlines")) {
return "UA" + flightnumrand;
}
else if (airline.equals("Porter_Airlines")){
return "PA" + flightnumrand;
}
return null;
}
// Prints all flights in flights array list (see class Flight toString() method)
// This one is done for you!
public void printAllFlights()
{
/**
* print all flights by going through the flightsMap treemap and printing the string version of each flight
*/
for (String key : flightsMap.keySet()){
System.out.println(flightsMap.get(key).toString());
}
}
// Given a flight number (e.g. "UA220"), check to see if there are economy seats available
// if so return true, if not return false
public void printSeat(String flightNum) throws FlightNotFoundException{
boolean found = false;
for (String key : flightsMap.keySet()){
if (flightsMap.get(key).flightNum.equals(flightNum)){
flightsMap.get(key).printSeats();
found =true;//if we find a flight, we print its seats and set found to true so that
//we will escape the exception thrown
}
}
if (found == false){//throws a flightNum not found exception using a variable foud
throw new FlightNotFoundException("Could not Find Flight: " + flightNum);
}
}
// Given a flight number string flightNum and a seat type, reserve a seat on a flight
// If successful, return a Reservation object
// NOTE: seat types are not used for basic Flight objects (seats are all "Economy Seats")
// class LongHaulFlight defines two seat types
// I suggest you first write this method *without* considering class LongHaulFlight
// once that works (test it!!), add the long haul flight code
public Reservation reserveSeatOnFlight(String flightNum,Passenger aPassenger) throws FlightHasNoFirstClassSeatsException, FlightNotFoundException,SeatOccupiedException,SeatDoesNotExistException,PassengerAlreadyOnFlightException {
/**
* @ param : String flightNum
* @ param : Passenger aPassenger
* if we can find the flight num and the flight does not contain that passenger, we will reserve a seat for them
* @ return : reservation
*/
if (flightsMap.containsKey(flightNum)){//getting the a passenger object using a flight number
flightsMap.get(flightNum).reserveSeat(aPassenger);//using the reserve seat method of each flight
return new Reservation(flightNum,aPassenger);//return a reservation that the flightReservation System can add
//to its arraylist of reservations
}
else{//throwing a flightnotfoundexception if we can't find the flight number in the tree
throw new FlightNotFoundException("Could not Find Flight " + flightNum);
}
}
/*
* Given a Reservation object, cancel the seat on the flight
*/
public void cancelReservation(String flightnum,Passenger aPassenger)throws FlightEmptyException,PassengerNotOnFlightException,FlightNotFoundException
{
// Get the flight number string from res
// Search flights to find the Flight object -
// if found, cancel the seat on the flight
Flight aflight;
boolean done = false;
for (String key:flightsMap.keySet()){
if (key.equals(flightnum)){
aflight = flightsMap.get(key);
aflight.cancelSeat(aPassenger);//we will find the flight using its flight num in the tree map
//then will call its cancel seat method and assign the passenger object as the paramter
done = true;
}
}
if (done==false){// if we get here, that means we couldn't find the flight. So we will throw the excpetion
throw new FlightNotFoundException("Could not find flight "+ flightnum);
}
}
public void printPassengersForAFlight(String flightnum) throws FlightNotFoundException,FlightEmptyException{
boolean found =false;
for (String str : flightsMap.keySet()){
if (str.equals(flightnum)){
flightsMap.get(str).printPassengers();
found = true;
}
}
if (found==false){
throw new FlightNotFoundException("Could not find flight: " + flightnum);
}
}
}