Advertisement
mmayoub

Airport system, Airport class

Aug 25th, 2017
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. package AirportPkg;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.Queue;
  5.  
  6. public class Airport extends Thread {
  7.     private boolean isOpen;
  8.     private Queue<Airplane> waitingAirplanes;
  9.     private AirportCaller myCaller; // to get airplanes
  10.  
  11.     public Airport() throws InterruptedException {
  12.         isOpen = true;
  13.         waitingAirplanes = new LinkedList<Airplane>();
  14.  
  15.         Utils.printLog("Tower", "Airport started to work\n");
  16.         this.start();
  17.  
  18.         myCaller = new AirportCaller(this);
  19.     }
  20.  
  21.     public boolean isOpen() {
  22.         return this.isOpen;
  23.     }
  24.  
  25.     public void close() throws InterruptedException {
  26.         // stop getting new airplanes
  27.         myCaller.stopCalling();
  28.         myCaller.join();
  29.  
  30.         Utils.printLog("Tower",
  31.                 "* * * * * Airport is going to be closed. * * * * *\n");
  32.         Utils.printLog("Tower", "new calls are not allowed.");
  33.  
  34.         // close airport
  35.         isOpen = false;
  36.  
  37.         // stop waiting for airplanes
  38.         synchronized (this) {
  39.             this.notify();
  40.         }
  41.     }
  42.  
  43.     // airplane is calling the airport to land or take off
  44.     public synchronized void newCall(Airplane airplane) {
  45.         Utils.printLog("Call", airplane + " is calling the airport.");
  46.         waitingAirplanes.add(airplane);
  47.  
  48.         Utils.printLog("Tower", airplane
  49.                 + " Wellcome to airport. wait for permission.");
  50.         Utils.printLog("wait",
  51.                 airplane + " is waiting to " + airplane.getFinalState());
  52.  
  53.         if (waitingAirplanes.size() == 1) {
  54.             // now airport has waiting airplanes, stop waiting
  55.             synchronized (this) {
  56.                 this.notify();
  57.             }
  58.         }
  59.     }
  60.  
  61.     @Override
  62.     public void run() {
  63.         while (isOpen || !waitingAirplanes.isEmpty()) {
  64.             if (waitingAirplanes.isEmpty() && isOpen) {
  65.                 // airport is empty while its open
  66.                 // wait for new call from an airplane
  67.                 synchronized (this) {
  68.                     try {
  69.                         Utils.printLog("Tower",
  70.                                 "Airport is empty. waiting for airplanes . . .\n");
  71.                         this.wait();
  72.                     } catch (InterruptedException e) {
  73.                         // TODO Auto-generated catch block
  74.                         e.printStackTrace();
  75.                     }
  76.                 }
  77.             }
  78.  
  79.             Airplane activeAirplane = waitingAirplanes.poll();
  80.  
  81.             if (activeAirplane != null) {
  82.                 // giving permission to airplane
  83.                 synchronized (activeAirplane) {
  84.                     Utils.printLog("Tower",
  85.                             activeAirplane + ", you have permition to "
  86.                                     + activeAirplane.getFinalState());
  87.                     activeAirplane.notify();
  88.                     try {
  89.                         // wait for airplane to finish
  90.                         activeAirplane.join();
  91.                     } catch (InterruptedException e) {
  92.                         // TODO Auto-generated catch block
  93.                         e.printStackTrace();
  94.                     }
  95.                     Utils.printLog("Tower", "continue to next airplane . . .\n");
  96.                 }
  97.             }
  98.         }
  99.         Utils.printLog("Tower", "Airport is now empty.\n");
  100.         Utils.printLog("Tower", "Airport is now closed.");
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement