it keep shows plane 5 as plane 4
this is the output :
ATC: Plane 5 cleared for emergency landing.
Plane 5 has made an emergency landing.
Gate 1: Plane 4 docking.
Gate 1: Plane 4 disembarking passengers.
Gate 1: Plane 4 disembarking 41 passengers.
Gate 1: Plane 4 embarking passengers.
ATC: Plane 4 cleared for landing.
Plane 4 has landed.
Gate 2: Plane 5 docking.
Gate 2: Plane 5 disembarking passengers.
Gate 2: Plane 5 disembarking 5 passengers.
Gate 2: Plane 5 embarking passengers.
ATC class
import java.util.concurrent.*;
import java.util.*;
public class ATC {
private final Airport airport;
private final PriorityBlockingQueue<Plane> landingQueue = new PriorityBlockingQueue<>();
private final List<Plane> servedPlanes = Collections.synchronizedList(new ArrayList<>());
private int totalPassengersDisembarked = 0;
private int totalPassengersEmbarked = 0;
private final List<Long> waitingTimes = Collections.synchronizedList(new ArrayList<>());
public ATC(Airport airport) {
this.airport = airport;
}
public void requestLanding(Plane plane) throws InterruptedException {
if (plane.isEmergency()) {
System.out.println("ATC: Plane " + plane.getId() + " requesting emergency landing!");
} else {
System.out.println("ATC: Plane " + plane.getId() + " requesting landing.");
}
landingQueue.put(plane);
}
public void grantPermissionToLand(Plane plane) throws InterruptedException {
airport.getAirportCapacity().acquire();
Plane highestPriorityPlane = landingQueue.take(); // Fetch the highest priority plane
if (highestPriorityPlane.isEmergency()) {
System.out.println("ATC: Plane " + highestPriorityPlane.getId() + " cleared for emergency landing.");
} else {
System.out.println("ATC: Plane " + highestPriorityPlane.getId() + " cleared for landing.");
}
highestPriorityPlane.land();
}
public Gate dock(Plane plane) throws InterruptedException {
Gate gate = airport.getGateQueue().take();
gate.dock(plane);
return gate;
}
public void disembark(Plane plane, Gate gate) throws InterruptedException {
gate.disembark(plane);
totalPassengersDisembarked += plane.getPassengerCountDisembark();
System.out.println("Gate " + gate.getId() + ": Plane " + plane.getId() + " disembarking " + plane.getPassengerCountDisembark() + " passengers.");
}
public void embark(Plane plane, Gate gate) throws InterruptedException {
gate.embark(plane);
totalPassengersEmbarked += plane.getPassengerCountEmbark();
System.out.println("Gate " + gate.getId() + ": Plane " + plane.getId() + " embarking " + plane.getPassengerCountEmbark() + " passengers.");
}
public void refuel(Plane plane) throws InterruptedException {
airport.getRefuelingTruck().acquire();
new RefuelingTruck().refuel(plane);
airport.getRefuelingTruck().release();
}
public void undock(Plane plane, Gate gate) throws InterruptedException {
gate.undock(plane);
airport.getGateQueue().put(gate);
}
public void takeOff(Plane plane) throws InterruptedException {
airport.getAirportCapacity().release();
System.out.println("ATC: Plane " + plane.getId() + " taking off.");
servedPlanes.add(plane);
waitingTimes.add(plane.getWaitingTime());
}
public void printStatistics() {
System.out.println("nATC: Printing statistics.");
System.out.println("Planes served: " + servedPlanes.size());
System.out.println("Total passengers disembarked: " + totalPassengersDisembarked);
System.out.println("Total passengers embarked: " + totalPassengersEmbarked);
System.out.println("Maximum waiting time: " + Collections.max(waitingTimes) + " ms");
System.out.println("Minimum waiting time: " + Collections.min(waitingTimes) + " ms");
System.out.println("Average waiting time: " + waitingTimes.stream().mapToLong(Long::longValue).average().orElse(0.0) + " ms");
}
}
Plane class
import java.util.concurrent.CountDownLatch;
import java.util.Random;
public class Plane implements Runnable, Comparable<Plane> {
private final int id;
private final ATC atc;
private final boolean emergencyLanding;
private final CountDownLatch latch;
private final int passengerCountDisembark;
private final int passengerCountEmbark;
private long waitingStartTime;
private long waitingEndTime;
public Plane(int id, ATC atc, boolean emergencyLanding, CountDownLatch latch) {
this.id = id;
this.atc = atc;
this.emergencyLanding = emergencyLanding;
this.latch = latch;
this.passengerCountDisembark = new Random().nextInt(51); // Random number of passengers disembarking between 0 and 50
this.passengerCountEmbark = new Random().nextInt(51); // Random number of passengers embarking between 0 and 50
}
public void run() {
try {
requestLanding();
atc.grantPermissionToLand(this); // Pass the plane to grantPermissionToLand method
Gate gate = atc.dock(this);
atc.disembark(this, gate);
atc.embark(this, gate);
atc.refuel(this);
atc.undock(this, gate);
atc.takeOff(this);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Plane " + id + " was interrupted.");
} finally {
latch.countDown();
}
}
private void requestLanding() throws InterruptedException {
waitingStartTime = System.currentTimeMillis();
atc.requestLanding(this);
}
public void land() {
waitingEndTime = System.currentTimeMillis();
if (emergencyLanding) {
System.out.println("Plane " + id + " has made an emergency landing.");
} else {
System.out.println("Plane " + id + " has landed.");
}
}
public int getId() {
return id;
}
public boolean isEmergency() {
return emergencyLanding;
}
public int getPassengerCountDisembark() {
return passengerCountDisembark;
}
public int getPassengerCountEmbark() {
return passengerCountEmbark;
}
public long getWaitingTime() {
return waitingEndTime - waitingStartTime;
}
@Override
public int compareTo(Plane other) {
if (this.emergencyLanding && !other.emergencyLanding) return -1;
if (!this.emergencyLanding && other.emergencyLanding) return 1;
return Integer.compare(this.id, other.id);
}
}
Gate Class
public class Gate {
private final int id;
public Gate(int id) {
this.id = id;
}
public void dock(Plane plane) throws InterruptedException {
System.out.println("Gate " + id + ": Plane " + plane.getId() + " docking.");
Thread.sleep(1000); // Simulate docking time
}
public void disembark(Plane plane) throws InterruptedException {
System.out.println("Gate " + id + ": Plane " + plane.getId() + " disembarking passengers.");
Thread.sleep(2000); // Simulate disembarking time
}
public void embark(Plane plane) throws InterruptedException {
System.out.println("Gate " + id + ": Plane " + plane.getId() + " embarking passengers.");
Thread.sleep(2000); // Simulate embarking time
}
public void undock(Plane plane) throws InterruptedException {
System.out.println("Gate " + id + ": Plane " + plane.getId() + " undocking.");
Thread.sleep(1000); // Simulate undocking time
}
public int getId() {
return id;
}
}
yk1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.