OOP-Projects-Java

Log | Files | Refs | README

commit eac5d3cd90010916681ab4678535c8406d86f826
parent 4f7eacfa0347cc29c43e78f90c8aab452fda55f0
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Mon, 20 Feb 2023 15:50:30 +0100

Added coffeemachine functionality and fixed coffee classes

Diffstat:
MOOP2023_a22willi_a21liltr_assignment3/src/fika/BlackCoffee.java | 35++++++++++++++++++++++++-----------
MOOP2023_a22willi_a21liltr_assignment3/src/fika/Cappuccino.java | 33+++++++++++++++++++++++----------
MOOP2023_a22willi_a21liltr_assignment3/src/fika/CoffeeMaker.java | 44++++++++++++++++++++++++++++++++++++++++++--
MOOP2023_a22willi_a21liltr_assignment3/src/fika/Latte.java | 33+++++++++++++++++++++++----------
MOOP2023_a22willi_a21liltr_assignment3/src/fika/Main.java | 2++
MOOP2023_a22willi_a21liltr_assignment3/src/fika/Worker.java | 6+++---
6 files changed, 117 insertions(+), 36 deletions(-)

diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/fika/BlackCoffee.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/BlackCoffee.java @@ -1,28 +1,41 @@ package fika; public class BlackCoffee implements Coffee { - private int energy; - public BlackCoffee() { - + /** + * Constructor of coffee class + * @param energy Sets the energy of the coffee. + */ + public BlackCoffee(int energy) { + this.energy = energy; } - + + /** + * Allows a coffee to be "used". + * @return returns the coffees energy. + */ @Override public int drink() { - // TODO Auto-generated method stub - return 0; + return this.energy; } - + + /** + * Get the energy of coffee + * @return returns energy as integer. + */ @Override public int getEnergy() { - // TODO Auto-generated method stub - return 0; + 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; } - + } diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/fika/Cappuccino.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/Cappuccino.java @@ -1,25 +1,38 @@ package fika; public class Cappuccino implements Coffee { - private int energy; - public Cappuccino() { - + /** + * Constructor of coffee class + * @param energy Sets the energy of the coffee. + */ + public Cappuccino(int energy) { + this.energy = energy; } - + + /** + * Allows a coffee to be "used". + * @return returns the coffees energy. + */ @Override public int drink() { - // TODO Auto-generated method stub - return 0; + return this.energy; } - + + /** + * Get the energy of coffee + * @return returns energy as integer. + */ @Override public int getEnergy() { - // TODO Auto-generated method stub - return 0; + 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; diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/fika/CoffeeMaker.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/CoffeeMaker.java @@ -1,12 +1,52 @@ package fika; +import java.util.Random; +import java.util.Timer; +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 Timer timer = new Timer(); + private int T = 2000; //the default time it takes the coffeemaker to create one coffee. + + private ConcurrentLinkedQueue<Coffee> coffeBuffer = new ConcurrentLinkedQueue<Coffee>(); + + private TimerTask task = new TimerTask() { + public void run() { + //only create a new coffee if there are less than 20 coffee in the machine. + if ( coffeBuffer.size() < 20) { + Coffee coffee = generateRandomCoffee(); + coffeBuffer.add(coffee); + } + + //announce how many coffees are available. + System.out.println("Coffee Machine has " + coffeBuffer.size() + " drinks in reserve."); + } + }; public CoffeeMaker() { - + timer.scheduleAtFixedRate(task, T / timeScale, T / timeScale); } - public void Run() { + private Coffee generateRandomCoffee() { + Random r = new Random(); + int coffeType = r.nextInt(0, 2); + int energy; + switch(coffeType) { + case 0: + energy = r.nextInt(25, 35); + return new BlackCoffee(energy); + case 1: + energy = r.nextInt(15, 20); + return new BlackCoffee(energy); + case 2: + energy = r.nextInt(20, 30); + return new Cappuccino(energy); + default: + System.out.println("Something went wrong"); + return null; + } } } diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/fika/Latte.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/Latte.java @@ -1,25 +1,38 @@ package fika; public class Latte implements Coffee { - private int energy; - public Latte() { - + /** + * Constructor of coffee class + * @param energy Sets the energy of the coffee. + */ + public Latte(int energy) { + this.energy = energy; } - + + /** + * Allows a coffee to be "used". + * @return returns the coffees energy. + */ @Override public int drink() { - // TODO Auto-generated method stub - return 0; + return this.energy; } - + + /** + * Get the energy of coffee + * @return returns energy as integer. + */ @Override public int getEnergy() { - // TODO Auto-generated method stub - return 0; + 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; diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/fika/Main.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/Main.java @@ -7,9 +7,11 @@ public class Main { Worker thr1 = new Worker("William"); Worker thr2 = new Worker("Lili"); Worker thr3 = new Worker("Sol"); + CoffeeMaker thr4 = new CoffeeMaker(); thr1.start(); thr2.start(); thr3.start(); + thr4.start(); } } diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/fika/Worker.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/Worker.java @@ -5,7 +5,7 @@ import java.util.Timer; import java.util.TimerTask; public class Worker 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 String name; private int energy; @@ -34,7 +34,7 @@ public class Worker extends Thread { this.energy = r.nextInt(30, 90); this.T = r.nextInt(500, 1500); - timer.scheduleAtFixedRate(task, T / TimeScale, T / TimeScale); + timer.scheduleAtFixedRate(task, T / timeScale, T / timeScale); } @@ -44,6 +44,6 @@ public class Worker extends Thread { this.energy = energy; this.T = r.nextInt(500, 1500); - timer.scheduleAtFixedRate(task, T / TimeScale, T / TimeScale); + timer.scheduleAtFixedRate(task, T / timeScale, T / timeScale); } }