-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProductController.java
448 lines (419 loc) · 15.1 KB
/
ProductController.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
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
import java.util.ArrayList;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Write a description of class ProductController here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class ProductController
{
private ArrayList<Product> productList;
private ArrayList<Product> searchResult;
/**
* A default constructor for objects of class ProductController.
*/
public ProductController()
{
productList = new ArrayList<Product>();
searchResult = new ArrayList<Product>();
}
/**
* A constructor for objects of class ProductController.
*/
public ProductController(ArrayList<Product> productList, ArrayList<Product> searchResult)
{
this.productList = productList;
this.searchResult = searchResult;
}
/**
* Create a addProduct method to add the product.
*/
public void addProduct(String name,double amount,String origin,int shelfLife,double discount,String category,ArrayList<String[]> sellingOptionList)
{
int size = productList.size();
String id = "";
category = category.substring(0,1).toUpperCase();
if (size == 0)
id = category + String.valueOf(1);
else
{
String idNumber = productList.get(size - 1).getId().substring(1);
String newNumber = String.valueOf(Integer.valueOf(idNumber) + 1);
id = category + newNumber;
}
Calendar date = Calendar.getInstance();
productList.add(new Product(id,name,amount,origin,shelfLife,discount,date,sellingOptionList));
System.out.println("New product added.");
}
/**
* Create a deleteProduct method to delete the product.
*/
public void deleteProduct()
{
System.out.println("Please enter the product id which you want delete:");
Scanner console = new Scanner(System.in);
String id = console.nextLine().trim();
int productIndex = productIndex(id);
if (productIndex == -1)
System.out.println("Product doesn't exist!");
else
{
productList.remove(productIndex);
System.out.println("Product deleted!");
}
}
/**
* Create a editAmount method to edit the product amount.
*/
public void editAmount(int index,double amount)
{
productList.get(index).setAmount(amount);
System.out.println("Amount edited!");
Scanner console = new Scanner(System.in);
System.out.println("Please press enter to continue!");
console.nextLine();
}
/**
* Create a editCategory method to edit the product category.
*/
public void editCategory(int index,String category)
{
String oldId = productList.get(index).getId();
String newId = category.substring(0,1).toUpperCase() + oldId.substring(1);
productList.get(index).setId(newId);
System.out.println("Category edited!");
Scanner console = new Scanner(System.in);
System.out.println("Please press enter to continue!");
console.nextLine();
}
/**
* Create a editProductName method to edit the product name.
*/
public void editProductName(int index,String name)
{
productList.get(index).setName(name);
System.out.println("Name edited!");
Scanner console = new Scanner(System.in);
System.out.println("Please press enter to continue!");
console.nextLine();
}
/**
* Create a editDiscount method to edit the product discount.
*/
public void editDiscount(int index,double discount)
{
productList.get(index).setDiscount(discount);
System.out.println("Discount edited!");
Scanner console = new Scanner(System.in);
System.out.println("Please press enter to continue!");
console.nextLine();
}
/**
* Create a editOrigin method to edit the product origin.
*/
public void editOrigin(int index,String origin)
{
productList.get(index).setOrigin(origin);
System.out.println("Origin edited!");
Scanner console = new Scanner(System.in);
System.out.println("Please press enter to continue!");
console.nextLine();
}
/**
* Create a editSellingOption method to edit the selling option.
*/
public void editSellingOption(int index,ArrayList<String[]> sellingOption)
{
productList.get(index).setSellingOptionList(sellingOption);
System.out.println("Selling option edited!");
Scanner console = new Scanner(System.in);
System.out.println("Please press enter to continue!");
console.nextLine();
}
/**
* Create a editShelfLife method to edit the shelf life.
*/
public void editShelfLife(int index,int shelfLife)
{
productList.get(index).setShelfLife(shelfLife);
System.out.println("Shelf life edited!");
Scanner console = new Scanner(System.in);
System.out.println("Please press enter to continue!");
console.nextLine();
}
/**
* Create a getInventoryUnit method to get the inventory unit.
*/
public char getInventoryUnit(int index)
{
return productList.get(index).inventoryUnit();
}
/**
* Create a getProductList method to get the product's list.
*/
public ArrayList<Product> getProductList()
{
return productList;
}
/**
* Create a getSearchResult method to get the search result.
*/
public ArrayList<Product> getSearchResult()
{
return searchResult;
}
/**
* Create a ifProductNameSame method to verify whether the product name already exist or not.
*/
private boolean ifProductNameSame(String name)
{
int size = productList.size();
for (int i = 0;i < size;i++)
if (productList.get(i).getName().equals(name))
{
System.out.println("Product name already exist,please change a new name!");
return true;
}
return false;
}
/**
* Create a initialProductList method to get the initial product list.
*/
public void initialProductList(ArrayList<String> detailList)
{
int size = detailList.size();
for(int i = 0;i < size;i++)
{
String[] productDetails = detailList.get(i).split(",");
Calendar date = strToCal(productDetails[6]);
int length = productDetails.length - 1;
int currentIndex = 7;
ArrayList<String[]> sellingOptionList = new ArrayList<String[]>();
while (currentIndex < length)
{
String[] option = new String[]{productDetails[currentIndex],productDetails[currentIndex + 1],productDetails[currentIndex + 2]};
sellingOptionList.add(option);
currentIndex += 3;
}
productList.add(new Product(productDetails[0],productDetails[1],Double.valueOf(productDetails[2]),productDetails[3],
Integer.parseInt(productDetails[4]),Double.parseDouble(productDetails[5]),date,sellingOptionList));
}
}
/**
* Create a productIndex method to get the product index.
*/
public int productIndex(String id)
{
int size = productList.size();
for (int i = 0;i < size;i++)
if (productList.get(i).getId().toUpperCase().equals(id.toUpperCase()))
return i;
return -1;
}
/**
* Create a searchByCategory method to get the result searched by category.
*/
public int searchByCategory(String category)
{
searchResult = new ArrayList<Product>();
Scanner console = new Scanner(System.in);
category = category.substring(0,1).toUpperCase();
if (category.equals("X"))
return -1;
int count = 0;
System.out.println(" Search Result ");
System.out.println("------------------------------------------------------------------------------------");
for (int i = 0;i < productList.size();i++)
{
if (productList.get(i).getId().substring(0,1).equals(category))
{
System.out.print(count + 1 +". ");
productList.get(i).displayProduct();
searchResult.add(productList.get(i));
count ++;
}
}
System.out.println("Total " + count + " results!");
return count;
}
/**
* Create a searchByDiscount method to get the result searched by discount.
*/
public int searchByDiscount(double minimumDiscount,double maxmumDiscount)
{
searchResult = new ArrayList<Product>();
Scanner console = new Scanner(System.in);
System.out.println(" Search Result ");
System.out.println("------------------------------------------------------------------------------------");
int count = 0;
for (int index = 0; index < productList.size(); index++)
{
if (productList.get(index).getDiscount() <= maxmumDiscount && productList.get(index).getDiscount() >= minimumDiscount)
{
System.out.print(index + 1 +". ");
productList.get(index).displayProduct();
searchResult.add(productList.get(index));
count ++;
}
}
System.out.println("Total " + count + " results!");
return count;
}
/**
* Create a searchByOrigin method to get the result searched by product origin.
*/
public int searchByOrigin()
{
searchResult = new ArrayList<Product>();
Scanner console = new Scanner(System.in);
System.out.println("Please enter product origin(Enter \"X\" to cancel): ");
String origin = console.nextLine().trim();
if (origin.equals("x") || origin.equals("X"))
return -1;
int count = 0;
System.out.println(" Search Result ");
System.out.println("------------------------------------------------------------------------------------");
for (int i = 0;i < productList.size();i++)
{
if (productList.get(i).getOrigin().toUpperCase().contains(origin.toUpperCase()))
{
System.out.print(count + 1 +". ");
productList.get(i).displayProduct();
searchResult.add(productList.get(i));
count++;
}
}
System.out.println("Total " + count + " results!");
return count;
}
/**
* Create a searchProductByName method to get the result searched by the product's name.
*/
public int searchProductByName()
{
searchResult = new ArrayList<Product>();
Scanner console = new Scanner(System.in);
System.out.println("Please enter product name(Enter \"X\" to cancel): ");
String name = console.nextLine().trim().toUpperCase();
if (name.equals("X"))
return -1;
int count = 0;
System.out.println(" Search Result ");
System.out.println("------------------------------------------------------------------------------------");
for (int i = 0;i < productList.size();i++)
{
if (productList.get(i).getName().toUpperCase().contains(name))
{
System.out.print(count + 1 +". ");
productList.get(i).displayProduct();
searchResult.add(productList.get(i));
count++;
}
}
System.out.println("Total " + count + " results!");
//System.out.println("Please press enter to continue!");
//console.nextLine();
return count;
}
/**
* Create a setProductList method to set the new product's list.
*/
public void setProductList(ArrayList<Product> newProductList)
{
productList = newProductList;
}
/**
* Create a setSearchResult method to transfer the search result (newSearchResult) into the searchResult.
*/
public void setSearchResult(ArrayList<Product> newSearchResult)
{
searchResult = newSearchResult;
}
/**
* Create a showSellingOption method to show the selling option.
*/
public int showSellingOption(int index)
{
ArrayList<String[]> option = productList.get(index).getSellingOptionList();
int size = option.size();
double discount = productList.get(index).getDiscount();
System.out.println("You are buying " + productList.get(index).getName() + ", The discount is " + discount);
for(int i = 0;i < size;i++)
{
int optionIndex = i + 1;
System.out.println("Selling option " + optionIndex);
String[] sellingOption = option.get(i);
String discountPrice = String.format("%.2f", Double.valueOf(sellingOption[2]) * discount);
System.out.println("Original price: AU$" + Double.valueOf(sellingOption[2]) + ", Price after discount: AU$" +
discountPrice + " for 1 " + sellingOption[0] + "(" + sellingOption[1] + " kg)");
}
return size;
}
/**
* Create a strToCal method to get date.
*/
private Calendar strToCal(String str)
{
Date date = new Date();
SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy");
try
{
date = sdf.parse(str);
}
catch(Exception e)
{
System.out.println("Calendar Exception.");
}
finally
{
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar;
}
}
/**
* Create a verifyProductId method to verify the product id.
*/
public int verifyProductId(String id)
{
int size = productList.size();
int productIndex = 0;
boolean found = false;
while (productIndex < size && !found)
{
if (productList.get(productIndex).getId().toUpperCase().equals(id.toUpperCase()))
found = true;
productIndex++;
}
if (found)
{
productIndex--;
if (productList.get(productIndex).ifExpired())
return -2;
else
return productIndex;
}
else
return -1;
}
/**
* Create a viewProduct method to display the product details.
*/
public int viewProduct()
{
System.out.println("\u000c");
System.out.println(" Product Details");
System.out.println("------------------------------------------------------------------------------------");
int size = productList.size();
for (int i = 0;i < size;i++)
{
System.out.print(i + 1 + ". ");
productList.get(i).displayProduct();
}
System.out.println("Total " + size + " results!");
Scanner console = new Scanner(System.in);
return size;
}
}