OOP-Projects-Java

Log | Files | Refs | README

commit e05f57934d3476fb93221e91df543b3ea85ebb94
parent c3af871d09282901e60ecc82e39384ffbed4d47d
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Mon, 20 Feb 2023 18:19:07 +0100

restructuring

Diffstat:
MOOP2023_a22willi_a21liltr_assignment3/src/fika/CoffeeMaker.java | 39+++++++++++++++++++++++++++++++++++----
MOOP2023_a22willi_a21liltr_assignment3/src/fika/Worker.java | 27+++++++++++++++++++--------
2 files changed, 54 insertions(+), 12 deletions(-)

diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/fika/CoffeeMaker.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/CoffeeMaker.java @@ -11,9 +11,14 @@ public class CoffeeMaker extends Thread { private int timeToCreateCoffee = 2000; //the default time it takes the coffeemaker to create one coffee. private int timeToServeCoffee = 1000; //the default time it takes the coffeemaker to serve one cup of coffee. - private CoffeeQueue coffeeQueue; - private ConcurrentLinkedQueue<Coffee> coffeeBuffer = new ConcurrentLinkedQueue<Coffee>(); + private CoffeeQueue coffeeQueue; //queue for workers. + private ConcurrentLinkedQueue<Coffee> coffeeBuffer; //queue for coffee. + + /** + * TimerTask used for creating coffee. + * Each cup is added to the coffeeBuffer where a worker then can retrieve it. + */ private TimerTask createCoffee = new TimerTask() { public void run() { //only create a new coffee if there are less than 20 coffee in the machine. @@ -27,6 +32,12 @@ public class CoffeeMaker extends Thread { } }; + + /** + * Timertask serveCoffee. + * Serves the first worker in the queue with the next cup of coffee from the coffeeBuffer; + * a concurrentLinkedQueue containing all cups of coffee the machine has created. + */ private TimerTask serveCoffee = new TimerTask() { public void run() { if (coffeeQueue.getSize() != 0 && coffeeBuffer.size() != 0) { @@ -38,12 +49,32 @@ public class CoffeeMaker extends Thread { } }; - public CoffeeMaker(CoffeeQueue coffeeQueue) { - this.coffeeQueue = coffeeQueue; + + /** + * Run thread. + * Start serveCoffee and CreateCoffee schedules. + */ + public void run() { timer.scheduleAtFixedRate(createCoffee, timeToCreateCoffee / timeScale, timeToCreateCoffee / timeScale); timer.scheduleAtFixedRate(serveCoffee, timeToServeCoffee / timeScale, timeToServeCoffee / timeScale); } + + /** + * Constructor, requires variable coffeeQueue + * @param coffeeQueue The queue that workers use to interface with the coffeemaker. + */ + public CoffeeMaker(CoffeeQueue coffeeQueue) { + this.coffeeQueue = coffeeQueue; + this.coffeeBuffer = new ConcurrentLinkedQueue<Coffee>(); + } + + + /** + * Generate a random coffee. Each coffee gets a random energy value + * depending on type. + * @return Coffee Returns a coffee of random type. + */ private Coffee generateRandomCoffee() { Random r = new Random(); int coffeType = r.nextInt(0, 2); diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/fika/Worker.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/Worker.java @@ -39,13 +39,16 @@ public class Worker extends Thread { } }; + /** - * start queuing to get coffee. + * Run thread. + * Starts scheduled task. */ - private void Queue() { - coffeeQueue.enQueue(this); + public void run() { + timer.scheduleAtFixedRate(task, T / timeScale, T / timeScale); } + /** * Constructor * Creates a worker. @@ -57,8 +60,6 @@ public class Worker extends Thread { this.coffeeQueue = coffeeQueue; this.energy = r.nextInt(30, 90); //set the starting value of energy to a random int. this.T = r.nextInt(500, 1500); //set the duration between each iteration of task to a random int. - - timer.scheduleAtFixedRate(task, T / timeScale, T / timeScale); } /** @@ -73,10 +74,17 @@ public class Worker extends Thread { this.coffeeQueue = coffeeQueue; this.energy = energy; this.T = r.nextInt(500, 1500); - - timer.scheduleAtFixedRate(task, T / timeScale, T / timeScale); } + + /** + * start queuing to get coffee. + */ + private void Queue() { + coffeeQueue.enQueue(this); + } + + /** * Allows a worker to drink a cup of coffee * @param coffee the coffee to drink @@ -85,7 +93,10 @@ public class Worker extends Thread { this.energy += coffee.getEnergy(); } - + /** + * Get the workers name + * @return returns the workers name. + */ public String getWorkerName() { return this.name; }