Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need it in 30 minutes. Am trying to make the below code run in my IDE by calling

ID: 3920739 • Letter: N

Question

Need it in 30 minutes. Am trying to make the below code run in my IDE by calling the main method. Wht should I do? Can you rewrite it please so it can have a

main method and still do what the question asks?

Here is the question

Create a final program that meets the requirements outlined below.

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.

#Here is the code

#automobile class

class automobile:

#constructor

def __init__(self,make="",model="",color="",year=2018,mileage=0):

self.make=make
  
self.model=model
  
self.color=color
  
self.year=year
  
self.mileage=mileage

#setter methods
  
def set_make(self,make):
  
self.make=make
  
def set_model(self,model):
  
self.model=model
  
def set_color(self,color):
  
self.color=color
  
def set_year(self,year):
  
self.year=year
  
def set_mileage(self,mileage):
  
self.mileage=mileage
  
#getter methods
  
def get_make(self):
  
return self.make
  
def get_model(self):
  
return self.model
  
def get_color(self):
  
return self.color
  
def get_year(self):
  
return self.year
  
def get_mileage(self):
  
return self.mileage

#end of automobile class

#method to add a new vehicle to the inventory

def add_vehicle(v_list):

make=input("Enter make: ")
  
model=input("Enter model: ")
  
color=input("Enter color: ")
  
year=int(input("Enter year: "))
  
mileage=int(input("Enter mileage: "))
  
v=automobile(make,model,color,year,mileage)
  
v_list.append(v)
  
print("Added successfully...")
  
#method to remove a vehicle from the inventory

def remove_vehicle(v_list):

index=int(input("Enter the index # of vehicle to be removed: "))
  
if index>=0 and index<len(v_list):
  
make=v_list[index].get_make()
  
model=v_list[index].get_model()
  
v_list.pop(index)
  
print(make,model,"is removed from inventory...")
  
else:
  
print("Invalid index")

#method to update a vehicle info in the inventory

def update_vehicle(v_list):

index=int(input("Enter the index # of vehicle to be updated: "))
  
if index>=0 and index<len(v_list):
  
make=input("Enter new make: ")
  
model=input("Enter new model: ")
  
color=input("Enter new color: ")
  
year=int(input("Enter new year: "))
  
mileage=int(input("Enter new mileage: "))
  
v_list[index].set_make(make)
  
v_list[index].set_model(model)
  
v_list[index].set_color(color)
  
v_list[index].set_year(year)
  
v_list[index].set_mileage(mileage)
  
print("updated successfully...")
  
else:
  
print("Invalid index")

#method to print all vehicle details in proper formatted order

def display_vehicles(v_list):

print('{:10} {:10} {:10} {:10} {:10} {:10}'.format('index#','MAKE','MODEL','COLOR','YEAR','MILEAGE'))
  
for i in range(len(v_list)):
  
v=v_list[i]
  
print('{:10} {:10} {:10} {:10} {:10} {:10}'.format(str(i),v.get_make(),v.get_model(),v.get_color(),str(v.get_year()),str(v.get_mileage())))
  
v_list=[] #initial list
  
#looping infinitely
  
while True:
  
#showing menu
  
print("1. Add vehicle")
  
print("2. Remove vehicle")
  
print("3. Update vehicle")
  
print("4. Display all vehicles")
  
print("5. Quit and print to text file")
  
#getting choice
  
ch=int(input("## Enter your choice"))
  
#performing actions based on choice
  
if ch==1:
  
add_vehicle(v_list)
  
elif ch==2:
  
remove_vehicle(v_list)
  
elif ch==3:
  
update_vehicle(v_list)
  
elif ch==4:
  
display_vehicles(v_list)
  
elif ch==5:
#file = open("inventory.txt", "w")
#for v in automobile.vehicle_list:
#file.write(str(v) + " ")
break;
  
  
else:
  
print("Invalid choice")

Explanation / Answer

Hi,
Please find below the required code:

Code for automobile class:

package main;

public class automobile {

private String make;

private String model;

private String color;

private int year;

private int mileage;

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 void setMake(String make) {

this.make = make;

}

public String getModel() {

return model;

}

public void setModel(String model) {

this.model = model;

}

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

public int getYear() {

return year;

}

public void setYear(int year) {

this.year = year;

}

public int getMileage() {

return mileage;

}

public void setMileage(int mileage) {

this.mileage = mileage;

}

}

code for test class with main method:

package main;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.List;

