Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ServicePkg;
- import java.util.LinkedList;
- import java.util.Queue;
- import java.util.Vector;
- public class Office implements Runnable {
- private Queue<Customer> customers;
- private Vector<Employee> employees;
- private Vector<Employee> waitingEmployees;
- private Thread myThread;
- private boolean isOpen;
- public Office(int maxCapacity) {
- this.customers = new LinkedList<Customer>();
- this.employees = new Vector<Employee>();
- myThread = new Thread(this);
- this.isOpen = false;
- Utils.printLog("Closed : " + "Office is Closed.");
- }
- public void open() {
- // TODO Auto-generated method stub
- this.isOpen = true;
- myThread.start();
- Utils.printLog("Opened : " + "Office was opened.");
- for (Employee employee : employees) {
- employee.startWorking();
- }
- }
- public boolean isOpen() {
- // TODO Auto-generated method stub
- return this.isOpen;
- }
- public void close() {
- // TODO Auto-generated method stub
- isOpen = false;
- try {
- Utils.printLog("Closing: " + "Office is being closed.");
- myThread.join();
- Utils.printLog("Closed : " + "Office was Closed.");
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void addEmployee() {
- // TODO Auto-generated method stub
- Employee newEmployee = new Employee(this);
- employees.add(newEmployee);
- Utils.printLog("Add Emp: " + newEmployee.getEmployeeName()
- + " was added to office.");
- if (isOpen) {
- newEmployee.startWorking();
- }
- }
- public void removeEmployee(String name) {
- // TODO Auto-generated method stub
- for (Employee employee : employees) {
- if (employee.getEmployeeName().equals(name)) {
- employee.stopWorking();
- }
- }
- }
- public synchronized void addCustomer() {
- // TODO Auto-generated method stub
- Customer newCustomer = new Customer();
- customers.add(newCustomer);
- Utils.printLog("Arrive : " + newCustomer
- + " start waiting to get service.");
- boolean cont = true;
- for (int i = 0; i < employees.size() && cont; i += 1) {
- if (employees.get(i).isWaiting()) {
- employees.get(i).stopWaiting();
- cont = false;
- }
- }
- }
- public synchronized Customer getNextCustomer() {
- // TODO Auto-generated method stub
- // if (!customers.isEmpty()) {
- Utils.printLog("Next Of: " + customers);
- // }
- return customers.poll();
- }
- public synchronized boolean hasCustomers() {
- // TODO Auto-generated method stub
- return !customers.isEmpty();
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- while (isOpen) {
- addCustomer();
- try {
- int timeToNext = Utils.getRandomTimeToNextCustomer();
- Thread.sleep(timeToNext);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- Utils.printLog("Closing: new customers not allowed.");
- Utils.printLog("Closing: waiting for employees to finish serving!");
- for (Employee employee : employees) {
- if (employee.isWorking()) {
- try {
- employee.stopWorking();
- employee.join();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- Utils.printLog("All Out: " + "All employees finished and was stopped!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement