그래서 사용자는 "Select 1"과 같은 코드를 입력 한 다음 "Options"를 입력해야합니다. 옵션은 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
Car 클래스에서{
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;
}
}
당신이 지금 가지고있는 것과 당신이 기대하는 것을 보여주십시오. 귀하의 질문을 이해하기 힘들어 그래서 그것에 더 자세히 추가하십시오 – Coder
'main()'전혀 이해가되지 않습니다. 'selectedCar' 란 무엇입니까? 'ShowOptions'는 이전에'option'의 마지막 값에 대해서만 문자열을 반환합니다 - 당신이 이전에 배열에 넣었던 문자열 전부는 아닙니다. – John3136