2016-04-27 9 views
0

Also arbeite ich an einem sehr einfachen Code, der Comparable ein Gemälde basierend auf Jahr, Künstler und Titel vergleicht.Implements Comparable Missing One Merkmal

Allerdings vergleicht mein Code nicht die Gemälde nach Titel, nur Jahr und Künstler.

public class Main { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // TODO code application logic here 


     Painting p1 = new Painting(Year.NINETYEIGHT, artist.ART1, title.TIT1); 
     Painting p2 = new Painting(Year.NINETYEIGHT, artist.ART1, title.TIT2); 

     System.out.println("p1: " + p1.toString()); 
     System.out.println("p2: " + p2.toString()); 

     if(p1.compareTo(p2)> 0){ 
      System.out.println(p1.toString() + " beats " + p2.toString()); 

     } else if(p1.compareTo(p2) < 0){ 
      System.out.println(p2.toString() + " beats " + p1.toString()); 
     } else{ 
      System.out.println("Same Everything"); 
     } 

    } 

} 

public enum Year { 
    NINETYSEVEN, NINETYEIGHT, NINETYNINE, TWOTHOUSAND 

} 

public enum artist { 
    ART1, ART2, ART3, 

} 
public enum title { 
    TIT1, TIT2,TIT3, 

} 

public class Painting implements Comparable { 

    private title title; 
    private Year year; 
    private artist artist; 

    public Painting(Year y, artist a, title t) { 
     title = t; 
     year = y; 
     artist = a; 

    } 

    @Override 
    public int compareTo(Object o) { 
     //compare values 
     Painting other = (Painting) o; 
     int yearCompare = this.year.compareTo(other.year); 
     int artistCompare = this.artist.compareTo(other.artist); 
     if (yearCompare == 0) { 
      //same year, compare artist 
      return this.artist.compareTo(other.artist); 

     } else if (artistCompare == 0) { 
      return this.title.compareTo(other.title); 

     } else { 

      return yearCompare; 

     } 
    } 

    @Override 
    public String toString() { 
     return title.name() + " by " + artist.name() + " produced " + year.name(); 
    } 

} 
+0

Mögliches Duplikat von [Verwendung von Komparator zur benutzerdefinierten Sortierung] (http://stackoverflow.com/questions/5245093/using-comparator-to-make-custom-sort) –

Antwort

0

Ihre if-else-Logik ist auf verschiedene Arten fehlerhaft. Es sollte mehr wie folgt aussehen:

int yearCompare = this.year.compareTo(other.year); 
if (yearCompare != 0) { 
    return yearCompare; 
} 

int artistCompare = this.artist.compareTo(other.artist); 
if (artistCompare != 0) { 
    return artistCompare; 
} 

return this.title.compareTo(other.title); 

Auf einer Seite zur Kenntnis, sollten Sie Generika und vermeiden Casting:

public class Painting implements Comparable<Painting> { 
    @Override 
    public int compareTo(Painting other) { 
     // no casting necessary 
    } 
} 
1

Dang. Langsam um ein paar Sekunden. Haha. Ich kam mit der gleichen Lösung wie Shmosel.

public int compareTo(Painting other) { 

    int yearCompare = year.compareTo(other.year); 
    if (yearCompare != 0) 
     return yearCompare; 

    int artistCompare = artist.compareTo(other.artist); 
    if (artistCompare != 0) 
     return artistCompare; 

    return title.compareTo(other.title); 
} 

Ein Unterschied. Ich würde in Betracht ziehen, Ihren Klassenheader zu ändern. Insbesondere würde ich ändern:

public class Painting implements Comparable 

zu:

public class Painting implements Comparable<Painting> 

Auf diese Weise statt den "raw" Objekttyp verwenden, Sie Malklasse der Methode compareTo() Unterschrift werden:

Mit anderen Worten, Sie müssen sich nicht darum kümmern oder prüfen, ob ein Argument eine Instanz von Painting ist!