Javaでの色の特定の順序のcompareToメソッドでリストをソートする方法

ロニーGiezen:

私が作るしようとしているcompareTo、白、黄色、オレンジ、赤、青、紫、黒:私は次の色の順でリストをソートするために使用することの方法を。最初の色はどこ白です。

私は果物の異なる種類を含むオブジェクトフルーツは、これらの果物は、拡張クラスで指定されています。すべての果物は、(また、色のリストである)色を持っています。

私は、もしそうなら-else文を書くためにしようとしていたその果実の色はすべての色の黄色の復帰1などとということであれば。しかし、それは動作していないようでした。

誰の助け私は、compareToメソッドを書くことができますか?

前もって感謝します!

私はcompareToメソッドを実装マイ抽象フルーツクラス:

public abstract class Fruit implements Comparable<Fruit>, Edible {

    String name;
    Color color;
    boolean fluid;


    public Fruit(String name, Color color, boolean fluid) {
        this.name = name;
        this.color = color;
        this.fluid = fluid;
    }

    public abstract boolean isRotten();



    @Override // I don't know how to create this one correctly
    public int compareTo(Fruit fruit) {
        if (this.getColor().getName().equals("white")){
        return 0;
    }
    if (this.getColor().getName().equals("yellow")){
        return 1;
    }
    if (this.getColor().getName().equals("orange")){
        return 2;
    }
    if (this.getColor().getName().equals("red")){
        return 3;
    }
    if (this.getColor().getName().equals("blue")){
        return 4;
    }
    if (this.getColor().getName().equals("purple")){
        return 5;
    }
    if (this.getColor().getName().equals("black")){
        return 6;
    }
    else return -1;

}

    public Color getColor() {
        return color;
    }

    @Override
    public boolean isEdible() {
        return !isRotten();
    }
}

色のクラス:

public class Color {


    private String name;

    public static final String WHITE = "white";
    public static final String YELLOW = "yellow";
    public static final String ORANGE = "orange";
    public static final String RED = "red";
    public static final String BLUE = "blue";
    public static final String PURPLE = "purple";
    public static final String BLACK = "black";


    public String getName() {
        return name;
    }

    public Color(String name) {
        this.name = name;
    }




}

私は、リストに別の果物を追加し、リストをソートクラス:

public class FruitSortingMachine {

    private List<Fruit> fruits = new ArrayList<>();

    /**
     * Instantiates a new Fruit sorting machine.
     */
    public FruitSortingMachine(){}

    /**
     * Sort.
     */
    public void sort() {
        Collections.sort(fruits);
    }

    /**
     * Gets fruits.
     *
     * @return the fruits
     */
    public List<Fruit> getFruits() {

        return this.fruits;
    }

    /**
     * Add fruit boolean.
     *
     * @param fruit the fruit
     * @return the boolean
     */
    public boolean addFruit(Fruit fruit) {
        if (!fruit.isEdible()){
            return false;
        }
        else this.fruits.add(fruit);
        return true;
    }
}
アンドロニカス:

私は回したいColor列挙型に:

public enum Color {

    WHITE("white", 0),
    YELLOW("yellow", 1),
    ORANGE = "orange", 2),
    RED("red", 3),
    BLUE("blue", 4),
    PURPLE("purple", 5),
    BLACK("black", 6);

    private String name;
    private int order;

    // getters, setters etc.

}

次に、あなたが比較できるColorことでSをorderフィールド:

@Override
public int compareTo(Fruit fruit) {
    return Integer.compare(this.getColor().getOrder(), fruit.getColor().getOrder());
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=373009&siteId=1
おすすめ