4

我对编程非常陌生,目前正在尝试编写汽车陈列室应用程序。我有一个车辆类,陈列室类,我正在尝试使用陈列室驱动程序进行输入。

我在将车辆对象添加到我的数组列表时遇到问题。有人可以指出我正确的方向。

我的代码:

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

基本上,我对如何将车辆对象添加到陈列室类中的数组列表感到困惑。如果有人能指出我正确的方向,我将不胜感激。

提前致谢。

4

3 回答 3

3

您必须实例化类 Showroom 才能使用其属性和方法

集合 theVehicles 是 Vehicle 而不是 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 );
   }
}

在 Vehicle 类中,'=' 运算符周围的错误,我想你想记住销售价值和客户姓名:

public void buyVehicle( String a, String b ) { // buy method for the vehicle
   dateSold = a;
   custName = b;
   sold = true;
}
于 2013-11-02T14:56:07.600 回答
0

我认为这

private ArrayList <Showroom> theVehicles;

应该是这个

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

还有这个

public boolean addVehicle( Showroom newVehicle )

应该

public boolean addVehicle( Vehicle newVehicle )

你不想要一个ArrayListof Vehicles 而不是ShowRooms 吗?

于 2013-11-02T14:51:26.477 回答
0

您的问题是您声明了 ShowRoom 对象的 ArrayList,但您想要的是 Vehicle 对象的 ArrayList。

private ArrayList<Vehicle> theVehicles;

public boolean addVehicle(Vehicle v) {
    theVehicles.add(v);
    return true;
}
于 2013-11-02T14:55:26.750 回答