-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserInterface.java
More file actions
558 lines (492 loc) · 17.5 KB
/
Copy pathUserInterface.java
File metadata and controls
558 lines (492 loc) · 17.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
import java.util.*;
import java.text.*;
import java.io.*;
public class UserInterface {
private static UserInterface userInterface;
private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static Warehouse warehouse;
private static final int EXIT = 0;
private static final int ADD_CLIENT = 1;
private static final int SHOW_CLIENTS = 2;
private static final int EDIT_CLIENT_ADDRESS = 3;
private static final int EDIT_CLIENT_PHONE = 4;
private static final int ADD_PRODUCT = 5;
private static final int SHOW_PRODUCTS = 6;
private static final int EDIT_PRODUCT_PRICE = 7;
private static final int ADD_SUPPLIER = 8;
private static final int SHOW_SUPPLIERS = 9;
private static final int EDIT_SUPPLIERS_ADDRESS = 10;
private static final int ADD_PRODUCT_TO_SUPPLIER = 11;
private static final int DISPLAY_PRODUCTS_OF_SUPPLIER = 12;
private static final int ADD_TO_CART = 13;
private static final int DISPLAY_CART = 14;
private static final int EDIT_CART = 15;
private static final int PROCESS_ORDER = 16;
private static final int DISPLAY_WAITLIST = 17;
private static final int RECEIVE_SHIPMENT = 18;
private static final int ACCEPT_PAYMENT = 19;
private static final int GET_TRANSACTIONS = 20;
private static final int SAVE = 21;
private static final int RETRIEVE = 22;
private static final int HELP = 23;
private UserInterface() {
if (yesOrNo("Look for saved data and use it?")) {
retrieve();
} else {
warehouse = Warehouse.instance(); // Instantiate Warehouse singleton
}
}
public static UserInterface instance() {
if (userInterface == null) {
return userInterface = new UserInterface();
} else {
return userInterface;
}
}
public String getToken(String prompt) {
do {
try {
System.out.println(prompt);
String line = reader.readLine();
StringTokenizer tokenizer = new StringTokenizer(line, "\n\r\f");
if (tokenizer.hasMoreTokens()) {
return tokenizer.nextToken();
}
} catch (IOException ioe) {
System.exit(0);
}
} while (true);
}
private boolean yesOrNo(String prompt) {
String more = getToken(prompt + " (Y|y)[es] or anything else for no");
if (more.charAt(0) != 'y' && more.charAt(0) != 'Y') {
return false;
}
return true;
}
public int getNumber(String prompt) {
do {
try {
String item = getToken(prompt);
Integer num = Integer.valueOf(item);
return num.intValue();
} catch (NumberFormatException nfe) {
System.out.println("Please input a number ");
}
} while (true);
}
public Calendar getDate(String prompt) {
do {
try {
Calendar date = new GregorianCalendar();
String item = getToken(prompt);
DateFormat df = SimpleDateFormat.getDateInstance(DateFormat.SHORT);
date.setTime(df.parse(item));
return date;
} catch (Exception fe) {
System.out.println("Please input a date as mm/dd/yy");
}
} while (true);
}
public int getCommand() {
do {
try {
int value = Integer.parseInt(getToken("Enter command (" + HELP + " for help) :"));
if (value >= EXIT && value <= HELP) {
return value;
}
} catch (NumberFormatException nfe) {
System.out.println("Enter a number");
}
} while (true);
}
// ADD_CLIENT : 1
public void addClient() {
String name = getToken("Enter client name: ");
String address = getToken("Enter address: ");
String phone = getToken("Enter phone number: ");
//String id = getToken("Enter ID: ");
Client result;
result = warehouse.addClient(name, address, phone);
if (result == null) {
System.out.println("Could not add member");
}
else {
System.out.println(result);
}
}
// SHOW_CLIENTS : 2
public void showClients() {
Iterator<Client> allClients = warehouse.getClients();
while (allClients.hasNext()) {
Client client = (Client) (allClients.next());
System.out.println(client.toString());
}
}
// EDIT_CLIENT_ADDRESS : 3
public void editClientAddress() {
//Iterator allClients = warehouse.getClients();
// get client id
String targetId = getToken("Please enter ID of target client: ");
// get new address
String newAddress = getToken("Please enter new address of client: ");
// search for client by id
// pass information to warehouse (bool function, false if not found)
if(warehouse.editClientAddress(targetId, newAddress)) {
System.out.println("Address updated.");
}
else {
System.out.println("Client ID " + targetId + " not found.");
}
}
// EDIT_CLIENT_PHONE : 4
public void editClientPhone() {
// get client id
String targetId = getToken("Please enter ID of target client: ");
// get new phone number
String newPhone = getToken("Please enter new phone number of client: ");
// set client's phone number to the new value
if(warehouse.editClientPhone(targetId, newPhone)) {
System.out.println("Phone number updated.");
}
else {
System.out.println("Client ID " + targetId + " not found.");
}
}
// ADD_PRODUCT : 5
public void addProduct() {
Product result;
do {
String name = getToken("Enter product name: ");
int quantity = getNumber("Enter quantity: ");
Double price = Double.parseDouble(getToken("Enter Value: "));
result = warehouse.addProduct(name, quantity, price);
if (result != null) {
System.out.println(result);
} else {
System.out.println("Product could not be added");
}
if (!yesOrNo("Add more Products?")) {
break;
}
} while (true);
}
// SHOW_PRODUCTS : 6
public void showProducts() {
Iterator<Product> allProducts = warehouse.getProducts();
while (allProducts.hasNext()) {
Product product = (Product) (allProducts.next());
System.out.println(product.toString());
}
}
// EDIT_PRODUCT_PRICE : 7
public void editProductPrice() {
//Iterator allProducts = warehouse.getProducts();
String name = getToken("Please enter name of target product: ");
Double price = Double.parseDouble(getToken("Enter new product price: "));
if(warehouse.editProductPrice(name, price)) {
System.out.println("Price updated.");
}
else {
System.out.println("Product name " + name + " not found.");
}
}
// ADD_SUPPLIER : 8
public void addSupplier() {
String name = getToken("Enter supplier name: ");
String address = getToken("Enter address: ");
Supplier result = warehouse.addSupplier(name, address);
if (result == null) {
System.out.println("Unable to add supplier");
} else {
System.out.println(result);
}
}
// SHOW_SUPPLIERS : 9
public void showSuppliers() {
Iterator<Supplier> allSuppliers = warehouse.getSuppliers();
while (allSuppliers.hasNext()) {
Supplier supplier = (Supplier) (allSuppliers.next());
System.out.println(supplier.toString());
}
}
// EDIT_SUPPLIER_ADDRESS : 10
public void editSupplierAddress() {
// get supplier name
String targetName = getToken("Please enter Name of target supplier: ");
// get new address
String newAddress = getToken("Please enter new address of supplier: ");
// search for supplier by name
// pass information to warehouse (bool function, false if not found)
if (warehouse.editSupplierAddress(targetName, newAddress)) {
System.out.println("Address updated.");
} else {
System.out.println("Supplier Name " + targetName + " not found.");
}
}
// ADD_PRODUCT_TO_SUPPLIER : 11
public void addProductToSupplier() {
// get supplier name
String targetName = getToken("Please enter Name of target supplier: ");
// get name of product
String productName = getToken("Please enter Name of product to be added: ");
// get suppliers price for product
float price = Float.parseFloat(getToken("Please enter the suppliers price of the product to be added: "));
// search for product by name
Iterator<Product> allProducts = warehouse.getProducts();
Product product = (Product) (allProducts.next());
while (allProducts.hasNext() && !(productName.equals(product.getName()))) {
product = (Product) (allProducts.next());
}
if (warehouse.insertProductToSupplier(targetName, product, price)) {
System.out.println("Product added.");
} else {
System.out.println("Supplier Name " + targetName + " not found.");
}
}
// DISPLAY_PRODUCTS_OF_SUPPLIER : 12
public void displayProductsOfSupplier() {
String targetName = getToken("Please enter Name of target supplier: ");
// display
if (!warehouse.showProductsOfSupplier(targetName)) {
System.out.println("Supplier Name " + targetName + " not found.");
}
}
// ADD_TO_CART : 13
private void addToCart() { // Will need: clientId, productName, quantity
System.out.println("Add to Cart selected.");
String clientId = getToken("Enter Client ID");
String productName = getToken("Enter Product Name");
int quantity = getNumber("Enter quantity");
// Check if client exists
if (!warehouse.clientExists(clientId)) {
System.out.println("Error: client not found");
return; // Stop here if not found
} else {
System.out.println("ID " + clientId + " found.");
}
// Check if product exists
if (!warehouse.productExists(productName)) {
System.out.println("Error: product not found");
return; // Stop here if not found
} else {
System.out.println("Name " + productName + " found.");
}
// Next, instantiate a CartItem object and add it to the Client's cart (CartItem
// list)
if (warehouse.addToCart(clientId, productName, quantity)) {
System.out.println("Successfully added item to cart");
} else {
System.out.println("Error: failed to add item to cart");
}
}
// DISPLAY_CART : 14
public void displayCart() {
String clientId = getToken("Please enter ID of target client: ");
// warehouse.displayCart(clientId);
Iterator<CartItem> cart = (warehouse.getClientById(clientId)).getCartItems();
while (cart.hasNext()) {
CartItem item = (CartItem) cart.next();
System.out.println(item.toString());
}
}
// EDIT_CART : 15
public void editCart() {
String clientId = getToken("Please enter the ID of the client whose cart to edit");
if (warehouse.clientExists(clientId)) { // Check if client exists
warehouse.displayCart(clientId);
String productName = getToken("Please enter the name of the product in the cart");
if (warehouse.inCart(clientId, productName)) { // Check if product exists in the client's cart **** CHANGE CODE TO REFLECT THIS ****
int newQuant = getNumber("Please enter a new quantity for " + productName + ", or 0 to remove");
warehouse.editCart(clientId, productName, newQuant);
}
}
else {
System.out.println("ID not found");
}
}
// PROCESS_ORDER : 16
public void processOrder() {
String clientId = getToken("Please enter the ID of the client whose cart you'd like to process");
warehouse.processOrder(clientId);
}
// DISPLAY_WAITLIST : 17
public void displayWaitlist() {
String targetProduct = getToken("Please enter Product Name to display its waitlist");
// get product, iterate through it's waitlist and print each toString'
Iterator<WaitlistItem> waitlist;
if (warehouse.getProductByName(targetProduct).getWaitlistItems() != null) {
waitlist = (warehouse.getProductByName(targetProduct)).getWaitlistItems();
} else {
return;
}
while (waitlist.hasNext()) {
WaitlistItem waitlistItem = (WaitlistItem) waitlist.next();
System.out.println(waitlistItem.toString());
}
}
// RECEIVE_SHIPMENT : 18
public void receiveShipment() {
String targetSupplier = getToken("Please enter Supplier Name to receive a shipment for it");
String targetProduct = getToken("Please enter Product name");
String targetQuantity = getToken("Please enter Quantity of product");
warehouse.receiveShipment(targetSupplier, targetProduct, targetQuantity);
}
// ACCEPT_PAYMENT : 19
public void acceptPayment() {
String clientId = getToken("Please enter the ID of the client you wish to accept payment");
if (warehouse.clientExists(clientId)) {
Double newPayment = Double.parseDouble(getToken("Enter payment: "));
warehouse.acceptPayment(clientId, newPayment);
}
else
System.out.println("Client ID doesnt exist.");
}
// GET_TRANSACTIONS : 20
public void getTransactions() {
String memberID = getToken("Enter member id");
Calendar date = getDate("Please enter the date for which you want records as mm/dd/yy");
Iterator<Transaction> result = warehouse.getTransactions(memberID, date);
if (result == null) {
System.out.println("Invalid Member ID");
} else {
while (result.hasNext()) {
Transaction transaction = (Transaction) result.next();
System.out.println(transaction.getType() + " " + transaction.getTitle() + "\n");
}
System.out.println("\n There are no more transactions \n");
}
}
// SAVE : 21
private void save() {
System.out.println("Save selected. Saving to file WarehouseData...");
if (warehouse.save()) {
System.out.println("Save successful");
} else {
System.out.println("Error: Save failed");
}
}
// RETRIEVE : 22
private void retrieve() {
try {
Warehouse tempWarehouse = Warehouse.retrieve();
if (tempWarehouse != null) {
System.out.println(" The warehouse has been successfully retrieved from the file WarehouseData \n" );
warehouse = tempWarehouse;
} else {
System.out.println("File doesnt exist; creating new warehouse" );
warehouse = Warehouse.instance();
}
} catch(Exception cnfe) {
cnfe.printStackTrace();
}
}
// HELP : 23
public void help() {
System.out.println("Enter a number corresponding to a command as indicated below:");
System.out.println(EXIT + " to Exit\n");
System.out.println(ADD_CLIENT + " to add a client");
System.out.println(SHOW_CLIENTS + " to display list of clients");
System.out.println(EDIT_CLIENT_ADDRESS + " to edit the address of a client");
System.out.println(EDIT_CLIENT_PHONE + " to edit the phone number of a client");
System.out.println(ADD_PRODUCT + " to add a product to the catalog");
System.out.println(SHOW_PRODUCTS + " to display the list of products in the catalog");
System.out.println(EDIT_PRODUCT_PRICE + " to edit the sales price of a product");
System.out.println(ADD_SUPPLIER + " to add a supplier");
System.out.println(SHOW_SUPPLIERS + " to display the list of suppliers");
System.out.println(EDIT_SUPPLIERS_ADDRESS + " to edit the address of a supplier");
System.out.println(ADD_PRODUCT_TO_SUPPLIER + " to add products to a suppliers catalog");
System.out.println(DISPLAY_PRODUCTS_OF_SUPPLIER + " to display products in a suppliers catalog");
System.out.println(ADD_TO_CART + " to add a product to a client's cart");
System.out.println(DISPLAY_CART + " to view the items in a client's cart");
System.out.println(EDIT_CART + " to change the quantity of an item in a client's cart");
System.out.println(PROCESS_ORDER + " to process order of items in a client's cart");
System.out.println(DISPLAY_WAITLIST + " to view a product's waitlist");
System.out.println(RECEIVE_SHIPMENT + " to receive a shipment");
System.out.println(ACCEPT_PAYMENT + " to accept payment");
System.out.println(GET_TRANSACTIONS + " to print transactions");
System.out.println(SAVE + " to save changes to a file");
System.out.println(RETRIEVE + " to retrieve");
System.out.println(HELP + " for help");
}
public void process() {
int command;
help();
while ((command = getCommand()) != EXIT) {
switch (command) {
case ADD_CLIENT:
addClient();
break;
case SHOW_CLIENTS:
showClients();
break;
case EDIT_CLIENT_ADDRESS:
editClientAddress();
break;
case EDIT_CLIENT_PHONE:
editClientPhone();
break;
case ADD_PRODUCT:
addProduct();
break;
case SHOW_PRODUCTS:
showProducts();
break;
case EDIT_PRODUCT_PRICE:
editProductPrice();
break;
case ADD_SUPPLIER:
addSupplier();
break;
case SHOW_SUPPLIERS:
showSuppliers();
break;
case EDIT_SUPPLIERS_ADDRESS:
editSupplierAddress();
break;
case ADD_PRODUCT_TO_SUPPLIER:
addProductToSupplier();
break;
case DISPLAY_PRODUCTS_OF_SUPPLIER:
displayProductsOfSupplier();
break;
case ADD_TO_CART:
addToCart();
break;
case DISPLAY_CART:
displayCart();
break;
case EDIT_CART:
editCart();
break;
case PROCESS_ORDER:
processOrder();
break;
case DISPLAY_WAITLIST:
displayWaitlist();
break;
case RECEIVE_SHIPMENT:
receiveShipment();
break;
case ACCEPT_PAYMENT:
acceptPayment();
break;
case GET_TRANSACTIONS:
getTransactions();
break;
case SAVE:
save();
break;
case RETRIEVE:
retrieve();
break;
case HELP:
help();
break;
}
}
}
public static void main(String[] s) {
UserInterface.instance().process();
}
}