0

So I'm working on a very basic code which implements Comparable comparing a painting based on year, artist and title.

However my code isn't comparing the paintings by title, just year and artist.

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

}
Ori Lentz
  • 3,668
  • 6
  • 22
  • 28
  • Possible duplicate of [Using comparator to make custom sort](http://stackoverflow.com/questions/5245093/using-comparator-to-make-custom-sort) – Bajirao Shinde Apr 27 '16 at 05:22

2 Answers2

1

Dang. Slow by just a few seconds. Haha. I came up with the same solution as 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);
}

One difference. I would consider changing your class header. Specifically, I would change:

public class Painting implements Comparable

to:

public class Painting implements Comparable<Painting>

This way, instead of using the "raw" Object type, you're Painting class's compareTo() method signature will become:

public int compareTo(Painting o)

In other words, you don't have to cast or worry about checking if an argument is an instance of Painting!

kylemart
  • 1,156
  • 1
  • 13
  • 25
0

Your if-else logic is flawed in several ways. It should more look like this:

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

On a side note, you should generics and avoid casting:

public class Painting implements Comparable<Painting> {
    @Override
    public int compareTo(Painting other) {
        // no casting necessary
    }
}
shmosel
  • 49,289
  • 6
  • 73
  • 138