-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUserInterface.java
More file actions
344 lines (320 loc) · 11.4 KB
/
UserInterface.java
File metadata and controls
344 lines (320 loc) · 11.4 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
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 ClientList clientList;
private ProductList productList;
private SupplierList supplierList;
private static final int EXIT = 0;
private static final int ADD_PRODUCT = 1;
private static final int ADD_CLIENT = 2;
private static final int ADD_SUPPLIER = 3;
private static final int ADD_TO_CART = 4;
private static final int EDIT_A_FIELD = 5;
private static final int ADD_ORDER = 6;
private static final int SHIPMENT = 7;
private static final int REMOVE_FROM_CART = 8;
private static final int PAYMENT = 9;
private static final int CHANGE_QUANTITY = 10;
private static final int MENU = 11;
private UserInterface() {
supplierList = SupplierList.instance();
productList = ProductList.instance();
clientList = ClientList.instance();
}
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);
}
public int getCommand() {
do {
try {
int value = Integer.parseInt(getToken("Enter a command. [Use command " + MENU + " for a list of commands]"));
if (value >= EXIT && value <= MENU) {
return value;
}
} catch (NumberFormatException nfe) {
System.out.println("Please enter a number");
}
} while (true);
}
public void menu() {
System.out.println("Enter the number corresponding to the desired command:");
System.out.println("Exit: " + EXIT);
System.out.println("Add a Product: " + ADD_PRODUCT);
System.out.println("Add a Client: " + ADD_CLIENT);
System.out.println("Add a Supplier: " + ADD_SUPPLIER);
System.out.println("Add to Cart: " + ADD_TO_CART);
System.out.println("Edit a Field: " + EDIT_A_FIELD);
System.out.println("Add a Order: " + ADD_ORDER);
System.out.println("Recieve a Shipment: " + SHIPMENT);
System.out.println("Remove from Cart: " + REMOVE_FROM_CART);
System.out.println("Make a Payment " + PAYMENT);
System.out.println("Change Quantity in Cart: " + CHANGE_QUANTITY);
System.out.println("Menu: " + MENU);
}
public void addProduct() {
boolean successful;
String productName = getToken("Enter the product name: ");
int productQuantity = Integer.parseInt(getToken("Enter the product quantity: "));
int productPrice = Integer.parseInt(getToken("Enter the product price: "));
Product newProduct = new Product(productName, productQuantity, productPrice);
successful = productList.insertProduct(newProduct);
if (successful) {
System.out.println("Product with details (" + newProduct.toString() + ") added successfully");
} else {
System.out.println("Issue adding product!");
}
}
public void addClient() {
boolean successful;
String clientName = getToken("Enter the client's name: ");
String clientAddress = getToken("Enter the client's address: ");
Client newClient = new Client(clientName, clientAddress); //DOESNT WANT TO CREATE THE CLIENT OR ADD TO LIST
successful = clientList.insertMember(newClient);
if (successful) {
System.out.println("Client with details (" + newClient.toString() + ") added successfully");
} else {
System.out.println("Issue adding client!");
}
}
public void addSupplier() {
boolean successful;
String supplierName = getToken("Enter the supplier name:");
Supplier newSupplier = new Supplier(supplierName);
successful = supplierList.insertSupplier(newSupplier);
if (successful) {
System.out.println("Supplier with name " + newSupplier.getName() + " and ID " + newSupplier.get_ID() + " added successfully");
} else {
System.out.println("Issue adding supplier!");
}
}
public void addToCart() {
int clientID = Integer.parseInt(getToken("Enter ID of client to add to their cart"));
int position = clientList.IDcheck(clientID);
Client item = clientList.get_listed_obj(position);
int productToAddID = Integer.parseInt(getToken("Please enter the product ID you wish to add: "));
Product productToAdd = productList.search(productToAddID);
int quant = Integer.parseInt(getToken("Enter the quantity to be added to the cart: "));
item.AddToCart(productToAdd, quant);
}
public void editAField() {
System.out.println("Would you like to edit");
System.out.println("1. Product");
System.out.println("2. Client");
System.out.println("3. Supplier");
int choice = Integer.parseInt(getToken("Enter your choice: "));
switch (choice) {
case 1: int productID = Integer.parseInt(getToken("Enter ID of product to edit"));
int position = productList.IDcheck(productID);
Product product = productList.get_listed_obj(position);
product = edit_product(product);
productList.set_listed_obj(position, product);
break;
case 2: int clientID = Integer.parseInt(getToken("Enter ID of client to edit"));
position = clientList.IDcheck(clientID);
Client client = clientList.get_listed_obj(position);
client = edit_client(client);
clientList.set_listed_obj(position, client);
break;
case 3: int supplierID = Integer.parseInt(getToken("Enter ID of supplier to edit"));
position = supplierList.IDcheck(supplierID);
Supplier supplier = supplierList.get_listed_obj(position);
supplier = edit_supplier(supplier);
supplierList.set_listed_obj(position, supplier);
break;
default:
System.out.println("Incorrect choice");
}
}
public Supplier edit_supplier (Supplier item) {
String userEdit = getToken("Edit NAME or LIST?");
switch (userEdit) {
case "NAME": String name = getToken("To what name?");
item.setName(name);
break;
case "LIST": int id = Integer.parseInt(getToken("ID of product to add, delete or edit"));
int IDcheck = productList.IDcheck(id);
if (IDcheck == -1) {
System.out.println("No product with that ID");
} else {
String choice = getToken("INSERT, REMOVE, or CHANGE?");
switch (choice) {
case "INSERT": String nameP = getToken("enter the product name");
String price_info = getToken("enter the product price information");
double price = Double.parseDouble(getToken("enter the product price"));
item.create_item(price, id, nameP, price_info);
break;
case "REMOVE":
item.delete_item(id);
break;
case "CHANGE":
item.grab_item(id);
break;
default: System.out.println("no action chosen, none taken");
}
}
break;
default: System.out.println("no action chosen, none taken");
}
return item;
}
public Client edit_client (Client item) {
String userEdit = getToken("Edit NAME or ADDRESS");
switch (userEdit) {
case "NAME": String name = getToken("To what name?");
item.setName(name);
break;
case "ADDRESS": String address = getToken("To what address?");
item.setAddress(address);
break;
default: System.out.println("no action chosen, none taken");
}
return item;
}
public Product edit_product (Product item) {
String userEdit = getToken("Edit NAME, PRICE, OR QUANTITY");
switch (userEdit) {
case "NAME": String name = getToken("To what name?");
item.setName(name);
break;
case "PRICE": int price = Integer.parseInt(getToken("To what price"));
item.setPrice(price);
break;
case "QUANTITY": int quantity = Integer.parseInt(getToken("To what quantity"));
item.setQuantity(quantity);
break;
default: System.out.println("no action chosen, none taken");
}
return item;
}
public void addOrder () {
int clientID = Integer.parseInt(getToken("Enter ID of client to add order from"));
int position = clientList.IDcheck(clientID);
Client item = clientList.get_listed_obj(position);
clientList.GenerateOrder(item);
}
public void shipment () {
int supplierID = Integer.parseInt(getToken("Enter the ID of the supplier to recieve shipment from"));
int position1 = supplierList.IDcheck(supplierID);
Supplier shipper = supplierList.get_listed_obj(position1);
Iterator<Supply> shipment = shipper.shipment();
while (shipment.hasNext()) {
Supply item = shipment.next();
int productID = item.print_name_id();
int quantity = Integer.parseInt(getToken("Enter the quantity received of the product (0 if none)"));
int position2 = productList.IDcheck(productID);
Product prod = productList.get_listed_obj(position2);
quantity = quantity + prod.getQuantity();
prod.setQuantity(quantity);
}
}
public void removeFromCart () {
int clientID = Integer.parseInt(getToken("Enter ID of client to delete from their cart"));
int position = clientList.IDcheck(clientID);
Client item = clientList.get_listed_obj(position);
int productID = Integer.parseInt(getToken("Please enter the product ID you wish to delete: "));
item.RemoveCartProduct(productID);
}
public void makePayment () {
int clientID = Integer.parseInt(getToken("Enter ID of client paying invoice"));
int position = clientList.IDcheck(clientID);
Client item = clientList.get_listed_obj(position);
int invoiceID = Integer.parseInt(getToken("Enter ID of invoice to pay"));
item.PayInvoice(invoiceID);
}
public void changeQuantity() {
int clientID = Integer.parseInt(getToken("Enter ID of client to edit quantity in their cart: "));
int position = clientList.IDcheck(clientID);
Client item = clientList.get_listed_obj(position);
int productID = Integer.parseInt(getToken("Enter the product ID you wish to change the quantity of: "));
int qty = Integer.parseInt(getToken("Enter the new quantity: "));
item.ChangeQuantity(productID, qty);
}
public Product findProduct(int pId) {
return productList.find(pId);
}
//add to waitlist
public Iterator getWaitList(integer pId) {
// get product waitlist
Product p = productList.find(pId);
return p.getWaitList();
}
///Waitlist for product maintained
public void productWait (integer pID) {
if (findProduct(pI) != null) {
Iterator wholeWaitList =getWaitList(pId);
while (wholeWaitList.hasNext()) {
WaitList waitList = (WaitList) (wholeWaitList.next());
Println(waitList.toString());
}
} else {
Println("Product not found.");
}
public void process() {
int command;
menu();
while ((command = getCommand()) != EXIT) {
switch (command) {
case ADD_PRODUCT:
addProduct();
break;
case ADD_CLIENT:
addClient();
break;
case ADD_SUPPLIER:
addSupplier();
break;
case ADD_TO_CART:
addToCart();
break;
case EDIT_A_FIELD:
editAField();
break;
case ADD_ORDER:
addOrder();
break;
case SHIPMENT:
shipment();
break;
case REMOVE_FROM_CART:
removeFromCart();
break;
case PAYMENT:
makePayment();
break;
case CHANGE_QUANTITY:
changeQuantity();
break;
case MENU:
menu();
break;
default:
System.out.println("Not a valid command");
command = EXIT;
}
}
}
public static void main(String[] args) {
UserInterface.instance().process();
}
}