2017-02-26 65 views
-1

因此,用戶必須輸入一個代碼,如「選擇1」,然後鍵入「選項」選項應該在最頂部給出ArrayList的輸出,而不是發出了最底部。我也需要使用4個課程。先謝謝你 !如果需要,我還可以提供更多細節! :]我不斷得到我的數組的最後一個輸入

編輯:當人輸入「選擇1」時,應該給該人他選擇了哪輛車。當他在後面輸入「選項」時,應該爲該特定車輛提供3個選項。除了這種情況,它只給我我的數組中的最後一個輸入的選項。

public static void main(String[] args) 
{ 
    Dealership dealership = new Dealership(); 
    System.out.println("To view a list of commands, enter Commands"); 
    Scanner sc = new Scanner(System.in); 
    while(true) 
    { 
     String input = sc.nextLine(); 
     String[] parts = input.split(" "); 
     switch(parts[0]) 
     { 
      case "Commands": 
       System.out.println("Commands: shows a list of all available commands\n" + 
       "Select [n]: selects car No. n and shows the details\n" + 
       "Options: show the details of options installed on the selected car\n" + 
       break; 

      case "Options": 
       System.out.println(dealership.ShowOptions()); 
       break; 
} 

Dealership.Class

{ 
private Car[] cars; 
private Car selectedCar; 
public Dealership() // Resonsibility: Hold the inventory of cars. 
{ 

    cars = new Car[10]; // 3 means there is 3 cars that goes from 0, 1, 2 which also is car 1, 2, 3 

    Engine engine = new Engine(FuelType.Gas, 4, 67001, 162, 24); //FuelType, int noOfCylinders, int capacity, int horsePower, float mpg 
    Interior interior = new Interior("Brown", "Maroon", false, false); //(String color1, String color2, boolean hasSunRoof, boolean hasMoonRoof) 
    Trunk trunk = new Trunk(true, false, true, true, "White"); // (boolean hasSpareTire, boolean hasFirstAidKit, boolean hasCarpet, boolean hasJack, String carpetColor) 
    Car car = new Car("Hyundai", 2006, "Sonata", 2500, "White", CarType.Sedan, engine, interior, trunk); // Parameter in the Public Car.Class section 

    cars[0] = car; // 

    engine = new Engine(FuelType.Gas, 4, 75708, 325, 17); 
    interior = new Interior("Black", "Blue", true, false); 
    trunk = new Trunk(true, false, true, false, "Black"); 
    car = new Car("Infiniti", 2016, "QX50", 38000, "Black", CarType.Sedan, engine, interior, trunk); 

    cars[1] = car; 

    engine = new Engine(FuelType.Gas, 4, 49967, 132, 26); 
    interior = new Interior("White", "Beige", true, true); 
    trunk = new Trunk(true, false, true, true, "Brown"); 
    car = new Car("Toyota", 2010, "Corolla", 7845, "Red", CarType.Sedan, engine, interior, trunk); 

    cars[2] = car; 



    public String ListAllCars() //Responsibility: Returns a description of all cars 
    { 

    String result = ""; 
    for(int i = 0; i < cars.length; i++) 
    { 
     Car c = cars[i]; 
     String s = c.toString(); 
     result = result + (i+1) + "-" + s + "\n"; 
    } 
    return result; 
    } 

public String SelectCar (int index) 
{ 
    selectedCar = cars[index - 1]; 
    return selectedCar.toString(); 
} 

public String ShowOptions() // Responsibility: Returns the options installed on the selected car 
{ 
    if(selectedCar != null) 
     return selectedCar.ShowOptions(); 
    else 
     return "Please select a car first"; 

} 
} 

Car.Class

{ 
private Option[] options; 
public String ShowOptions() 
    { 
    options = new Option[3]; 
    Option option = new Option("Monthly Car Wash", " $500 and you will get a two year free car wash", " TurboCharge", " Pay $350 and increase your horsepower " 
           + " by 15 with a turbo charger", " Type-R Wing"," For $100, you can get a wing that will help your car feel more aerodynamic."); 
    options[0] = option; 

    option = new Option("Wax Job","For $50 more, we will wax your car", "Paint Protection", "We will provide paint protection for $100", "Moon Roof", 
         "We'll add in a Moon Roof for you for $50"); 
    options[1] = option; 

    option = new Option("a First Aid Kit.", " For $20, we will add in a First Aid Kid", " new carpets.", " We will change the color of your carpet for $50", " TurboCharged.", 
         " We will add turbo to your car for $400.");  
    options[2] = option; 
    return option.toString(); 
    } 
} 

Option.Class

{ 
private String name; 
private String name2; 
private String name3; 
private String description; 
private String description2; 
private String description3; 
    public Option (String name, String description, String name2, String description2, String name3, String description3) 
    { 
     this.name = name; 
     this.name2 = name2; 
     this.name3 = name3; 
     this.description = description; 
     this.description2 = description2; 
     this.description3 = description3; 
    } 
    public String toString() //Responsibility: Returns a description of the option(Name, Description) 
    { 
     String options = "The first option for this car has " + name + description + ", the second option for this car has" + name2 + description2 + "," 
     + " and the third option for this car has" + name3 + description3 + "."; 
     return options; 
    } 
} 
+0

顯示你現在有什麼以及你的期望。你的問題很難理解,所以請添加更多細節 – Coder

+0

'main()'根本沒有意義。什麼是'selectedCar'? 'ShowOptions'只返回'option'的最後一個值的字符串 - 不是以前放入數組中的所有字符串。 – John3136

回答

0

在汽車類,你應該返回options.toString( )和你的經銷商班壽我已經返回一個String數組。

+0

對不起,noob問題,但如何返回一個字符串數組?我的大腦有點油炸了。 –

+0

返回類型可能應該是String [] –

相關問題