-
Notifications
You must be signed in to change notification settings - Fork 0
/
Menu.java
703 lines (640 loc) · 26.2 KB
/
Menu.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
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/**
* @author Taylor Carr
*/
package assign5_tac91;
import java.io.*;
import java.util.*;
import static java.lang.System.out;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* This menu handles all user input.
*/
public class Menu {
static ArrayList<Package> packageList = new ArrayList<Package>();
static ArrayList<Users> userList = new ArrayList<>();
static ArrayList<Completed> completedList = new ArrayList<>();
/**
* <h2> Read Serialization File </h2>
*
* This method reads the ser file (if one exists) and adds that
* information to the database.
*/
public static void readFile() {
String filename = "packages.ser";
String filename2 = "users.ser";
String filename3 = "completed.ser";
try {
FileInputStream fin = new FileInputStream(filename);
ObjectInputStream ois = new ObjectInputStream(fin);
packageList = (ArrayList<Package>) ois.readObject();
FileInputStream fin2 = new FileInputStream(filename2);
ObjectInputStream ois2 = new ObjectInputStream(fin2);
userList = (ArrayList<Users>) ois2.readObject();
FileInputStream fin3 = new FileInputStream(filename3);
ObjectInputStream ois3 = new ObjectInputStream(fin3);
completedList = (ArrayList<Completed>) ois3.readObject();
}
catch (EOFException exc){
// End of file
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, exc);
}
catch (IOException ioe){
//out.println("IO Error");
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ioe);
}
catch (ClassNotFoundException cnfe){
//out.println("Class not found");
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, cnfe);
}
}
/**
* <h2> Display Menu </h2>
*
* This method displays the menu to the user.
* @return boolean
*//*
public static boolean displayMenu(){
System.out.println("---------------------------------------------------------");
System.out.println("| Menu |");
System.out.println("---------------------------------------------------------");
System.out.println("|1. Show all existing package records in the database. |");
System.out.println("|2. Add new package record to the database. |");
System.out.println("|3. Delete a package record from database. |");
System.out.println("|4. Search for a package using tracking number. |");
System.out.println("|5. Show a list of users. |");
System.out.println("|6. Add a new user to the database. |");
System.out.println("|7. Update user info. |");
System.out.println("|8. Complete a shipping transaction. |");
System.out.println("|9. Show completed shipping transactions. |");
System.out.println("|10. Exit program. |");
System.out.println("---------------------------------------------------------");
System.out.println();
System.out.print("Select an action: ");
Scanner cin = new Scanner(System.in);
int choice = cin.nextInt();
switch(choice){
case 1:
displayDatabase();
break;
case 2:
addPackage();
break;
case 3:
deletePackage();
break;
case 4:
search();
break;
case 5:
showUsers();
// showRange();
break;
case 6:
addUser();
break;
case 7:
updateUser();
break;
case 8:
completeTransaction();
break;
case 9:
showCompleted();
break;
case 10:
try {
saveDatabase();
}
catch (Exception e) {
out.println("Error: Couldn't save database");
}
return false;
default:
System.out.print("Invalid input");
break;
}
return true;
}*/
/**
* <h2> Display Database </h2>
*
* This method displays the current database of packages to the user.
*/
// Option 1
/*
public static void displayDatabase(){
sortPackageList(); // Sort before display
Package displayObj;
out.println("------------------------------------------------------------------------------");
out.println("| Database |");
out.println("------------------------------------------------------------------------------");
out.println("| Tracking # | Specification | Class | Other |");
out.println("------------------------------------------------------------------------------");
for (int i = 0; i < packageList.size(); i++){
displayObj = packageList.get(i);
out.format("|%12s|", displayObj.getTrackingNumber());
out.format("%15s|", displayObj.getSpec());
out.format("%9s|", displayObj.getMC());
if (displayObj instanceof Envelope){
out.print(" Height: ");
out.format("%12d|", ((Envelope) displayObj).getHeight());
out.print(" Width: ");
out.format("%7d|", ((Envelope) displayObj).getWidth());
}
else if (displayObj instanceof Box){
out.print(" Dimensions: ");
out.format("%8d|", ((Box) displayObj).getDimensions());
out.print(" Volume: ");
out.format("%6d|", ((Box) displayObj).getVolume());
}
else if (displayObj instanceof Crate){
out.print(" Content: ");
out.format("%11s|", ((Crate) displayObj).getContent());
out.print(" Weight: ");
out.format(" %2.1f|", ((Crate) displayObj).getWeight());
}
else { // Drum
out.print(" Material: ");
out.format("%10s|", ((Drum) displayObj).getMaterial());
out.print(" Diameter: ");
out.format("%3.1f|", ((Drum) displayObj).getDiameter());
}
out.println();
out.println("------------------------------------------------------------------------------");
// out.println();
}
}*/
/**
* <h2> Add a Package </h2>
*
* This method adds a package to the database.
*/
/* Option 2
public static void addPackage(){
Package newPackage = new Package();
out.println("What type of package is it?");
out.println("Envelope | Box | Crate | Drum");
out.println();
Scanner cin = new Scanner(System.in);
String input = cin.nextLine();
switch (input) {
case "Envelope":
case "envelope":
Envelope envelope = new Envelope();
envelope.newEnvelope();
packageList.add(envelope);
break;
case "Box":
case "box":
Box box = new Box();
box.newBox();
packageList.add(box);
break;
case "Crate":
case "crate":
Crate crate = new Crate();
crate.newCrate();
packageList.add(crate);
break;
case "Drum":
case "drum":
Drum drum = new Drum();
drum.newDrum();
packageList.add(drum);
break;
default:
out.println("Invalid type");
break;
}
sortPackageList(); // Sort list after insert;
}*/
/**
* <h2> Remove a Package </h2>
*
* This method deletes a package from the database using the tracking number.
*/
// Option 3
/*
public static void deletePackage(){
boolean found = false;
int i = 0;
out.print("Enter the tracking number of the package you want to delete: ");
Scanner cin = new Scanner(System.in);
String input = cin.nextLine();
while (i < packageList.size() && found == false){
Package deleteObj = packageList.get(i);
if (input.length() < 5){
out.println();
out.println("Invalid tracking number");
out.println();
i = packageList.size();
}
if (deleteObj.getTrackingNumber().equals(input)){
packageList.remove(i);
out.println();
out.println("Package " + input + " removed");
out.println();
found = true;
}
else if (i == packageList.size()-1){
out.println();
out.println("No package matches the given tracking number");
out.println();
i++;
}
else {
i++;
}
}
}*/
/**
* <h2> Search for a Package </h2>
*
* This method asks the user for a tracking number and searches for a
* package in the database with the matching number.
*/
// Option 4
/*
public static void search(){
boolean found = false;
int i = 0;
out.print("Enter the tracking number of the package you want to find: ");
Scanner cin = new Scanner(System.in);
String input = cin.nextLine();
while (i < packageList.size() && found == false){
Package foundObj = packageList.get(i);
if (input.length() != 5){
out.println();
out.println("Invalid tracking number");
out.println();
i = packageList.size();
}
if (foundObj.getTrackingNumber().equals(input)){
out.println();
out.println("------------------------------------------------------------------------------");
out.println("| Tracking # | Specification | Class | Other |");
out.println("------------------------------------------------------------------------------");
out.format("|%12s|", foundObj.getTrackingNumber());
out.format("%15s|", foundObj.getSpec());
out.format("%9s|", foundObj.getMC());
if (foundObj instanceof Envelope){
out.print(" Height: ");
out.format("%12d|", ((Envelope) foundObj).getHeight());
out.print(" Width: ");
out.format("%7d|", ((Envelope) foundObj).getWidth());
}
else if (foundObj instanceof Box){
out.print(" Dimensions: ");
out.format("%8d|", ((Box) foundObj).getDimensions());
out.print(" Volume: ");
out.format("%6d|", ((Box) foundObj).getVolume());
}
else if (foundObj instanceof Crate){
out.print(" Content: ");
out.format("%11s|", ((Crate) foundObj).getContent());
out.print(" Weight: ");
out.format(" %2.1f|", ((Crate) foundObj).getWeight());
}
else {
out.print(" Material: ");
out.format("%10s|", ((Drum) foundObj).getMaterial());
out.print(" Diameter: ");
out.format("%3.1f|", ((Drum) foundObj).getDiameter());
}
out.println();
out.println("------------------------------------------------------------------------------");
found = true;
}
else if (i == packageList.size()-1){
out.println();
out.println("No package matches the given tracking number");
out.println();
i++;
}
else {
i++;
}
}
}*/
/**
* <h2> Show Users </h2>
*
* This method reads shows a list of users (employees and customers
* currently in the database.
*/
// Option 5
/*
public static void showUsers(){
out.println();
out.println("-------------------------------------------------------------------------------------");
out.println("| Users |");
out.println("-------------------------------------------------------------------------------------");
out.println("| ID # | Name | Other |");
out.println("-------------------------------------------------------------------------------------");
for (Users userList1 : userList) {
out.format("|%6d|", userList1.getID());
out.format("%16s|", userList1.getName());
if (userList1 instanceof Employee){
out.print(" SSN: ");
out.format("%10d|", ((Employee) userList1).getSSN());
out.print(" Salary: ");
out.format("$%2.2f|", ((Employee) userList1).getSalary());
out.print(" Bank #: ");
out.format("%17d|", ((Employee) userList1).getBankNumber());
}
else {
out.print(" Phone #: ");
out.format("%13s|", ((Customer) userList1).getPhoneNumber());
out.print(" Address: ");
out.format("%25s|", ((Customer) userList1).getAddress());
}
out.println();
out.println("-------------------------------------------------------------------------------------");
}
}*/
/**
* <h2> Add User </h2>
*
* This method adds a user to the database/ArrayList
*/
// Option 6
/*
public static void addUser(){
out.println();
out.println("Is the new user an employee or a customer? ");
Scanner cin = new Scanner(System.in);
String input = cin.nextLine();
switch (input){
case "Employee":
case "employee":
Employee employee = new Employee();
employee.newEmployee();
userList.add(employee);
break;
case "Customer":
case "customer":
Customer customer = new Customer();
customer.newCustomer();
userList.add(customer);
break;
default:
out.println("Invalid user type.");
break;
}
}*/
/**
* <h2> Update User </h2>
*
* This method allows the someone to update a data field for a user
* in the database.
*/
// Option 7
/*
public static void updateUser(){
boolean found = false;
int i = 0;
out.print("Enter the ID of the user you want to update: ");
Scanner cin = new Scanner(System.in);
int input = cin.nextInt();
while (i < userList.size() && found == false){
Users foundUser = userList.get(i);
if (input < 11111 || input > 99999){
out.println();
out.println("Invalid User ID number");
out.println();
i = packageList.size();
}
if (input == foundUser.getID()) {
out.println("What do you want to change?");
if (foundUser instanceof Employee){
out.println("Name, SSN, Salary, Bank Number");
cin.nextLine();
String answer = cin.nextLine();
switch(answer){
case "Name":
case "name":
out.print("Enter the full name: ");
answer = cin.nextLine();
String[] name = answer.split(" ");
foundUser.setName(name[0], name[1]);
break;
case "SSN":
case "ssn":
out.print("Enter the SSN: ");
int SSN = cin.nextInt();
((Employee) foundUser).setSSN(SSN);
break;
case "Salary":
case "salary":
out.print("Enter the Salary: ");
float salary = cin.nextFloat();
((Employee) foundUser).setSalary(salary);
break;
case "Bank Number":
case "bank number":
out.print("Enter the Bank Number: ");
int bankNum = cin.nextInt();
((Employee) foundUser).setSSN(bankNum);
break;
default:
out.println("Invalid answer.");
break;
}
}
else {
out.println("Name, Phone Number, Address");
cin.nextLine();
String answer = cin.nextLine();
switch(answer){
case "Name":
case "name":
out.print("Enter the full name: ");
answer = cin.nextLine();
String[] name = answer.split(" ");
foundUser.setName(name[0], name[1]);
break;
case "Phone Number":
case "phone number":
out.print("Enter the phone number: ");
answer = cin.nextLine();
((Customer) foundUser).setPhoneNumber(answer);
break;
case "Address":
case "address":
out.print("Enter the address: ");
answer = cin.nextLine();
((Customer) foundUser).setAddress(answer);
break;
default:
out.println("Invalid answer.");
break;
}
}
out.println();
out.println("Field Updated.");
out.println();
found = true;
}
else if (i == userList.size()-1){
out.println();
out.println("No user matches the given ID number");
out.println();
i++;
}
else {
i++;
}
}
}*/
/**
* <h2> Complete Transaction </h2>
*
* This method takes a package from the package list and moves it
* over to the completed list, with the addition of customer, employee,
* shipping and cost information.
*/
//Option 8
/*
public static void completeTransaction(){
out.print("Enter customer ID number: ");
Scanner cin = new Scanner(System.in);
int customerID = cin.nextInt();
Users searchObj;
Package packageObj;
boolean valid = false;
int employeeID = 0;
String trackingNum = null;
for (int i = 0; i < userList.size(); i++) {
searchObj = userList.get(i);
if (customerID == searchObj.getID() && searchObj instanceof Customer){
valid = true;
}
else if (valid == false && i == userList.size() - 1){
out.println("Customer not found.");
}
}
if (valid == true){
valid = false;
out.print("Enter employee ID number: ");
employeeID = cin.nextInt();
for (int i = 0; i < userList.size(); i++) {
searchObj = userList.get(i);
if (employeeID == searchObj.getID() && searchObj instanceof Employee){
valid = true;
}
else if (valid == false && i == userList.size() - 1){
out.println("Employee not found.");
}
}
}
if (valid == true){
valid = false;
out.print("Enter the tracking number: ");
cin.nextLine();
trackingNum = cin.nextLine();
for(int i = 0; i < packageList.size(); i++){
packageObj = packageList.get(i);
if (packageObj.getTrackingNumber().equals(trackingNum)){
valid = true;
packageList.remove(i);
}
else if (valid == false && i == packageList.size() - 1){
out.println("Package not found.");
}
}
}
if (valid == true){
Completed completedObj = new Completed();
completedObj.setCustomerID(customerID);
completedObj.setEmployeeID(employeeID);
completedObj.setTrackingNumber(trackingNum);
out.print("What was the shipping cost? $");
float cost = cin.nextFloat();
completedObj.setCost(cost);
completedObj.setShippingDate();
completedObj.setDeliveryDate();
completedList.add(completedObj);
}
}*/
/**
* <h2> Show Completed Transactions </h2>
*
* This method displays all of the completed transactions recorded
* in the database.
*/
// Option 9
/*
public static void showCompleted(){
Completed completedObj;
SimpleDateFormat dateFormat = new SimpleDateFormat ("MM/dd/yyyy");
out.println("------------------------------------------------------------------");
out.println("| Completed Transactions |");
out.println("------------------------------------------------------------------");
out.println("| Cust # | Tracking # | Ship Date | Del Date | Cost | Emp # |");
out.println("------------------------------------------------------------------");
for (int i = 0; i < completedList.size(); i++){
completedObj = completedList.get(i);
out.format("|%8d|", completedObj.getCustomerID());
out.format("%12s| ", completedObj.getTrackingNumber());
Date shipDate = completedObj.getShippingDate();
out.print(dateFormat.format(shipDate));
out.print(" | ");
Date delDate = completedObj.getDeliveryDate();
out.print(dateFormat.format(delDate));
out.print(" | ");
out.format("$%2.2f", completedObj.getCost());
out.format("|%7d|", completedObj.getEmployeeID());
out.println();
out.println("------------------------------------------------------------------");
}
out.println();
}*/
/**
* <h2> Store the Databases </h2>
*
* This method saves the databases to ser files before the program closes.
*/
//Option 10
public static void saveDatabase() {
sortPackageList(); // Sort List before Saving
try {
FileOutputStream fout = new FileOutputStream("packages.ser");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(packageList);
fout.close();
FileOutputStream fout2 = new FileOutputStream("users.ser");
ObjectOutputStream oos2 = new ObjectOutputStream(fout2);
oos2.writeObject(userList);
fout2.close();
FileOutputStream fout3 = new FileOutputStream("completed.ser");
ObjectOutputStream oos3 = new ObjectOutputStream(fout3);
oos3.writeObject(completedList);
fout3.close();
Logger.getLogger(GUI.class.getName()).info("Saving...");
}
catch (IOException e){
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, e);
}
}
/**
* Sorts Packages in the ArrayList
*/
public static void sortPackageList(){
Package sortObj, sortObj2, temp;
int compare;
for (int i = 0; i < packageList.size(); i++){
for (int j = 0; j < packageList.size() - 1; j++){
sortObj = packageList.get(j);
sortObj2 = packageList.get(j+1);
compare = sortObj.getTrackingNumber().compareTo(sortObj2.getTrackingNumber());
if (compare > 0){
temp = packageList.get(j);
packageList.set(j, sortObj2);
packageList.set(j+1, temp);
}
}
}
}
}