-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientUI.java
More file actions
332 lines (302 loc) · 14.7 KB
/
ClientUI.java
File metadata and controls
332 lines (302 loc) · 14.7 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
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* A class that uses java swing to provide a user interface for the client.
* The client will be able to sign in, change information, view stock, and request orders.
*
* @author Medha Swarnachandrabalaji, Alex Auyon, Ryan Wei, Hasti Abbasi Kenarsari
*/
public class ClientUI {
private static ClientBase clientBase;
private static Inventory inventory;
private static String username;
private static boolean done;
public static void start() {
try{
clientBase = new ClientBase();
inventory = new Inventory();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
done = false;
while(!done) {
int ans = JOptionPane.showOptionDialog(null, "Welcome! please sign up or log in", "Sign up or Log in", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] {"Sign up", "Log in"}, null);
if (ans == JOptionPane.YES_OPTION) {
signUp();
} else if (ans == JOptionPane.NO_OPTION) {
done = logIn();
} else {
done = true;
}
}
JOptionPane.showMessageDialog(null, "Thank you for using our services. Have a great day!");
}
private static boolean logIn() {
username = JOptionPane.showInputDialog("Enter your username: ");
if(clientBase.containsClient(username)) {
loggedIn();
return false;
} else if(username == null) {
return false;
} else {
JOptionPane.showMessageDialog(null, "Username not found. Please try again.");
logIn();
}
return true;
}
private static void loggedIn() {
int ans = JOptionPane.showOptionDialog(null, "How can we help you today?", "Options", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] {"Information", "Orders", "Log out"}, null);
if(ans == JOptionPane.YES_OPTION) {
info();
} else if(ans == JOptionPane.NO_OPTION) {
orders();
}
}
private static void info() {
int ans = JOptionPane.showOptionDialog(null, "What would you like to do?", "Options", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] {"View information", "Change information", "Back"}, null);
if(ans == JOptionPane.YES_OPTION) {
viewInfo();
} else if(ans == JOptionPane.NO_OPTION) {
changeInfo();
} else {
loggedIn();
}
}
private static void viewInfo() {
String info = clientBase.getClient(username).toString();
JOptionPane.showMessageDialog(null, info);
info();
}
private static void changeInfo() {
int ans = JOptionPane.showOptionDialog(null, "What would you like to change?", "Options", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] {"Address", "Phone number", "Email", "Age", "Back"}, null);
if(ans == JOptionPane.YES_OPTION) {
String oldAddress = clientBase.getClient(username).getAddress();
if(oldAddress.equals("")) {
oldAddress = "No address on file.";
}
String address = JOptionPane.showInputDialog("Enter your new address: ");
clientBase.changeClientInfo(username, address, null, null, -1);
JOptionPane.showMessageDialog(null, "Information updated.\n" + displayChange(oldAddress, address));
changeInfo();
} else if(ans == JOptionPane.NO_OPTION) {
String oldPhone = clientBase.getClient(username).getPhone();
if(oldPhone.equals("")) {
oldPhone = "No phone number on file.";
}
String phone = JOptionPane.showInputDialog("Enter your new phone number: ");
clientBase.changeClientInfo(username, null, phone, null, -1);
JOptionPane.showMessageDialog(null, "Information updated.\n" + displayChange(oldPhone, phone));
changeInfo();
} else if(ans == JOptionPane.CANCEL_OPTION) {
String oldEmail = clientBase.getClient(username).getEmail();
if(oldEmail.equals("")) {
oldEmail = "No email on file.";
}
String email = JOptionPane.showInputDialog("Enter your new email: ");
clientBase.changeClientInfo(username, null, null, email, -1);
JOptionPane.showMessageDialog(null, "Information updated.\n" + displayChange(oldEmail, email));
changeInfo();
} else if(ans == 3) {
boolean ageDone = false;
while(!ageDone) {
int oldAge = clientBase.getClient(username).getAge();
int age = -1;
try {
age = Integer.parseInt(JOptionPane.showInputDialog("Enter your new age: "));
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid age. Please try again.");
break;
}
if (age < 120 && age > 0) {
clientBase.changeClientInfo(username, null, null, null, age);
JOptionPane.showMessageDialog(null, "Information updated.\n" + displayChange(String.valueOf(oldAge), String.valueOf(age)));
ageDone = true;
} else {
JOptionPane.showMessageDialog(null, "Invalid age. Please try again.");
break;
}
}
changeInfo();
} else {
info();
}
}
private static void orders() {
int ans = JOptionPane.showOptionDialog(null, "What would you like to do?", "Options", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] {"View Stock", "View orders", "Request order", "Cancel order", "Back"}, null);
if(ans == JOptionPane.YES_OPTION) {
viewStock();
} else if(ans == JOptionPane.NO_OPTION) {
viewOrders();
} else if(ans == 2) {
requestOrder();
orders();
} else if(ans == 3) {
cancelOrder();
orders();
} else {
loggedIn();
}
}
private static void viewStock() {
String stock = inventory.toString();
JOptionPane.showMessageDialog(null, stock, "Stock", JOptionPane.INFORMATION_MESSAGE);
orders();
}
private static void viewOrders() {
JOptionPane.showMessageDialog(null, username + "'s orders: \n" + ordersToString(clientBase.getOrders(username)), "Orders", JOptionPane.INFORMATION_MESSAGE);
orders();
}
private static boolean requestOrder() {
boolean requestDone = false;
int quantityInt = -1;
String produceName = JOptionPane.showInputDialog(null, "Enter the produce you would like to order: ");
if(produceName == null) {
JOptionPane.showMessageDialog(null, "Order cancelled. Returning to main menu.", "Cancelled", JOptionPane.INFORMATION_MESSAGE);
return requestDone;
} else if (Inventory.contains(produceName) && !produceName.equals("")) {
String quantity = JOptionPane.showInputDialog(null, "Enter the quantity of " + produceName + " you would like to order: ");
if (quantity == null) {
JOptionPane.showMessageDialog(null, "Order cancelled. Returning to main menu.", "Cancelled", JOptionPane.INFORMATION_MESSAGE);
return requestDone;
} else {
try {
quantityInt = Integer.parseInt(quantity);
if(quantityInt < 0) {
JOptionPane.showMessageDialog(null, "Invalid value. Returning to main menu.", "Cancelled", JOptionPane.INFORMATION_MESSAGE);
return requestDone;
} else {
if(Inventory.inStock(produceName, quantityInt)) {
clientBase.addProduce(username, produceName, quantityInt);
JOptionPane.showMessageDialog(null, "Order successful!", "Success", JOptionPane.INFORMATION_MESSAGE);
return requestDone = true;
} else {
JOptionPane.showMessageDialog(null, "Order failed. Not enough produce in stock.", "Error", JOptionPane.ERROR_MESSAGE);
return requestDone;
}
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid quantity.", "Error", JOptionPane.ERROR_MESSAGE);
return requestDone;
}
}
} else {
JOptionPane.showMessageDialog(null, "Produce not found.", "Error", JOptionPane.ERROR_MESSAGE);
return requestDone;
}
}
private static boolean cancelOrder() {
boolean cancelDone = false;
String produceName = JOptionPane.showInputDialog(null, "Enter the produce you would like to cancel: ");
if(produceName == null) {
JOptionPane.showMessageDialog(null, "Order cancellation cancelled. Returning to main menu.", "Cancelled", JOptionPane.INFORMATION_MESSAGE);
return cancelDone;
} else if (Inventory.contains(produceName) && !produceName.equals("")) {
String quantity = JOptionPane.showInputDialog(null, "Enter the quantity of " + produceName + " you would like to cancel: ");
if (quantity == null) {
JOptionPane.showMessageDialog(null, "Order cancellation cancelled. Returning to main menu.", "Cancelled", JOptionPane.INFORMATION_MESSAGE);
return cancelDone;
} else {
try {
int quantityInt = Integer.parseInt(quantity);
if(quantityInt < 0) {
JOptionPane.showMessageDialog(null, "Invalid value. Returning to main menu.", "Cancelled", JOptionPane.INFORMATION_MESSAGE);
return cancelDone;
} else {
if(clientBase.cancelOrder(username, produceName, quantityInt)) {
JOptionPane.showMessageDialog(null, "Order cancellation successful!", "Success", JOptionPane.INFORMATION_MESSAGE);
return cancelDone = true;
} else {
JOptionPane.showMessageDialog(null, "Order cancellation failed. Not enough produce in stock.", "Error", JOptionPane.ERROR_MESSAGE);
return cancelDone;
}
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid quantity.", "Error", JOptionPane.ERROR_MESSAGE);
return cancelDone;
}
}
} else {
JOptionPane.showMessageDialog(null, "Produce not found.", "Error", JOptionPane.ERROR_MESSAGE);
return cancelDone;
}
}
private static boolean signUp() {
JPanel signUpPanel = new JPanel();
signUpPanel.setLayout(new GridLayout(6, 2));
signUpPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
signUpPanel.add(new JLabel("Username: "));
JTextField nameField = new JTextField(20);
signUpPanel.add(nameField);
signUpPanel.add(new JLabel("Email: "));
JTextField emailField = new JTextField(20);
signUpPanel.add(emailField);
signUpPanel.add(new JLabel("Phone Number: "));
JTextField phoneField = new JTextField(20);
signUpPanel.add(phoneField);
signUpPanel.add(new JLabel("Address: "));
JTextField addressField = new JTextField(20);
signUpPanel.add(addressField);
signUpPanel.add(new JLabel("Age: "));
JTextField ageField = new JTextField(20);
signUpPanel.add(ageField);
int option = JOptionPane.showConfirmDialog(null, signUpPanel, "Sign Up", JOptionPane.OK_CANCEL_OPTION);
if(option == JOptionPane.OK_OPTION) {
String username = nameField.getText();
String address = addressField.getText();
String phone = phoneField.getText();
String email = emailField.getText();
int age = -1;
try {
age = Integer.parseInt(ageField.getText());
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid age.", "Error", JOptionPane.ERROR_MESSAGE);
return false;
}
if (username.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please enter a username.", "Error", JOptionPane.ERROR_MESSAGE);
}
// makes sure either the phone number or email are inputted
if (phone.isEmpty() && email.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please provide either a phone number or an email address.", "Error", JOptionPane.ERROR_MESSAGE);
}
try {
clientBase.addClient(username, address, phone, email, age);
JOptionPane.showMessageDialog(null, "Sign up successful!", "Success", JOptionPane.INFORMATION_MESSAGE);
return true;
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Error occured during sign up. Please try again.", "Error", JOptionPane.ERROR_MESSAGE);
return false;
}
}
return false;
}
private static String displayChange(String unchanged, String changed) {
return unchanged + " -> " + changed;
}
private static String ordersToString(Produce[] array) {
StringBuilder sb = new StringBuilder();
Produce currentItem = new Produce("placeholder", "none");
if(currentItem.equals(array[0])) {
sb.append("No orders found");
} else {
Produce previousItem = null;
int count = 1;
for (int i = 0; i < array.length; i++) {
currentItem = array[i];
if (previousItem != null && currentItem.equals(previousItem)) {
count++;
} else if (previousItem != null) {
sb.append(" " + previousItem.getName() + " x" + count + "\n");
count = 1;
}
previousItem = array[i];
}
sb.append(" " + previousItem.getName() + " x" + count + "\n");
}
return sb.toString();
}
}