feat: add JoinQueueEvent
This commit is contained in:
committed by
Yadunand Prem
parent
681c5de6cf
commit
a02b575867
1
.gitignore
vendored
1
.gitignore
vendored
@@ -4,3 +4,4 @@ Lab1.pdf
|
|||||||
|
|
||||||
checkstyle.jar
|
checkstyle.jar
|
||||||
cs2030_checks.xml
|
cs2030_checks.xml
|
||||||
|
*.swp
|
||||||
|
|||||||
@@ -28,13 +28,7 @@ class ArrivalEvent extends Event {
|
|||||||
if (this.shop.isQueueFull()) {
|
if (this.shop.isQueueFull()) {
|
||||||
return new Event[] { new DepartureEvent(this.getTime(), customer, shop) };
|
return new Event[] { new DepartureEvent(this.getTime(), customer, shop) };
|
||||||
}
|
}
|
||||||
Queue queue = this.shop.getQueue();
|
return new Event[] { new JoinQueueEvent(customer, shop) };
|
||||||
System.out
|
|
||||||
.println(String.format("%s: %s joined queue %s",
|
|
||||||
super.toString(),
|
|
||||||
this.customer, queue));
|
|
||||||
queue.enq(customer);
|
|
||||||
return new Event[] {};
|
|
||||||
}
|
}
|
||||||
return new Event[] {
|
return new Event[] {
|
||||||
new ServiceBeginEvent(this.getTime(), customer, shop, availableCounter) };
|
new ServiceBeginEvent(this.getTime(), customer, shop, availableCounter) };
|
||||||
@@ -46,6 +40,6 @@ class ArrivalEvent extends Event {
|
|||||||
return String.format("%s: %s arrived %s",
|
return String.format("%s: %s arrived %s",
|
||||||
super.toString(),
|
super.toString(),
|
||||||
this.customer,
|
this.customer,
|
||||||
this.shop.getQueue());
|
this.shop.queueString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,10 +32,10 @@ class DepartureEvent extends Event {
|
|||||||
@Override
|
@Override
|
||||||
public Event[] simulate() {
|
public Event[] simulate() {
|
||||||
// when customer departs, check if there are customers in queue
|
// 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[] {};
|
return new Event[] {};
|
||||||
}
|
}
|
||||||
Customer c = (Customer) this.shop.getQueue().deq();
|
Customer c = this.shop.leaveQueue();
|
||||||
return new Event[] {
|
return new Event[] {
|
||||||
new ServiceBeginEvent(this.getTime(), c, this.shop, this.counter),
|
new ServiceBeginEvent(this.getTime(), c, this.shop, this.counter),
|
||||||
};
|
};
|
||||||
|
|||||||
24
JoinQueueEvent.java
Normal file
24
JoinQueueEvent.java
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
15
Shop.java
15
Shop.java
@@ -41,5 +41,20 @@ public class Shop {
|
|||||||
public boolean isQueueFull() {
|
public boolean isQueueFull() {
|
||||||
return this.queue.isFull();
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user