OOP-Projects-Java

Log | Files | Refs | README

commit 8c53dfb7116c0c316485a7fb6920bfab35fd8dde
parent cc1bf91aa24c3131790fecb061728ce68bb48532
Author: William Lindholm <William_Lindholm@outlook.com>
Date:   Mon, 20 Feb 2023 20:20:20 +0100

Fixed bugs and added more comments.

Diffstat:
MOOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeMaker.java | 28++++++++++++++++------------
MOOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeQueue.java | 21+++++++++++++++++++++
MOOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Main.java | 19+++++++++++++++----
MOOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Worker.java | 48+++++++++++++++++++++++++++++++++++++++++++++---
DOOP2023_a22willi_a21liltr_assignment3/src/cofee/BlackCoffee.java | 49-------------------------------------------------
DOOP2023_a22willi_a21liltr_assignment3/src/cofee/Cappuccino.java | 43-------------------------------------------
DOOP2023_a22willi_a21liltr_assignment3/src/cofee/Coffee.java | 9---------
DOOP2023_a22willi_a21liltr_assignment3/src/cofee/Latte.java | 42------------------------------------------
AOOP2023_a22willi_a21liltr_assignment3/src/coffee/BlackCoffee.java | 49+++++++++++++++++++++++++++++++++++++++++++++++++
AOOP2023_a22willi_a21liltr_assignment3/src/coffee/Cappuccino.java | 43+++++++++++++++++++++++++++++++++++++++++++
AOOP2023_a22willi_a21liltr_assignment3/src/coffee/Coffee.java | 9+++++++++
AOOP2023_a22willi_a21liltr_assignment3/src/coffee/Latte.java | 42++++++++++++++++++++++++++++++++++++++++++
MOOP2023_a22willi_a21liltr_assignment3/src/module-info.java | 1+
13 files changed, 241 insertions(+), 162 deletions(-)

diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeMaker.java b/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeMaker.java @@ -5,16 +5,19 @@ import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.ConcurrentLinkedQueue; -import cofee.BlackCoffee; -import cofee.Cappuccino; -import cofee.Coffee; +import coffee.BlackCoffee; +import coffee.Cappuccino; +import coffee.Coffee; +import coffee.Latte; public class CoffeeMaker extends Thread { - private static int timeScale = 1; // used to change speed of simulation (default = 1) - private Timer timer = new Timer(); + 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 Timer timer; + private Random random; + private CoffeeQueue coffeeQueue; //queue for workers. private ConcurrentLinkedQueue<Coffee> coffeeBuffer; //queue for coffee. @@ -71,6 +74,8 @@ public class CoffeeMaker extends Thread { public CoffeeMaker(CoffeeQueue coffeeQueue) { this.coffeeQueue = coffeeQueue; this.coffeeBuffer = new ConcurrentLinkedQueue<Coffee>(); + this.timer = new Timer(); + this.random = new Random(); } @@ -80,19 +85,18 @@ public class CoffeeMaker extends Thread { * @return Coffee Returns a coffee of random type. */ private Coffee generateRandomCoffee() { - Random r = new Random(); - int coffeType = r.nextInt(0, 2); + int coffeType = random.nextInt(1, 4); int energy; switch(coffeType) { - case 0: - energy = r.nextInt(25, 35); - return new BlackCoffee(energy); case 1: - energy = r.nextInt(15, 20); + energy = random.nextInt(25, 35); return new BlackCoffee(energy); case 2: - energy = r.nextInt(20, 30); + 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"); diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeQueue.java b/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeQueue.java @@ -33,10 +33,31 @@ public class CoffeeQueue { } /** + * 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. + */ + public boolean inQueue(Worker w) { + if (coffeeQueue.contains(w)) { + return true; + } + return false; + } } diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Main.java b/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Main.java @@ -1,19 +1,30 @@ package breakRoom; -public class Main { +public class Main extends Thread{ public static void main(String[] args) throws InterruptedException { CoffeeQueue queue = new CoffeeQueue(); CoffeeMaker coffeeMachine = new CoffeeMaker(queue); - Worker worker1 = new Worker("William", queue); - Worker worker2 = new Worker("Lili", queue); - Worker worker3 = new Worker("Sol", queue); + //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). worker1.start(); worker2.start(); worker3.start(); + worker4.start(); coffeeMachine.start(); + + //wait for 20 seconds + Thread.sleep(20000); + + //exit simulation + System.exit(0); } } diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Worker.java b/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Worker.java @@ -4,13 +4,14 @@ import java.util.Random; import java.util.Timer; import java.util.TimerTask; -import cofee.Coffee; +import coffee.Coffee; public class Worker extends Thread { private static 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 Timer timer = new Timer(); @@ -32,12 +33,19 @@ public class Worker extends Thread { if (energy <= 0) { System.out.println(name + " is going home with energy level " + energy); task.cancel(); - } else if (energy < 30) { + } else if (energy < 30 || onBreak == true) { + onBreak = true; System.out.println(name + " is taking a break with energy level " + energy); Queue(); } 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; + } } }; @@ -60,6 +68,7 @@ public class Worker extends Thread { 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. } @@ -81,9 +90,21 @@ public class Worker extends Thread { /** * start queuing to get coffee. + * NOTE: only adds the same worker once. */ private void Queue() { - coffeeQueue.enQueue(this); + if (!(coffeeQueue.inQueue(this))) { + System.out.println("======" + name + " not in queue======"); + coffeeQueue.enQueue(this); + } else { + System.out.println("======" + name + " already in queue======"); + } + } + + private void deQueue() { + if (coffeeQueue.inQueue(this)) { + coffeeQueue.deQueue(this); + } } @@ -102,4 +123,25 @@ public class Worker extends Thread { public String getWorkerName() { return this.name; } + + + /** + * 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. + */ + @Override + public boolean equals(Object object) { + boolean isEqual = false; + + if (object != null && object instanceof Worker) + { + isEqual = this.name == ((Worker) object).name; + } + + return isEqual; + } } diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/cofee/BlackCoffee.java b/OOP2023_a22willi_a21liltr_assignment3/src/cofee/BlackCoffee.java @@ -1,49 +0,0 @@ -package cofee; - -public class BlackCoffee implements Coffee { - private int energy; - private String type; - - /** - * Constructor of coffee class - * @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. - */ - @Override - public int getEnergy() { - return this.energy; - } - - /** - * Set the energy of cofee 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. - */ - @Override - public String getType() { - return this.type; - } - -} diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/cofee/Cappuccino.java b/OOP2023_a22willi_a21liltr_assignment3/src/cofee/Cappuccino.java @@ -1,43 +0,0 @@ -package cofee; - -public class Cappuccino implements Coffee { - private int energy; - private String type; - - /** - * Constructor of coffee class - * @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. - */ - @Override - public int getEnergy() { - return this.energy; - } - - /** - * Set the energy of cofee object. - * @param int energy sets the energy of coffee object. - */ - @Override - public void setEnergy(int energy) { - this.energy = energy; - } - - /** - * Return 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/cofee/Coffee.java b/OOP2023_a22willi_a21liltr_assignment3/src/cofee/Coffee.java @@ -1,9 +0,0 @@ -package cofee; - -public interface Coffee { - public int getEnergy(); - - public String getType(); - - public void setEnergy(int energy); -} diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/cofee/Latte.java b/OOP2023_a22willi_a21liltr_assignment3/src/cofee/Latte.java @@ -1,42 +0,0 @@ -package cofee; - -public class Latte implements Coffee { - private int energy; - private String type; - - /** - * Constructor of coffee class - * @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. - */ - @Override - public int getEnergy() { - return this.energy; - } - - /** - * Set the energy of cofee object. - * @param int energy sets the energy of coffee object. - */ - @Override - public void setEnergy(int energy) { - this.energy = energy; - } - - /** - * Return 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/BlackCoffee.java b/OOP2023_a22willi_a21liltr_assignment3/src/coffee/BlackCoffee.java @@ -0,0 +1,49 @@ +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. + */ + public BlackCoffee(int energy) { + this.energy = energy; + this.type = "Black Coffee"; + } + + + /** + * Get the energy of coffee + * @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. + */ + @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. + */ + @Override + public String getType() { + return this.type; + } + +} diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/coffee/Cappuccino.java b/OOP2023_a22willi_a21liltr_assignment3/src/coffee/Cappuccino.java @@ -0,0 +1,43 @@ +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. + */ + public Cappuccino(int energy) { + this.energy = energy; + this.type = "Cappuccino"; + } + + /** + * Get the energy of coffee + * @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. + */ + @Override + public void setEnergy(int energy) { + this.energy = energy; + } + + /** + * Return 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 @@ -0,0 +1,9 @@ +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 @@ -0,0 +1,42 @@ +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. + */ + public Latte(int energy) { + this.energy = energy; + this.type = "Latte"; + } + + /** + * Get the energy of coffee + * @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. + */ + @Override + public void setEnergy(int energy) { + this.energy = energy; + } + + /** + * Return 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/module-info.java b/OOP2023_a22willi_a21liltr_assignment3/src/module-info.java @@ -6,4 +6,5 @@ * */ module OOP2023_a22willi_a21liltr_assignment3 { + } \ No newline at end of file