From ae8e984866f54d632ffc9601046984c542aee49a Mon Sep 17 00:00:00 2001 From: YADUNAND PREM Date: Thu, 1 Sep 2022 11:37:00 +0800 Subject: [PATCH] Finished Lab2 fix: update test.sh feat: reformat and remote BaseShopEvent chore: add comments --- .gitignore | 3 +++ ArrivalEvent.java | 61 ++++++++++++++++++++++++++++-------------- BaseShopEvent.java | 22 --------------- Customer.java | 33 +++++++++++++++++------ DepartureEvent.java | 51 +++++++++++++++++++++++++---------- Queue.java | 9 ++++--- ServiceBeginEvent.java | 45 +++++++++++++++++-------------- ServiceEndEvent.java | 39 ++++++++++++++++----------- Shop.java | 39 ++++++++++++++++++++------- ShopCounter.java | 43 ++++++++++++++++------------- ShopSimulation.java | 10 +++---- test.sh | 3 +++ 12 files changed, 219 insertions(+), 139 deletions(-) delete mode 100644 BaseShopEvent.java diff --git a/.gitignore b/.gitignore index a55a06c..c6228eb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ Lab1.pdf *.class + +checkstyle.jar +cs2030_checks.xml \ No newline at end of file diff --git a/ArrivalEvent.java b/ArrivalEvent.java index 94462bc..a645b2a 100644 --- a/ArrivalEvent.java +++ b/ArrivalEvent.java @@ -1,30 +1,51 @@ /** + * The ArrivalEvent is an Event which handles the arrival of a customer + * It decides whether a customer should go into queue or go to a counter + * It also allocates a counter to the customer. This event thus returns either a + * DepartureEvent or a ServiceBeginEvent + * * @author Yadunand Prem * @version CS2030S AY22/23 Semester 2 */ -class ArrivalEvent extends BaseShopEvent { +class ArrivalEvent extends Event { - double serviceTime; + private Customer customer; + private Shop shop; - public ArrivalEvent(double time, Customer customer, Shop shop, double serviceTime) { - super(time, customer, shop); - this.serviceTime = serviceTime; + public ArrivalEvent(Customer customer, Shop shop) { + super(customer.getArrivalTIme()); + this.customer = customer; + this.shop = shop; + } + + @Override + public Event[] simulate() { + ShopCounter availableCounter = this.shop.getAvailableCounter(); + // check if counters are available. If none, push customer to queue if not full. + // If full, customer departs + if (availableCounter == null) { + if (this.shop.getQueue().isFull()) { + return new Event[] { new DepartureEvent(this.getTime(), customer, shop) }; + } + Queue queue = this.shop.getQueue(); + System.out + .println(String.format("%s: %s joined queue %s", + super.toString(), + this.customer, queue)); + queue.enq(customer); + return new Event[] {}; } + return new Event[] { + new ServiceBeginEvent(this.getTime(), customer, shop, availableCounter) }; - @Override - public Event[] simulate() { - ShopCounter availableCounter = this.shop.getAvailableCounter(); - if (availableCounter == null) { - return new Event[] { new DepartureEvent(this.getTime(), customer, shop) }; - } - return new Event[] { - new ServiceBeginEvent(this.getTime(), customer, shop, this.serviceTime, availableCounter) }; + } - } - - @Override - public String toString() { - return super.toString() + String.format(": %s arrives", this.customer); - } -} \ No newline at end of file + @Override + public String toString() { + return String.format("%s: %s arrived %s", + super.toString(), + this.customer, + this.shop.getQueue()); + } +} diff --git a/BaseShopEvent.java b/BaseShopEvent.java deleted file mode 100644 index 29c5034..0000000 --- a/BaseShopEvent.java +++ /dev/null @@ -1,22 +0,0 @@ - -/** - * This class encapsulates an event in the shop - * simulation. Your task is to replace this - * class with new classes, following proper OOP principles. - * - * @author Yadunand Prem - * @version CS2030S AY22/23 Semester 2 - */ - -abstract class BaseShopEvent extends Event { - - final Customer customer; - Shop shop; - - public BaseShopEvent(double time, Customer customer, Shop shop) { - super(time); - this.customer = customer; - this.shop = shop; - } - -} \ No newline at end of file diff --git a/Customer.java b/Customer.java index d85d7c3..ab97aa9 100644 --- a/Customer.java +++ b/Customer.java @@ -1,17 +1,34 @@ /** + * This is a data class which holds information about the Customer. + * It has a incrementing counter for the customer id; + * * @author Yadunand Prem * @version CS2030S AY22/23 Semester 2 */ public class Customer { - final private int id; + private static int lastId = 0; - public Customer(int id) { - this.id = id; - } + private final int id; + private final double serviceTime; + private final double arrivalTIme; - @Override - public String toString() { - return "Customer " + id; - } + public Customer(double serviceTime, double arrivalTime) { + this.id = lastId++; + this.serviceTime = serviceTime; + this.arrivalTIme = arrivalTime; + } + + public double getServiceTime() { + return this.serviceTime; + } + + public double getArrivalTIme() { + return arrivalTIme; + } + + @Override + public String toString() { + return "C" + id; + } } diff --git a/DepartureEvent.java b/DepartureEvent.java index f39db34..0c12ddf 100644 --- a/DepartureEvent.java +++ b/DepartureEvent.java @@ -1,22 +1,45 @@ /** + * The DepartureEvent is an Event which handles the end of a service. + * It handles allocating customers in queue to a counter. + * * @author Yadunand Prem * @version CS2030S AY22/23 Semester 2 */ -class DepartureEvent extends BaseShopEvent { +class DepartureEvent extends Event { - public DepartureEvent(double time, Customer customer, Shop shop) { - super(time, customer, shop); + private ShopCounter counter; + private Customer customer; + private Shop shop; + + public DepartureEvent(double time, Customer customer, Shop shop) { + super(time); + this.customer = customer; + this.shop = shop; + } + + public DepartureEvent(double time, Customer customer, Shop shop, ShopCounter counter) { + super(time); + this.customer = customer; + this.shop = shop; + this.counter = counter; + } + + @Override + public String toString() { + return String.format("%s: %s departed", super.toString(), this.customer); + } + + @Override + public Event[] simulate() { + // when customer departs, check if there are customers in queue + if (this.shop.getQueue().isEmpty() || this.counter == null) { + return new Event[] {}; } + Customer c = (Customer) this.shop.getQueue().deq(); + return new Event[] { + new ServiceBeginEvent(this.getTime(), c, this.shop, this.counter), + }; - @Override - public String toString() { - return super.toString() - + String.format(": %s departed", this.customer); - } + } - @Override - public Event[] simulate() { - return new Event[] {}; - } - -} \ No newline at end of file +} diff --git a/Queue.java b/Queue.java index b2c28e4..56e4d91 100644 --- a/Queue.java +++ b/Queue.java @@ -59,7 +59,8 @@ class Queue { /** * Remove the object from the queue. * - * @return null if the queue is empty; the object removed from the queue otherwise. + * @return null if the queue is empty; the object removed from the queue + * otherwise. */ public Object deq() { if (this.isEmpty()) { @@ -101,8 +102,8 @@ class Queue { /** * Returns the string representation of the queue. * - * @return A string consisting of the string representation of - * every object in the queue. + * @return A string consisting of the string representation of + * every object in the queue. */ @Override public String toString() { @@ -113,7 +114,7 @@ class Queue { str += this.items[i] + " "; i = (i + 1) % this.maxSize; count++; - } + } return str + "]"; } } diff --git a/ServiceBeginEvent.java b/ServiceBeginEvent.java index 82771cb..bd6867d 100644 --- a/ServiceBeginEvent.java +++ b/ServiceBeginEvent.java @@ -1,29 +1,34 @@ /** + * The ServiceBeginEvent is an Event which handles the starting of a service. + * It handles occupying a counter and also generating a serviceEndEvent + * * @author Yadunand Prem * @version CS2030S AY22/23 Semester 2 */ -class ServiceBeginEvent extends BaseShopEvent { +class ServiceBeginEvent extends Event { - double serviceTime; - ShopCounter counter; + private ShopCounter counter; + private Customer customer; + private Shop shop; - public ServiceBeginEvent(double time, Customer customer, Shop shop, double serviceTime, ShopCounter counter) { - super(time, customer, shop); - this.serviceTime = serviceTime; - this.counter = counter; - } + public ServiceBeginEvent(double time, Customer customer, Shop shop, ShopCounter counter) { + super(time); + this.customer = customer; + this.shop = shop; + this.counter = counter; + } - @Override - public String toString() { - return super.toString() - + String.format(": %s service begin (by %s)", this.customer, this.counter); - } + @Override + public String toString() { + return super.toString() + + String.format(": %s service begin (by %s)", this.customer, this.counter); + } - @Override - public Event[] simulate() { - this.counter.occupy(); - double endTime = this.getTime() + this.serviceTime; - return new Event[] { - new ServiceEndEvent(endTime, this.customer, this.shop, this.counter) }; - } + @Override + public Event[] simulate() { + this.counter.occupy(); + double endTime = this.getTime() + this.customer.getServiceTime(); + return new Event[] { + new ServiceEndEvent(endTime, this.customer, this.shop, this.counter) }; + } } diff --git a/ServiceEndEvent.java b/ServiceEndEvent.java index d6f4bb4..2646b23 100644 --- a/ServiceEndEvent.java +++ b/ServiceEndEvent.java @@ -1,25 +1,32 @@ /** + * The ServiceEndEvent is an Event which handles the end of a service. + * It handles freeing the counter and also generating a departure event + * * @author Yadunand Prem * @version CS2030S AY22/23 Semester 2 */ -class ServiceEndEvent extends BaseShopEvent { +class ServiceEndEvent extends Event { - ShopCounter counter; + private ShopCounter counter; + private Customer customer; + private Shop shop; - public ServiceEndEvent(double time, Customer customer, Shop shop, ShopCounter counter) { - super(time, customer, shop); - this.counter = counter; - } + public ServiceEndEvent(double time, Customer customer, Shop shop, ShopCounter counter) { + super(time); + this.customer = customer; + this.shop = shop; + this.counter = counter; + } - @Override - public String toString() { - return super.toString() - + String.format(": %s service done (by %s)", this.customer, this.counter); - } + @Override + public String toString() { + return super.toString() + + String.format(": %s service done (by %s)", this.customer, this.counter); + } - @Override - public Event[] simulate() { - this.counter.free(); - return new Event[] { new DepartureEvent(this.getTime(), customer, shop) }; - } + @Override + public Event[] simulate() { + this.counter.free(); + return new Event[] { new DepartureEvent(this.getTime(), customer, shop, counter) }; + } } \ No newline at end of file diff --git a/Shop.java b/Shop.java index 93a7427..6cacf45 100644 --- a/Shop.java +++ b/Shop.java @@ -1,19 +1,40 @@ /** + * Shop is a data class which holds information about a shop. + * It stores the list of counters and the queue in which new customers can join. + * * @author Yadunand Prem * @version CS2030S AY22/23 Semester 2 */ public class Shop { - private ShopCounter[] counters; + private ShopCounter[] counters; + private Queue queue; - public Shop(ShopCounter[] counters) { - this.counters = counters; + public Shop(int numOfCounters, int queueSize) { + this.counters = new ShopCounter[numOfCounters]; + for (int i = 0; i < numOfCounters; i++) { + this.counters[i] = new ShopCounter(); } - public ShopCounter getAvailableCounter() { - for (int i = 0; i < this.counters.length; i++) { - if (this.counters[i].isAvailable()) - return counters[i]; - } - return null; + this.queue = new Queue(queueSize); + } + + /** + * getAvailableCounter returns the first available counter it finds. + * If there are none, returns null + * + * @return the available counter or null if none found + */ + + public ShopCounter getAvailableCounter() { + for (int i = 0; i < this.counters.length; i++) { + if (this.counters[i].isAvailable()) { + return counters[i]; + } } + return null; + } + + public Queue getQueue() { + return queue; + } } diff --git a/ShopCounter.java b/ShopCounter.java index 1728e58..c0529f8 100644 --- a/ShopCounter.java +++ b/ShopCounter.java @@ -1,31 +1,36 @@ /** + * This is a data class which holds information about a counter. It has a + * incrementing counter for the conuter Id. + * * @author Yadunand Prem * @version CS2030S AY22/23 Semester 2 */ public class ShopCounter { - final private int id; - private boolean available; + private static int lastId; - public boolean isAvailable() { - return available; - } + private final int id; + private boolean available; - public void occupy() { - this.available = false; - } + public boolean isAvailable() { + return available; + } - public void free() { - this.available = true; - } + public void occupy() { + this.available = false; + } - public ShopCounter(int id) { - this.id = id; - this.available = true; - } + public void free() { + this.available = true; + } - @Override - public String toString() { - return "Counter " + id; - } + public ShopCounter() { + this.id = lastId++; + this.available = true; + } + + @Override + public String toString() { + return "S" + id; + } } diff --git a/ShopSimulation.java b/ShopSimulation.java index da401fd..f37b0d9 100644 --- a/ShopSimulation.java +++ b/ShopSimulation.java @@ -26,19 +26,15 @@ class ShopSimulation extends Simulation { public ShopSimulation(Scanner sc) { initEvents = new Event[sc.nextInt()]; int numOfCounters = sc.nextInt(); + int maxQueueSize = sc.nextInt(); - ShopCounter[] availableCounters = new ShopCounter[numOfCounters]; - for (int i = 0; i < numOfCounters; i++) { - availableCounters[i] = new ShopCounter(i); - } - - Shop shop = new Shop(availableCounters); + Shop shop = new Shop(numOfCounters, maxQueueSize); int id = 0; while (sc.hasNextDouble()) { double arrivalTime = sc.nextDouble(); double serviceTime = sc.nextDouble(); - initEvents[id] = new ArrivalEvent(arrivalTime, new Customer(id), shop, serviceTime); + initEvents[id] = new ArrivalEvent(new Customer(serviceTime, arrivalTime), shop); id += 1; } } diff --git a/test.sh b/test.sh index aab80a4..bd4af59 100644 --- a/test.sh +++ b/test.sh @@ -70,4 +70,7 @@ elif [ $num_failed -eq 0 ] then echo "$PROG: passed everything 🎉" fi + +# Run style checker +java -jar ./checkstyle.jar -c ./cs2030_checks.xml *.java # vim:noexpandtab:sw=4:ts=4