-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrontEndDeveloper.java
225 lines (208 loc) · 8.05 KB
/
FrontEndDeveloper.java
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
// --== CS400 File Header Information ==--
// Name: Srinath Srinivasan
// Email: [email protected]
// Team: NG
// TA: Daniel Finer
// Lecturer: Gary Dahl
// Notes to Grader: My part of the project.
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
/*
* This class executes the front end interface of the Grocery Store Database.
*/
public class FrontEndDeveloper {
private final static String WELCOME_MSG =
"======== WELCOME to the Grocery Store Database ========";
private final static String GOODBYE_MSG =
"======== Thank you for using this Application! ========";
private final static String MENU = "\nCOMMAND MENU:\n"
+ "[A] Add a grocery item profile to the database.\n"
+ "[S] search for a specific grocery item in the database. \n"
+ "[R] Removes a specified grocery item profile from the hashtable.\n"
+ "[C] Clears the entire grocery store hashtable.\n"
+ "[L] Lists every item with details and the total number of items in the hashtable.\n"
+ "[F] Reads a given file and adds all properly formatted items in that list to the hashtable.\n"
+ "[H] Display this menu again. \n" + "[Q] Quit program.";
private static BackEndDeveloper backEnd = new BackEndDeveloper();
/*
* This method executes the user interface and allows the user to run possible commands that
* utilize the Grocery Store database.
*
* @param String command
*/
public static void processUserCommandLine(String command) throws InputMismatchException {
String[] input = command.trim().split(" ");
Scanner scnr;
String serial = "";
String itemName = "";
double price = 0;
int quantity = 0;
int location = 0;
switch (input[0].toUpperCase()) {
case "A":
while (true) {
try {
scnr = new Scanner(System.in);
System.out.print("Enter 12 Digit Serial Number (12 numerical digits): ");
serial = scnr.next();
backEnd.serialValidity(serial);
backEnd.alreadyExists(serial);
} catch (Exception e) {
System.out.println(e.getMessage());
continue;
}
break;
}
System.out.print("Enter Item Name: ");
itemName = scnr.next();
while (true) {
try {
scnr = new Scanner(System.in);
System.out.print("Enter Item Price (A 2 decimal number such as 3.00): ");
price = scnr.nextDouble();
} catch (InputMismatchException e) {
System.out.println("Not a valid price!");
continue;
}
break;
}
while (true) {
try {
scnr = new Scanner(System.in);
System.out.print("Enter the Quantity Currently Available: ");
quantity = scnr.nextInt();
} catch (InputMismatchException e) {
System.out.println("Not a Number!");
continue;
}
break;
}
while (true) {
try {
scnr = new Scanner(System.in);
System.out.print("Enter the Location in the Store (0-9): ");
location = scnr.nextInt();
if (location > 9 || location < 0) {
System.out.println("Location is not in range");
continue;
}
} catch (InputMismatchException e) {
System.out.println("Not a Number!");
continue;
}
break;
}
GroceryStoreItem itemToAdd =
new GroceryStoreItem(serial, itemName, price, quantity, location);
backEnd.add(itemToAdd);
break;
case "S":
scnr = new Scanner(System.in);
System.out.print("Enter the Serial Number of the Item You Are Looking for: ");
serial = scnr.next();
GroceryStoreItem itemInfo = null;
try {
itemInfo = (GroceryStoreItem) backEnd.search(serial);
} catch (NoSuchElementException e) {
System.out.println(e.getMessage());
break;
}
System.out.println("Item Serial Number: " + itemInfo.getSerialNumber());
System.out.println("Item Name: " + itemInfo.getName());
System.out.println("Item Price: " + itemInfo.getCost());
System.out.println("Item Quantity in Store: " + itemInfo.getQuantity());
System.out.println("Item Location: " + itemInfo.getLocation());
break;
case "R":
try {
scnr = new Scanner(System.in);
System.out.print("Enter the serial number of the item you would like removed: ");
serial = scnr.next();
} catch (NoSuchElementException e) {
System.out.println("Not a valid Serial Number!");
}
GroceryStoreItem removedItem = null;
removedItem = (GroceryStoreItem) backEnd.remove(serial);
if (removedItem == null) {
System.out.println("Serial number either invalid or not found!");
} else {
System.out.println("Removed Item Serial Number: " + removedItem.getSerialNumber());
System.out.println("Removed Item Name: " + removedItem.getName());
System.out.println("Removed Item Price: " + removedItem.getCost());
System.out.println("Removed Item Quantity: " + removedItem.getQuantity());
System.out.println("Removed Item Location: " + removedItem.getLocation());
}
break;
case "C":
backEnd.clear();
System.out.println("All items removed from table");
break;
case "L":
String[] serialNumbersInList = backEnd.list();
GroceryStoreItem currentItem;
String currentSerialNumber;
String currentName;
double currentPrice;
int currentQuantity;
int currentLocation;
for (int i = 0; i < serialNumbersInList.length; i++) {
currentItem = (GroceryStoreItem) backEnd.search(serialNumbersInList[i]);
currentSerialNumber = (String) currentItem.getSerialNumber();
currentName = (String) currentItem.getName();
currentPrice = (double) currentItem.getCost();
currentQuantity = (int) currentItem.getQuantity();
currentLocation = (int) currentItem.getLocation();
System.out.println("Item " + (i + 1) + ": ");
System.out.println("Serial Number: " + currentSerialNumber);
System.out.println("Name: " + currentName);
System.out.println("Price: $" + currentPrice);
System.out.println("Quantity: " + currentQuantity);
System.out.println("Location: " + currentLocation);
System.out.println();
}
System.out.println("==========================================");
System.out.println("Total net worth of store: $" + backEnd.sumGrocery());
break;
case "F":
DataWrangler dataWrangler = new DataWrangler(backEnd);
scnr = new Scanner(System.in);
System.out.println("What is the name of the file that you would like to read from: ");
String fileToRead = scnr.next();
dataWrangler.readFile(fileToRead);
break;
case "H":
System.out.println(MENU);
break;
default:
System.out.println("WARNING. Invalid command. Please enter H to refer to the menu.");
}
}
/*
* This method initializes the scanner that will allow user input and provides an informative user
* interface.
*/
private static void driver() {
Scanner scnr = new Scanner(System.in);
String promptCommandLine = "\nENTER COMMAND: ";
System.out.print(MENU);
System.out.print(promptCommandLine);
String line = scnr.nextLine().trim();
char c = line.charAt(0);
while (Character.toUpperCase(c) != 'Q') {
processUserCommandLine(line);
System.out.println(promptCommandLine);
line = scnr.nextLine().trim();
c = line.charAt(0);
}
scnr.close();
}
/*
* The main method executes the driver() method and provides relevant information.
*/
public static void main(String[] args) {
System.out.println(WELCOME_MSG);
driver();
System.out.println(GOODBYE_MSG);
}
}