4

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.

1
  • List<Showroom> theVehicles should be read as: "theVehicles is a list of show rooms". This is obviously wrong. theVehicles should be a list of vehicles. Commented Nov 2, 2013 at 14:53

3 Answers 3

3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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?

2 Comments

Yes sorry, I want an ArrayList of all the Vehicles
So then, why do you have ArrayList<Showroom>? Should be ArrayList<Vehicle>, no?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.