4. return " USED Vehicle:" +
super.toString() +
"Years Old:t" + (this.currentYear - this.modelYear) +
" Previous State:t" + this.previousLicenseState + " ";
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
publicclass Assignment5 {
static Scanner scanner = new Scanner(System.in);
/**
* Clear keyboard buffer
*/
publicstaticvoid clearBuf(){
scanner.nextLine();
}
/**
* Prints the menu
*/
publicstaticvoid displayMenu(){
System.out.println("ChoicettAction ");
System.out.println("------tt------ ");
System.out.println("AttAdd Vehicle ");
System.out.println("CttCompute License Fee ");
System.out.println("DttCount Certain Vehicles ");
System.out.println("LttList Vehicles ");
System.out.println("QttQuit ");
System.out.println("?ttDisplay Help ");
System.out.println("What action would you like to perform? ");
}
publicstaticvoid main(String args[]) {
List vehicleList= new ArrayList();
char choice = ' ';
while(true) {
displayMenu();
5. choice = Character.toUpperCase(scanner.next().charAt(0));
clearBuf();
switch (choice) {
case 'A': //Add Vehicle
System.out.println("Please enter some vehicle information to add: ");
Vehicle vehicle = VehicleParser.parseStringToVehicle(scanner.nextLine());
if(vehicle != null)
vehicleList.add(vehicle);
break;
case 'C': //Compute License Fee
for (Vehicle veh : vehicleList) {
veh.computeLicenseFee();
}
System.out.println("License fee computed ");
break;
case 'D': //Count Certain Vehicles
System.out.println("Please enter a year to compare: ");
int year = scanner.nextInt();
clearBuf();
int vehCount = 0;
for (Vehicle veh : vehicleList) {
if(veh.modelYear > year)
vehCount += 1;
}
System.out.println("The number of vehicles that are newer than " + year + " is: " + vehCount
+ " ");
break;
case 'L': //List Vehicles
if(vehicleList.size() > 0) {
for (Vehicle veh : vehicleList) {
System.out.println(veh);
}
} else
System.out.println("No vehicle ");
break;
case 'Q': //Quit
6. System.exit(0);
break;
case '?': //Display Help
break;
default:
System.out.println("Unknown action ");
}
}
}
}
SAMPLE OUTPUT:
Choice Action
------ ------
A Add Vehicle
C Compute License Fee
D Count Certain Vehicles
L List Vehicles
Q Quit
? Display Help
What action would you like to perform?
e
Unknown action
Choice Action
------ ------
A Add Vehicle
C Compute License Fee
D Count Certain Vehicles
L List Vehicles
Q Quit
? Display Help
What action would you like to perform?
l
No vehicle
Choice Action
7. ------ ------
A Add Vehicle
C Compute License Fee
D Count Certain Vehicles
L List Vehicles
Q Quit
? Display Help
What action would you like to perform?
a
Please enter some vehicle information to add:
NewVehicle/GM/2016/gas/25000.50
Choice Action
------ ------
A Add Vehicle
C Compute License Fee
D Count Certain Vehicles
L List Vehicles
Q Quit
? Display Help
What action would you like to perform?
c
License fee computed
Choice Action
------ ------
A Add Vehicle
C Compute License Fee
D Count Certain Vehicles
L List Vehicles
Q Quit
? Display Help
What action would you like to perform?
l
NEW Vehicle:
8. Make: GM
Model Year: 2016
Motive Power: gas
License Fee: $3920.075
Price: $25000.5
Choice Action
------ ------
A Add Vehicle
C Compute License Fee
D Count Certain Vehicles
L List Vehicles
Q Quit
? Display Help
What action would you like to perform?
q
Solution
publicclass VehicleParser {
publicstatic Vehicle parseStringToVehicle(String lineToParse) {
String[] vehicleInfo = lineToParse.split("/");
if(vehicleInfo[0].equalsIgnoreCase("NewVehicle")) {
//type/make/modelYear/motivePower/vehiclePrice
returnnew NewVehicle(vehicleInfo[1],
Integer.parseInt(vehicleInfo[2]),
vehicleInfo[3],
Double.parseDouble(vehicleInfo[4]));
} elseif(vehicleInfo[0].equalsIgnoreCase("UsedVehicle")) {
//type/make/modelYear/motivePower/previousState/currentYear
returnnew UsedVehicle(vehicleInfo[1],
Integer.parseInt(vehicleInfo[2]),
vehicleInfo[3],
vehicleInfo[4],
Integer.parseInt(vehicleInfo[5]));
12. /**
* Prints the menu
*/
publicstaticvoid displayMenu(){
System.out.println("ChoicettAction ");
System.out.println("------tt------ ");
System.out.println("AttAdd Vehicle ");
System.out.println("CttCompute License Fee ");
System.out.println("DttCount Certain Vehicles ");
System.out.println("LttList Vehicles ");
System.out.println("QttQuit ");
System.out.println("?ttDisplay Help ");
System.out.println("What action would you like to perform? ");
}
publicstaticvoid main(String args[]) {
List vehicleList= new ArrayList();
char choice = ' ';
while(true) {
displayMenu();
choice = Character.toUpperCase(scanner.next().charAt(0));
clearBuf();
switch (choice) {
case 'A': //Add Vehicle
System.out.println("Please enter some vehicle information to add: ");
Vehicle vehicle = VehicleParser.parseStringToVehicle(scanner.nextLine());
if(vehicle != null)
vehicleList.add(vehicle);
break;
case 'C': //Compute License Fee
for (Vehicle veh : vehicleList) {
veh.computeLicenseFee();
}
System.out.println("License fee computed ");
break;
case 'D': //Count Certain Vehicles
System.out.println("Please enter a year to compare: ");
13. int year = scanner.nextInt();
clearBuf();
int vehCount = 0;
for (Vehicle veh : vehicleList) {
if(veh.modelYear > year)
vehCount += 1;
}
System.out.println("The number of vehicles that are newer than " + year + " is: " + vehCount
+ " ");
break;
case 'L': //List Vehicles
if(vehicleList.size() > 0) {
for (Vehicle veh : vehicleList) {
System.out.println(veh);
}
} else
System.out.println("No vehicle ");
break;
case 'Q': //Quit
System.exit(0);
break;
case '?': //Display Help
break;
default:
System.out.println("Unknown action ");
}
}
}
}
SAMPLE OUTPUT:
Choice Action
------ ------
A Add Vehicle
C Compute License Fee
D Count Certain Vehicles
L List Vehicles
14. Q Quit
? Display Help
What action would you like to perform?
e
Unknown action
Choice Action
------ ------
A Add Vehicle
C Compute License Fee
D Count Certain Vehicles
L List Vehicles
Q Quit
? Display Help
What action would you like to perform?
l
No vehicle
Choice Action
------ ------
A Add Vehicle
C Compute License Fee
D Count Certain Vehicles
L List Vehicles
Q Quit
? Display Help
What action would you like to perform?
a
Please enter some vehicle information to add:
NewVehicle/GM/2016/gas/25000.50
Choice Action
------ ------
A Add Vehicle
C Compute License Fee
D Count Certain Vehicles
15. L List Vehicles
Q Quit
? Display Help
What action would you like to perform?
c
License fee computed
Choice Action
------ ------
A Add Vehicle
C Compute License Fee
D Count Certain Vehicles
L List Vehicles
Q Quit
? Display Help
What action would you like to perform?
l
NEW Vehicle:
Make: GM
Model Year: 2016
Motive Power: gas
License Fee: $3920.075
Price: $25000.5
Choice Action
------ ------
A Add Vehicle
C Compute License Fee
D Count Certain Vehicles
L List Vehicles
Q Quit
? Display Help
What action would you like to perform?
q