OOP-Projects-Java

Log | Files | Refs | README

commit 5253c7631515da7a19564c528b6906666f1a6734
parent e05f57934d3476fb93221e91df543b3ea85ebb94
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Mon, 20 Feb 2023 18:23:18 +0100

Created separate package for coffee classes

Diffstat:
AOOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeMaker.java | 102+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
AOOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeQueue.java | 23+++++++++++++++++++++++
AOOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Main.java | 22++++++++++++++++++++++
AOOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Worker.java | 105+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
AOOP2023_a22willi_a21liltr_assignment3/src/cofee/BlackCoffee.java | 49+++++++++++++++++++++++++++++++++++++++++++++++++
AOOP2023_a22willi_a21liltr_assignment3/src/cofee/Cappuccino.java | 43+++++++++++++++++++++++++++++++++++++++++++
AOOP2023_a22willi_a21liltr_assignment3/src/cofee/Coffee.java | 9+++++++++
AOOP2023_a22willi_a21liltr_assignment3/src/cofee/Latte.java | 42++++++++++++++++++++++++++++++++++++++++++
DOOP2023_a22willi_a21liltr_assignment3/src/fika/BlackCoffee.java | 49-------------------------------------------------
DOOP2023_a22willi_a21liltr_assignment3/src/fika/Cappuccino.java | 43-------------------------------------------
DOOP2023_a22willi_a21liltr_assignment3/src/fika/Coffee.java | 9---------
DOOP2023_a22willi_a21liltr_assignment3/src/fika/CoffeeMaker.java | 98-------------------------------------------------------------------------------
DOOP2023_a22willi_a21liltr_assignment3/src/fika/CoffeeQueue.java | 23-----------------------
DOOP2023_a22willi_a21liltr_assignment3/src/fika/Latte.java | 42------------------------------------------
DOOP2023_a22willi_a21liltr_assignment3/src/fika/Main.java | 22----------------------
DOOP2023_a22willi_a21liltr_assignment3/src/fika/Worker.java | 103-------------------------------------------------------------------------------
16 files changed, 395 insertions(+), 389 deletions(-)

diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeMaker.java b/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeMaker.java @@ -0,0 +1,102 @@ +package breakRoom; + +import java.util.Random; +import java.util.Timer; +import java.util.TimerTask; +import java.util.concurrent.ConcurrentLinkedQueue; + +import cofee.BlackCoffee; +import cofee.Cappuccino; +import cofee.Coffee; + +public class CoffeeMaker extends Thread { + private static int timeScale = 1; // used to change speed of simulation (default = 1) + private Timer timer = new Timer(); + 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 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. + */ + 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) { + Coffee coffee = generateRandomCoffee(); + coffeeBuffer.add(coffee); + } + + //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. + */ + private TimerTask serveCoffee = new TimerTask() { + public void run() { + 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"); + } + } + }; + + + /** + * Run thread. + * Start serveCoffee and CreateCoffee schedules. + */ + public void run() { + timer.scheduleAtFixedRate(createCoffee, timeToCreateCoffee / timeScale, timeToCreateCoffee / timeScale); + timer.scheduleAtFixedRate(serveCoffee, timeToServeCoffee / timeScale, timeToServeCoffee / timeScale); + } + + + /** + * Constructor, requires variable coffeeQueue + * @param coffeeQueue The queue that workers use to interface with the coffeemaker. + */ + public CoffeeMaker(CoffeeQueue coffeeQueue) { + this.coffeeQueue = coffeeQueue; + this.coffeeBuffer = new ConcurrentLinkedQueue<Coffee>(); + } + + + /** + * 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() { + 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/breakRoom/CoffeeQueue.java b/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/CoffeeQueue.java @@ -0,0 +1,23 @@ +package breakRoom; + +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 Worker deQueue() { + return coffeeQueue.remove(); + } + + public int getSize() { + return coffeeQueue.size(); + } +} diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Main.java b/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Main.java @@ -0,0 +1,22 @@ +package breakRoom; + +public class Main { + + public static void main(String[] args) throws InterruptedException { + + 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(); + thr4.start(); + } + +} diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Worker.java b/OOP2023_a22willi_a21liltr_assignment3/src/breakRoom/Worker.java @@ -0,0 +1,105 @@ +package breakRoom; + +import java.util.Random; +import java.util.Timer; +import java.util.TimerTask; + +import cofee.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 Random r = new Random(); + 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. + */ + private TimerTask task = new TimerTask() { + public void run() { + energy--; + + /* + * 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); + 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); + } + } + }; + + + /** + * Run thread. + * Starts scheduled task. + */ + public void run() { + timer.scheduleAtFixedRate(task, T / timeScale, T / timeScale); + } + + + /** + * 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.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. + */ + public Worker(String name, CoffeeQueue coffeeQueue, int energy) { + this.name = name; + this.coffeeQueue = coffeeQueue; + this.energy = energy; + this.T = r.nextInt(500, 1500); + } + + + /** + * start queuing to get coffee. + */ + private void Queue() { + coffeeQueue.enQueue(this); + } + + + /** + * Allows a worker to drink a cup of coffee + * @param coffee the coffee to drink + */ + public void drink(Coffee coffee) { + this.energy += coffee.getEnergy(); + } + + /** + * Get the workers name + * @return returns the workers name. + */ + public String getWorkerName() { + return this.name; + } +} diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/cofee/BlackCoffee.java b/OOP2023_a22willi_a21liltr_assignment3/src/cofee/BlackCoffee.java @@ -0,0 +1,49 @@ +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 @@ -0,0 +1,43 @@ +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 @@ -0,0 +1,9 @@ +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 @@ -0,0 +1,42 @@ +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/fika/BlackCoffee.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/BlackCoffee.java @@ -1,49 +0,0 @@ -package fika; - -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/fika/Cappuccino.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/Cappuccino.java @@ -1,43 +0,0 @@ -package fika; - -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/fika/Coffee.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/Coffee.java @@ -1,9 +0,0 @@ -package fika; - -public interface Coffee { - public int getEnergy(); - - public String getType(); - - public void setEnergy(int energy); -} diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/fika/CoffeeMaker.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/CoffeeMaker.java @@ -1,98 +0,0 @@ -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 = 1; // used to change speed of simulation (default = 1) - private Timer timer = new Timer(); - 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 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. - */ - 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) { - Coffee coffee = generateRandomCoffee(); - coffeeBuffer.add(coffee); - } - - //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. - */ - private TimerTask serveCoffee = new TimerTask() { - public void run() { - 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"); - } - } - }; - - - /** - * Run thread. - * Start serveCoffee and CreateCoffee schedules. - */ - public void run() { - timer.scheduleAtFixedRate(createCoffee, timeToCreateCoffee / timeScale, timeToCreateCoffee / timeScale); - timer.scheduleAtFixedRate(serveCoffee, timeToServeCoffee / timeScale, timeToServeCoffee / timeScale); - } - - - /** - * Constructor, requires variable coffeeQueue - * @param coffeeQueue The queue that workers use to interface with the coffeemaker. - */ - public CoffeeMaker(CoffeeQueue coffeeQueue) { - this.coffeeQueue = coffeeQueue; - this.coffeeBuffer = new ConcurrentLinkedQueue<Coffee>(); - } - - - /** - * 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() { - 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/CoffeeQueue.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/CoffeeQueue.java @@ -1,23 +0,0 @@ -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 Worker deQueue() { - return coffeeQueue.remove(); - } - - public int getSize() { - return coffeeQueue.size(); - } -} diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/fika/Latte.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/Latte.java @@ -1,42 +0,0 @@ -package fika; - -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/fika/Main.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/Main.java @@ -1,22 +0,0 @@ -package fika; - -public class Main { - - public static void main(String[] args) throws InterruptedException { - - 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(); - thr4.start(); - } - -} diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/fika/Worker.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/Worker.java @@ -1,103 +0,0 @@ -package fika; - -import java.util.Random; -import java.util.Timer; -import java.util.TimerTask; - -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 Random r = new Random(); - 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. - */ - private TimerTask task = new TimerTask() { - public void run() { - energy--; - - /* - * 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); - 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); - } - } - }; - - - /** - * Run thread. - * Starts scheduled task. - */ - public void run() { - timer.scheduleAtFixedRate(task, T / timeScale, T / timeScale); - } - - - /** - * 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.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. - */ - public Worker(String name, CoffeeQueue coffeeQueue, int energy) { - this.name = name; - this.coffeeQueue = coffeeQueue; - this.energy = energy; - this.T = r.nextInt(500, 1500); - } - - - /** - * start queuing to get coffee. - */ - private void Queue() { - coffeeQueue.enQueue(this); - } - - - /** - * Allows a worker to drink a cup of coffee - * @param coffee the coffee to drink - */ - public void drink(Coffee coffee) { - this.energy += coffee.getEnergy(); - } - - /** - * Get the workers name - * @return returns the workers name. - */ - public String getWorkerName() { - return this.name; - } -}