Advertisement
mmayoub

ServiveOffice, Employee class

Aug 20th, 2017
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 KB | None | 0 0
  1. package ServicePkg;
  2.  
  3. public class Employee extends Thread {
  4.     private static int employeeCounter = 0;
  5.  
  6.     private String employeeName;
  7.     private Office myOffice;
  8.  
  9.     private boolean isWorking;
  10.  
  11.     public Employee(Office myOffice) {
  12.         Employee.employeeCounter += 1;
  13.         this.employeeName = "Employee #" + Employee.employeeCounter;
  14.  
  15.         this.myOffice = myOffice;
  16.         isWorking = false;
  17.     }
  18.  
  19.     public String getEmployeeName() {
  20.         return this.employeeName;
  21.     }
  22.  
  23.     public boolean isWorking() {
  24.         return this.isWorking;
  25.     }
  26.  
  27.     public void startWorking() {
  28.         if (!isWorking) {
  29.             isWorking = true;
  30.             start();
  31.  
  32.             Utils.printLog("Working: " + employeeName + " start to work.");
  33.         }
  34.     }
  35.  
  36.     public synchronized void stopWorking() {
  37.         isWorking = false;
  38.         if (isWaiting()) {
  39.             stopWaiting();
  40.         }
  41.     }
  42.  
  43.     public synchronized boolean isWaiting() {
  44.         return getState() == Thread.State.WAITING;
  45.     }
  46.  
  47.     public void stopWaiting() {
  48.         if (isWaiting()) {
  49.             synchronized (this) {
  50.                 notify();
  51.             }
  52.         }
  53.     }
  54.  
  55.     @Override
  56.     public void run() {
  57.         while (isWorking) {
  58.             Utils.printLog("Calling: " + this.employeeName
  59.                     + " is calling for customer.");
  60.             Customer customer = this.myOffice.getNextCustomer();
  61.             if (customer != null) {
  62.                 int serviceTime = Utils.getRandomServiceTime();
  63.                 Utils.printLog("Served : " + customer
  64.                         + " started to get service from " + this.employeeName
  65.                         + " for " + serviceTime + "ms");
  66.  
  67.                 try {
  68.                     Thread.sleep(serviceTime);
  69.                 } catch (InterruptedException e) {
  70.                     // TODO Auto-generated catch block
  71.                     e.printStackTrace();
  72.                 }
  73.                 Utils.printLog("EndSrv : " + customer + " has finished. "
  74.                         + this.employeeName + " is free.");
  75.  
  76.             } else if (myOffice.isOpen()) {
  77.  
  78.                 // waiting for customers
  79.                 synchronized (this) {
  80.                     try {
  81.                         wait();
  82.                     } catch (InterruptedException e) {
  83.                         // TODO Auto-generated catch block
  84.                         e.printStackTrace();
  85.                     }
  86.                 }
  87.  
  88.             } else {
  89.                 this.stopWorking();
  90.             }
  91.  
  92.         }
  93.         Utils.printLog("No Work: " + this.employeeName + " stop working.");
  94.     }
  95.  
  96.     @Override
  97.     public String toString() {
  98.  
  99.         return this.employeeName;
  100.     }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement