-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlane.java
executable file
·81 lines (74 loc) · 3.86 KB
/
Plane.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
import java.util.concurrent.BlockingQueue;
public class Plane implements Runnable {
private String flightStatus;
private String cleaningRefuelingStatus;
private int flightNum;
private BlockingQueue<Runway> runways = null; //initialization
private BlockingQueue<Gate> gates = null;
private Runway runway; //for getting
private Gate gate;
private Boolean random;
//Constructor . getting values from main
public Plane (int flightNum, Boolean random, BlockingQueue<Runway> runways, BlockingQueue<Gate> gates) {
this.flightNum = flightNum;
this.runways = runways;
this.gates = gates;
//checking whether true or false in random which was generated in main
if (random == false) {
flightStatus = "landing";
}else if (random == true) {
flightStatus = "taking off";
}
//printing after creation of a plane
System.out.println(java.time.LocalTime.now() +" Aircraft "+flightNum+" has reached the airport and is requesting to " + flightStatus +".");
random = !random; //switching random value for cleaning and refueling
if (random == true) {
cleaningRefuelingStatus = "Required";
}else {
cleaningRefuelingStatus = "not";
}
}
public void run() {
try {
runway = runways.take();
//System.out.println(java.time.LocalTime.now() + runway.getName() + " is being utilized by aircraft " + flightNum + ".");
System.out.println(java.time.LocalTime.now() + " The aircraft " + flightNum + " will be " + flightStatus + " on " + runway.getName()+".");
if (flightStatus == "landing") {
Thread.sleep(5000); // 5 seconds
System.out.println(java.time.LocalTime.now() + " Aircraft " + flightNum + " has be landed and " + runway.getName() + " has been freed."); //printing after status ended..
//runways.add(runway); // giving back runway or freeing it for other planes
//need to assign gate -------
try { //assigning gate to the flights that the landing status
gate = gates.take();
runways.add(runway); // giving back runway or freeing it for other planes
//System.out.println(java.time.LocalTime.now() + " The Gate " + gate.getName() + " is being utilized by " + flightNum + "");
System.out.println(java.time.LocalTime.now() + " The Passengers of aircraft " + flightNum + " are disembarking " + "at the " + gate.getName() + ".");
if (cleaningRefuelingStatus == "Required") {
System.out.println(java.time.LocalTime.now() + " Aircraft " + flightNum + " is getting refueled and cleaned" + " at the " + gate.getName() + ".");
Thread.sleep(5000); // Putting thread to sleep to mimic cleaning and refueling - 5 seconds
}
System.out.println(java.time.LocalTime.now() + " The Passengers of have embarked on Aircraft " + flightNum + " at the " + gate.getName() + " and requesting take-off.");
//System.out.println(java.time.LocalTime.now() +" This aircraft in gate -> " + gate.getName() + "is requesting runway!"); // try to take runway
try { //taking off after getting gate
runway = runways.take();
gates.add(gate); // free up the gate
//System.out.println(java.time.LocalTime.now() + " The Runway " + runway.getName() + " is being utilized by " + flightNum + ".");
Thread.sleep(3000); // take off time - 3 seconds
System.out.println(java.time.LocalTime.now() + " Aircraft " + flightNum + " has taken off at " + runway.getName() + " and freed it.");
runways.add(runway);
}catch(InterruptedException e) {
e.printStackTrace();
}
}catch(InterruptedException e) {
e.printStackTrace();
}
} else if (flightStatus == "taking off") {
Thread.sleep(3000);
System.out.println(java.time.LocalTime.now() + " Aircraft " + flightNum + " has taken off at " + runway.getName() + " and freed it."); //printing after status ended..
runways.add(runway); // giving back runway or freeing it for other planes
}
}catch(InterruptedException e){
e.printStackTrace();
}
}
}