比较使用多个班级的两辆车

时间:2017-02-26 00:40:43

标签: java comparison

嗨,我有一个问题,我需要帮助!我的问题是如何使用"比较"两个使用多个类的汽车的情况(例如,UserInput:比较1 2 - > Dealership.Class - > Car.class - > Engine.class - >返回结果)并让它​​返回我这两款车的区别,如MPG,HorsePower,fuelType等等!任何解释都会很棒,因为在我决定寻求帮助之前,我已经多次尝试并且未能做到这一点。提前谢谢,如果代码很长,对不起! 编辑:我希望我的结果是这样的," Car 1比车2有更多的里程,Car 2比car 1更新,而Car 1和Car 2都使用gas。"

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("Compare [n1] [n2]: Compares two cars");
            break;

            case "Compare":
            int index1, index2;
            index1 = Integer.parseInt(parts[1]);
            index2 = Integer.parseInt(parts[2]);
            System.out.println("Comparing " + index1 + " & " + index2);
            break;
        }
    }
}

public class Dealership 
{
private Car[] cars;
private Car selectedCar;

    public Dealership()
    { 
        cars = new Car[3];

        Engine engine = new Engine(FuelType.Gas, 4, 67001, 162, 24);
        Interior interior = new Interior("Brown", "Maroon", false, false); 
        Trunk trunk = new Trunk(true, false, true, true, "White");
        Car car = new Car("Hyundai", 2006, "Sonata", 2500, "White", CarType.Sedan, engine, interior, trunk);

        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 Compare(int index1, int index2) // Left this blank because I just realized I did something wrong here.
    {  
    }
}

public class Car 
{
    private String make;
    private int year;
    private String model;
    private int price;
    private String color;
    private CarType type; 
    private Engine engine;
    private Interior interior;
    private Trunk trunk;
    private Option[] options;
        public Car (String make, int year, String model, int price, String color, CarType type)
        {
            this.make = make;                                           
            this.year = year;  
            this.model = model;
            this.price = price;
            this.color = color;
            this.type = type;
        }
        public Car (String make, int year, String model, int price, String color, CarType type, 
            Engine engine, Interior interior, Trunk trunk)
        {
            this(make, year, model, price, color, type);
            this.engine = engine;
            this.interior = interior;
            this.trunk = trunk;
        }
        String CompareTo (Car otherCar) //Possibly wrong
        {
            if (this.year > otherCar.year) return "Newer";
            if (this.year < otherCar.year) return "Older";
            if (this.price > otherCar.price) return "More expensive";
            if (this.price < otherCar.price) return "Is cheaper"; 
        }
}
enum CarType {Sedan, SUV, CrossOver, Truck}; 

public class Engine
{
    private FuelType fuelType;
    private int noOfCylinders;
    private int capacity; // Gas Tank
    private int horsePower;
    private float mpg;
        public Engine (FuelType fuelType, int noOfCylinders, int capacity, int horsePower, float mpg)
        {
            this.fuelType = fuelType;
            this.noOfCylinders = noOfCylinders;
            this.capacity = capacity;
            this.horsePower = horsePower;
            this.mpg = mpg;
        }
//List of Methods Int/Float that I need to add to help me with comparison
        public int getHorsePower()
        {
            if (this.horsePower > anotherCar.horsePower) //Does not work, No Symbol for anotherCar
            return "Has more horsepower"; // Where I'm currently stuck on
        }
        public float getMpg()
        {
            return;
        }
        public FuelType getFuelType()
        {
            return;
        }
        public int getCapacity()
        {
            return;
        }
        public int getNoOfCylinders()
        {
            return;
        }
}
enum FuelType {Gas, Electric, Hybrid, Diesel} 

另外,我没有添加其他2个类,如Trunk,Interior,因为它似乎没什么必要。

1 个答案:

答案 0 :(得分:0)

这绝不是最好的答案,但这只是我在为您的问题发布的评论中提到的建议。

String compareTo (Car otherCar, String property) //Possibly wrong
{
    String message;
    if(property == "MPG") {
      if(this.mpg > otherCar.mpg) return "some message";
      if(this.mpg < otherCar.mpg) return "some message";
    else if(property == "year") {
      if (this.year > otherCar.year) return "Newer";
      if (this.year < otherCar.year) return "Older";
    }
    else if(property == "price") {
      if (this.price > otherCar.price) return "More expensive";
      if (this.price < otherCar.price) return "Is cheaper"; 
    }
  }

}

另一方面,你可以这样做。

String compareByPrice(Car otherCar) {

}

String compareByYear(Car otherCar) {

}

// ... and so forth

但这里的问题是“你真的需要方法中的完整汽车对象吗?”不,你可能只需要汽车本身的领域。