无法将车辆添加到阵列列表中

时间:2014-01-09 06:18:19

标签: java

我有4个班级..出租车公司,车辆,出租车和班车 我让出租车和班车从车辆类继承..所以现在不是添加穿梭巴士和添加出租车我只是想能够添加车辆..但我得到一个错误说类车辆的构造车辆不能适用于给类型..我不知道什么是错的,因为我所做的只是修改添加出租车方法,工作正常..

出租车公司课程

import java.util.*;
public class TaxiCo
{
    // The name of this company.
    private String companyName;
    // The name of the company's base.
    private final String base;    
    // The fleet of taxis.
    private ArrayList<Vehicle> VehicleFleet;
    // A value for allocating taxi ids.
    private int nextID;
    // A list of available destinations for shuttles.
    private ArrayList<String> destinations;

    /**
     * Constructor for objects of class TaxiCo.
     * @param name The name of this company.
     */
    public TaxiCo(String name)
    {
        companyName = name;
        base = "base";
        VehicleFleet = new ArrayList<Vehicle>();
        nextID = 1;
        destinations = new ArrayList<String>();
        fillDestinations();
    }

    /**
     * Record that we have a new Vehicle.
     * A unique ID will be allocated.
     */
     public void addVehicle()
    {
        Vehicle vehicle = new Vehicle(base, "Vehicle #" + nextID);
        taxiFleet.add(vehicle);
        // Increment the ID for the next one.
        nextID++;
    }

    /**
     * Return the Vehicle with the given id.
     * @param id The id of the Vehicle to be returned.
     * @return The matching Vehicle, or null if no match is found.
     */
    public Vehicle lookup(String id)
    {
        boolean found = false;
        Vehicle Vehicle = null;
        Iterator<Vehicle> it = VehicleFleet.iterator();
        while(!found && it.hasNext()) {
            Vehicle = it.next();
            if(id.equals(Vehicle.getID())) {
                found = true;
            }
        }
        if(found) {
            return Vehicle;
        }
        else {
            return null;
        }
    }

    /**
     * Show the status of the Vehicle fleet. 
     */
    public void showStatus()
    {
        System.out.println("Current status of the " + companyName + " fleet");
        for(Vehicle Vehicle : VehicleFleet) {
            System.out.println(Vehicle.getStatus());
        }
    }

    /**
     * Put all the possible shuttle destinations in a list.
     */
    private void fillDestinations()
    {
        destinations.add("Canterbury West");
        destinations.add("Canterbury East");
        destinations.add("The University");
        destinations.add("Whitstable");
        destinations.add("Herne Bay");
        destinations.add("Sainsbury's");
        destinations.add("Darwin");
        destinations.add("Keynes");
    }
}

1 个答案:

答案 0 :(得分:2)

addVehicle()类的TaxiCo方法中,您将2 String传递给构造函数,而您只用1 String作为参数定义它。

public void addVehicle()
{
    Vehicle vehicle = new Vehicle(base, "Vehicle #" + nextID); // BAD! Calling with 2 Strings
    taxiFleet.add(vehicle);
    // Increment the ID for the next one.
    nextID++;
}

您必须更改它,因此您只需1 String作为参数传递。类似的东西:

Vehicle vehicle = new Vehicle("Just one STRING!");

修改:您也使用方法Taxi

中的addTaxi构造函数执行此操作