-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDriver.java
More file actions
337 lines (317 loc) · 11.8 KB
/
Driver.java
File metadata and controls
337 lines (317 loc) · 11.8 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
331
332
333
334
335
336
337
package master1;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
/**
* DSA Project Driver
*
* @author Meriel Stein, Jon Spratt
* @version 12.1.2016
*/
public class Driver {
/**
* Buffered reader for user input
*/
private static BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
public static void main(String[] args) {
// Instance variables to be used during application runtime
ShoppingCenter center = new ShoppingCenter();
Customer cust = new Customer();
Customer high_time_cust = cust;
Item item = null;
String item_name = "N/A";
String cust_name = "N/A";
String input = "";
// Primitives to be used during application runtime
boolean done = false;
int menuSelection = -1;
int restockThreshold = 0;
int amount = 0;
int lineNumber = 0;
try {
System.out.println("Welcome to the Shopping Center System!\n");
System.out.println("Please specify your stock.");
System.out.print("How many items do you have? ");
amount = Integer.parseInt(br.readLine().trim());
System.out.println(amount);
System.out.print("Please specify restocking amount: ");
restockThreshold = Integer.parseInt(br.readLine().trim());
System.out.println(restockThreshold);
AscendingOrderList<Item, String> inventory = new AscendingOrderList<Item, String>();
for (int i = 0; i < amount; i++) {
System.out.print(">>Enter item name : ");
String name = br.readLine().trim();
System.out.println(name);
System.out.print(">>How many " + name + "s? ");
int numItems = Integer.parseInt(br.readLine().trim());
System.out.println(numItems);
System.out.println(numItems + " " + name
+ "(s) have been placed in stock.");
item = new Item(name, restockThreshold, numItems);
inventory.add(item);
}
center.setInventory(inventory);
ListRAB<CheckoutLine> lines = new ListRAB<CheckoutLine>();
lines.add(0, new CheckoutLine("checkout line #1"));
lines.add(1, new CheckoutLine("checkout line #2"));
lines.add(2, new ExpressLine("express checkout line"));
System.out
.print("Please select the checkout line that should check out customers first (regular1/regular2/express): ");
input = br.readLine().trim();
System.out.println(input);
if (input.equalsIgnoreCase("regular2")) {
lines.remove(1);
lines.add(0, new CheckoutLine("checkout line #2"));
} else if (input.equalsIgnoreCase("express")) {
lines.remove(2);
lines.add(0, new ExpressLine("express checkout line"));
}
center.setCheckoutLines(lines);
// Application runtime loop
while (!done) {
displayMenu();
try {
System.out.print(">>Make your menu selection now: ");
menuSelection = Integer.parseInt(br.readLine().trim());
System.out.println(menuSelection);
switch (menuSelection) {
case 1: // Customer enters Shopping Center.
boolean successful = false;
while (!successful) {
System.out.print("Enter customer name: ");
cust_name = br.readLine().trim();
System.out.println(cust_name);
// add if customer is not in ShoppingCenter already
try {
center.addCustomer(new Customer(cust_name));
System.out.println("Customer " + cust_name
+ " is now in the Shopping Center.");
successful = true;
} catch (ListIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
}
}
break;
case 2: // Customer picks an item and places it the shopping
// cart.
if (center.getCustomers().isEmpty()) {
System.out
.println("No one is in the Shopping Center!");
break;
}
System.out.print(">>Enter customer name : ");
cust_name = br.readLine().trim();
System.out.println(cust_name);
try {
// Find customer by name
cust = center.getCustomer(cust_name);
System.out.print(">>What item does " + cust_name
+ " want? ");
item_name = br.readLine().trim();
System.out.println(item_name);
// Reduce center's inventory by one for that item.
center.adjustStock(item_name, -1);
// Add item to their cart.
cust.addItemToCart();
System.out.println("Customer " + cust.getName()
+ " now has " + cust.getCart()
+ " item(s) in their shopping cart.");
} catch (ItemException e) {
System.out.println(e.getMessage());
} catch (ListIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
}
// Increment shopping time for all customers
center.incrementTime();
break;
case 3: // Customer removes an item from the shopping cart.
if (center.getCustomers().isEmpty()) {
System.out.println("No one is in the Shopping Center!");
break;
} else {
successful = false;
while(!successful){
System.out.print(">>Enter customer name : ");
cust_name = br.readLine().trim();
System.out.println(cust_name);
try {
cust = center.getCustomer(cust_name);
cust.removeItemFromCart();
System.out.println("Customer " + cust.getName()
+ " now has " + cust.getCart()
+ " item(s) in their shopping cart.");
successful = true;
} catch (CartException e) { // Cart is already empty
System.out.println(e.getMessage());
} catch (ListIndexOutOfBoundsException e) {
//Customer not found
System.out.println(e.getMessage());
}
}
}
// Increment shopping time for all customers
center.incrementTime();
break;
case 4: // Customer is done shopping.
AscendingOrderList<Customer, String> customers = center
.getCustomers();
if (customers.isEmpty()) {
System.out
.println("No customers in the Shopping Center!");
break;
}
high_time_cust = customers.get(0);
for (int i = 1; i < customers.size(); i++) {
cust = customers.get(i);
if (cust.getShoppingTime() > high_time_cust
.getShoppingTime()) {
high_time_cust = cust;
} else {
high_time_cust = cust;
}
}
center.removeCustomer(high_time_cust);
// If the customer who has been in the Shopping Center
// the longest has no items in the cart, then s/he can
// either leave or return to shopping center without standing
// in line to check out. If the customer returns to
// shopping, the customer's shopping time is reset.
if (high_time_cust.getCart() == 0) {
System.out
.print("Should customer "
+ high_time_cust.getName()
+ " leave the Shopping Center or keep on shopping? Leave?(Y/N): ");
input = br.readLine().trim().toLowerCase();
if (input.startsWith("y")) {
System.out
.println("Customer "
+ high_time_cust.getName()
+ " is now leaving the Shopping Center.");
break;
} else {
high_time_cust.resetShoppingTime();
high_time_cust.leaveCheckout();
center.addCustomer(high_time_cust);
System.out
.println("Customer "
+ high_time_cust.getName()
+ " with 0 items returned to shopping.");
break;
}
}
// Determine which line to queue high_time_customer in
// and queue them
high_time_cust.enterCheckout();
CheckoutLine tempLine = center
.checkoutCustomer(high_time_cust);
System.out.println("After "
+ high_time_cust.getShoppingTime()
+ " minutes shopping Customer "
+ high_time_cust.getName() + " has entered "
+ tempLine.getName() + ".");
break;
case 5: // Customer checks out.
// Customers who are at the front of the lines can check
// out and leave or can decide to return and shop some
// more. Customers check out in a fair manner: all
// three checkout lines take turns in checking out
// customers. If there is no customer in the line whose
// turn
// it is to check out a customer, then the next checkout
// line that has customers in line will check out the
// first customer in line.
if (center.linesEmpty()) {
System.out.println("No customers in any line.");
break;
} else {
lines = center.getCheckoutLines();
CheckoutLine l = lines
.get(lineNumber = lineNumber % 3);
while (l.getLength() == 0) {
l = lines.get(lineNumber = ++lineNumber % 3);
}
Customer customer = l.serveCustomer();
System.out
.print("Should customer "
+ customer.getName()
+ " check out or keep on shopping? Checkout?(Y/N): ");
input = br.readLine().trim().toLowerCase();
System.out.println(input);
if (input.startsWith("y")) {
System.out
.println("Customer "
+ customer.getName()
+ " is now leaving the Shopping Center.");
} else {// If the customer decides to return to
// shopping, the time spent in the Shopping
// Center is
// reset.
customer.resetShoppingTime();
customer.leaveCheckout();
center.addCustomer(customer);
System.out.println("Customer "
+ customer.getName() + " with "
+ customer.getCart()
+ " items returned to shopping.");
}
}
break;
case 6: // Print info about customers who are shopping.
center.printCustomers();
break;
case 7: // Print info about customers in checkout lines.
center.printCheckoutLines();
break;
case 8: // Print info about items at or below re-stocking
// level.
center.printRestockableItems();
break;
case 9: // Reorder an item.
try {
System.out
.print(">>Enter item name to be re-ordered: ");
input = br.readLine().trim();
System.out.println(input);
center.getInventoryItem(input);
System.out.print(">>Enter restocking amount: ");
amount = Integer.parseInt(br.readLine().trim());
System.out.println(amount);
amount = center.adjustStock(input, amount);
System.out.println("Stock now has " + amount + " "
+ input + "s.");
} catch (ListIndexOutOfBoundsException e) {
System.out.println(input + " is not in stock!");
}
break;
case 10: // Close the Shopping Center.
System.out
.println("The Shopping Center is closing...come back tomorrow.");
done = true;
break;
}
} catch (NumberFormatException e) {
System.out.println("Re-check your input and try again.");
} catch (ListIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
}// end try/catch
} // end while(!done) loop
} catch (IOException e) {
System.out.println("Re-check your input and try again.");
}
} // end main
private static void displayMenu() {
System.out
.println("Select from the following menu:"
+ "\n\t1. Customer enters Shopping Center."
+ "\n\t2. Customer picks an item and places it the shopping cart."
+ "\n\t3. Customer removes an item from the shopping cart."
+ "\n\t4. Customer is done shopping."
+ "\n\t5. Customer checks out."
+ "\n\t6. Print info about customers who are shopping."
+ "\n\t7. Print info about customers in checkout lines."
+ "\n\t8. Print info about items at or below re-stocking level."
+ "\n\t9. Reorder an item."
+ "\n\t10. Close the Shopping Center.");
} // end displayMenu
} // end class Driver