/
CarDealershipSimulator.java
596 lines (589 loc) · 19.5 KB
/
CarDealershipSimulator.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
/*
* Name: Jacky Ly
* Student ID: 500890960
* Section: CPS209-031
*/
/*
* Import statements
*/
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class CarDealershipSimulator
{
/**
* Displays the header given the string and format
*
* @param header
*/
private static void addHeader(String header) {
System.out.println();
System.out.println(header);
for (int i = 0; i < header.length() + 13; i++) {
System.out.print("-");
}
System.out.println();
}
/*
* Displays the commands list
*/
private static void commandsMenu() {
System.out.println();
System.out.println("COMMANDS MENU");
System.out.printf("%7s | %-40s %-50s %-40s %s\n", "General", "ADD - Add Cars To Inventory", "L - Load Inventory", "Q - Quit Program", "HELP - Display Commands Menu");
System.out.printf("%7s | %-40s %s\n", "Actions", "BUY [VIN] - Buy Car By VIN #", "RET - Return Last Transaction", "RET [ID] - Return Transaction with 'ID'");
System.out.printf("%7s | %-40s %-50s %s\n", "Sort", "SPR - Sort By Price", "SSR - Sort By Safety Rating", "SMR - Sort By Max Range");
System.out.printf("%7s | %-40s %-50s %-40s %s\n", "Filter", "FPR [min] [max] - Filter By Price", "FEL - Filter By Electric", "FAW - Filter By AWD", "FCL - Clear Filters");
System.out.printf("%7s | %-40s %-50s %s\n %-40s %s\n", "Sales", "SALES - All Transactions (2019)", "SALES [m] - All Transactions By Month (0-11)", "SALES TEAM - List of Sales Team Names", "SALES TOPSP - Top Salesperson(s)", "SALES STATS - Sales Statistics");
}
/**
* Main method of the class
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
/*
* Initialize class objects and variables
*/
CarDealership newDealer = new CarDealership();
ArrayList<Car> cars = new ArrayList<Car>();
SalesTeam salesTeam = new SalesTeam();
final String carHeader = String.format("%-3s %-5s %-11s %-7s %-8s %-5s %-8s %-10s %-5s %-3s %-7s", "##", "VIN", "Brand", "Color", "Model", "MaxR", "SafetyR", "Price ($)", "AWD", "RT", "BattType");
final String transHeader = String.format("%-8s %-5s %-7s %-13s %-13s %s", "TransID", "Type", "VIN #", "Salesperson", "Price ($)", "Date");
final String salesHeader = String.format("%-15s %15s", "Salesperson", "# of Cars Sold");
final String[] monthNames = newDealer.getAccSystem().monthNames;
String line = "", command = "";
/*
* Reads data from file and continues if the data is valid
*/
if(addFromFile(cars)) {
Scanner input = new Scanner(System.in);
System.out.println("\nWelcome to Car Dealership Simulator 2!");
commandsMenu();
System.out.println("\nRun the 'ADD' command to get started!");
System.out.print("Enter a command (HELP for commands menu): ");
/*
* When there is another command entered, process the command
*/
while (input.hasNextLine()) {
try {
line = input.nextLine();
Scanner commandLine = new Scanner(line);
command = commandLine.next();
/*
* If the input is equal to one of the commands, call the appropriate method(s)
*/
switch (command) {
/**
* Adds cars to the dealership inventory
*/
case "ADD":
if (!commandLine.hasNext()) {
if (newDealer.isEmpty) {
newDealer.addCars(cars);
System.out.println("\nAdded cars to dealership inventory.");
break;
}
commandLine.close();
throw new IllegalArgumentException("\nERROR: Cars already added to the inventory!");
}
/*
* Any command with other arguments will throw an exception
*/
commandLine.close();
throw new NoSuchElementException();
/*
* Displays the car inventory in the console
*/
case "L":
if (!commandLine.hasNext()) {
if (!newDealer.isEmpty) {
System.out.println("\nDEALERSHIP INVENTORY:");
addHeader(carHeader);
newDealer.displayInventory();
System.out.println("----------------------------------------------------------------------------------------------");
System.out.println("\nInventory loaded successfully.");
break;
}
commandLine.close();
throw new IllegalArgumentException("\nERROR: Car inventory is empty!");
}
/*
* Any command with other arguments will throw an exception
*/
commandLine.close();
throw new NoSuchElementException();
/*
* Buys a car given the VIN number
*/
case "BUY":
if (commandLine.hasNextInt()) {
int VIN = commandLine.nextInt();
if (!commandLine.hasNext()) {
String currentCar = newDealer.buyCar(VIN);
System.out.println("\nTRANSACTION INFO:");
addHeader(transHeader);
System.out.println(currentCar);
System.out.println("\nCar with VIN #" + VIN + " bought successfully.");
break;
}
/*
* Any command with other arguments will throw an exception
*/
commandLine.close();
throw new NoSuchElementException();
}
commandLine.close();
throw new IllegalArgumentException("\nERROR: Invalid VIN # or not specified!");
case "RET":
/*
* Returns the last 'BUY' transaction
*/
if (!commandLine.hasNext()) {
int id = newDealer.lastTransID;
newDealer.returnCar(id);
break;
}
/*
* Returns a specific transaction given the ID
*/
else if (commandLine.hasNextInt()) {
int id = commandLine.nextInt();
if (!commandLine.hasNext()) {
newDealer.returnCar(id);
break;
}
/*
* Any command with other arguments will throw an exception
*/
commandLine.close();
throw new NoSuchElementException();
}
/*
* Commands for sales and transactions
*/
case "SALES":
/*
* Prints all transactions for the year (2019)
*/
if (!commandLine.hasNext()) {
if (newDealer.getAccSystem().getTransMap().size() > 0) {
System.out.println("\nALL TRANSACTIONS (2019):");
addHeader(transHeader);
}
newDealer.getAccSystem().getAllTrans();
break;
}
/*
* Prints all transactions for the given month
*/
else if (commandLine.hasNextInt()) {
int month = commandLine.nextInt();
if (!commandLine.hasNext()) {
if (newDealer.getAccSystem().getTransMap().size() > 0) {
if (month >= 0 && month < 12) {
System.out.println("\nTRANSACTIONS for " + monthNames[month] + ":");
addHeader(transHeader);
}
else {
commandLine.close();
throw new IllegalArgumentException("\nERROR: Invalid month selection! Please enter a number from 0-11!");
}
}
newDealer.getAccSystem().getMonthTrans(month);
break;
}
/*
* Any command with other arguments will throw an exception
*/
commandLine.close();
throw new NoSuchElementException();
}
else {
String arg = commandLine.next();
/*
* Prints all the salesperson names
*/
if (arg.equals("TEAM")) {
if (!commandLine.hasNext()) {
System.out.println("\nSALES TEAM:");
System.out.println("--------------------------------------------------------------");
System.out.println(salesTeam.getAllSP());
break;
}
/*
* Any command with other arguments will throw an exception
*/
commandLine.close();
throw new NoSuchElementException();
}
/*
* Prints the sales person who sold the most cars of the year
*/
else if (arg.equals("TOPSP")) {
if (!commandLine.hasNext()) {
if (newDealer.getAccSystem().getTransMap().size() > 0) {
System.out.println("\nTOP SALES PERSON(S):");
addHeader(salesHeader);
}
System.out.println(newDealer.getAccSystem().getTopSP());
break;
}
/*
* Any command with other arguments will throw an exception
*/
commandLine.close();
throw new NoSuchElementException();
}
/*
* Prints the stats of the sales team
*/
else if (arg.equals("STATS")) {
if (!commandLine.hasNext()) {
System.out.println("\nSALES STATISTICS:");
System.out.println("--------------------------------------------------------------");
System.out.printf("Total Sales: $%.2f\n", newDealer.getAccSystem().getTotalSales());
System.out.printf("Average Sales Per Month: $%.2f\n", newDealer.getAccSystem().getAvgSales());
System.out.printf("Total Cars Sold: %d\n", newDealer.getAccSystem().getTotalSold());
System.out.printf("Total Car Returns: %d\n", newDealer.getAccSystem().getTotalReturns());
System.out.printf("Highest Sales Month(s):\n%s", newDealer.getAccSystem().getHighestMonth());
break;
}
/*
* Any command with other arguments will throw an exception
*/
commandLine.close();
throw new NoSuchElementException();
}
/*
* Any command with other arguments will throw an exception
*/
commandLine.close();
throw new NoSuchElementException();
}
/*
* Sorts the list by price (from least to most expensive)
*/
case "SPR":
if (!commandLine.hasNext()) {
if (!newDealer.isEmpty) {
newDealer.sortByPrice();
System.out.println("\nInventory sorted by price.");
break;
}
commandLine.close();
throw new IllegalArgumentException("\nERROR: Car inventory is empty!");
}
/*
* Any command with other arguments will throw an exception
*/
commandLine.close();
throw new NoSuchElementException();
/*
* Sorts the list by safety rating (lowest to highest)
*/
case "SSR":
if (!commandLine.hasNext()) {
if (!newDealer.isEmpty) {
newDealer.sortBySafetyRating();
System.out.println("\nInventory sorted by safety rating.");
break;
}
commandLine.close();
throw new IllegalArgumentException("\nERROR: Car inventory is empty!");
}
/*
* Any command with other arguments will throw an exception
*/
commandLine.close();
throw new NoSuchElementException();
/*
* Sorts the list by max range (from min to max)
*/
case "SMR":
if (!commandLine.hasNext()) {
if (!newDealer.isEmpty) {
newDealer.sortByMaxRange();
System.out.println("\nInventory sorted by max range.");
break;
}
commandLine.close();
throw new IllegalArgumentException("\nERROR: Car inventory is empty!");
}
/*
* Any command with other arguments will throw an exception
*/
commandLine.close();
throw new NoSuchElementException();
/*
* Filters the list by price given a minimum and maximum price
*/
case "FPR":
if (commandLine.hasNextInt()) {
int minPrice = commandLine.nextInt();
if (commandLine.hasNextInt()) {
int maxPrice = commandLine.nextInt();
if (!commandLine.hasNext()) {
if (!newDealer.isEmpty) {
newDealer.filterByPrice(minPrice, maxPrice);
System.out.println("\nInventory filtered by price between $" + minPrice + " and $" + maxPrice + ".");
break;
}
commandLine.close();
throw new IllegalArgumentException("\nERROR: Car inventory is empty!");
}
/*
* Any command with other arguments will throw an exception
*/
commandLine.close();
throw new NoSuchElementException();
}
/*
* If second argument is invalid or missing
*/
commandLine.close();
throw new IllegalArgumentException("\nERROR: Invalid max price or not specified!");
}
/*
* If first argument is invalid or missing
*/
commandLine.close();
throw new IllegalArgumentException("\nERROR: Invalid price range or not specified!");
/*
* Filters the list by electric cars (only shows cars that are electric)
*/
case "FEL":
if (!commandLine.hasNext()) {
if (!newDealer.isEmpty) {
newDealer.filterByElectric();
System.out.println("\nInventory filtered by electric cars.");
break;
}
commandLine.close();
throw new IllegalArgumentException("\nERROR: Car inventory is empty!");
}
/*
* Any command with other arguments will throw an exception
*/
commandLine.close();
throw new NoSuchElementException();
/*
* Filters the list by AWD (only shows cars that are AWD)
*/
case "FAW":
if (!commandLine.hasNext()) {
if (!newDealer.isEmpty) {
newDealer.filterByAWD();
System.out.println("\nInventory filtered by AWD cars.");
break;
}
commandLine.close();
throw new IllegalArgumentException("\nERROR: Car inventory is empty!");
}
/*
* Any command with other arguments will throw an exception
*/
commandLine.close();
throw new NoSuchElementException();
/*
* Clears the filters currently set (all filters are reset to false)
*/
case "FCL":
if (!commandLine.hasNext()) {
if (!newDealer.isEmpty) {
newDealer.FiltersClear();
System.out.println("\nFilters cleared successfully.");
break;
}
commandLine.close();
throw new IllegalArgumentException("\nERROR: Car inventory is empty!");
}
/*
* Any command with other arguments will throw an exception
*/
commandLine.close();
throw new NoSuchElementException();
/*
* Displays the commands list in the console
*/
case "HELP":
if (!commandLine.hasNext()) {
commandsMenu();
break;
}
/*
* Any command with other arguments will throw an exception
*/
commandLine.close();
throw new NoSuchElementException();
/*
* Terminates the program
*/
case "Q":
if (!commandLine.hasNext()) {
commandLine.close();
System.out.println("\nThank you for using Car Dealership Simulator! Have a great day!");
return;
}
/*
* Any command with other arguments will throw an exception
*/
commandLine.close();
throw new NoSuchElementException();
/*
* Any other unknown commands will throw an exception
*/
default:
commandLine.close();
throw new NoSuchElementException();
}
commandLine.close();
line = "";
}
/*
* Catches any command that is not recognized
*/
catch (NoSuchElementException NSE) {
System.out.println("\nERROR: '" + line + "' is an unrecognized command! Please check the commands list and try again!");
line = "";
}
/*
* Catches the exception where an index is being accessed that is greater or less than the size of the object
*/
catch (IndexOutOfBoundsException IOB) {
System.out.println(IOB.getMessage());
}
/*
* Catches wrong data type for any arguments
*/
catch (IllegalArgumentException IAE) {
System.out.println(IAE.getMessage());
}
/**
* Catches any other exception
*/
catch (Exception ex) {
System.out.println("\nERROR: " + ex + " has occurred! Please try again!");
}
System.out.print("\nEnter another command (HELP for commands menu): ");
}
input.close();
}
}
/**
* Reads from a text file and converts the data to Car objects, then add them to the array list
*
* @param cars ArrayList of Car objects
* @throws FileNotFoundException
*/
private static boolean addFromFile(ArrayList<Car> cars) throws FileNotFoundException {
try {
String filename = "cars.txt";
Scanner scan = new Scanner(new File(filename));
ArrayList<Object> specsList = new ArrayList<Object>();
/*
* Initialize variables and constants
*/
final String BATTERY_TYPE = "Lithium";
final int NUM_WHEELS = 4;
int modelInt, powerInt, rechargeTimeFile = 0;
boolean aWDValue;
/*
* While the file still has data to be imported
*/
while (scan.hasNextLine()) {
String line = scan.nextLine();
Scanner specsLine = new Scanner(line);
while (specsLine.hasNext()) {
String specs = specsLine.next();
specsList.add(specs);
}
specsLine.close();
/*
* Extracts the data from the array list and prepares to construct a Car object
*/
String brandFile = (String)specsList.get(0);
String colorFile = (String)specsList.get(1);
String modelFile = (String)specsList.get(2);
switch (modelFile) {
case "SEDAN":
modelInt = Car.SEDAN;
break;
case "SUV":
modelInt = Car.SUV;
break;
case "SPORTS":
modelInt = Car.SPORTS;
break;
case "MINIVAN":
modelInt = Car.MINIVAN;
break;
default:
throw new IllegalArgumentException("\nERROR: The file contains an invalid model!");
}
String powerFile = (String)specsList.get(3);
switch (powerFile) {
case "ELECTRIC_MOTOR":
powerInt = Car.ELECTRIC_MOTOR;
break;
case "GAS_ENGINE":
powerInt = Car.GAS_ENGINE;
break;
default:
throw new IllegalArgumentException("\nERROR: The file contains an invalid power type!");
}
double safetyRatingFile = Double.parseDouble((String)specsList.get(4));
int maxRangeFile = Integer.parseInt((String)specsList.get(5));
String aWDFile = (String)specsList.get(6);
switch(aWDFile) {
case "AWD":
aWDValue = true;
break;
case "2WD":
aWDValue = false;
break;
default:
throw new IllegalArgumentException("\nERROR: The file contains an invalid AWD value!");
}
int priceFile = Integer.parseInt((String)specsList.get(7));
if (specsList.size() == 9) {
rechargeTimeFile = Integer.parseInt((String)specsList.get(8));
/*
* Constructs an electric car object with imported data
*/
ElectricCar car = new ElectricCar(brandFile, colorFile, powerInt, NUM_WHEELS, modelInt, maxRangeFile, safetyRatingFile, priceFile, aWDValue, rechargeTimeFile, BATTERY_TYPE);
cars.add(car);
}
else {
/*
* Constructs a car object with imported data
*/
Car car = new Car(brandFile, colorFile, powerInt, NUM_WHEELS, modelInt, maxRangeFile, safetyRatingFile, priceFile, aWDValue);
cars.add(car);
}
specsList.clear();
}
scan.close();
return true;
}
/*
* Catches invalid data type or values
*/
catch (IllegalArgumentException IAE) {
System.out.println("\n" + IAE);
return false;
}
/*
* Catches invalid file name
*/
catch (FileNotFoundException e) {
System.out.println("\nERROR: " + e + "\nPlease check the file location and try again!");
return false;
}
}
}