2016-04-27 19 views
0

小さな車のディーラープログラムがありますが、追加された車やバイクを表示することを選択するまでは、すべてが正しく動作しているようです(メニュー、arraylistには車とバイクを追加します) 。サブクラスで実行している表示方法は実行されますが、入力した値は表示されません。モデル=ヌル、価格= 0.0、燃料タイプ=ヌルなどarraylistを表示するとヌル値が表示されます

誰かがここにいるのは数秒で解決できると確信しています!より多くのコードが必要な場合 はちょうど私はエラーが私はちょうどスーパークラス車とドライバーの入力や表示方法

おかげ

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"); 
    } 
} 
+0

あなたは私たちが 'Car'クラスを示していただけますか? –

+0

これはいわゆるリストです – 3kings

答えて

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

あなたが不足しているを持っている場所がわからないんだけど、頼みますこれはあなたのコードです。あなたはsevral行のリストを挙げましたが、与えられたコードにはarraylistはありません。あなたのクラスでこれを宣言し、そのインスタンス変数

与えられた詳細を見て、正しい答えを与えるために、その本当にハードに作るが、あなたのクラス図はこれに非常にsimmilarなものであるならば、あなたのコードは

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; 
+0

申し訳ありませんが、私はその問題の根本であるかどうかは分かりませんでしたので、私が提供したコードには含まれていませんでした – Dylan

+0

それはそれです、元気いっぱい!愚かな間違い。 – Dylan

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; 

など

関連する問題