I am making a simulator but it have some display error

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;
    }
}

New contributor

yk1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật