JAVA Create an automobile class that will be used by a dealership as a vehicle i
ID: 3875013 • Letter: J
Question
JAVA
Create an automobile class that will be used by a dealership as a vehicle inventory program. The following attributes should be present in your automobile class:
private string make
private string model
private string color
private int year
private int mileage
Your program should have appropriate methods such as:
constructor
add a new vehicle
remove a vehicle
update vehicle attributes
At the end of your program, it should allow the user to output all vehicle inventory to a text file.
Your program should have appropriate methods such as:
constructor
add a new vehicle
remove a vehicle
update vehicle attributes
Explanation / Answer
Note:
Please find the below Automobile class and coresponding sample output.
Please revert back in case of any concerns or anything needs to be changed.
Program:
import java.util.ArrayList;
import java.util.Scanner;
public class Automobile {
private String make;
private String model;
private String color;
private int year;
private int mileage;
private ArrayList<Automobile> vehicleList;
public Automobile() {
vehicleList = new ArrayList<Automobile>();
}
public Automobile(String make, String model, String color, int year,int mileage) {
this.make = make;
this.model = model;
this.color = color;
this.year = year;
this.mileage = mileage;
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public String getColor() {
return color;
}
public int getYear() {
return year;
}
public int getMileage() {
return mileage;
}
public void setMake(String make) {
this.make = make;
}
public void setModel(String model) {
this.model = model;
}
public void setColor(String color) {
this.color = color;
}
public void setYear(int year) {
this.year = year;
}
public void setMileage(int mileage) {
this.mileage = mileage;
}
public void addVehicle(Automobile vehicle) {
vehicleList.add(vehicle);
}
public void removeVehicle(String make, String model, String color, int year, int mileage) {
for (int i=0;i<vehicleList.size();i++) {
Automobile vehicle = vehicleList.get(i);
if (vehicle.getMake().equalsIgnoreCase(make) && vehicle.getModel().equalsIgnoreCase(model)
&& vehicle.getColor().equalsIgnoreCase(color) && vehicle.getYear()==year
&& vehicle.getMileage()==mileage) {
vehicleList.remove(vehicle);
}
}
}
public void updateVehicle(String makeOld, String modelOld, String colorOld, int yearOld, int mileageOld,
String makeNew, String modelNew, String colorNew, int yearNew, int mileageNew) {
for (int i=0;i<vehicleList.size();i++) {
Automobile vehicle = vehicleList.get(i);
if (vehicle.getMake().equalsIgnoreCase(makeOld) && vehicle.getModel().equalsIgnoreCase(modelOld)
&& vehicle.getColor().equalsIgnoreCase(colorOld) && vehicle.getYear()==yearOld
&& vehicle.getMileage()==mileageOld) {
vehicle.setMake(makeNew);
vehicle.setModel(modelNew);
vehicle.setColor(colorNew);
vehicle.setYear(yearNew);
vehicle.setMileage(mileageNew);
}
}
}
public void Display() {
BufferedWriter bw = null;
try {
// creating File Writer to write text file
FileWriter fw = new FileWriter("automobile_output.txt");
// creating Buffered Writer to write text file
bw = new BufferedWriter(fw);
System.out.println(" Vehicles Information: ");
for (int i=0;i<vehicleList.size();i++) {
Automobile vehicle = vehicleList.get(i);
System.out.println("Vehicle "+(i+1)+":");
bw.write("Vehicle "+(i+1)+":");
bw.newLine();
System.out.println("Make: " + vehicle.getMake());
bw.write("Make: " + vehicle.getMake());
bw.newLine();
System.out.println("Model: " + vehicle.getModel());
bw.write("Model: " + vehicle.getModel());
bw.newLine();
System.out.println("Color: " + vehicle.getColor());
bw.write("Color: " + vehicle.getColor());
bw.newLine();
System.out.println("Year: " + vehicle.getYear());
bw.write("Year: " + vehicle.getYear());
bw.newLine();
System.out.println("Mileage: " + vehicle.getMileage());
bw.write("Mileage: " + vehicle.getMileage());
bw.newLine();
bw.newLine();
System.out.println("");
}
bw.flush();// Flushing File Writer
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Automobile automobile = new Automobile();
Scanner userInput = new Scanner(System.in);
while(true) {
System.out.println(" Select Below option:");
System.out.println("Enter 1 for Add Vehicle");
System.out.println("Enter 2 for Update Vehicle");
System.out.println("Enter 3 for Remove Vehicle");
System.out.println("Enter 4 for Display Vehicles");
System.out.println("Enter 5 for Exit");
System.out.print("Chose option: ");
int option = userInput.nextInt();
userInput.nextLine();
if (option == 1) {
System.out.println(" Enter Vehicle Information to Add:");
System.out.print("Enter Maker: ");
String maker = userInput.nextLine();
System.out.print("Enter Model: ");
String model = userInput.nextLine();
System.out.print("Enter Color: ");
String color = userInput.nextLine();
System.out.print("Enter Manufacturing Year: ");
int year = userInput.nextInt();
userInput.nextLine();
System.out.print("Enter Mileage: ");
int mileage = userInput.nextInt();
userInput.nextLine();
Automobile vehicle = new Automobile(maker, model, color, year, mileage);
automobile.addVehicle(vehicle);
} else if (option == 2) {
System.out.println(" Enter Old Vehicle Information to Update:");
System.out.print("Enter Maker: ");
String makerOld = userInput.nextLine();
System.out.print("Enter Model: ");
String modelOld = userInput.nextLine();
System.out.print("Enter Color: ");
String colorOld = userInput.nextLine();
System.out.print("Enter Manufacturing Year: ");
int yearOld = userInput.nextInt();
userInput.nextLine();
System.out.print("Enter Mileage: ");
int mileageOld = userInput.nextInt();
userInput.nextLine();
System.out.println(" Enter New Vehicle Information to Update:");
System.out.print("Enter Maker: ");
String makerNew = userInput.nextLine();
System.out.print("Enter Model: ");
String modelNew = userInput.nextLine();
System.out.print("Enter Color: ");
String colorNew = userInput.nextLine();
System.out.print("Enter Manufacturing Year: ");
int yearNew = userInput.nextInt();
userInput.nextLine();
System.out.print("Enter Mileage: ");
int mileageNew = userInput.nextInt();
userInput.nextLine();
automobile.updateVehicle(makerOld, modelOld, colorOld, yearOld, mileageOld,
makerNew, modelNew, colorNew, yearNew, mileageNew);
} else if (option == 3) {
System.out.println(" Enter Vehicle Information to Remove:");
System.out.print("Enter Maker: ");
String maker = userInput.nextLine();
System.out.print("Enter Model: ");
String model = userInput.nextLine();
System.out.print("Enter Color: ");
String color = userInput.nextLine();
System.out.print("Enter Manufacturing Year: ");
int year = userInput.nextInt();
userInput.nextLine();
System.out.print("Enter Mileage: ");
int mileage = userInput.nextInt();
userInput.nextLine();
automobile.removeVehicle(maker, model, color, year, mileage);
} else if (option == 4) {
automobile.Display();
} else {
System.out.println("Exit!");
break;
}
}
userInput.close();
}
}
Output:
Select Below option:
Enter 1 for Add Vehicle
Enter 2 for Update Vehicle
Enter 3 for Remove Vehicle
Enter 4 for Display Vehicles
Enter 5 for Exit
Chose option: 1
Enter Vehicle Information to Add:
Enter Maker: Hyundai
Enter Model: i20
Enter Color: Black
Enter Manufacturing Year: 2016
Enter Mileage: 14
Select Below option:
Enter 1 for Add Vehicle
Enter 2 for Update Vehicle
Enter 3 for Remove Vehicle
Enter 4 for Display Vehicles
Enter 5 for Exit
Chose option: 1
Enter Vehicle Information to Add:
Enter Maker: Maruti
Enter Model: Swift
Enter Color: Red
Enter Manufacturing Year: 2015
Enter Mileage: 15
Select Below option:
Enter 1 for Add Vehicle
Enter 2 for Update Vehicle
Enter 3 for Remove Vehicle
Enter 4 for Display Vehicles
Enter 5 for Exit
Chose option: 1
Enter Vehicle Information to Add:
Enter Maker: Toyota
Enter Model: Corolla
Enter Color: Green
Enter Manufacturing Year: 2014
Enter Mileage: 12
Select Below option:
Enter 1 for Add Vehicle
Enter 2 for Update Vehicle
Enter 3 for Remove Vehicle
Enter 4 for Display Vehicles
Enter 5 for Exit
Chose option: 4
Vehicles Information:
Vehicle 1:
Make: Hyundai
Model: i20
Color: Black
Year: 2016
Mileage: 14
Vehicle 2:
Make: Maruti
Model: Swift
Color: Red
Year: 2015
Mileage: 15
Vehicle 3:
Make: Toyota
Model: Corolla
Color: Green
Year: 2014
Mileage: 12
Select Below option:
Enter 1 for Add Vehicle
Enter 2 for Update Vehicle
Enter 3 for Remove Vehicle
Enter 4 for Display Vehicles
Enter 5 for Exit
Chose option: 2
Enter Old Vehicle Information to Update:
Enter Maker: Maruti
Enter Model: Swift
Enter Color: Red
Enter Manufacturing Year: 2015
Enter Mileage: 15
Enter New Vehicle Information to Update:
Enter Maker: Maruti
Enter Model: Swift
Enter Color: White
Enter Manufacturing Year: 2016
Enter Mileage: 18
Select Below option:
Enter 1 for Add Vehicle
Enter 2 for Update Vehicle
Enter 3 for Remove Vehicle
Enter 4 for Display Vehicles
Enter 5 for Exit
Chose option: 4
Vehicles Information:
Vehicle 1:
Make: Hyundai
Model: i20
Color: Black
Year: 2016
Mileage: 14
Vehicle 2:
Make: Maruti
Model: Swift
Color: White
Year: 2016
Mileage: 18
Vehicle 3:
Make: Toyota
Model: Corolla
Color: Green
Year: 2014
Mileage: 12
Select Below option:
Enter 1 for Add Vehicle
Enter 2 for Update Vehicle
Enter 3 for Remove Vehicle
Enter 4 for Display Vehicles
Enter 5 for Exit
Chose option: 3
Enter Vehicle Information to Remove:
Enter Maker: Toyota
Enter Model: Corolla
Enter Color: Green
Enter Manufacturing Year: 2014
Enter Mileage: 12
Select Below option:
Enter 1 for Add Vehicle
Enter 2 for Update Vehicle
Enter 3 for Remove Vehicle
Enter 4 for Display Vehicles
Enter 5 for Exit
Chose option: 4
Vehicles Information:
Vehicle 1:
Make: Hyundai
Model: i20
Color: Black
Year: 2016
Mileage: 14
Vehicle 2:
Make: Maruti
Model: Swift
Color: White
Year: 2016
Mileage: 18
Select Below option:
Enter 1 for Add Vehicle
Enter 2 for Update Vehicle
Enter 3 for Remove Vehicle
Enter 4 for Display Vehicles
Enter 5 for Exit
Chose option: 5
Exit!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.