显示arraylist只显示空值

时间:2016-04-27 17:25:27

标签: java arraylist

我在这里有一个小型汽车经销商计划,一切似乎都正常工作(菜单,添加汽车和自行车到arraylist),除非选择显示已添加的汽车或自行车。我在子类中运行的显示方法但它不显示我输入的任何值。它只显示如下:Model = null,price = 0.0,fueltype = null等

我相信这里有人能够在几秒钟内解决它! 如果需要更多代码,请问,我不确定错误在哪里,所以我只有超级车辆和驾驶员输入和显示方法

由于

    public class Vehicles
    {
private final String make = "BMW";
private String model;
private double price;
private String colour;
private int stock;
private double fuelMpg;
private float displacement;
private int topSpeed;

public Vehicles()
{
    model = "";
    price = 0.0;
    colour = "";
    stock = 0;
    fuelMpg = 0;
    displacement = 0;
    topSpeed = 0;
}

public Vehicles(String model, double price, String colour, int stock, double fuelMpg, float displacement, int topSpeed)



    model = model;
    price = price;
    colour = colour;
    stock = stock;
    fuelMpg = fuelMpg;
    displacement = displacement;
    topSpeed = topSpeed;
}

public void display()
{
    System.out.println("Make: " + make);
    System.out.println("Model: " + model);
    System.out.println("Price: " + price);
    System.out.println("Colour: " + colour);
    System.out.println("fuelMpg: " + fuelMpg);
    System.out.println("displacement: " + displacement);
    System.out.println("topSpeed: " + topSpeed);
}



public void inputCarDetails()
{
    Scanner scan = new Scanner(System.in);
    String model, colour, fuelType, frame;
    int doors, stock, topSpeed, stroke, noSeats, noVehicles, noCar;
    float displacement;
    double price, fuelMpg;
    boolean sunroof;
    Vehicles car;
    System.out.println("Enter the amount of cars you want to add to the brochure");
    noCar = scan.nextInt(); scan.nextLine();
    for (int i = 0; i < noCar; i++) {
        System.out.println("----Entering car details----");
        System.out.println("\nEnter model");
        model = scan.nextLine();
        System.out.println("Enter Price");
        price = scan.nextDouble(); scan.nextLine();
        System.out.println("Enter colour");
        colour = scan.nextLine();
        System.out.println("Enter no. in stock");
        stock = scan.nextInt(); scan.nextLine();
        System.out.println("Enter MPG");
        fuelMpg = scan.nextDouble(); scan.nextLine();
        System.out.println("Enter displacement");
        displacement = scan.nextInt(); scan.nextLine();
        System.out.println("Enter top speed");
        topSpeed = scan.nextInt(); scan.nextLine();
        System.out.println("Enter no. of doors");
        doors = scan.nextInt(); scan.nextLine();
        System.out.println("Enter fuel type");
        fuelType = scan.nextLine(); 
        System.out.println("Enter sunroof (true/false)");
        sunroof = scan.nextBoolean(); scan.nextLine();

        car = new Cars(model, price, colour, stock, fuelMpg, displacement, topSpeed, doors, fuelType, sunroof);
        list.add(car);
    }
}

public void inputBikeDetails()
{
    Scanner scan = new Scanner(System.in);
    String model, colour, fuelType, frame;
    int doors, stock, displacement, topSpeed, stroke, noSeats, noVehicles, noBike;
    double price, fuelMpg;
    boolean sunroof;
    Vehicles bike;

    System.out.println("Enter the amount of bikes you want to add to the brochure");
    noBike = scan.nextInt(); scan.nextLine();
    for (int i = 0; i < noBike; i++) {
        System.out.println("----Entering bike details----");
        System.out.println("\nEnter model");
        model = scan.nextLine();
        System.out.println("Enter Price");
        price = scan.nextDouble(); scan.nextLine();
        System.out.println("Enter colour");
        colour = scan.nextLine();
        System.out.println("Enter no. in stock");
        stock = scan.nextInt(); scan.nextLine();
        System.out.println("Enter MPG");
        fuelMpg = scan.nextDouble(); scan.nextLine();
        System.out.println("Enter displacement");
        displacement = scan.nextInt(); scan.nextLine();
        System.out.println("Enter top speed");
        topSpeed = scan.nextInt(); scan.nextLine();
        System.out.println("Enter engine stroke");
        stroke = scan.nextInt(); scan.nextLine();
        System.out.println("Enter no. of seats");
        noSeats = scan.nextInt(); scan.nextLine();
        System.out.print("Enter the frame type");
        frame = scan.nextLine();

        bike = new Bikes(model, price, colour, stock, fuelMpg, displacement, topSpeed, stroke, noSeats, frame);
        list.add(bike);

    }
}

public void displayCars()
{
    if (list.isEmpty()) {
        System.out.println("Unfortunatly, we have no cars on sale at the moment");
    }
    else {
        System.out.println("\n****Car Brochure****");
        for (Vehicles v : list)
            if (v instanceof Cars) {
                v.display();
            }
    }
}

public void displayBikes()
{
    if (list.isEmpty()) {
        System.out.println("Unfortunatly, we have no motorbikes on sale at the moment");
    }
    else {
        System.out.println("\n****Motorbike Brochure****");
        for (Vehicles v : list)
            if (v instanceof Bikes) {
                v.display();
            }
    }
} 

public static void main (String[] args) // main method
{
    BMWdriver driver = new BMWdriver();
    driver.Driver();
    driver.startMenu();
    driver.inputCarDetails();
    driver.inputBikeDetails();
}



    public class Cars extends Vehicles
    {
private int doors;
private String fuelType;
private final String layout = "RWD";
private boolean sunroof;

public Cars()
{
    super();
    doors = 0;
    fuelType = "";
    sunroof = false;
}

public Cars(String model, double price, String colour, int stock, double fuelMpg, float displacement, int topSpeed, int doors, String fuelType, boolean sunroof)
{
    super(model, price, colour, stock, fuelMpg, displacement, topSpeed);
    doors = doors;
    fuelType = fuelType;
    sunroof = sunroof;
}

public void display()
{
    super.display();
    System.out.println("No. of doors: " + doors);
    System.out.println("Fuel: " + fuelType);
    System.out.println("Wheel layout: " + layout);
    if (sunroof = false) {
        System.out.println("This car has no sunroof");
    }
    else {
        System.out.println("This car has a sunroof");
    }
}

2 个答案:

答案 0 :(得分:0)

ArrayList <Vehicles> list = new ArrayList<Vehicles>(); 

你在代码中遗漏了这个。你已经在sevral行上提到了一个列表,但是在给定的代码中没有arraylist。在你的类中声明它并使其成为实例变量

通过查看给定的详细信息很难给出正确的答案,但如果您的类图与此非常类似,那么您的代码正在运行

class Vehicle{
}
class Bike extends Vehicle{
}
class Car extends Vehicle{
}

并在构造函数中,您必须分配值

this.model = model;
this.price = price;
this.colour = colour;
this.stock = stock;
this.fuelMpg = fuelMpg;
this.displacement = displacement;
this.topSpeed = topSpeed;

答案 1 :(得分:0)

public Vehicles(String model, double price, String colour, int stock, double fuelMpg, float displacement, int topSpeed)



    model = model;
    price = price;
    colour = colour;
    stock = stock;
    fuelMpg = fuelMpg;
    displacement = displacement;
    topSpeed = topSpeed;
}

这些作业什么都不做 - 你需要:

this.model = model;