nus/DepartureEvent.java
YADUNAND PREM ae8e984866 Finished Lab2
fix: update test.sh

feat: reformat and remote BaseShopEvent

chore: add comments
2022-11-08 18:03:35 +08:00

46 lines
1.1 KiB
Java

/**
* 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 Event {
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),
};
}
}