Linked Questions

Popular Questions

Adding vehicles to array list with constructor

Asked by At

I have created my class called AutoMobile(); I now need to create methods for an Auto Inventory Program to add a vehicle, print the list of vehicles, remove the vehicles, and update the attributes.

What I am confused on, is how do I call the constructor to create a new car while accepting user input for the constructor object and the fields in the constructor?

This is my class:

public class AutoMobile {

//initialize private variables
private String make;
private String model;
private String color;
private int year;
private int mileage;

public AutoMobile() {
    this("Default make", "Default model", "Default color", 2019, 0);
    System.out.println("Empty constructor called");
}

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;
}

I was able to make it so I can have the list in the main. However, I am having a tough time with how to print out the contents of the list.

public class Main {


public static AutoMobile addAuto(List<AutoMobile> autoInventory) {
    Scanner addCar = new Scanner(System.in);

    System.out.println("Enter Vehicle Make: ");
    String make = addCar.nextLine();

    System.out.println("Enter Vehicle Model: ");
    String model = addCar.nextLine();

    System.out.println("Enter Vehicle Color: ");
    String color = addCar.nextLine();

    System.out.println("Enter Vehicle Year: ");
    int year = addCar.nextInt();

    System.out.println("Enter Vehicle Mileage: ");
    int mileage = addCar.nextInt();

    AutoMobile car =  new AutoMobile(make, model, color, year, mileage);

    autoInventory.add(car);

    addCar.close();

  return car;


}

public static void removeAuto() {
    //todo will be used to remove auto from invetory list

}

public static void printVehicles(List<AutoMobile> autoInventory) {
    //todo allows user to print inventory lsit

}

public static void updateAttributes() {
    //todo allows user to update attribute of specific vehicle

}


public static void main(String[] args) {
    List<AutoMobile> autoInventory = new ArrayList<AutoMobile>();

   AutoMobile newCar = (addAuto(autoInventory));

    for (AutoMobile val: autoInventory) {
        System.out.println(val);

    }



}

}

It's not printing the actual list:

enter image description here

Related Questions