OOP-Projects-Java

Log | Files | Refs | README

commit d104d25283589db6f16beef6ebcb5acc7bddb02f
parent 58103506a41fb3fcc7086879b918e917dc1e9b44
Author: William Lindholm <William_Lindholm@outlook.com>
Date:   Mon, 20 Feb 2023 20:34:05 +0100

Fixed more bugs

Diffstat:
MOOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeMaker.java | 13+++++++++----
MOOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Worker.java | 22++++++++++++++++------
2 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeMaker.java b/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeMaker.java @@ -14,6 +14,8 @@ public class CoffeeMaker extends Thread { private static int timeScale = 1; // used to change speed of simulation (default = 1). 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 int coffeBufferSize = 20; //the max amount of coffees the coffeemaker can store. + private int maxWorkerEnergy = 100; //do not give worker more coffee if it exceeds this number. private Timer timer; private Random random; @@ -28,8 +30,8 @@ public class CoffeeMaker extends Thread { */ private TimerTask createCoffee = new TimerTask() { public void run() { - //only create a new coffee if there are less than 20 coffee in the machine. - if ( coffeeBuffer.size() < 20) { + //only create a new coffee if there are less than x coffees in the machine. + if ( coffeeBuffer.size() < coffeBufferSize) { Coffee coffee = generateRandomCoffee(); coffeeBuffer.add(coffee); } @@ -50,8 +52,11 @@ public class CoffeeMaker extends Thread { if (coffeeQueue.getSize() != 0 && coffeeBuffer.size() != 0) { Worker worker = coffeeQueue.deQueue(); Coffee coffee = coffeeBuffer.remove(); - worker.drink(coffee); - System.out.println(worker.getWorkerName() + " enjoyed a " + coffee.getType() + " with " + coffee.getEnergy() + " energy"); + + if (worker.getEnergy() < maxWorkerEnergy ) { + worker.drink(coffee); + System.out.println(worker.getWorkerName() + " enjoyed a " + coffee.getType() + " with " + coffee.getEnergy() + " energy"); + } } } }; diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Worker.java b/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Worker.java @@ -25,6 +25,13 @@ public class Worker extends Thread { public void run() { energy--; + //if the worker has replenished energy, the worker should no longer be on break. + if (energy >= 100 && onBreak == true) { + System.out.println(name + " goes back to work with energy level " + energy); + onBreak = false; + } + + /* * The worker goes home if energy is at 0 or below. * The worker goes to get coffee if its energy level is below 30. @@ -40,12 +47,6 @@ public class Worker extends Thread { } else { System.out.println(name + " Is working with energy level " + energy); } - - //if the worker has replenished energy, the worker should no longer be on break. - if (energy >= 100 && onBreak == true) { - System.out.println(name + " goes back to work with energy level " + energy); - onBreak = false; - } } }; @@ -128,6 +129,15 @@ public class Worker extends Thread { /** + * Get the ammount of energy the worker currently has. + * @return energy as int. + */ + public int getEnergy() { + return this.energy; + } + + + /** * Override of equals function. * Only compare the names of the workers to determine if they are queuing. * This allows comparisons even if worker energy changes.