This repository was archived by the owner on Nov 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShop.java
More file actions
598 lines (518 loc) · 16.9 KB
/
Shop.java
File metadata and controls
598 lines (518 loc) · 16.9 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
import java.util.*;
import java.awt.*;
import java.io.*;
/**
* Shop to store tools
*
* @author George Broadley
* @version 1.0.0
*/
public class Shop
{
// instance variables
//private List<ShopItem> shopItemMap;
//private List<Customer> customerMap;
private HashMap<String, ShopItem> shopItemMap;
private HashMap<String, Customer> customerMap;
private HashMap<String, ShopItemReservation> itemReservationMap;
private Random randomGenerator;
private Diary diary;
private String dumpCustomerDataFileName;
private String dumpItemReservationDataFileName;
/**
* Constructor for objects of class Shop
*/
public Shop(String shopName)
{
shopItemMap = new HashMap<String, ShopItem>();
customerMap = new HashMap<String, Customer>();
itemReservationMap = new HashMap<String, ShopItemReservation>();
randomGenerator = new Random();
diary = new Diary();
dumpCustomerDataFileName = shopName + "customer_dump.txt";
dumpItemReservationDataFileName = shopName + "reservation_dump.txt";
readItemData("items_all.txt");
reloadSystem();
}
public void storeCustomer(Customer customer)
{
customerMap.put(customer.getCustomerID(), customer);
}
public void storeItemReservation(ShopItemReservation shopItemReservation)
{
diary.addItemReservation(shopItemReservation);
itemReservationMap.put(shopItemReservation.getReservationNo(), shopItemReservation);
}
public void printAllCustomers()
{
if (customerMap.isEmpty())
{
System.out.println("There are no customers to show");
}
else
{
for (Customer customer:customerMap.values())
{
customer.printDetails();
}
}
}
public void readCustomerData()
{
FileDialog fileDialog = new FileDialog(new Frame(), "Open", FileDialog.LOAD);
fileDialog.setVisible(true);
readCustomerData(fileDialog.getDirectory() + fileDialog.getFile());
}
public void readCustomerData(String filename)
{
if (filename == null)
{
System.out.println("Error 01: File not found, please try again with a valid file");
}
else
{
Scanner scanner = new Scanner("");
try
{
File file = new File(filename);
scanner = new Scanner(file);
}
catch(FileNotFoundException e)
{
System.out.println("Error 02: File not found, please try again with a valid file");
}
while (scanner.hasNext())
{
String nextLine = scanner.nextLine().trim();
if (checkIfEmptyOrComment(nextLine))
{
//
}
else
{
Customer customer = new Customer();
Scanner fieldScanner = new Scanner(nextLine);
fieldScanner.useDelimiter(",");
customer.readData(fieldScanner);
customer.setIdIfUnknown(generateCustomerID("AB-", 6));
customerMap.put(customer.getCustomerID(), customer);
fieldScanner.close();
}
}
scanner.close();
}
}
public void readItemReservationData()
{
FileDialog fileDialog = new FileDialog(new Frame(), "Open", FileDialog.LOAD);
fileDialog.setVisible(true);
readItemReservationData(fileDialog.getDirectory() + fileDialog.getFile());
}
public void readItemReservationData(String filename)
{
if (filename == null)
{
System.out.println("Error 01: File not found, please try again with a valid file");
}
else
{
Scanner scanner = new Scanner("");
try
{
File file = new File(filename);
scanner = new Scanner(file);
}
catch(FileNotFoundException e)
{
System.out.println("Error 02: File not found, please try again with a valid file");
}
while (scanner.hasNext())
{
String nextLine = scanner.nextLine().trim();
if (checkIfEmptyOrComment(nextLine))
{
//
}
else
{
ShopItemReservation shopItemReservation = new ShopItemReservation();
Scanner fieldScanner = new Scanner(nextLine);
fieldScanner.useDelimiter(",");
shopItemReservation.readData(fieldScanner);
itemReservationMap.put(shopItemReservation.getReservationNo(), shopItemReservation);
fieldScanner.close();
}
}
scanner.close();
}
}
public void writeCustomerData()
{
FileDialog fileDialog = new FileDialog(new Frame(), "Open", FileDialog.LOAD);
fileDialog.setVisible(true);
writeCustomerData(fileDialog.getDirectory() + fileDialog.getFile());
}
public void writeCustomerData(String filename)
{
PrintWriter pWriter = null;
try
{
pWriter = new PrintWriter(filename);
}
catch(FileNotFoundException e)
{
System.out.println("File does not exist");
}
if (!customerMap.isEmpty())
{
for (Customer customer:customerMap.values())
{
customer.writeData(pWriter);
}
//pWriter.println();
pWriter.close();
}
else
{
System.out.println("There is no data to write");
}
}
public void writeItemReservationData()
{
FileDialog fileDialog = new FileDialog(new Frame(), "Open", FileDialog.LOAD);
fileDialog.setVisible(true);
writeItemReservationData(fileDialog.getDirectory() + fileDialog.getFile());
}
public void writeItemReservationData(String filename)
{
PrintWriter pWriter = null;
try
{
pWriter = new PrintWriter(filename);
}
catch(FileNotFoundException e)
{
System.out.println("File does not exist");
}
if (!itemReservationMap.isEmpty())
{
for (ShopItemReservation shopItemReservation:itemReservationMap.values())
{
shopItemReservation.writeData(pWriter);
}
//pWriter.println();
pWriter.close();
}
else
{
System.out.println("There is no data to write");
}
}
/**
* Reader for the shop data. Takes file input from dialog and puts that data into the tool arraylist.
*/
public void readItemData()
{
FileDialog fileDialog = new FileDialog(new Frame(), "Open", FileDialog.LOAD);
fileDialog.setVisible(true);
readItemData(fileDialog.getDirectory() + fileDialog.getFile());
}
public void readItemData(String filename)
{
if (filename == null)
{
System.out.println("Error 01: File not found, please try again with a valid file");
}
else
{
Scanner scanner = new Scanner("");
try
{
File file = new File(filename);
scanner = new Scanner(file);
}
catch(FileNotFoundException e)
{
System.out.println("Error 02: File not found, please try again with a valid file");
}
String typeOfData = "[Electric";
while (scanner.hasNext())
{
String nextLine = scanner.nextLine().trim();
if (checkIfEmptyOrComment(nextLine))
{
//
}
else if (nextLine.startsWith("["))
{
typeOfData = nextLine;
}
else
{
ShopItem newItem = getItemType(typeOfData);
if (newItem != null)
{
Scanner fieldScanner = new Scanner(nextLine);
fieldScanner.useDelimiter(",");
newItem.extractData(fieldScanner);
shopItemMap.put(newItem.getItemCode(), newItem);
fieldScanner.close();
}
else
{
System.out.println("Error 03: Type could not be recognised.");
}
}
}
scanner.close();
}
}
public ShopItemReservation getItemReservation(String reservationNo)
{
return itemReservationMap.get(reservationNo);
}
/**
* Method returns true if the line inputted is either a comment (line starts with "//") or empty, else the method returns false.
*/
private boolean checkIfEmptyOrComment(String lineToCheck)
{
if (lineToCheck.equals(""))
{
return true;
}
else if (lineToCheck.startsWith("//"))
{
return true;
}
else
{
return false;
}
}
/**
* method returns the type of item or null if it doesn't match the item
*/
private ShopItem getItemType(String typeOfData)
{
if (typeOfData.toLowerCase().startsWith("[electric"))
{
return new ElectricTool();
}
else if (typeOfData.toLowerCase().startsWith("[hand"))
{
return new HandTool();
}
else if (typeOfData.toLowerCase().startsWith("[perishable"))
{
return new Perishable();
}
else if (typeOfData.toLowerCase().startsWith("[workwear"))
{
return new Workwear();
}
else
{
return null;
}
}
/**
* Method loops through each tool in the arraylist and calls that tool's printDetails method.
*/
public void printItemDetails()
{
if (shopItemMap.isEmpty())
{
System.out.println("There are no tools to show");
}
else
{
for (ShopItem shopItem:shopItemMap.values())
{
shopItem.printDetails();
}
}
}
/**
* Method loops through each reservation in the hashmap and calls that reservation's printDetails method.
*/
public void printItemReservationsDetails()
{
if (itemReservationMap.isEmpty())
{
System.out.println("There are no reservations to show");
}
else
{
for (ShopItemReservation shopItemReservation:itemReservationMap.values())
{
shopItemReservation.printDetails();
}
}
}
private String generateCustomerID(String prefix, int length)
{
String id = prefix + (randomGenerator.nextInt(9) + 1);
for (int i = 1; i < length; i++)
{
id = id + randomGenerator.nextInt(10);
}
for(Customer customer: customerMap.values())
{
if (customer.getCustomerID().equals(id))
{
id = generateCustomerID(prefix, length);
return id;
}
}
return id;
}
private String generateReservationNo()
{
Scanner scanner = new Scanner("");
int lastId = 0;
try
{
File file = new File("reservationNo.txt");
scanner = new Scanner(file);
lastId = scanner.nextInt();
scanner.close();
PrintWriter pWriter = new PrintWriter("reservationNo.txt");
pWriter.println(lastId+1);
pWriter.close();
}
catch(FileNotFoundException e)
{
System.out.println("Error 03: No reservation file, generating file.");
try
{
PrintWriter pWriter = new PrintWriter("reservationNo.txt");
pWriter.println(1);
pWriter.close();
}
catch(FileNotFoundException f)
{
System.out.println("Error 04: Generating reservation file failed.");
}
}
String id = Integer.toString(lastId+1);
while (id.length() < 6)
{
id = "0" + id;
}
return id;
}
public boolean makeItemReservation(String customerID, String itemID, String startDate, int noOfDays)
{
String reservationNo = generateReservationNo();
if (!customerMap.containsKey(customerID))
{
System.out.println("CustomerID");
return false;
}
else if (!shopItemMap.containsKey(itemID))
{
System.out.println("itemID");
return false;
}
else if (!DateUtil.isValidDateString(startDate))
{
System.out.println("startDate");
return false;
}
else if (noOfDays < 0)
{
System.out.println("noOfDays");
return false;
}
else
{
for (int currDays = 0; currDays <= noOfDays; currDays++)
{
ShopItemReservation[] shopItemReservations = diary.getItemReservations(DateUtil.incrementDate(DateUtil.convertStringToDate(startDate), currDays));
if (shopItemReservations != null)
{
for(ShopItemReservation shopItemReservation:shopItemReservations)
{
if(shopItemReservation.getItemID() == itemID)
{
System.out.println("Duplicate");
return false;
}
}
}
}
ShopItemReservation shopItemReservation = new ShopItemReservation(reservationNo, itemID, customerID, startDate, noOfDays);
storeItemReservation(shopItemReservation);
return true;
}
}
public boolean deleteItemReservation(String reservationNo)
{
if (!itemReservationMap.containsKey(reservationNo))
{
return false;
}
else
{
diary.deleteItemReservation(itemReservationMap.get(reservationNo));
itemReservationMap.remove(reservationNo);
return true;
}
}
public void printDiaryEntries(String startDate, String endDate)
{
if (!DateUtil.isValidDateString(startDate))
{
System.out.println("Start date is not a valid date");
}
else if (!DateUtil.isValidDateString(endDate))
{
System.out.println("End date is not a valid date");
}
else
{
diary.printEntries(DateUtil.convertStringToDate(startDate), DateUtil.convertStringToDate(endDate));
}
}
public void closeDownSystem()
{
writeCustomerData(dumpCustomerDataFileName);
writeItemReservationData(dumpItemReservationDataFileName);
}
public void reloadSystem()
{
readCustomerData(dumpCustomerDataFileName);
readItemReservationData(dumpItemReservationDataFileName);
}
public int getNumberOfItems()
{
return shopItemMap.size();
}
public int getNumberOfCustomers()
{
return customerMap.size();
}
public int getNumberOfReservations()
{
return itemReservationMap.size();
}
/**
* Getters and Setters below
*/
public HashMap getShopItemList()
{
return shopItemMap;
}
public void setShopItemList(HashMap<String, ShopItem> shopItemMap)
{
this.shopItemMap = shopItemMap;
}
public ShopItem getItem(String id)
{
return shopItemMap.get(id);
}
public Customer getCustomer(String id)
{
return customerMap.get(id);
}
}