Advertisement
mmayoub

ServiveOffice, Office class

Aug 20th, 2017
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.28 KB | None | 0 0
  1. package ServicePkg;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.Queue;
  5. import java.util.Vector;
  6.  
  7. public class Office implements Runnable {
  8.     private Queue<Customer> customers;
  9.     private Vector<Employee> employees;
  10.     private Vector<Employee> waitingEmployees;
  11.  
  12.     private Thread myThread;
  13.     private boolean isOpen;
  14.  
  15.     public Office(int maxCapacity) {
  16.         this.customers = new LinkedList<Customer>();
  17.         this.employees = new Vector<Employee>();
  18.         myThread = new Thread(this);
  19.         this.isOpen = false;
  20.  
  21.         Utils.printLog("Closed : " + "Office is Closed.");
  22.     }
  23.  
  24.     public void open() {
  25.         // TODO Auto-generated method stub
  26.         this.isOpen = true;
  27.         myThread.start();
  28.         Utils.printLog("Opened : " + "Office was opened.");
  29.  
  30.         for (Employee employee : employees) {
  31.             employee.startWorking();
  32.         }
  33.     }
  34.  
  35.     public boolean isOpen() {
  36.         // TODO Auto-generated method stub
  37.         return this.isOpen;
  38.     }
  39.  
  40.     public void close() {
  41.         // TODO Auto-generated method stub
  42.         isOpen = false;
  43.         try {
  44.             Utils.printLog("Closing: " + "Office is being closed.");
  45.             myThread.join();
  46.             Utils.printLog("Closed : " + "Office was Closed.");
  47.         } catch (InterruptedException e) {
  48.             // TODO Auto-generated catch block
  49.             e.printStackTrace();
  50.         }
  51.  
  52.     }
  53.  
  54.     public void addEmployee() {
  55.         // TODO Auto-generated method stub
  56.         Employee newEmployee = new Employee(this);
  57.         employees.add(newEmployee);
  58.  
  59.         Utils.printLog("Add Emp: " + newEmployee.getEmployeeName()
  60.                 + " was added to office.");
  61.  
  62.         if (isOpen) {
  63.             newEmployee.startWorking();
  64.         }
  65.     }
  66.  
  67.     public void removeEmployee(String name) {
  68.         // TODO Auto-generated method stub
  69.         for (Employee employee : employees) {
  70.             if (employee.getEmployeeName().equals(name)) {
  71.                 employee.stopWorking();
  72.             }
  73.  
  74.         }
  75.     }
  76.  
  77.     public synchronized void addCustomer() {
  78.         // TODO Auto-generated method stub
  79.         Customer newCustomer = new Customer();
  80.         customers.add(newCustomer);
  81.         Utils.printLog("Arrive : " + newCustomer
  82.                 + " start waiting to get service.");
  83.  
  84.         boolean cont = true;
  85.         for (int i = 0; i < employees.size() && cont; i += 1) {
  86.             if (employees.get(i).isWaiting()) {
  87.                 employees.get(i).stopWaiting();
  88.                 cont = false;
  89.             }
  90.  
  91.         }
  92.  
  93.     }
  94.  
  95.     public synchronized Customer getNextCustomer() {
  96.         // TODO Auto-generated method stub
  97.  
  98.         // if (!customers.isEmpty()) {
  99.         Utils.printLog("Next Of: " + customers);
  100.         // }
  101.  
  102.         return customers.poll();
  103.     }
  104.  
  105.     public synchronized boolean hasCustomers() {
  106.         // TODO Auto-generated method stub
  107.         return !customers.isEmpty();
  108.     }
  109.  
  110.     @Override
  111.     public void run() {
  112.         // TODO Auto-generated method stub
  113.         while (isOpen) {
  114.             addCustomer();
  115.             try {
  116.                 int timeToNext = Utils.getRandomTimeToNextCustomer();
  117.                 Thread.sleep(timeToNext);
  118.             } catch (InterruptedException e) {
  119.                 // TODO Auto-generated catch block
  120.                 e.printStackTrace();
  121.             }
  122.         }
  123.  
  124.         Utils.printLog("Closing: new customers not allowed.");
  125.         Utils.printLog("Closing: waiting for employees to finish serving!");
  126.         for (Employee employee : employees) {
  127.             if (employee.isWorking()) {
  128.                 try {
  129.                     employee.stopWorking();
  130.                     employee.join();
  131.                 } catch (InterruptedException e) {
  132.                     // TODO Auto-generated catch block
  133.                     e.printStackTrace();
  134.                 }
  135.             }
  136.         }
  137.         Utils.printLog("All Out: " + "All employees finished and was stopped!");
  138.     }
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement