-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppManager.java
More file actions
330 lines (289 loc) · 13.5 KB
/
AppManager.java
File metadata and controls
330 lines (289 loc) · 13.5 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
class AppManager {
private static final String VERSION = "1.3";
private static final String TITLE = "Customer Counting Tool v" + VERSION;
private EventHandler handler;
private JFrame frame = new JFrame(TITLE);
private String userDefinedTitle = "";
private JPanel customerQueue = new JPanel(new FlowLayout(FlowLayout.TRAILING));
private JPanel serviceArea;
private NavigableMap<Integer, JPanel> customersInQueue = new TreeMap<>();
private JPanel[] customersBeingServed;
private JLabel[] customersBeingServedLabels;
private JLabel statLabel;
AppManager() {
this.frame.setLayout(new BorderLayout());
this.frame.setBounds(30, 30, 1000, 600);
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.userDefinedTitle = JOptionPane.showInputDialog(frame, "What are you counting? (This will also be the savefile name.)");
this.frame.setTitle(TITLE + " - " + this.userDefinedTitle);
boolean correctInput = false;
int maxServers = 0;
do {
String userInput = JOptionPane.showInputDialog(frame, "How many servers do we have?");
try {
maxServers = Integer.parseInt(userInput);
correctInput = true;
} catch (NumberFormatException e) {
System.err.println(e);
}
} while (!correctInput);
this.handler = new EventHandler(maxServers);
this.customersBeingServed = new JPanel[maxServers];
this.customersBeingServedLabels = new JLabel[maxServers];
this.initToolBar();
this.initGraphics();
this.initStatBar();
this.frame.setVisible(true);
}
private void updateStats() {
this.statLabel.setText(this.handler.getStats());
}
private void customerArrives() {
int arrivalId = this.handler.arrive();
JPanel sprite = Customer.getQueuePanel();
JPopupMenu menu = new JPopupMenu();
JMenuItem leave = new JMenuItem(new AbstractAction() {
public void actionPerformed(ActionEvent actionEvent) {
AppManager.this.customerLeaves(arrivalId);
}
});
leave.setText("Leave queue");
menu.add(leave);
sprite.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent mouseEvent) {
if (SwingUtilities.isRightMouseButton(mouseEvent)) {
menu.show(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent.getY());
}
}
public void mousePressed(MouseEvent mouseEvent) {}
public void mouseReleased(MouseEvent mouseEvent) {}
public void mouseEntered(MouseEvent mouseEvent) {}
public void mouseExited(MouseEvent mouseEvent) {}
});
this.customersInQueue.put(arrivalId, sprite);
this.customerQueue.removeAll();
for (Integer id : this.customersInQueue.descendingKeySet()) {
this.customerQueue.add(this.customersInQueue.get(id));
}
this.customerQueue.revalidate();
this.customerQueue.repaint();
this.updateStats();
}
private void customerLeaves(int customerId) {
this.handler.leave(customerId);
JPanel sprite = this.customersInQueue.get(customerId);
this.customerQueue.remove(sprite);
this.customersInQueue.remove(customerId);
this.customerQueue.revalidate();
this.customerQueue.repaint();
}
private void serveCustomer(int serverIndex) {
int customerId = this.handler.serve(serverIndex);
if (customerId != -1) {
JPanel sprite = this.customersInQueue.get(customerId);
this.customerQueue.remove(sprite);
this.customersInQueue.remove(customerId);
this.customersBeingServed[serverIndex].setBackground(Color.GREEN);
this.customersBeingServedLabels[serverIndex].setText("");
this.customerQueue.revalidate();
this.customerQueue.repaint();
} else {
System.out.println("UI: -1 returned by handler::serve");
}
this.updateStats();
}
private void doubleServeCustomer(int serverIndex) {
int[] customersServed = this.handler.doubleServe(serverIndex);
this.customersBeingServedLabels[serverIndex].setText("");
int serveCount = 0;
for (int customerId : customersServed) {
if (customerId != -1) {
JPanel sprite = this.customersInQueue.get(customerId);
this.customerQueue.remove(sprite);
this.customersInQueue.remove(customerId);
this.customersBeingServed[serverIndex].setBackground(Color.GREEN);
serveCount++;
} else {
System.out.println("UI: -1 returned by handler::doubleServe");
}
}
if (serveCount == 2) this.customersBeingServedLabels[serverIndex].setText("2");
this.customerQueue.revalidate();
this.customerQueue.repaint();
this.updateStats();
}
private void doneServing(int serverIndex) {
this.handler.done(serverIndex);
this.customersBeingServed[serverIndex].setBackground(Color.BLACK);
this.customersBeingServedLabels[serverIndex].setText("");
this.updateStats();
}
private void terminate() {
List<Customer> allCusts = this.handler.getAllCustsSorted();
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss aa");
Date now = new Date();
try {
FileWriter writer = new FileWriter(Parser.dashed(this.userDefinedTitle) + "-timestamps-" + now.getTime() + ".csv");
writer.append("Arrive,Left,Serve,Done,Server\n");
for (Customer c : allCusts) {
writer.append(formatter.format(c.getArrival()) + ",");
if (c.getLeft() != null) {
writer.append(formatter.format(c.getLeft()) + ",");
} else {
writer.append("NA,");
}
if (c.getServed() != null) {
writer.append(formatter.format(c.getServed()) + ",");
} else {
writer.append("NA,");
}
if (c.getDone() != null) {
writer.append(formatter.format(c.getDone()) + ",");
} else {
writer.append("NA,");
}
if (c.getServed() != null) {
writer.append(c.getServerId() + "\n");
} else {
writer.append("NA\n");
}
}
writer.flush();
writer.close();
writer = new FileWriter(Parser.dashed(this.userDefinedTitle) + "-data-" + now.getTime() + ".csv");
writer.append("Inter-arrival, Queue time, Service time\n , ");
for (int i = 0; i < allCusts.size(); i++) {
Customer c = allCusts.get(i);
if (i == 0) {
writer.append((double) (c.getQueueTime()) / 1000 + ", ");
writer.append((double) (c.getServiceTime()) / 1000 + "\n");
} else {
writer.append((double) (c.getArrival().getTime() - allCusts.get(i - 1).getArrival().getTime()) / 1000 + ", ");
writer.append((double) (c.getQueueTime()) / 1000 + ", ");
writer.append((double) (c.getServiceTime()) / 1000 + "\n");
}
}
writer.flush();
writer.close();
} catch (IOException e) {
System.err.println(e);
}
this.frame.dispose();
}
private void initGraphics() {
JPanel graphics = new JPanel();
graphics.setLayout(new GridLayout(1, 2));
graphics.setBackground(Color.BLACK);
this.customerQueue.setBackground(Color.BLACK);
this.serviceArea = new JPanel();
this.serviceArea.setLayout(new BoxLayout(serviceArea, BoxLayout.Y_AXIS));
this.serviceArea.setBackground(Color.BLACK);
for (int i = 0; i < this.handler.getServerCount(); i++) {
int serverIndex = i;
JPanel serverToolbar = new JPanel(new BorderLayout());
serverToolbar.setBackground(Color.BLACK);
JPanel customerServedHolder = new JPanel(new FlowLayout(FlowLayout.LEFT));
customerServedHolder.setBackground(Color.BLACK);
JPanel customerServed = Customer.getServingPanel();
customerServed.setBackground(Color.BLACK);
JLabel customerLabel = new JLabel("");
customerLabel.setLayout(new FlowLayout(FlowLayout.CENTER));
customerLabel.setBackground(null);
customerServed.add(customerLabel);
this.customersBeingServed[i] = customerServed;
this.customersBeingServedLabels[i] = customerLabel;
customerServedHolder.add(customerServed);
serverToolbar.add(customerServedHolder, BorderLayout.WEST);
JPanel serverController = new JPanel();
serverController.setBackground(Color.BLACK);
JButton serveButton = new JButton("Serve");
serveButton.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent mouseEvent) {
AppManager.this.serveCustomer(serverIndex);
}
public void mousePressed(MouseEvent mouseEvent) {}
public void mouseReleased(MouseEvent mouseEvent) {}
public void mouseEntered(MouseEvent mouseEvent) {}
public void mouseExited(MouseEvent mouseEvent) {}
});
serverController.add(serveButton);
JButton doubleServeButton = new JButton("Serve x2");
doubleServeButton.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent mouseEvent) {
AppManager.this.doubleServeCustomer(serverIndex);
}
public void mousePressed(MouseEvent mouseEvent) {}
public void mouseReleased(MouseEvent mouseEvent) {}
public void mouseEntered(MouseEvent mouseEvent) {}
public void mouseExited(MouseEvent mouseEvent) {}
});
serverController.add(doubleServeButton);
JButton doneButton = new JButton("Done");
doneButton.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent mouseEvent) {
AppManager.this.doneServing(serverIndex);
}
public void mousePressed(MouseEvent mouseEvent) {}
public void mouseReleased(MouseEvent mouseEvent) {}
public void mouseEntered(MouseEvent mouseEvent) {}
public void mouseExited(MouseEvent mouseEvent) {}
});
serverController.add(doneButton);
JPanel serverControllerHolder = new JPanel(new FlowLayout(FlowLayout.CENTER));
serverControllerHolder.setBackground(Color.BLACK);
serverControllerHolder.add(serverController);
serverToolbar.add(serverControllerHolder);
this.serviceArea.add(serverToolbar);
}
graphics.add(this.customerQueue);
graphics.add(this.serviceArea);
this.frame.add(graphics);
}
private void initStatBar() {
this.statLabel = new JLabel(this.handler.getStats());
JPanel statBar = new JPanel(new FlowLayout(FlowLayout.LEFT));
statBar.setBackground(Color.BLACK);
statBar.add(this.statLabel);
this.statLabel.setForeground(Color.GRAY);
this.frame.add(statBar, BorderLayout.SOUTH);
}
private void initToolBar() {
JPanel customerToolbar = new JPanel();
customerToolbar.setLayout(new FlowLayout());
customerToolbar.setBackground(Color.BLACK);
JButton arriveButton = new JButton("<html><u>A</u>rrive</html>");
arriveButton.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
AppManager.this.customerArrives();
}
public void mousePressed(MouseEvent mouseEvent) {}
public void mouseReleased(MouseEvent mouseEvent) {}
public void mouseEntered(MouseEvent mouseEvent) {}
public void mouseExited(MouseEvent mouseEvent) {}
});
customerToolbar.add(arriveButton);
JButton endButton = new JButton("End & Export");
endButton.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent mouseEvent) {
AppManager.this.terminate();
}
public void mousePressed(MouseEvent mouseEvent) {}
public void mouseReleased(MouseEvent mouseEvent) {}
public void mouseEntered(MouseEvent mouseEvent) {}
public void mouseExited(MouseEvent mouseEvent) {}
});
customerToolbar.add(endButton);
this.frame.add(customerToolbar, BorderLayout.NORTH);
}
}