我不断收到我的数组的最后一个输入

时间:2017-02-26 05:23:06

标签: java arrays list

因此用户必须输入“Select 1”之类的代码,然后键入“Options”选项应该在最顶部给出ArrayList的输出,而不是它给出最底层。我也需要使用4个班级。先感谢您 !如果需要,我还可以提供更多细节! :

编辑:当用户输入“选择1”时,它应该向该人提供他选择的汽车。当他输入“选项”之后,它应该为该特定车辆提供3个选项。除非在这种情况下,它只给我数组中选项的最后一个输入。

*FOR REC1 IN C1
      LOOP*
    VAR_X := VAR_X+REC1.B
    *--This where i need the value of sum(rec1.b) to calculate VAR_PCT:=(REC1.B/SUM(REC1.B))*100
       DBMS_OUTPUT.PUT_LINE(REC1.A ||'|'|| REC1.B ||'|'||VAR_PCT)
    END LOOP;*

Dealership.Class

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;
}

Car.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";

}
}

Option.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(); 
    }
}

1 个答案:

答案 0 :(得分:0)

在Car类中,你应该返回options.toString(),你的经销商类应该返回一个String数组。