OOP-Projects-Java

Log | Files | Refs | README

commit f24606e7f0e68c0ba749df26026ac8a1211c4a85
parent eac5d3cd90010916681ab4678535c8406d86f826
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Mon, 20 Feb 2023 16:32:12 +0100

Work in progress (adding queue system for workers)

Diffstat:
MOOP2023_a22willi_a21liltr_assignment3/src/fika/CoffeeMaker.java | 28+++++++++++++++++++---------
AOOP2023_a22willi_a21liltr_assignment3/src/fika/CoffeeQueue.java | 19+++++++++++++++++++
MOOP2023_a22willi_a21liltr_assignment3/src/fika/Main.java | 15++++++++++-----
MOOP2023_a22willi_a21liltr_assignment3/src/fika/Worker.java | 14++++++++++----
4 files changed, 58 insertions(+), 18 deletions(-)

diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/fika/CoffeeMaker.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/CoffeeMaker.java @@ -6,27 +6,37 @@ import java.util.TimerTask; import java.util.concurrent.ConcurrentLinkedQueue; public class CoffeeMaker extends Thread { - private static int timeScale = 10; // used to change speed of simulation (default = 1) + private static int timeScale = 1; // used to change speed of simulation (default = 1) private Timer timer = new Timer(); - private int T = 2000; //the default time it takes the coffeemaker to create one coffee. + 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 ConcurrentLinkedQueue<Coffee> coffeBuffer = new ConcurrentLinkedQueue<Coffee>(); + private CoffeeQueue coffeeQueue; + private ConcurrentLinkedQueue<Coffee> coffeeBuffer = new ConcurrentLinkedQueue<Coffee>(); - private TimerTask task = new TimerTask() { + private TimerTask createCoffee = new TimerTask() { public void run() { //only create a new coffee if there are less than 20 coffee in the machine. - if ( coffeBuffer.size() < 20) { + if ( coffeeBuffer.size() < 20) { Coffee coffee = generateRandomCoffee(); - coffeBuffer.add(coffee); + coffeeBuffer.add(coffee); } //announce how many coffees are available. - System.out.println("Coffee Machine has " + coffeBuffer.size() + " drinks in reserve."); + System.out.println("Coffee Machine has " + coffeeBuffer.size() + " drinks in reserve."); } }; - public CoffeeMaker() { - timer.scheduleAtFixedRate(task, T / timeScale, T / timeScale); + private TimerTask serveCoffee = new TimerTask() { + public void run() { + + } + }; + + public CoffeeMaker(CoffeeQueue coffeeQueue) { + this.coffeeQueue = coffeeQueue; + timer.scheduleAtFixedRate(createCoffee, timeToCreateCoffee / timeScale, timeToCreateCoffee / timeScale); + timer.scheduleAtFixedRate(serveCoffee, timeToServeCoffee / timeScale, timeToServeCoffee / timeScale); } private Coffee generateRandomCoffee() { diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/fika/CoffeeQueue.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/CoffeeQueue.java @@ -0,0 +1,19 @@ +package fika; + +import java.util.concurrent.ConcurrentLinkedQueue; + +public class CoffeeQueue { + private ConcurrentLinkedQueue<Worker> coffeeQueue; + + public CoffeeQueue() { + coffeeQueue = new ConcurrentLinkedQueue<Worker>(); + } + + public void enQueue(Worker w) { + coffeeQueue.add(w); + } + + public void deQueue() { + coffeeQueue.remove(); + } +} diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/fika/Main.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/Main.java @@ -3,11 +3,16 @@ package fika; public class Main { public static void main(String[] args) throws InterruptedException { - // TODO Auto-generated method stub - Worker thr1 = new Worker("William"); - Worker thr2 = new Worker("Lili"); - Worker thr3 = new Worker("Sol"); - CoffeeMaker thr4 = new CoffeeMaker(); + + CoffeeQueue queue = new CoffeeQueue(); + + CoffeeMaker thr4 = new CoffeeMaker(queue); + + + Worker thr1 = new Worker("William", queue); + Worker thr2 = new Worker("Lili", queue); + Worker thr3 = new Worker("Sol", queue); + thr1.start(); thr2.start(); thr3.start(); diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/fika/Worker.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/Worker.java @@ -12,6 +12,7 @@ public class Worker extends Thread { private Random r = new Random(); private int T; //delay used between every iteration. private Timer timer = new Timer(); + private CoffeeQueue coffeeQueue; private TimerTask task = new TimerTask() { public void run() { @@ -22,25 +23,30 @@ public class Worker extends Thread { task.cancel(); } else if (energy < 30) { System.out.println(name + " is taking a break with energy level " + energy); + Queue(); } else { System.out.println(name + " Is working with energy level " + energy); } } }; + private void Queue() { + coffeeQueue.enQueue(this); + } + - public Worker(String name) { + public Worker(String name, CoffeeQueue coffeeQueue) { this.name = name; + this.coffeeQueue = coffeeQueue; this.energy = r.nextInt(30, 90); this.T = r.nextInt(500, 1500); timer.scheduleAtFixedRate(task, T / timeScale, T / timeScale); - - } - public Worker(String name, int energy) { + public Worker(String name, CoffeeQueue coffeeQueue, int energy) { this.name = name; + this.coffeeQueue = coffeeQueue; this.energy = energy; this.T = r.nextInt(500, 1500);