Repository navigation
Expand file tree
/
Copy pathPlane.java
More file actions
264 lines (203 loc) · 7.28 KB
/
Copy pathPlane.java
File metadata and controls
264 lines (203 loc) · 7.28 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
import java.util.Random;
public class Plane implements Runnable {
private final String planeName;
private final ATC atc;
private double fuelLevel;
private final boolean isEmergency;
private final Random rand = new Random();
private final int numPassengerDisembark;
private final int numPassengerEmbark;
// Ensure if they finish, they release it
private final Object refuelLock = new Object();
private boolean waitingForLanding = false;
private boolean canLand = false;
private boolean waitingForTakeOff = false;
private boolean canTakeOff = false;
private boolean waitingForRefueling = false;
private boolean refuelingCompleted = false;
public Plane(int id, ATC atc) {
this.planeName = "Plane-" + id;
this.atc = atc;
// Fixed the fifth plane encountered emergency situation
if (id == 5) {
this.fuelLevel = 5.0 + rand.nextDouble() * 5.0; // Low fuel level
this.isEmergency = true;
System.out.println(planeName + " created with fuel level: " + String.format("%.1f", fuelLevel) + "% \u001B[31m(Emergency)\u001B[0m");
}
else {
this.fuelLevel = 20.0 + rand.nextDouble() * 40.0;
this.isEmergency = false;
System.out.println(planeName + " created with fuel level: " + String.format("%.1f", fuelLevel) + "%");
}
// Random generate passengers
this.numPassengerDisembark = 30 + rand.nextInt(21);
this.numPassengerEmbark = 30 + rand.nextInt(21);
}
public String getName() {
return planeName;
}
public boolean isEmergency() {
return isEmergency;
}
public double getFuelLevel() {
return fuelLevel;
}
public int getnumPassengerDisembark() {
return numPassengerDisembark;
}
public int getnumPassengerEmbark() {
return numPassengerEmbark;
}
// Just waiting, not landing yet
public synchronized boolean waitingLandApproval() {
return waitingForLanding && !canLand;
}
// Approved from atc
public synchronized void availableToLand() {
canLand = true;
}
// Just waiting, not takeoff yet
public synchronized boolean waitingTakeOffApproval() {
return waitingForTakeOff && !canTakeOff;
}
// Approved from atc
public synchronized void approveAndStartTakeOff() {
canTakeOff = true;
}
public synchronized boolean waitingForRefueling() {
return waitingForRefueling && !refuelingCompleted;
}
public void refuelingCompleted() {
synchronized (refuelLock) {
refuelingCompleted = true;
fuelLevel = 100.0; // Update fuel level
refuelLock.notifyAll();
}
}
@Override
public void run() {
try {
// First step
requestLanding();
// Second step
land();
// Find and assign gate
Gate assignedGate = getAssignGateNumber();
if (assignedGate == null) {
System.out.println(planeName + ": ERROR - No gate available!");
return;
}
// third step
coastToGate(assignedGate);
// fourth step
dockAtGate(assignedGate);
// fifth step
notifyGroundServices(assignedGate);
waitForGroundOperations(assignedGate);
// sixth step
undockAndCoast(assignedGate);
// Eighth step
requestTakeOff();
// Ninth step
takeoff();
// For statistical
atc.getAirport().incrementPlanesServed();
atc.getAirport().countPassengerServed(numPassengerDisembark);
atc.getAirport().countPassengerBoarded(numPassengerEmbark);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
private void requestLanding() throws InterruptedException {
long waitStart = System.currentTimeMillis();
// Add to queue
atc.addLandingQueue(this);
// Wait atc approve
synchronized (this) {
waitingForLanding = true;
while (!canLand) {
wait();
}
waitingForLanding = false;
}
long waitTime = System.currentTimeMillis() - waitStart;
atc.getAirport().recordWaitingTime(waitTime);
}
private void land() throws InterruptedException {
// lock runway
synchronized (atc.getAirport().getRunwayLock()) {
// For statistical
atc.getAirport().enterAirport();
System.out.println(planeName + ": Landing.");
Thread.sleep(2000);
System.out.println(planeName + ": Landed.");
Thread.sleep(1000);
// Notify atc
atc.planeLanded(this);
}
}
private Gate getAssignGateNumber() {
return atc.assignGateNumber(this);
}
private void coastToGate(Gate gate) throws InterruptedException {
System.out.println(planeName + ": Coasting to Gate-" + gate.getGateNumber() + ".");
Thread.sleep(1000);
}
private void dockAtGate(Gate gate) throws InterruptedException {
System.out.println(planeName + ": Docked at Gate-" + gate.getGateNumber() + ".");
Thread.sleep(500);
}
private void notifyGroundServices(Gate gate) {
// Notify Gate to start disembark + cleaning
gate.openGate(this);
// Wait atc assign
synchronized (this) {
waitingForRefueling = true;
}
atc.requestRefueling(this);
}
private void waitForGroundOperations(Gate gate) throws InterruptedException {
// Waiting finish disembark & clean & embark
while (!gate.operationCompleted()) {
Thread.sleep(100);
}
// Wait until refuel done
synchronized (refuelLock) {
while (!refuelingCompleted) {
refuelLock.wait();
}
}
}
private void undockAndCoast(Gate gate) throws InterruptedException {
System.out.println(planeName + ": Undocking from Gate-" + gate.getGateNumber() + ".");
Thread.sleep(500);
// Set gate available
gate.release();
System.out.println(planeName + ": Coasting to runway.");
Thread.sleep(1000);
}
private void requestTakeOff() throws InterruptedException {
// Add to queue
atc.addTakeOffQueue(this);
// Wait atc approve
synchronized (this) {
waitingForTakeOff = true;
while (!canTakeOff) {
wait();
}
waitingForTakeOff = false;
}
}
private void takeoff() throws InterruptedException {
// Lock runway
synchronized (atc.getAirport().getRunwayLock()) {
System.out.println(planeName + ": Taking-off.");
Thread.sleep(2000);
System.out.println(planeName + ": Departed.");
// For statistical
atc.getAirport().exitAirport();
atc.planeDeparted(this);
}
}
}