commit c3af871d09282901e60ecc82e39384ffbed4d47d
parent f24606e7f0e68c0ba749df26026ac8a1211c4a85
Author: William Lindholm <william_lindholm@outlook.com>
Date: Mon, 20 Feb 2023 17:29:36 +0100
Finished coffeequeue, workers can now drink coffee to increase energy
Diffstat:
7 files changed, 91 insertions(+), 34 deletions(-)
diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/fika/BlackCoffee.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/BlackCoffee.java
@@ -2,6 +2,7 @@ package fika;
public class BlackCoffee implements Coffee {
private int energy;
+ private String type;
/**
* Constructor of coffee class
@@ -9,16 +10,9 @@ public class BlackCoffee implements Coffee {
*/
public BlackCoffee(int energy) {
this.energy = energy;
+ this.type = "Black Coffee";
}
- /**
- * Allows a coffee to be "used".
- * @return returns the coffees energy.
- */
- @Override
- public int drink() {
- return this.energy;
- }
/**
* Get the energy of coffee
@@ -37,5 +31,19 @@ public class BlackCoffee implements Coffee {
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
@@ -2,6 +2,7 @@ package fika;
public class Cappuccino implements Coffee {
private int energy;
+ private String type;
/**
* Constructor of coffee class
@@ -9,15 +10,7 @@ public class Cappuccino implements Coffee {
*/
public Cappuccino(int energy) {
this.energy = energy;
- }
-
- /**
- * Allows a coffee to be "used".
- * @return returns the coffees energy.
- */
- @Override
- public int drink() {
- return this.energy;
+ this.type = "Cappuccino";
}
/**
@@ -38,4 +31,13 @@ public class Cappuccino implements Coffee {
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 +1,9 @@
package fika;
public interface Coffee {
- public int drink();
-
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
@@ -29,7 +29,12 @@ public class CoffeeMaker extends Thread {
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");
+ }
}
};
diff --git a/OOP2023_a22willi_a21liltr_assignment3/src/fika/CoffeeQueue.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/CoffeeQueue.java
@@ -13,7 +13,11 @@ public class CoffeeQueue {
coffeeQueue.add(w);
}
- public void deQueue() {
- coffeeQueue.remove();
+ 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
@@ -2,6 +2,7 @@ package fika;
public class Latte implements Coffee {
private int energy;
+ private String type;
/**
* Constructor of coffee class
@@ -9,15 +10,7 @@ public class Latte implements Coffee {
*/
public Latte(int energy) {
this.energy = energy;
- }
-
- /**
- * Allows a coffee to be "used".
- * @return returns the coffees energy.
- */
- @Override
- public int drink() {
- return this.energy;
+ this.type = "Latte";
}
/**
@@ -38,4 +31,12 @@ public class Latte implements Coffee {
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/Worker.java b/OOP2023_a22willi_a21liltr_assignment3/src/fika/Worker.java
@@ -14,10 +14,19 @@ public class Worker extends Thread {
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();
@@ -30,20 +39,35 @@ public class Worker extends Thread {
}
};
+ /**
+ * start queuing to get coffee.
+ */
private void Queue() {
coffeeQueue.enQueue(this);
}
-
+ /**
+ * 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);
- this.T = r.nextInt(500, 1500);
+ 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.
timer.scheduleAtFixedRate(task, T / timeScale, T / timeScale);
}
+ /**
+ * 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;
@@ -52,4 +76,17 @@ public class Worker extends Thread {
timer.scheduleAtFixedRate(task, T / timeScale, T / timeScale);
}
+
+ /**
+ * Allows a worker to drink a cup of coffee
+ * @param coffee the coffee to drink
+ */
+ public void drink(Coffee coffee) {
+ this.energy += coffee.getEnergy();
+ }
+
+
+ public String getWorkerName() {
+ return this.name;
+ }
}