-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
73 lines (59 loc) · 2.21 KB
/
Copy pathServer.java
File metadata and controls
73 lines (59 loc) · 2.21 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.function.Supplier;
class Server {
private final int serverId;
private final double serverFreeTime;
private final Queue queue;
private final Supplier<Double> restTimes;
Server(int serverId, double serverFreeTime, Queue queue, Supplier<Double> restTimes) {
this.serverId = serverId;
this.serverFreeTime = serverFreeTime;
this.queue = queue;
this.restTimes = restTimes;
}
public int getServerId() {
return this.serverId;
}
public Queue getQueue() {
return this.queue;
}
public double getServerFreeTime() {
return this.serverFreeTime;
}
public Supplier<Double> getRestTime() {
return this.restTimes;
}
public boolean serverAvail(Customer cust) {
return this.serverFreeTime <= cust.getArrivalTime();
}
public Server serveWaitingCustomer(Customer cust) {
ImList<Customer> waitingCust = this.getQueue().getQueueList();
for (Customer customer : waitingCust) {
if (customer == cust) {
int index = waitingCust.indexOf(cust);
waitingCust = waitingCust.remove(index);
}
}
return new Server(this.serverId, this.serverFreeTime,
new Queue(this.getQueue().getQMax(), waitingCust), this.restTimes);
}
public Server addCustomer(Customer cust) {
ImList<Customer> customers = this.getQueue().getQueueList();
if (customers.size() < this.getQueue().getQMax()) {
customers = customers.add(cust);
return new Server(this.serverId, this.serverFreeTime,
new Queue(this.getQueue().getQMax(), customers), this.restTimes);
} else {
return this; // will not add anymore if Queue is full
}
}
public Server setFreeTime(double time) {
return new Server(this.serverId, time, this.queue, this.restTimes);
}
public boolean queueNotFull() {
return this.getQueue().getCurrentQ() < this.getQueue().getQMax();
}
public String toString() {
return String.format("%d %.3f %s", this.getServerId(),
this.getServerFreeTime(), this.getQueue().toString());
}
}