-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelfCheck.java
More file actions
120 lines (96 loc) · 3.85 KB
/
SelfCheck.java
File metadata and controls
120 lines (96 loc) · 3.85 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package cs2030.simulator;
import java.util.Optional;
import java.util.function.Supplier;
import cs2030.util.ImList;
import cs2030.util.Pair;
class SelfCheck extends Server {
private final ImList<Pair<Pair<Double, Boolean>, Optional<Customer>>> waitCustomers;
SelfCheck(int id, int maxQ, int numSelfCheck) {
super(id, maxQ);
ImList<Pair<Pair<Double, Boolean>, Optional<Customer>>> temp =
ImList.<Pair<Pair<Double, Boolean>, Optional<Customer>>>of();
for (int i = 1; i <= numSelfCheck; i++) {
temp = temp.add(Pair.<Pair<Double, Boolean>,
Optional<Customer>>of(Pair.<Double, Boolean>of(0.0, true), Optional.empty()));
}
this.waitCustomers = temp;
}
SelfCheck(int id, int maxQ, ImList<Pair<Pair<Double, Boolean>,
Optional<Customer>>> waitCustomers, ImList<Customer> queue) {
super(id, maxQ, queue);
this.waitCustomers = waitCustomers;
}
SelfCheck(Server server, ImList<Pair<Pair<Double, Boolean>,
Optional<Customer>>> waitCustomers) {
super(server.getID(), server.getWaitUntil(), server.getCurrCustomer(),
server.getQueue(), server.getMaxQ(), server.getRestTime(), server.isIdle());
this.waitCustomers = waitCustomers;
}
double getWaitUntil() {
int i = 0;
double minWaitTime = this.waitCustomers.get(0).first().first();
for (; i < this.waitCustomers.size(); i++) {
if (this.waitCustomers.get(i).first().first() < minWaitTime) {
minWaitTime = this.waitCustomers.get(i).first().first();
}
}
return minWaitTime;
}
SelfCheck queue(Customer customer) {
return new SelfCheck(super.queue(customer), this.waitCustomers);
}
SelfCheck done(Customer customer) {
int i = 0;
while (i < this.waitCustomers.size()) {
if (this.waitCustomers.get(i).second().equals(Optional.<Customer>of(customer))) {
break;
}
i++;
}
ImList<Pair<Pair<Double, Boolean>, Optional<Customer>>> temp =
waitCustomers.set(i, Pair.<Pair<Double, Boolean>,
Optional<Customer>>of(Pair.<Double, Boolean>of(waitCustomers
.get(i)
.first().first(), true), Optional.empty()));
return new SelfCheck(super.getID(), super.getMaxQ(), temp, super.getQueue());
}
SelfCheck maybeRest() {
return this;
}
SelfCheck serve(Customer customer, double time) {
int i = 0;
while (i < this.waitCustomers.size()) {
if (this.waitCustomers.get(i).first().second() == true) {
break;
}
i++;
}
ImList<Pair<Pair<Double, Boolean>, Optional<Customer>>> temp =
waitCustomers.set(i, Pair.<Pair<Double, Boolean>,
Optional<Customer>>of(Pair.<Double, Boolean>of(time, false),
Optional.<Customer>of(customer)));
return new SelfCheck(super.getID(), super.getMaxQ(), temp,
(super.getQueue().size() != 0) ? super.getQueue().remove(0).second() :
super.getQueue());
}
boolean isIdle() {
ImList<Pair<Pair<Double, Boolean>, Optional<Customer>>> temp =
this.waitCustomers.filter(x -> x.first().second() == true);
return (temp.isEmpty()) ? false : true;
}
boolean isSelfCheck() {
return true;
}
@Override
public String toString() {
int i = 0;
while (i < this.waitCustomers.size()) {
if (this.waitCustomers.get(i).second().equals(Optional.empty())) {
break;
}
i++;
}
int count = (i == this.waitCustomers.size()) ? 0 : i;
return "self-check " + (super.getID() + count);
}
}