Comment trier la liste avec la méthode compareTo d'ordre spécifique de couleurs en Java

Ronny Giezen:

Je suis en train de faire une compareTométhode que j'utiliser pour trier la liste dans l'ordre des couleurs suivantes: blanc, jaune, orange, rouge, bleu, violet, noir. Le blanc est la première couleur.

J'ai un objet de fruits qui contient différents types de fruits, ces fruits sont spécifiés dans les classes étendues. Tous les fruits ont une couleur (qui est également dans la liste des couleurs).

Je tentais d'écrire une déclaration if-else de sorte que si la couleur du fruit est jaune retour 1 etc., et que pour toutes les couleurs. Mais cela ne semble pas fonctionner.

Quelqu'un peut-il me aider à écrire la méthode compareTo?

Merci d'avance!

Ma classe abstraite de fruits où je mets en œuvre la méthode 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();
    }
}

La classe de couleur:

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




}

La classe où ajouter les différents fruits à une liste et trier la liste:

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

Je tourne Coloren ENUM:

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.

}

Ensuite , vous pouvez comparer les Colors du orderterrain:

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

Je suppose que tu aimes

Origine http://43.154.161.224:23101/article/api/json?id=373011&siteId=1
conseillé
Classement