OOP-Projects-Java

Log | Files | Refs | README

CoffeeMaker.java (3598B)


      1 package breakRoom;
      2 
      3 import java.util.Random;
      4 import java.util.Timer;
      5 import java.util.TimerTask;
      6 import java.util.concurrent.ConcurrentLinkedQueue;
      7 
      8 import coffee.BlackCoffee;
      9 import coffee.Cappuccino;
     10 import coffee.Coffee;
     11 import coffee.Latte;
     12 
     13 public class CoffeeMaker extends Thread {
     14 	private static final int timeScale = 1; // used to change speed of simulation (default = 1).
     15 	private int timeToCreateCoffee = 2000; // the default time it takes the coffeemaker to create one coffee.
     16 	private int timeToServeCoffee = 1000; // the default time it takes the coffeemaker to serve one cup of coffee.
     17 	private int coffeBufferSize = 20; // the max amount of coffees the coffeemaker can store.
     18 	private int maxWorkerEnergy = 100; // do not give worker more coffee if it exceeds this number.
     19 
     20 	private Timer timer;
     21 	private Random random;
     22 
     23 	private CoffeeQueue coffeeQueue; // queue for workers.
     24 	private ConcurrentLinkedQueue<Coffee> coffeeBuffer; // queue for coffee.
     25 
     26 	/**
     27 	 * TimerTask used for creating coffee. Each cup is added to the coffeeBuffer
     28 	 * where a worker then can retrieve it.
     29 	 */
     30 	private TimerTask createCoffee = new TimerTask() {
     31 		public void run() {
     32 			// only create a new coffee if there are less than x coffees in the machine.
     33 			if (coffeeBuffer.size() < coffeBufferSize) {
     34 				Coffee coffee = generateRandomCoffee();
     35 				coffeeBuffer.add(coffee);
     36 			}
     37 
     38 			// announce how many coffees are available.
     39 			System.out.println("Coffee Machine has " + coffeeBuffer.size() + " drinks in reserve.");
     40 		}
     41 	};
     42 
     43 	/**
     44 	 * Timertask serveCoffee. Serves the first worker in the queue with the next cup
     45 	 * of coffee from the coffeeBuffer; a concurrentLinkedQueue containing all cups
     46 	 * of coffee the machine has created.
     47 	 */
     48 	private TimerTask serveCoffee = new TimerTask() {
     49 		public void run() {
     50 			if (coffeeQueue.getSize() != 0 && coffeeBuffer.size() != 0) {
     51 				Worker worker = coffeeQueue.deQueue();
     52 				Coffee coffee = coffeeBuffer.remove();
     53 
     54 				if (worker.getEnergy() < maxWorkerEnergy) {
     55 					worker.drink(coffee);
     56 					System.out.println(worker.getWorkerName() + " enjoyed a " + coffee.getType() + " with "
     57 							+ coffee.getEnergy() + " energy");
     58 				}
     59 			}
     60 		}
     61 	};
     62 
     63 	/**
     64 	 * Run thread. Start serveCoffee and CreateCoffee schedules.
     65 	 */
     66 	public void run() {
     67 		timer.scheduleAtFixedRate(createCoffee, timeToCreateCoffee / timeScale, timeToCreateCoffee / timeScale);
     68 		timer.scheduleAtFixedRate(serveCoffee, timeToServeCoffee / timeScale, timeToServeCoffee / timeScale);
     69 	}
     70 
     71 	/**
     72 	 * Used to cancel the scheduled tasks.
     73 	 */
     74 	public void cancel() {
     75 		createCoffee.cancel();
     76 		serveCoffee.cancel();
     77 	}
     78 
     79 	/**
     80 	 * Constructor, requires variable coffeeQueue
     81 	 * 
     82 	 * @param coffeeQueue The queue that workers use to interface with the
     83 	 *                    coffeemaker.
     84 	 */
     85 	public CoffeeMaker(CoffeeQueue coffeeQueue) {
     86 		this.coffeeQueue = coffeeQueue;
     87 		this.coffeeBuffer = new ConcurrentLinkedQueue<Coffee>();
     88 		this.timer = new Timer();
     89 		this.random = new Random();
     90 	}
     91 
     92 	/**
     93 	 * Generate a random coffee. Each coffee gets a random energy value depending on
     94 	 * type.
     95 	 * 
     96 	 * @return Coffee Returns a coffee of random type.
     97 	 */
     98 	private Coffee generateRandomCoffee() {
     99 		int coffeType = random.nextInt(1, 4);
    100 		int energy;
    101 
    102 		switch (coffeType) {
    103 		case 1:
    104 			energy = random.nextInt(25, 35);
    105 			return new BlackCoffee(energy);
    106 		case 2:
    107 			energy = random.nextInt(15, 20);
    108 			return new Latte(energy);
    109 		case 3:
    110 			energy = random.nextInt(20, 30);
    111 			return new Cappuccino(energy);
    112 		default:
    113 			System.out.println("Something went wrong");
    114 			return null;
    115 		}
    116 	}
    117 }