-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventoryApp.java
More file actions
1213 lines (932 loc) · 41.6 KB
/
InventoryApp.java
File metadata and controls
1213 lines (932 loc) · 41.6 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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.util.*;
import java.io.*;
import java.time.*;
import java.time.format.DateTimeParseException;
//holds filters
class ViewCriteria {
private String sortOrder = "Entry Date";
private String sortDirection = "Ascending";
private String brandFilter = "";
private String modelFilter = "";
private String engineNumberFilter = "";
private String entryDateFilter = "";
private String purchaseDateFilter = "";
//filter checks
public boolean matches(Stock s) {
boolean brandMatch = brandFilter.isEmpty() || s.getProduct().getBrand().toLowerCase().contains(brandFilter.toLowerCase());
boolean modelMatch = modelFilter.isEmpty() || s.getProduct().getModel().toLowerCase().contains(modelFilter.toLowerCase());
boolean engineMatch = engineNumberFilter.isEmpty() || s.getEngineNumber().toUpperCase().contains(engineNumberFilter.toUpperCase());
boolean entryDateMatch = entryDateFilter.isEmpty() || s.getEntryDate().toString().startsWith(entryDateFilter);
boolean purchaseDateMatch = purchaseDateFilter.isEmpty() || s.getPurchaseDate().toString().startsWith(purchaseDateFilter);
return brandMatch && modelMatch && engineMatch && entryDateMatch && purchaseDateMatch;
}
public void reset() {
sortOrder = "Entry Date";
sortDirection = "Ascending";
brandFilter = "";
modelFilter = "";
engineNumberFilter = "";
entryDateFilter = "";
purchaseDateFilter = "";
}
public String getActiveSortOrder(){
return sortOrder + ", " + sortDirection;
}
public String getStrActiveFilters(){
List<String> activeList = new ArrayList<>();
if (!brandFilter.isEmpty()) activeList.add("Brand = " + brandFilter);
if (!modelFilter.isEmpty()) activeList.add("Model = " + modelFilter);
if (!engineNumberFilter.isEmpty()) activeList.add("Engine Number = " + engineNumberFilter);
if (!entryDateFilter.isEmpty()) activeList.add("EntryDate = " + entryDateFilter);
if (!purchaseDateFilter.isEmpty()) activeList.add("PurchaseDate = " + purchaseDateFilter);
if (activeList.isEmpty()) {
return "None";
} else {
return (String.join(", ", activeList));
}
}
public String allFilters(){
String b = brandFilter.isEmpty() ? "None" : brandFilter;
String m = modelFilter.isEmpty() ? "None" : modelFilter;
String en = engineNumberFilter.isEmpty() ? "None" : engineNumberFilter;
String e = entryDateFilter.isEmpty() ? "None" : entryDateFilter;
String p = purchaseDateFilter.isEmpty() ? "None" : purchaseDateFilter;
return String.format("Brand = %s, Model = %s, Engine Number = %s, Entry Date = %s, Purchase Date = %s", b, m, en, e, p);
}
public void setSortOrder(String newSortOrder){
sortOrder = newSortOrder;
}
public void setSortDirection(String newSortDirection){
sortDirection = newSortDirection;
}
public String getSortOrder(){
return sortOrder;
}
public String getSortDirection(){
return sortDirection;
}
public void setBrandFilter(String newBrandFilter){
brandFilter = newBrandFilter;
}
public void setModelFilter(String newModelFilter){
modelFilter = newModelFilter;
}
public void setEngineNumberFilter(String newEngineNumberFilter){
engineNumberFilter = newEngineNumberFilter;
}
public void setEntryDateFilter(String newEntryDateFilter){
entryDateFilter = newEntryDateFilter;
}
public void setPurchaseDateFilter(String newPurchaseDateFilter){
purchaseDateFilter = newPurchaseDateFilter;
}
}
//bridges InventoryApp to csvParser
class InventorySystem{
private HashMap<Integer, Product> productMap;
private HashMap<Integer, Stock> stockMap;
private CsvParser csvParser;
private int nextProductId;
private int nextStockId;
public InventorySystem(){
this.csvParser = new CsvParser();
this.productMap = csvParser.returnProducts();
this.stockMap = csvParser.returnStocks(productMap);
this.nextProductId = csvParser.getNextProductId();
this.nextStockId = csvParser.getNextStockId();
}
public int generateStockId() {
return nextStockId++;
}
public int generateProductId() {
return nextProductId++;
}
public void addStocks(int productId, String[] engineNumbers){
Product product = productMap.get(productId); //hashmap for O(1) lookup of product
for (String engineNumber : engineNumbers){
int newStockId = generateStockId();
Stock newStock = new Stock(newStockId, product, engineNumber, LocalDateTime.now(),null);
stockMap.put(newStockId,newStock);
}
csvParser.saveStocks(stockMap);
csvParser.saveConfig(nextProductId, nextStockId);
System.out.println("Successfully added " + engineNumbers.length + " stock/s to the system.");
}
public void updateStockProduct(Stock stock, int newProductId){
Product p = productMap.get(newProductId);
stock.setProduct(p);
csvParser.saveStocks(stockMap);
}
public void updateStockEngineNumber(Stock stock, String newEngineNumber){
stock.setEngineNumber(newEngineNumber);
csvParser.saveStocks(stockMap);
}
public void updateStockPurchaseDateTime(Stock stock){
LocalDateTime newPurchaseDateTime = LocalDateTime.now();
stock.setPurchaseDateTime(newPurchaseDateTime);
csvParser.saveStocks(stockMap);
}
public void deleteStock(int stockId){
stockMap.remove(stockId);
csvParser.saveStocks(stockMap);
}
public void addProductType(String brand, String newProductName){
int newProductId = generateProductId();
Product newProduct = new Product(newProductId, brand, newProductName);
productMap.put(newProductId,newProduct);
csvParser.saveProducts(productMap);
csvParser.saveConfig(nextProductId, nextStockId);
}
public void updateProductBrand(Product product, String newBrand){
product.setBrand(newBrand);
csvParser.saveProducts(productMap);
}
public void updateProductModel(Product product, String newModel){
product.setModel(newModel);
csvParser.saveProducts(productMap);
}
public void deleteProductType(int productId){
productMap.remove(productId);
csvParser.saveProducts(productMap);
}
public ArrayList<Stock> filterStockOptions(ViewCriteria vc){
ArrayList<Stock> stockArray = new ArrayList<>();
for (Stock s : stockMap.values()){
if (vc.matches(s)) stockArray.add(s);
}
return stockArray;
}
public List<Stock> mergeSort(List<Stock> inventoryList, ViewCriteria vc){
if (inventoryList.size() <= 1) return inventoryList;
int mid = inventoryList.size() / 2;
List<Stock> left = mergeSort(inventoryList.subList(0, mid),vc);
List<Stock> right = mergeSort(inventoryList.subList(mid, inventoryList.size()),vc);
return merge(left, right, vc);
}
public List<Stock> merge(List<Stock> left, List<Stock> right, ViewCriteria vc){
List<Stock> result = new ArrayList<>();
int l = 0, r = 0;
while (l < left.size() && r < right.size()) {
if (compareStocks(left.get(l), right.get(r), vc) <= 0) {
result.add(left.get(l));
l++;
} else {
result.add(right.get(r));
r++;
}
}
while (l < left.size()) result.add(left.get(l++));
while (r < right.size()) result.add(right.get(r++));
return result;
}
private int compareStocks(Stock s1, Stock s2, ViewCriteria vc) {
int result = 0; // result = 0 means s1 equals s2 in terms of order, result = -1 means s1 should be behind s2, result = 1 means s1 must be in front of s2
String order = vc.getSortOrder();
switch (order) {
case "Entry Date":
result = Integer.compare(s1.getStockId(), s2.getStockId()); //using stock id for entry date sort order
break;
case "Purchase Date":
if (s1.getPurchaseDateTime() == null && s2.getPurchaseDateTime() == null) result = 0;
else if (s1.getPurchaseDateTime() == null) result = 1;
else if (s2.getPurchaseDateTime() == null) result = -1;
else result = s1.getPurchaseDateTime().compareTo(s2.getPurchaseDateTime());
break;
case "Brand and Model":
String bm1 = s1.getProduct().getBrand() + s1.getProduct().getModel();
String bm2 = s2.getProduct().getBrand() + s2.getProduct().getModel();
result = bm1.compareToIgnoreCase(bm2);
break;
default:
result = Integer.compare(s1.getStockId(), s2.getStockId());
break;
}
//if sort direction is descending, reverse sign of result to flip
if (vc.getSortDirection().equalsIgnoreCase("Descending")) {
result *= -1;
}
return result;
}
public HashMap<Integer, String> getHmBrands(){
// get unique brands
Set<String> uniqueBrands = new TreeSet<>();
for (Product p : productMap.values()) {
uniqueBrands.add(p.getBrand());
}
// hashmap for easier mapping of the index the user will enter and the actual brand value to be stored in sortFilter in viewCriteria
// also for easier validation using isValidOpt method
HashMap<Integer, String> menuMap = new HashMap<>();
int index = 1;
for (String brand : uniqueBrands) {
menuMap.put(index++, brand);
}
return menuMap;
}
public HashMap<Integer, Product> getHmProducts(){
return productMap;
}
public Collection<Product> getObjProducts(){
return productMap.values();
}
public HashMap<Integer, Stock> getHmStocks(){
return stockMap;
}
public Collection<Stock> getObjStocks(){
return stockMap.values();
}
}
//handles all prompting and printing to the user
public class InventoryApp{
private InventorySystem inventorySystem = new InventorySystem();
private ViewCriteria viewCriteria = new ViewCriteria();
private Scanner sc;
public InventoryApp(){
this.sc = new Scanner(System.in);
this.inventorySystem = new InventorySystem();
}
public void start(){
boolean exitApp = false;
String mainChoice;
do{
System.out.println("\n=======================");
System.out.println("MotorPH Inventory System");
System.out.println("=======================");
System.out.println("MAIN MENU: What to do?");
System.out.println("1 - Add Stock/s");
System.out.println("2 - View Inventory");
System.out.println("3 - Configure Product Types");
System.out.println("0 - Exit Program");
System.out.println("=======================");
System.out.print("Enter choice: ");
mainChoice = sc.nextLine();
switch (mainChoice){
case "1":
promptAddStocks();
System.out.println("Press enter to go back to main menu.");
sc.nextLine();
break;
case "2":
promptViewInventory();
System.out.println("Press enter to go back to main menu.");
sc.nextLine();
break;
case "3":
promptConfigureProductTypes();
System.out.println("Press enter to go back to main menu.");
sc.nextLine();
break;
case "0":
System.out.println("Closing MotorPH inventory system.");
exitApp = true;
sc.close();
break;
default:
System.out.println("\nInvalid option, please enter only numbers 0-3");
sc.nextLine();
break;
}
}while(exitApp==false);
}
/*
PROMPT METHODS
*/
public void promptAddStocks(){
String productInput;
//print product type options, prompt user for input, then validate input
do {
System.out.println("\n=======================");
printProductsOptions(); //print product types options
System.out.println("0 | Go Back"); //option for back
System.out.println("=======================");
System.out.println("ADD STOCKS MENU: Which product type to add stocks for? Please choose a product type ID from the options above.");
System.out.println("=======================");
System.out.print("Enter product type ID: ");
productInput = sc.nextLine();
} while (!isValidOpt(productInput, inventorySystem.getHmProducts()));
//if user wants to go back and cancel adding of stocks
if (productInput.equals("0")){
System.out.println("Cancelled add stocks.");
return;
};
Integer productId = Integer.parseInt(productInput);
Product product = inventorySystem.getHmProducts().get(productId); //O(1) hashmap lookup
//ask user how many stocks to add and validate input
int quantity = 0;
while (true) {
System.out.println("\n=======================");
System.out.printf("ADD STOCKS MENU: How many stocks to add for product %s %s",product.brand,product.model);
System.out.println("\n=======================");
System.out.print("Enter number of stocks to add: ");
String quantityInput = sc.nextLine();
try {
quantity = Integer.parseInt(quantityInput);
//if user wants to go back
if (quantity == 0) {
System.out.println("Cancelled add stocks.");
return;
};
if (quantity > 0) break; //if input is valid, break from while loop
System.out.println("\nInvalid input. Please enter a number greater than zero.");
} catch (NumberFormatException e) {
System.out.println("\nInvalid input. Please enter a whole number.");
}
}
System.out.println("\n=======================");
//get engine numbers of new stocks
String[] engineNumbers = new String[quantity];
for (int i = 0; i < quantity; i++) {
System.out.printf("Enter Engine Number for unit %d: ", (i + 1));
engineNumbers[i] = sc.nextLine();
}
//add stocks to system
inventorySystem.addStocks(productId, engineNumbers);
}
public void promptViewInventory(){
String viewChoice="";
ArrayList<Stock> inventoryView;
while(true){
System.out.println("\n=======================");
System.out.println("INVENTORY VIEW");
System.out.println("=======================");
inventoryView = inventorySystem.filterStockOptions(viewCriteria); //get all stocks that pass filters
printInventoryView(inventoryView); //has the mergeSort method
System.out.println("=======================");
System.out.printf("Currently viewing %d stocks out of %d", inventoryView.size(),inventorySystem.getHmStocks().size());
System.out.println("\nActive Filters: " + viewCriteria.getStrActiveFilters()); //print active filters to let user know
System.out.println("Sorted By: " + viewCriteria.getActiveSortOrder()); //print active sort order to let user know
System.out.println("=======================");
System.out.println("VIEW OPTIONS:");
System.out.println("1 - Update | 2 - Delete | 3 - Sort | 4 - Search/Filter | 5 - Reset View | 0 - Go Back");
System.out.println("=======================");
System.out.print("Enter choice: ");
viewChoice = sc.nextLine();
switch (viewChoice){
case "0":
return;
case "1":
promptUpdate(inventoryView);
break;
case "2":
promptDelete(inventoryView);
break;
case "3":
promptSort();
break;
case "4":
promptFilter();
break;
case "5":
viewCriteria.reset();
break;
default:
System.out.println("\nInvalid option, please enter only numbers 0-5");
sc.nextLine();
break;
}
}
}
public void promptDelete(ArrayList<Stock> inventoryView){
String stockChoice = "";
while(true){
System.out.println("\n=======================");
System.out.println("INVENTORY VIEW");
System.out.println("=======================");
printInventoryView(inventoryView); //print stock options to delete
System.out.println("\n=======================");
System.out.print("Enter ID of stock to delete (Press 0 to Go Back): ");
stockChoice = sc.nextLine();
if (stockChoice.equals("0")) return;
if (isStockInView(stockChoice,inventoryView)){
break;
}
System.out.println("Invalid input. Please enter a valid stock ID that is currently in view.");
}
inventorySystem.deleteStock(Integer.parseInt(stockChoice));
System.out.println("Successfully deleted stock from system.");
sc.nextLine();
}
public void promptUpdate(ArrayList<Stock> inventoryView){
String stockChoice = "";
String fieldChoice = "";
while(true){
System.out.println("\n=======================");
System.out.println("INVENTORY VIEW");
System.out.println("=======================");
printInventoryView(inventoryView); //print stock options to update
System.out.println("=======================");
System.out.print("Enter ID of stock to update (Press 0 to Go Back): ");
stockChoice = sc.nextLine();
if (stockChoice.equals("0")) return;
if (isStockInView(stockChoice,inventoryView)){
break;
}
System.out.println("Invalid input. Please enter a valid stock ID that is currently in view.");
}
int stockId = Integer.parseInt(stockChoice);
Stock s = inventorySystem.getHmStocks().get(stockId); // O(1) lookup using hashmap
boolean updating = true;
while(updating){
System.out.println("=======================");
System.out.println("UPDATING STOCK: " + s.toMenuOption());
System.out.println("=======================");
System.out.println("1 - Brand and Model | 2 - Engine Number | 3 - Purchase Date | 0 - Go Back");
System.out.print("Enter data field to update: ");
fieldChoice = sc.nextLine();
switch (fieldChoice){
case "0":
updating = false;
break;
case "1":
String newProductId = "";
do{
System.out.println("=======================");
printProductsOptions();
System.out.println("=======================");
System.out.print("Enter new product type ID of stock: ");
} while(isValidOpt(newProductId, inventorySystem.getHmProducts()));
inventorySystem.updateStockProduct(s, Integer.parseInt(newProductId));
System.out.println("Successfully updated stock.");
sc.nextLine();
break;
case "2":
String newEngineNumber = "";
System.out.println("=======================");
System.out.print("Enter new engine number: ");
newEngineNumber = sc.nextLine();
inventorySystem.updateStockEngineNumber(s, newEngineNumber);
System.out.println("Successfully updated stock.");
sc.nextLine();
break;
case "3":
if (s.getPurchaseDateTime() != null){
System.out.println("Stock was already recorded as purchased.");
return;
}
String isPurchased = "";
System.out.println("=======================");
System.out.println("Purchase date will be set to current time.");
System.out.print("Enter confirmation that stock has been purchased (Y/N): ");
isPurchased = sc.nextLine();
if (isPurchased.equalsIgnoreCase("N")){
System.out.println("Cancelled updating of purchase date.");
sc.nextLine();
return;
}
inventorySystem.updateStockPurchaseDateTime(s);
System.out.println("Successfully updated stock.");
sc.nextLine();
break;
default:
System.out.println("Invalid input. Please enter numbers 0-3 only.");
sc.nextLine();
break;
}
}
}
public void promptSort(){
String sortChoice = "";
while(true){
System.out.println("\n=======================");
System.out.println("CURRENT SORT ORDER: " + viewCriteria.getActiveSortOrder());
System.out.println("=======================");
System.out.println("1 - Edit Sort By | 2 - Edit Sort Direction | 0 - Go Back");
System.out.println("=======================");
System.out.print("Enter choice: ");
sortChoice = sc.nextLine();
switch (sortChoice){
case "0":
return;
case "1":
promptSortBy();
break;
case "2":
promptSortDirection();
break;
default:
System.out.println("Invalid input. Please enter only numbers 0-2.");
sc.nextLine();
break;
}
}
}
public void promptSortBy(){
String sortByChoice = "";
while (true){
System.out.println("\n=======================");
System.out.println("Which field to sort by?");
System.out.println("1 - Entry Date (Default)");
System.out.println("2 - Purchase Date");
System.out.println("3 - Brand and Model");
System.out.println("0 - Go Back");
System.out.println("=======================");
System.out.print("Enter choice: ");
sortByChoice = sc.nextLine();
switch (sortByChoice) {
case "0":
return;
case "1":
viewCriteria.setSortOrder("Entry Date");
return;
case "2":
viewCriteria.setSortOrder("Purchase Date");
return;
case "3":
viewCriteria.setSortOrder("Brand and Model");
return;
default:
System.out.println("Invalid input. Please enter only numbers 0-3.");
sc.nextLine();
break;
}
}
}
public void promptSortDirection(){
String sortDirectionChoice = "";
while (true){
System.out.println("\n=======================");
System.out.println("Which field to sort by?");
System.out.println("1 - Ascending");
System.out.println("2 - Descending");
System.out.println("0 - Go Back");
System.out.println("=======================");
System.out.print("Enter choice: ");
sortDirectionChoice = sc.nextLine();
switch (sortDirectionChoice) {
case "0":
return;
case "1":
viewCriteria.setSortDirection("Ascending");
return;
case "2":
viewCriteria.setSortDirection("Descending");
return;
default:
System.out.println("Invalid input. Please enter only numbers 0-2.");
sc.nextLine();
break;
}
}
}
public void promptFilter(){
String viewChoice = "";
while(true){
System.out.println("\n=======================");
System.out.println("CURRENT SEARCH FILTERS:");
System.out.println(viewCriteria.allFilters());
System.out.println("=======================");
System.out.println("Which filter to edit?");
System.out.println("1 - Brand | 2 - Model | 3 - Engine Number | 4 - Entry Date | 5 - Purchase Date | 0 - Go Back");
System.out.println("=======================");
System.out.print("Enter choice: ");
viewChoice = sc.nextLine();
switch (viewChoice){
case "0":
return;
case "1":
promptBrandFilter();
break;
case "2":
promptModelFilter();
break;
case "3":
promptEngineNumberFilter();
break;
case "4":
promptEntryDateFilter();
break;
case "5":
promptPurchaseDateFilter();
break;
default:
System.out.println("Invalid input. Please enter only numbers 0-5.");
sc.nextLine();
break;
}
}
}
public void promptBrandFilter(){
String brandInput = "";
// get unique brands
HashMap<Integer, String> menuMap = inventorySystem.getHmBrands();
do{
System.out.println("\n=======================");
printBrandsOptions(menuMap);
System.out.println("0 | Go Back"); //option for back
System.out.println("=======================");
System.out.print("Enter brand index or press enter to remove filter: ");
brandInput = sc.nextLine();
if (brandInput.equals("0")) return;
if (!(isValidOpt(brandInput, inventorySystem.getHmProducts()))){
System.out.println("Invalid input. Please enter valid brand index.");
sc.nextLine();
}
} while(!brandInput.equals("") && !isValidOpt(brandInput, menuMap));
viewCriteria.setBrandFilter(menuMap.get(Integer.parseInt(brandInput)));
}
public void promptModelFilter(){
String modelInput = "";
do{
System.out.println("\n=======================");
printProductsOptions(); //print product types options
System.out.println("0 | Go Back"); //option for back
System.out.println("=======================");
System.out.println("Any previously set brand filter will be overriden to selected model's brand.");
System.out.print("Enter model ID or press enter to remove filter: ");
modelInput = sc.nextLine();
if (modelInput.equals("0")) return;
if (!(isValidOpt(modelInput, inventorySystem.getHmProducts()))){
System.out.println("Invalid input. Please enter valid product type id.");
sc.nextLine();
}
} while (!(isValidOpt(modelInput, inventorySystem.getHmProducts()) || modelInput.equals("")));
Product model = inventorySystem.getHmProducts().get(Integer.parseInt(modelInput)); //hashmap used to be able to get brand and model easily from modelInput
if (!(modelInput.equals(""))){
viewCriteria.setBrandFilter(model.getBrand());
}
viewCriteria.setModelFilter(model.getModel());
}
public void promptEngineNumberFilter(){
String engineNumberInput = "";
System.out.println("\n=======================");
System.out.println("Any other filters will be overriden.");
System.out.print("Enter engine number to search or press enter to remove filter: ");
engineNumberInput = sc.nextLine();
//override other filters since engine numbers are more specific, for easier searching of specific engine numbers
if (!(engineNumberInput.equals(""))){
viewCriteria.setBrandFilter("");
viewCriteria.setModelFilter("");
viewCriteria.setEntryDateFilter("");
viewCriteria.setPurchaseDateFilter("");
}
viewCriteria.setEngineNumberFilter(engineNumberInput);
}
public void promptEntryDateFilter(){
String entryDateInput = "";
do{
System.out.println("\n=======================");
System.out.print("Enter entry date (YYYY-MM or YYYY-MM-DD) or press enter to remove filter: ");
entryDateInput = sc.nextLine();
if (!(isValidDate(entryDateInput))){
System.out.println("Invalid input. Please enter valid dates in YYYY-MM or YYYY-MM-DD format only.");
}
} while (!(isValidDate(entryDateInput) || entryDateInput.equals("")));
viewCriteria.setEntryDateFilter(entryDateInput);
}
public void promptPurchaseDateFilter(){
String purchaseDateInput = "";
do{
System.out.println("\n=======================");
System.out.print("Enter purchase date (YYYY-MM or YYYY-MM-DD) or press enter to remove filter: ");
purchaseDateInput = sc.nextLine();
if (!(isValidDate(purchaseDateInput))){
System.out.println("Invalid input. Please enter valid dates in YYYY-MM or YYYY-MM-DD format only.");
}
} while (!(isValidDate(purchaseDateInput) || purchaseDateInput.equals("")));
viewCriteria.setPurchaseDateFilter(purchaseDateInput);
}
public void promptConfigureProductTypes(){
String configureChoice ="";
while(true){
System.out.println("\n=======================");
System.out.println("PRODUCT TYPES VIEW");
System.out.println("=======================");
printProductsOptions();
System.out.println("=======================");
System.out.println("CONFIGURE OPTIONS:");
System.out.println("1 - Add Product Type | 2 - Edit Product Type | 3 - Delete Product Type | 0 - Go Back");
System.out.println("=======================");
System.out.print("Enter choice: ");
configureChoice = sc.nextLine();
switch (configureChoice){
case "0":
return;
case "1":
promptAddProduct();
break;
case "2":
promptEditProduct();
break;
case "3":
promptDeleteProduct();
break;
default:
System.out.println("Invalid input please enter only numbers 0-3.");
sc.nextLine();
break;
}
}
}
public void promptAddProduct(){
String brandInput = "";
// get unique brands
HashMap<Integer, String> menuMap = inventorySystem.getHmBrands();
do{
System.out.println("\n=======================");
printBrandsOptions(menuMap);
System.out.println("N | Enter new brand");
System.out.println("0 | Go Back");
System.out.println("=======================");
System.out.print("Enter brand index, 0 to go back, or N for new brand: ");
brandInput = sc.nextLine();
if (brandInput.equals("0")) return;
if (!(isValidOpt(brandInput, inventorySystem.getHmProducts()) || brandInput.equalsIgnoreCase("N"))){
System.out.println("Invalid input. Please enter valid brand index.");
sc.nextLine();
}
} while(!brandInput.equals("") && !isValidOpt(brandInput, menuMap) && !brandInput.equalsIgnoreCase("N"));
String actualBrand = "";
if (brandInput.equalsIgnoreCase("N")){
System.out.print("Enter new brand: ");
actualBrand = sc.nextLine();
} else{
actualBrand = menuMap.get(Integer.parseInt(brandInput));
}
String newProductName = "";
System.out.print("Enter new name of product type: ");
newProductName = sc.nextLine();
inventorySystem.addProductType(actualBrand,newProductName);
System.out.println("\nSuccessfully added product type.");
}
public void promptEditProduct(){
String productChoice = "";
do{
System.out.println("\n=======================");
printProductsOptions();
System.out.println("0 | Go Back");
System.out.println("=======================");
System.out.println("Which product type to edit?");
System.out.print("Enter ID of product type to edit: ");
productChoice = sc.nextLine();
if (productChoice.equals("0")) return;
if (!isValidOpt(productChoice, inventorySystem.getHmProducts())){
System.out.println("Invalid input. Please enter valid product IDs only or 0 to go back.");
}
} while(!(isValidOpt(productChoice, inventorySystem.getHmProducts()) || productChoice.equals("0")));
String fieldChoice = "";
Product actualProduct = inventorySystem.getHmProducts().get(Integer.parseInt(productChoice));
while (true){
System.out.println("=======================");
System.out.println("Currently editing product type: " + actualProduct.toMenuOption());
System.out.println("=======================");
System.out.println("Which field to edit?");
System.out.println("1 - Brand | 2 - Model | 0 - Go Back");
fieldChoice = sc.nextLine();
switch (fieldChoice) {
case "0":
return;