-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServeEvent.java
46 lines (37 loc) · 1.38 KB
/
ServeEvent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package cs2030.simulator;
import java.util.Optional;
import java.util.function.Function;
class ServeEvent extends CustomerAssignedEvent {
ServeEvent(double time, Customer customer, Server server) {
super(time, customer, server);
}
@Override
SimulatorState process(SimulatorState state) {
Server server = this.retrieveServer(state)
.serveCustomer(this.getCustomer(), this.getTime());
return state.updateServer(server);
}
@Override
Optional<Event> nextEvent(SimulatorState state) {
Function<Server, Optional<Event>> scheduleNextCustomer = s -> {
return s.nextInQueue().map(c -> new ServeEvent(s.estimateServeTime(c), c, s));
};
return Optional.<Event>of(
new DoneEvent(getCompletionTime(), this.getCustomer(), this.getServer(),
scheduleNextCustomer)
);
}
double getCompletionTime() {
return this.getTime() + this.getCustomer().getServiceTime();
}
@Override
public String toString() {
return String.format("%s serves by %s", super.toString(), this.getServer());
}
@Override
SimulatorStats updateStats(SimulatorState state, SimulatorStats stats) {
return stats
.trackWaitingTime(this.getTime() - this.getCustomer().getArrivalTime())
.trackCustomerServed();
}
}