public class test {

public static void main (String[] args){

String name = null;

List<automobile> inventory = new ArrayList<automobile>();

BufferedReader reader =

new BufferedReader(new InputStreamReader(System.in));

while(true){

// Reading data using readLine

System.out.println("1. Add vehicle");

System.out.println("2. Remove vehicle");

System.out.println("3. Update vehicle");

System.out.println("4. Display all vehicle");

System.out.println("5. Quit and print to text file");

System.out.println("Enter menu option: ");

try {

name = reader.readLine();

} catch (IOException e) {

System.out.println("cause: "+e.getMessage());

}

int input = Integer.parseInt(name);

System.out.println("Input entered: "+input);

switch(input)

{

case 1:

addVehicle(inventory);

break;

case 2:

removeVehicle(inventory);

break;

case 3:

updateVehicle(inventory);

break;

case 4:

displayVehicles(inventory);

break;

case 5:

writeToFile(inventory);

System.exit(0);

break;

default:

System.out.println("invalid input");

}

}

}

public static void addVehicle(List<automobile> inv){

String make, model, color;

int year, mileage;

BufferedReader reader =

new BufferedReader(new InputStreamReader(System.in));

try {

System.out.println("Enter make: ");

make = reader.readLine();

System.out.println("Enter model: ");

model = reader.readLine();

System.out.println("Enter color: ");

color = reader.readLine();

System.out.println("Enter year: ");

year = Integer.parseInt(reader.readLine());

System.out.println("Enter mileage: ");

mileage = Integer.parseInt(reader.readLine());

automobile veh = new automobile(make,model,color,year,mileage);

inv.add(veh);

System.out.println("Added successfully...");

} catch (NumberFormatException e) {

System.out.println("Exception: "+e.getMessage());

} catch (IOException e) {

System.out.println("Exception: "+e.getMessage());

}

}

public static void updateVehicle(List<automobile> inv){

String make, model, color;

int year, mileage,idx;

BufferedReader reader =

new BufferedReader(new InputStreamReader(System.in));

try {

System.out.println("Enter index of vehicle to be updated:");

idx = Integer.parseInt(reader.readLine());

if (idx +1 >inv.size() ){

System.out.println("invalid index");

return;

}

automobile veh = inv.get(idx);

System.out.println("Enter new make: ");

make = reader.readLine();

veh.setMake(make);

System.out.println("Enter new model: ");

model = reader.readLine();

veh.setModel(model);

System.out.println("Enter new color: ");

color = reader.readLine();

veh.setColor(color);

System.out.println("Enter new year: ");

year = Integer.parseInt(reader.readLine());

veh.setYear(year);

System.out.println("Enter new mileage: ");

mileage = Integer.parseInt(reader.readLine());

veh.setMileage(mileage);

System.out.println("Updated successfully...");

} catch (NumberFormatException e) {

System.out.println("Exception: "+e.getMessage());

} catch (IOException e) {

System.out.println("Exception: "+e.getMessage());

}

}

public static void removeVehicle(List<automobile> inv){

int idx;

BufferedReader reader =

new BufferedReader(new InputStreamReader(System.in));

try {

System.out.println("Enter index of vehicle to be removed:");

idx = Integer.parseInt(reader.readLine());

if (idx +1 >inv.size() ){

System.out.println("invalid index");

return;

}

inv.remove(idx);

System.out.println("Vehicle removed successfully...");

} catch (NumberFormatException e) {

System.out.println("Exception: "+e.getMessage());

} catch (IOException e) {

System.out.println("Exception: "+e.getMessage());

}

}

public static void displayVehicles(List<automobile> inv){

System.out.println("Available vehicles are: ");

for (int i=0;i<inv.size();i++){

automobile veh = inv.get(i);

System.out.println(i+": "+veh.getMake()+", "+veh.getModel()+", "+

veh.getMileage()+", "+veh.getColor()+", "+veh.getYear());

}

}

public static void writeToFile(List<automobile> inv){

String filepath = "./inventory.txt";

try {

File file = new File(filepath);

file.createNewFile();

PrintWriter out = new PrintWriter(new FileWriter(filepath));

System.out.println("Writing to file: "+filepath);

for (int i=0;i<inv.size();i++){

automobile veh = inv.get(i);

out.println(i+": "+veh.getMake()+", "+veh.getModel()+", "+

veh.getMileage()+", "+veh.getColor()+", "+veh.getYear());

}

out.close();

} catch (IOException e) {

System.out.println("Exception: "+e.getMessage());

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote