Destro77
Destro77

Reputation: 125

Adding objects to ArrayList from another class

I'm very new to programming and currently trying to write a car showroom application. I've got a vehicle class, showroom class and I'm trying to use a showroom driver for the input.

I'm having problems adding vehicle objects to my arraylist. Could someone point me in the correct direction.

My code:

public class Vehicle {

    private String manufacturer;
    private String model;
    private String custName;
    private String vin;
    private String dateMan;
    private String dateSold;
    private Boolean sold;
    private char tax;
    private double cost;

    public Vehicle(String a, String b, String c, String d) {
        manufacturer = a;
        model = b;
        vin = c;
        dateMan = d;
    }

    public String toString() {
        String s = "Manufacturer: " + manufacturer + "  Model: "
                + model + "  vin: " + vin + "Date Manufactured:" + dateMan
                + "Cost: " + cost;
        return s;
    }

    public void buyVehicle(String a, String b) {  //buy method for the vehicle
        a = dateSold;
        b = custName;
        sold = true;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public String getModel() {
        return model;
    }

    public String getCustName() {
        return custName;
    }

    public String getVin() {
        return vin;
    }

    public String getDateMan() {
        return dateMan;
    }

    public String getDateSold() {
        return dateSold;
    }

    public Boolean getSold() {
        return sold;
    }

    public char getTax() {
        return tax;
    }

    public double getCost() {
        return cost;
    }
}

.

import java.util.ArrayList;

public class Showroom {

    private ArrayList<Showroom> theVehicles;

    public Showroom() {
        theVehicles = new ArrayList<Showroom>();
    }

    public boolean addVehicle(Showroom newVehicle) {
        theVehicles.add(newVehicle);
        return true;
    }
}

.

import java.util.*;

public class ShowroomDriver {

    public static void main(String[] args) {
        Vehicle v1 = new Vehicle("renault", "clio", "12", "290890");
        Showroom.addVehicle(v1);
    }
}

Basically, I'm confused to how I add vehicle objects to the arraylist within the showroom class. If anyone could point me in the right direction I would really appreciate it.

Thanks in advance.

Upvotes: 4

Views: 36345

Answers (3)

Aubin
Aubin

Reputation: 14883

You have to instantiate the class Showroom to use its properties and methods

The collection theVehicles is of Vehicle not Showroom.

package cars;

import java.util.ArrayList;
import java.util.List;

public class Showroom {

   private final List<Vehicle> theVehicles = new ArrayList<>();

   public boolean addVehicle( Vehicle newVehicle ) {
      theVehicles.add( newVehicle );
      return true;
   }

   public static void main( String[] args ) {
      final Showroom showroom = new Showroom();
      final Vehicle v1 = new Vehicle( "renault", "clio", "12", "290890" );
      showroom.addVehicle( v1 );
   }
}

in Vehicle class, a mistake around '=' operator, I suppose you want to memorize the sold value and the customer name:

public void buyVehicle( String a, String b ) { // buy method for the vehicle
   dateSold = a;
   custName = b;
   sold = true;
}

Upvotes: 3

slessans
slessans

Reputation: 687

Your problem is that you declared an ArrayList of ShowRoom objects, but what you want is an ArrayList of Vehicle objects.

private ArrayList<Vehicle> theVehicles;

public boolean addVehicle(Vehicle v) {
    theVehicles.add(v);
    return true;
}

Upvotes: 0

Paul Samsotha
Paul Samsotha

Reputation: 209132

I Think this

private ArrayList <Showroom> theVehicles;

Shoulde be this

private ArrayList <Vehicle> theVehicles;
theVehicles = new ArrayList <Vehicle> ();

And this

public boolean addVehicle( Showroom newVehicle )

Should be

public boolean addVehicle( Vehicle newVehicle )

Don't you want an ArrayList of Vehicles and not ShowRooms?

Upvotes: 0

Related Questions