forked from jahchwe/store_operation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoreOperation.java
More file actions
202 lines (137 loc) · 5.18 KB
/
StoreOperation.java
File metadata and controls
202 lines (137 loc) · 5.18 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
import java.io.File;
import java.io.FileNotFoundException;
import java.time.LocalTime;
import java.util.ArrayList;
public class StoreOperation {
public static void main(String[] args) throws FileNotFoundException {
int time = 0;
ArrayList<CustomerNode> complete = new ArrayList<CustomerNode>();
String customerFile = args[0];
String queriesFile = args[1];
//File customers = new File("CustomersData.txt");
//File queries = new File("QueriesContent.txt");
File customers = new File(customerFile);
File queries = new File(queriesFile);
//one queue for all customers in a day
CustomerList today = new CustomerList();
today.createList(customers);
//getting queries
today.getQueries(queries);
//one queue to model customers in the store at any given time.
CustomerList store = new CustomerList();
Worker cashier = new Worker();
int tempLongBreak = 0;
int noon = 0;
//initialize first customer temp
CustomerNode temp = today.dequeue();
while (!today.isEmpty() || !store.isEmpty()) {
//if noon, reset clock to be 0 - 11:59:59;
if (time == 43200) {
noon = 1;
time = 0;
}
else if (time > 43200) {
noon = 1;
time = time%43200;
}
//customer enters store if its their time
//will not enqueue customers after 5PM
if ((noon == 0 && temp.seconds >= 18000 && time >= temp.seconds)|| (noon == 1 && time >= temp.seconds)) {//(time >= temp.seconds) {
store.enqueue(temp);
if (!today.isEmpty()) {
//update temp
temp = today.dequeue();
continue;
}
}
////if current number of people in the store is greater than the previous greatest, make greatest current
int currentLength = store.currentLength();
if (currentLength > today.longestLength) {
today.longestLength = currentLength;
}
////
////if we are open (i.e. it is 9AM or later) and folks are at the door waiting;
//32400 == 9AM
if ((noon == 0 && time >= 32400 && !store.isEmpty()) || (noon == 1 && !store.isEmpty() && time < 18000)) {
CustomerNode serving = store.dequeue();
complete.add(serving);
//calculate wait time by subtracting entering time from current time
serving.wait = Math.abs(time - serving.seconds);
//add to total number served
today.numServed++;
//end of cashier break; check if break is longer than the previous longest.
if (tempLongBreak > cashier.longest) {
cashier.longest = tempLongBreak;
tempLongBreak = 0;
}
//problem: need to add all nodes that occur during 300 sec interval to list to correct
//max list length
time += today.servetime;
//continue because time of serving already added;
continue;
}
//if its past closing time, set the rest of the customers' waiting times to 18000 - arrival time
if (noon == 1 && time >= 18000) {
temp.wait = Math.abs(18000 - temp.seconds);
while (!store.isEmpty()) {
//set wait for customers in store taht werent served
CustomerNode current = store.dequeue();
current.wait = Math.abs(18000 - current.seconds);
complete.add(current);
}
break;
}
////
//else if the store is empty;
if ((noon == 0 && time >= 32400 && store.isEmpty()) || (noon == 1 && time < 18000 && store.isEmpty())) {
//calculate cashier idle and break
cashier.totalIdle++;
tempLongBreak++;
}
if (store.isEmpty() && today.isEmpty() && noon == 0) {
//if no one else coming, cashier has rest of day off
cashier.totalIdle += 43200 - time + 18000;
tempLongBreak += 43200 - time + 18000;
break;
}
if (store.isEmpty() && today.isEmpty() && noon == 1) {
cashier.totalIdle += 18000 - time;
tempLongBreak += 18000 - time;
break;
}
time++;
}
//end of cashier break; check if break is longer than the previous longest.
if (tempLongBreak > cashier.longest) {
cashier.longest = tempLongBreak;
tempLongBreak = 0;
}
//handle inputted queries
handleQueries(today, cashier, complete);
System.out.println("Done");
// TODO Auto-generated method stub
}
public static void handleQueries(CustomerList input, Worker worker, ArrayList<CustomerNode> complete) {
//use query info to respond. Query objects created in CustomerList class
for (int i = 0; i < input.queryList.size(); i++) {
switch (input.queryList.get(i).queryCode) {
case 1:
System.out.printf("%-30s%s%d%n", input.queryList.get(i).name+ ":", " ", input.numServed);
break;
case 2:
System.out.printf("%-30s%s%d%n", input.queryList.get(i).name+ ":", " ", worker.longest);
break;
case 3:
System.out.printf("%-30s%s%d%n", input.queryList.get(i).name+ ":", " ", worker.totalIdle);
break;
case 4:
System.out.printf("%-30s%s%d%n", input.queryList.get(i).name+ ":", " ", input.longestLength);
break;
case 5:
CustomerNode asked = complete.get(input.queryList.get(i).CustomerID - 1);
System.out.printf("%-30s%s%d%n", input.queryList.get(i).name+ ":" + " " + input.queryList.get(i).CustomerID, " ", asked.wait);
break;
}
}
}
}