OOP-Projects-Java

Log | Files | Refs | README

Worker.java (3974B)


      1 package breakRoom;
      2 
      3 import java.util.Random;
      4 import java.util.Timer;
      5 import java.util.TimerTask;
      6 
      7 import coffee.Coffee;
      8 
      9 public class Worker extends Thread {
     10 	private static final int timeScale = 1; // used to change speed of simulation (default = 1)
     11 
     12 	private String name;
     13 	private int energy;
     14 	private Boolean onBreak;
     15 	private Random r = new Random();
     16 	private int T; // delay used between every iteration.
     17 	private Timer timer = new Timer();
     18 	private CoffeeQueue coffeeQueue;
     19 
     20 	/**
     21 	 * Timertask, runs periodically to determine how much energy the worker has and
     22 	 * to determine the workers next move.
     23 	 */
     24 	private TimerTask task = new TimerTask() {
     25 		public void run() {
     26 			setEnergy(getEnergy() -1);
     27 
     28 			// if the worker has replenished energy, the worker should no longer be on
     29 			// break.
     30 			if (energy >= 100 && onBreak) {
     31 				System.out.println(name + " goes back to work with energy level " + energy);
     32 				onBreak = false;
     33 			}
     34 
     35 			/*
     36 			 * The worker goes home if energy is at 0 or below. The worker goes to get
     37 			 * coffee if its energy level is below 30. Otherwise keep working.
     38 			 */
     39 			if (energy <= 0) {
     40 				System.out.println(name + " is going home with energy level " + energy);
     41 				deQueue();
     42 				cancel();
     43 			} else if (energy < 30 || onBreak) {
     44 				onBreak = true;
     45 				System.out.println(name + " is taking a break with energy level " + energy);
     46 				Queue();
     47 			} else {
     48 				System.out.println(name + " Is working with energy level " + energy);
     49 			}
     50 		}
     51 	};
     52 
     53 	/**
     54 	 * Run thread. Starts scheduled task.
     55 	 */
     56 	public void run() {
     57 		timer.scheduleAtFixedRate(task, T / timeScale, T / timeScale);
     58 	}
     59 
     60 	/**
     61 	 * Used to cancel the scheduled task.
     62 	 */
     63 	public void cancel() {
     64 		task.cancel();
     65 	}
     66 
     67 	/**
     68 	 * Constructor Creates a worker.
     69 	 * 
     70 	 * @param name        Name of the worker.
     71 	 * @param coffeeQueue The queue for getting coffee.
     72 	 */
     73 	public Worker(String name, CoffeeQueue coffeeQueue) {
     74 		this.name = name;
     75 		this.coffeeQueue = coffeeQueue;
     76 		this.onBreak = false;
     77 		this.energy = r.nextInt(30, 90); // set the starting value of energy to a random int.
     78 		this.T = r.nextInt(500, 1500); // set the duration between each iteration of task to a random int.
     79 	}
     80 
     81 	/**
     82 	 * Constructor Creates a worker.
     83 	 * 
     84 	 * @param name        Name of the worker.
     85 	 * @param coffeeQueue The queue for getting coffee.
     86 	 * @param energy      The ammount of energy the worker has.
     87 	 */
     88 	public Worker(String name, CoffeeQueue coffeeQueue, int energy) {
     89 		this.name = name;
     90 		this.coffeeQueue = coffeeQueue;
     91 		this.energy = energy;
     92 		this.T = r.nextInt(500, 1500);
     93 	}
     94 
     95 	/**
     96 	 * start queuing to get coffee. NOTE: only adds the same worker once.
     97 	 */
     98 	private void Queue() {
     99 		if (!(coffeeQueue.inQueue(this))) {
    100 			coffeeQueue.enQueue(this);
    101 		}
    102 	}
    103 
    104 	/**
    105 	 * Remove worker from queue.
    106 	 */
    107 	private void deQueue() {
    108 		if (coffeeQueue.inQueue(this)) {
    109 			coffeeQueue.deQueue(this);
    110 		}
    111 	}
    112 
    113 	/**
    114 	 * Allows a worker to drink a cup of coffee
    115 	 * 
    116 	 * @param coffee the coffee to drink
    117 	 */
    118 	public synchronized void drink(Coffee coffee) {
    119 		this.energy += coffee.getEnergy();
    120 	}
    121 
    122 	/**
    123 	 * Get the workers name
    124 	 * 
    125 	 * @return returns the workers name.
    126 	 */
    127 	public String getWorkerName() {
    128 		return this.name;
    129 	}
    130 
    131 	/**
    132 	 * Get the ammount of energy the worker currently has.
    133 	 * 
    134 	 * @return energy as int.
    135 	 */
    136 	public synchronized int getEnergy() {
    137 		return this.energy;
    138 	}
    139 	
    140 	public synchronized void setEnergy(int energy) {
    141 		this.energy = energy;
    142 	}
    143 
    144 	/**
    145 	 * Override of equals function. Only compare the names of the workers to
    146 	 * determine if they are queuing. This allows comparisons even if worker energy
    147 	 * changes.
    148 	 * 
    149 	 * NOTE: since it only compares names to know if worker is in coffeeQueue, no
    150 	 * two workers can have the same name.
    151 	 */
    152 	@Override
    153 	public boolean equals(Object object) {
    154 		boolean isEqual = false;
    155 
    156 		if (object != null && object instanceof Worker) {
    157 			isEqual = this.name == ((Worker) object).name;
    158 		}
    159 
    160 		return isEqual;
    161 	}
    162 }