OOP-Projects-Java

Log | Files | Refs | README

commit 8a162638b1adbbe57b5429284452edaeb6a05bb4
parent dd2aae601e9588913a116d79e75b83343bc95c41
Author: William Lindholm <William_Lindholm@outlook.com>
Date:   Sat, 25 Feb 2023 12:21:35 +0100

Reformatted code using automatic code formatter

Diffstat:
MOOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeMaker.java | 103++++++++++++++++++++++++++++++++++++++-----------------------------------------
MOOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeQueue.java | 33++++++++++++++++-----------------
MOOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Main.java | 22+++++++++++-----------
MOOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Worker.java | 105++++++++++++++++++++++++++++++++++++-------------------------------------------
MOOP2023_a22willi_a21liltr_assignment3/src/coffee/BlackCoffee.java | 23+++++++++++++----------
MOOP2023_a22willi_a21liltr_assignment3/src/coffee/Cappuccino.java | 19+++++++++++--------
MOOP2023_a22willi_a21liltr_assignment3/src/coffee/Coffee.java | 4++--
MOOP2023_a22willi_a21liltr_assignment3/src/coffee/Latte.java | 18+++++++++++-------
8 files changed, 162 insertions(+), 165 deletions(-)

diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeMaker.java b/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeMaker.java @@ -12,78 +12,75 @@ import coffee.Latte; public class CoffeeMaker extends Thread { private static final 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 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; - - private CoffeeQueue coffeeQueue; //queue for workers. - private ConcurrentLinkedQueue<Coffee> coffeeBuffer; //queue for 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. + * 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 x coffees in the machine. - if ( coffeeBuffer.size() < coffeBufferSize) { + // 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); } - - //announce how many coffees are available. + + // announce how many coffees are available. System.out.println("Coffee Machine has " + coffeeBuffer.size() + " drinks in reserve."); } }; - - + /** - * 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. + * 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) { Worker worker = coffeeQueue.deQueue(); Coffee coffee = coffeeBuffer.remove(); - - if (worker.getEnergy() < maxWorkerEnergy ) { + + if (worker.getEnergy() < maxWorkerEnergy) { worker.drink(coffee); - System.out.println(worker.getWorkerName() + " enjoyed a " + coffee.getType() + " with " + coffee.getEnergy() + " energy"); + System.out.println(worker.getWorkerName() + " enjoyed a " + coffee.getType() + " with " + + coffee.getEnergy() + " energy"); } } } }; - - + /** - * Run thread. - * Start serveCoffee and CreateCoffee schedules. + * Run thread. Start serveCoffee and CreateCoffee schedules. */ public void run() { timer.scheduleAtFixedRate(createCoffee, timeToCreateCoffee / timeScale, timeToCreateCoffee / timeScale); timer.scheduleAtFixedRate(serveCoffee, timeToServeCoffee / timeScale, timeToServeCoffee / timeScale); } - - + /** - * Used to cancel the scheduled tasks. + * Used to cancel the scheduled tasks. */ public void cancel() { createCoffee.cancel(); serveCoffee.cancel(); } - - + /** * Constructor, requires variable coffeeQueue - * @param coffeeQueue The queue that workers use to interface with the coffeemaker. + * + * @param coffeeQueue The queue that workers use to interface with the + * coffeemaker. */ public CoffeeMaker(CoffeeQueue coffeeQueue) { this.coffeeQueue = coffeeQueue; @@ -91,30 +88,30 @@ public class CoffeeMaker extends Thread { this.timer = new Timer(); this.random = new Random(); } - - + /** - * Generate a random coffee. Each coffee gets a random energy value - * depending on type. - * @return Coffee Returns a coffee of random type. + * 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() { int coffeType = random.nextInt(1, 4); int energy; - - switch(coffeType) { - case 1: - energy = random.nextInt(25, 35); - return new BlackCoffee(energy); - case 2: - energy = random.nextInt(15, 20); - return new Latte(energy); - case 3: - energy = random.nextInt(20, 30); - return new Cappuccino(energy); - default: - System.out.println("Something went wrong"); - return null; + + switch (coffeType) { + case 1: + energy = random.nextInt(25, 35); + return new BlackCoffee(energy); + case 2: + energy = random.nextInt(15, 20); + return new Latte(energy); + case 3: + energy = random.nextInt(20, 30); + return new Cappuccino(energy); + default: + System.out.println("Something went wrong"); + return null; } } } diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeQueue.java b/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeQueue.java @@ -4,57 +4,56 @@ import java.util.concurrent.ConcurrentLinkedQueue; public class CoffeeQueue { private ConcurrentLinkedQueue<Worker> coffeeQueue; - + /** - * Constructor, - * creates a new coffeeQueue where workers - * can queue to get their fill of coffee. + * Constructor, creates a new coffeeQueue where workers can queue to get their + * fill of coffee. */ public CoffeeQueue() { coffeeQueue = new ConcurrentLinkedQueue<Worker>(); } - - + /** * Add worker to queue. - * @param w The worker to add. + * + * @param w The worker to add. */ public void enQueue(Worker w) { coffeeQueue.add(w); } - - + /** * Removes worker from the queue. + * * @return returns the removed worker. */ public Worker deQueue() { return coffeeQueue.remove(); } - - + /** * Remove specific worker from coffeeQueue. + * * @param w the worker to remove. */ public void deQueue(Worker w) { coffeeQueue.remove(w); } - - + /** * get the ammount of workers currently queuing for coffee. + * * @return size of coffeeQueue. */ public int getSize() { return coffeeQueue.size(); } - - + /** * Used to determine if worker exists in queue. - * @param w the worker whose presence we are observing. - * @return true if the specified worker exists, else false. + * + * @param w the worker whose presence we are observing. + * @return true if the specified worker exists, else false. */ public boolean inQueue(Worker w) { if (coffeeQueue.contains(w)) { diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Main.java b/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Main.java @@ -1,36 +1,36 @@ package breakRoom; -public class Main extends Thread{ +public class Main extends Thread { public static void main(String[] args) throws InterruptedException { CoffeeQueue queue = new CoffeeQueue(); CoffeeMaker coffeeMachine = new CoffeeMaker(queue); - - //declare workers + + // declare workers Worker worker1 = new Worker("worker1", queue); Worker worker2 = new Worker("worker2", queue); Worker worker3 = new Worker("worker3", queue); Worker worker4 = new Worker("worker4", queue); - - //start all threads. (start simulation). + + // start all threads. (start simulation). worker1.start(); worker2.start(); worker3.start(); worker4.start(); coffeeMachine.start(); - + System.out.println("======Started Simulation====="); - - //wait for 20 seconds + + // wait for 20 seconds Thread.sleep(20000); - - //stop all threads. (exit simulation). + + // stop all threads. (exit simulation). worker1.cancel(); worker2.cancel(); worker3.cancel(); worker4.cancel(); coffeeMachine.cancel(); - + System.out.println("======Closed Simulation====="); } diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Worker.java b/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Worker.java @@ -8,34 +8,33 @@ import coffee.Coffee; public class Worker extends Thread { private static final int timeScale = 1; // used to change speed of simulation (default = 1) - + private String name; private int energy; private Boolean onBreak; private Random r = new Random(); - private int T; //delay used between every iteration. + private int T; // delay used between every iteration. private Timer timer = new Timer(); private CoffeeQueue coffeeQueue; - + /** - * Timertask, runs periodically to determine how much energy the worker has - * and to determine the workers next move. + * Timertask, runs periodically to determine how much energy the worker has and + * to determine the workers next move. */ private TimerTask task = new TimerTask() { public void run() { energy--; - - //if the worker has replenished energy, the worker should no longer be on break. + + // if the worker has replenished energy, the worker should no longer be on + // break. if (energy >= 100 && onBreak) { 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. - * Otherwise keep working. + * The worker goes home if energy is at 0 or below. The worker goes to get + * coffee if its energy level is below 30. Otherwise keep working. */ if (energy <= 0) { System.out.println(name + " is going home with energy level " + energy); @@ -49,45 +48,41 @@ public class Worker extends Thread { } } }; - - + /** - * Run thread. - * Starts scheduled task. + * Run thread. Starts scheduled task. */ public void run() { timer.scheduleAtFixedRate(task, T / timeScale, T / timeScale); } - + /** - * Used to cancel the scheduled task. + * Used to cancel the scheduled task. */ public void cancel() { task.cancel(); } - - + /** - * Constructor - * Creates a worker. - * @param name Name of the worker. - * @param coffeeQueue The queue for getting coffee. + * Constructor Creates a worker. + * + * @param name Name of the worker. + * @param coffeeQueue The queue for getting coffee. */ public Worker(String name, CoffeeQueue coffeeQueue) { this.name = name; this.coffeeQueue = coffeeQueue; this.onBreak = false; - 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. + 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. } - - + /** - * Constructor - * Creates a worker. - * @param name Name of the worker. - * @param coffeeQueue The queue for getting coffee. - * @param energy The ammount of energy the worker has. + * Constructor Creates a worker. + * + * @param name Name of the worker. + * @param coffeeQueue The queue for getting coffee. + * @param energy The ammount of energy the worker has. */ public Worker(String name, CoffeeQueue coffeeQueue, int energy) { this.name = name; @@ -95,19 +90,16 @@ public class Worker extends Thread { this.energy = energy; this.T = r.nextInt(500, 1500); } - - + /** - * start queuing to get coffee. - * NOTE: only adds the same worker once. + * start queuing to get coffee. NOTE: only adds the same worker once. */ private void Queue() { if (!(coffeeQueue.inQueue(this))) { coffeeQueue.enQueue(this); } } - - + /** * Remove worker from queue. */ @@ -116,51 +108,50 @@ public class Worker extends Thread { coffeeQueue.deQueue(this); } } - - + /** * Allows a worker to drink a cup of coffee + * * @param coffee the coffee to drink */ public synchronized void drink(Coffee coffee) { this.energy += coffee.getEnergy(); } - + /** * Get the workers name + * * @return returns the workers name. */ public String getWorkerName() { return this.name; } - - + /** * Get the ammount of energy the worker currently has. - * @return energy as int. + * + * @return energy as int. */ public synchronized 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. + * 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. * - * NOTE: since it only compares names to know if worker is in coffeeQueue, - * no two workers can have the same name. + * NOTE: since it only compares names to know if worker is in coffeeQueue, no + * two workers can have the same name. */ @Override public boolean equals(Object object) { boolean isEqual = false; - if (object != null && object instanceof Worker) - { - isEqual = this.name == ((Worker) object).name; - } + if (object != null && object instanceof Worker) { + isEqual = this.name == ((Worker) object).name; + } - return isEqual; + return isEqual; } } diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/coffee/BlackCoffee.java b/OOP2023_a22willi_a21liltr_assignment3/src/coffee/BlackCoffee.java @@ -3,43 +3,46 @@ package coffee; public class BlackCoffee implements Coffee { private int energy; private String type; - + /** * Constructor of coffee class - * @param energy Sets the energy of the coffee. + * + * @param energy Sets the energy of the coffee. */ public BlackCoffee(int energy) { this.energy = energy; this.type = "Black Coffee"; } - - + /** * Get the energy of coffee - * @return returns energy as integer. + * + * @return returns energy as integer. */ @Override public int getEnergy() { return this.energy; } - + /** * Set the energy of cofee object. - * @param int energy sets the energy of coffee object. + * + * @param int energy sets the energy of coffee object. */ @Override public void setEnergy(int energy) { this.energy = energy; } - + @Override public String toString() { return super.toString(); } - + /** * Return the type of coffee. - * @return type The type of coffee. + * + * @return type The type of coffee. */ @Override public String getType() { diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/coffee/Cappuccino.java b/OOP2023_a22willi_a21liltr_assignment3/src/coffee/Cappuccino.java @@ -3,28 +3,31 @@ package coffee; public class Cappuccino implements Coffee { private int energy; private String type; - + /** * Constructor of coffee class - * @param energy Sets the energy of the coffee. + * + * @param energy Sets the energy of the coffee. */ public Cappuccino(int energy) { this.energy = energy; this.type = "Cappuccino"; } - + /** * Get the energy of coffee - * @return returns energy as integer. + * + * @return returns energy as integer. */ @Override public int getEnergy() { return this.energy; } - + /** * Set the energy of cofee object. - * @param int energy sets the energy of coffee object. + * + * @param int energy sets the energy of coffee object. */ @Override public void setEnergy(int energy) { @@ -33,11 +36,11 @@ public class Cappuccino implements Coffee { /** * Return the type of coffee. - * @return type The type of coffee. + * + * @return type The type of coffee. */ @Override public String getType() { return this.type; } - } diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/coffee/Coffee.java b/OOP2023_a22willi_a21liltr_assignment3/src/coffee/Coffee.java @@ -2,8 +2,8 @@ package coffee; public interface Coffee { public int getEnergy(); - + public String getType(); - + public void setEnergy(int energy); } diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/coffee/Latte.java b/OOP2023_a22willi_a21liltr_assignment3/src/coffee/Latte.java @@ -3,28 +3,31 @@ package coffee; public class Latte implements Coffee { private int energy; private String type; - + /** * Constructor of coffee class - * @param energy Sets the energy of the coffee. + * + * @param energy Sets the energy of the coffee. */ public Latte(int energy) { this.energy = energy; this.type = "Latte"; } - + /** * Get the energy of coffee - * @return returns energy as integer. + * + * @return returns energy as integer. */ @Override public int getEnergy() { return this.energy; } - + /** * Set the energy of cofee object. - * @param int energy sets the energy of coffee object. + * + * @param int energy sets the energy of coffee object. */ @Override public void setEnergy(int energy) { @@ -33,7 +36,8 @@ public class Latte implements Coffee { /** * Return the type of coffee. - * @return type The type of coffee. + * + * @return type The type of coffee. */ @Override public String getType() {