2016-04-27 13 views
0

Ich habe ein kleines Autohausprogramm hier, alles scheint richtig zu funktionieren (Menüs, Hinzufügen von Autos und Fahrrädern zu arraylist), außer bis es zur Wahl kommt, entweder die Autos oder Fahrräder anzuzeigen, die hinzugefügt haben . Die Anzeige-Methode, die ich in den Unterklassen habe, läuft, aber es zeigt keinen der Werte an, die ich eingegeben habe. Es zeigt nur etwas wie: Modell = Null, Preis = 0.0, Kraftstofftyp = Null usw.Arraylist zeigt nur Nullwerte

Ich bin sicher, dass jemand hier in der Lage sein wird, es in wenigen Sekunden zu lösen! Wenn mehr Code benötigt wird, fragen Sie einfach, ich bin nicht sicher, wo der Fehler ist so, ich habe nur die Oberklasse Fahrzeuge und der Fahrer Eingabe- und Anzeigemethoden

Dank

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

Könnten Sie bitte zeigen Sie uns 'Car' Klasse? –

+0

Wo ist diese so genannte 'liste'? – 3kings

Antwort

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

Sie vermissen das in deinem Code. Sie haben eine Liste mit mehreren Zeilen erwähnt, aber auf dem angegebenen Code gibt es keine Arraylist. erklären dies in Ihrer Klasse und machen es eine Instanzvariable

seine wirklich schwer, indem man die gemachten Angaben eine richtige Antwort zu geben, aber wenn Ihr Klassendiagramm dieses dann wird der Code etwas sehr simmilar zu arbeitet

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

und in Ihrem Konstruktor müssen Sie

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

Entschuldigung, ich habe das an der Spitze davon, ich war nur nicht sicher, ob es die Wurzel des Problems war, also hatte ich es nicht in den Code, den ich zur Verfügung gestellt – Dylan

+0

Das ist es, Prost Jungs! Dummer Fehler. – 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; 
} 

Diese Zuordnungen nichts tun, um die Werte zuweisen - Sie brauchen:

this.model = model; 

usw.