feat: clean up DepartureEvent

This commit is contained in:
Yadunand Prem 2022-09-08 16:07:52 +08:00
parent 8d5961fdd3
commit b33ff933ec
3 changed files with 7 additions and 25 deletions

View File

@ -37,7 +37,7 @@ class ArrivalEvent extends Event {
if (!this.shop.isQueueFull()) {
return new Event[] { new JoinShopQueueEvent(customer, shop) };
}
return new Event[] { new DepartureEvent(this.getTime(), customer, shop) };
return new Event[] { new DepartureEvent(this.getTime(), customer) };
}

View File

@ -7,21 +7,11 @@
*/
class DepartureEvent extends Event {
private ServiceCounter counter;
private Customer customer;
private Shop shop;
public DepartureEvent(double time, Customer customer, Shop shop) {
public DepartureEvent(double time, Customer customer) {
super(time);
this.customer = customer;
this.shop = shop;
}
public DepartureEvent(double time, Customer customer, Shop shop, ServiceCounter counter) {
super(time);
this.customer = customer;
this.shop = shop;
this.counter = counter;
}
@Override
@ -32,15 +22,7 @@ class DepartureEvent extends Event {
@Override
public Event[] simulate() {
// when customer departs, check if there are customers in queue
if (this.shop.isQueueEmpty() || this.counter == null) {
return new Event[] {};
}
Customer c = this.shop.leaveQueue();
// Move this to ServiceEndEvent
return new Event[] {
new ServiceBeginEvent(this.getTime(), c, this.shop, this.counter),
};
return new Event[] {};
}
}

View File

@ -33,13 +33,13 @@ class ServiceEndEvent extends Event {
if (!this.shop.isQueueEmpty()) {
return new Event[] {
new DepartureEvent(this.getTime(), this.customer, this.shop, this.counter),
new DepartureEvent(this.getTime(), this.customer),
new ServiceBeginEvent(this.getTime(), serviceCustomer, this.shop, this.counter),
new JoinCounterQueueEvent(this.getTime(), this.shop.leaveQueue(), this.shop, counter)
};
}
return new Event[] {
new DepartureEvent(this.getTime(), this.customer, this.shop, this.counter),
new DepartureEvent(this.getTime(), this.customer),
new ServiceBeginEvent(this.getTime(), serviceCustomer, this.shop, this.counter),
};
}
@ -47,12 +47,12 @@ class ServiceEndEvent extends Event {
// customers in the shop queue.
if (!this.shop.isQueueEmpty()) {
return new Event[] {
new DepartureEvent(this.getTime(), this.customer, this.shop, this.counter),
new DepartureEvent(this.getTime(), this.customer),
new ServiceBeginEvent(this.getTime(), this.shop.leaveQueue(), this.shop, this.counter),
};
}
// else there are no more customers in the queue, and the counter can be freed
this.counter.free();
return new Event[] { new DepartureEvent(this.getTime(), this.customer, this.shop, this.counter) };
return new Event[] { new DepartureEvent(this.getTime(), this.customer) };
}
}