-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathRestaurant.java
More file actions
249 lines (219 loc) · 7.83 KB
/
Restaurant.java
File metadata and controls
249 lines (219 loc) · 7.83 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import java.util.*;
class Table implements Comparable<Table> {
private final int size; // number of chairs
private int freeSpace; // number of free chairs
public Table(int size) {
this.size = size;
this.freeSpace = size;
}
public int getSize() {
return size;
}
public int getFreeSpace() {
return freeSpace;
}
public void setFreeSpace(int freeSpace) {
this.freeSpace = this.freeSpace - freeSpace;
}
//Tables are sorting according to the rules
@Override
public int compareTo(Table table) {
boolean isEmpty1 = this.getFreeSpace() == this.getSize();
boolean isEmpty2 = table.getFreeSpace() == table.getSize();
//Prior to empty tables
if (!isEmpty1 && isEmpty2) {
return 1;
} else if (isEmpty1 && !isEmpty2) {
return -1;
} else {
//shift right table with no available places
if (this.getFreeSpace() == 0) {
return 1;
} else if (table.getFreeSpace() == 0) {
return -1;
} else {
//if there are two tables with the same free space choose smaller one
if (this.getFreeSpace() - table.getFreeSpace() == 0) {
return this.getSize() - table.getSize();
} else {
return this.getFreeSpace() - table.getFreeSpace();
}
}
}
}
}
class ClientsGroup {
private final int size; // number of clients
private Table table; // table
public ClientsGroup(int size) {
this.size = size;
}
public int getSize() {
return size;
}
public Table getTable() {
return table;
}
public void setTable(Table table) {
this.table = table;
}
}
//Creating singleton
class RestManager {
private static final RestManager instance = new RestManager();
private final List<Table> tableList = new ArrayList<>();
public final List<ClientsGroup> clientsQueue = new ArrayList<>();
private final List<ClientsGroup> clientsAtTables = new ArrayList<>();
private RestManager() {
//Create a simple table set
for (int i = 2; i < 7; i++) {
tableList.add(new Table(i));
}
}
//Return singleton instance
public static synchronized RestManager getInstance() {
return instance;
}
//Searching a free table in the table list
public boolean searchTable(ClientsGroup client) {
for (Table table : tableList) {
if (table.getFreeSpace() == 0) break;
if (table.getFreeSpace() >= client.getSize()) {
//add the table link to the client, decrease table's free space and add the client to the client list
client.setTable(table);
table.setFreeSpace(client.getSize());
clientsAtTables.add(client);
Collections.sort(tableList);
return true;
}
}
return false;
}
// new client(s) show up
public void onArrive(ClientsGroup group) {
//if table hasn't found add client to queue
if (!searchTable(group)) {
clientsQueue.add(group);
System.out.println("There is no space for a new client =(");
} else {
System.out.println("Place for a new client has found");
}
}
// client(s) leave, either served or simply abandoning the queue
public void onLeave(ClientsGroup group) {
boolean isFound = false;
//removing the client from the client list and restore a table free space
clientsAtTables.remove(group);
group.getTable().setFreeSpace(-1 * group.getSize());
Collections.sort(tableList);
//Searching a client from the queue for a new space
if (!clientsQueue.isEmpty()) {
System.out.println("Searching a client from queue....");
System.out.println("Before");
showQueue();
Iterator iterator = clientsQueue.iterator();
while (iterator.hasNext()) {
ClientsGroup client = (ClientsGroup)iterator.next();
if (searchTable(client)) {
//Remove the client from the queue
iterator.remove();
isFound = true;
System.out.println("Place for " + client.getSize() + " client has found");
}
}
if (!isFound) {
System.out.println("There is no suitable client from the queue");
}
}
else {
System.out.println("Queue is empty");
}
}
// return table where a given client group is seated,
// or null if it is still queuing or has already left
public Table lookup(ClientsGroup group) {
return group.getTable();
}
//auxiliary functions
public void showQueue() {
System.out.print("Tables: ");
for (Table table : tableList) {
System.out.print("(" + table.getFreeSpace() + "\\" + table.getSize() + ") ");
}
System.out.print("\nQueue: ");
for (ClientsGroup client : clientsQueue) {
System.out.print(client.getSize() + " ");
}
System.out.println();
}
public ClientsGroup getRandomClient() {
if (!clientsAtTables.isEmpty()) {
return clientsAtTables.get(new Random().nextInt(clientsAtTables.size()));
}
return null;
}
}
public class Restaurant {
public static void main(String[] args) throws InterruptedException {
//Create a queue of clients
Thread t1 = new Thread(new ClientsQueue());
t1.start();
Thread.sleep(10_000);
//Random client decides to leave
Thread t2 = new Thread(new EnoughFun());
t2.start();
/*Thread.sleep(15_000);
t1.interrupt();
t2.interrupt();*/
}
}
class ClientsQueue extends Thread {
@Override
public void run() {
RestManager manager = RestManager.getInstance();
while (true) {
try {
Thread.sleep((int) (Math.random() * 5000));
ClientsGroup clientsGroup = new ClientsGroup(new Random().nextInt(6) + 1);
System.out.println("\nNew client: " + clientsGroup.getSize());
System.out.println("Before");
manager.showQueue();
manager.onArrive(clientsGroup);
System.out.println("After");
manager.showQueue();
} catch (InterruptedException e) {
break;
}
}
}
}
class EnoughFun extends Thread {
@Override
public void run() {
RestManager manager = RestManager.getInstance();
while (true) {
try {
Thread.sleep((int) (Math.random() * 5000));
ClientsGroup clientsGroup;
//Choose client from queue or from table
if (Math.random() < 0.5) {
clientsGroup = manager.getRandomClient();
if (clientsGroup != null) {
System.out.println("\nClient " + clientsGroup.getSize() + " has left a table");
manager.onLeave(clientsGroup);
System.out.println("After");
manager.showQueue();
}
} else {
//There is no manager work if client(s) from queue decide to leave
if (!manager.clientsQueue.isEmpty()) {
manager.clientsQueue.remove(new Random().nextInt(manager.clientsQueue.size()));
System.out.println("\nSomeone has left the queue...");
}
}
} catch (InterruptedException e) {
break;
}
}
}
}