feat: add JoinQueueEvent

This commit is contained in:
Yadunand Prem 2022-09-08 02:30:04 +08:00 committed by Yadunand Prem
parent 681c5de6cf
commit a02b575867
5 changed files with 44 additions and 10 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ Lab1.pdf
checkstyle.jar
cs2030_checks.xml
*.swp

View File

@ -28,13 +28,7 @@ class ArrivalEvent extends Event {
if (this.shop.isQueueFull()) {
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 JoinQueueEvent(customer, shop) };
}
return new Event[] {
new ServiceBeginEvent(this.getTime(), customer, shop, availableCounter) };
@ -46,6 +40,6 @@ class ArrivalEvent extends Event {
return String.format("%s: %s arrived %s",
super.toString(),
this.customer,
this.shop.getQueue());
this.shop.queueString());
}
}

View File

@ -32,10 +32,10 @@ class DepartureEvent extends Event {
@Override
public Event[] simulate() {
// when customer departs, check if there are customers in queue
if (this.shop.getQueue().isEmpty() || this.counter == null) {
if (this.shop.isQueueEmpty() || this.counter == null) {
return new Event[] {};
}
Customer c = (Customer) this.shop.getQueue().deq();
Customer c = this.shop.leaveQueue();
return new Event[] {
new ServiceBeginEvent(this.getTime(), c, this.shop, this.counter),
};

24
JoinQueueEvent.java Normal file
View File

@ -0,0 +1,24 @@
class JoinQueueEvent extends Event {
private Customer customer;
private Shop shop;
public JoinQueueEvent(Customer customer, Shop shop) {
super(customer.getArrivalTIme());
this.customer = customer;
this.shop = shop;
}
@Override
public Event[] simulate() {
this.shop.joinQueue(customer);
return new Event[] {};
}
@Override
public String toString() {
return String.format("%s: %s joined queue %s",
super.toString(),
this.customer, this.shop.queueString());
}
}

View File

@ -41,5 +41,20 @@ public class Shop {
public boolean isQueueFull() {
return this.queue.isFull();
}
public boolean isQueueEmpty() {
return this.queue.isEmpty();
}
public void joinQueue(Customer customer) {
this.queue.enq(customer);
}
public Customer leaveQueue() {
return (Customer) this.queue.deq();
}
public String queueString() {
return this.queue.toString();
}
}