-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
84 lines (74 loc) · 2.39 KB
/
Server.java
File metadata and controls
84 lines (74 loc) · 2.39 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
import java.util.*;
class Server {
private static int COUNTS = 0;
private int id;
private Optional<Customer> nowServing = Optional.empty();
private Optional<Customer> secondServing = Optional.empty();
private List<Customer> doneServing = new ArrayList<>();
Server() {
this.id = ++COUNTS;
}
void doubleServe(Customer first, Customer second) {
this.nowServing.ifPresent(c -> {
c.done();
doneServing.add(c);
});
this.secondServing.ifPresent(c -> {
c.done();
doneServing.add(c);
});
assert first != null && second != null;
first.serve(this.id);
second.serve(this.id);
this.nowServing = Optional.of(first);
this.secondServing = Optional.of(second);
}
void serve(Customer toServe) {
this.nowServing.ifPresent(c -> {
c.done();
doneServing.add(c);
});
this.secondServing.ifPresent(c -> {
c.done();
doneServing.add(c);
this.secondServing = Optional.empty();
});
assert toServe != null;
toServe.serve(this.id);
this.nowServing = Optional.of(toServe);
this.print();
}
void done() {
this.nowServing.ifPresent(c -> {
c.done();
doneServing.add(c);
this.nowServing = Optional.empty();
this.print();
});
this.secondServing.ifPresent(c -> {
c.done();
doneServing.add(c);
this.secondServing = Optional.empty();
this.print();
});
}
int getDoneCount() {
return this.doneServing.size();
}
List<Customer> getAllCusts() {
List<Customer> allCusts = new ArrayList<>();
this.nowServing.ifPresent(allCusts::add);
this.secondServing.ifPresent(allCusts::add);
allCusts.addAll(this.doneServing);
return allCusts;
}
private void print() {
System.out.println("----- SERVER NO. " + id + " -----");
System.out.println("--- Now Serving ---");
System.out.println(this.nowServing);
System.out.println(this.secondServing);
System.out.println("--- Done Serving ---");
System.out.println(this.doneServing);
System.out.println("");
}
}