@SuppressWarnings("unchecked")

class MyBooks implements Comparable{

    static final CompareLastName c1 = new CompareLastName();
    static final CompareFirstName c2 = new CompareFirstName();
    static final CompareTitle   c3 = new CompareTitle();
    static final ComparePrice c4 = new ComparePrice();
    static final CompareISBN  c5 = new CompareISBN();
    static final CompareDate c6 = new CompareDate();

    private String lastname, firstname, title, price, isbn, date;

    public MyBooks(String ln, String fn, String te, String pe, String in, String de) {

        lastname = ln;
        firstname = fn;
        title = te;
        price = pe;
        isbn = in;
        date = de;

    }// end MyBooks constructor


    public int compareTo (MyBooks b1, MyBooks b2) {  

        return 1; 
    } // end method compareTo required by Comparable interface

    public String toString () {
       return "\t" + lastname + "\t" + firstname + "\t" + title + "\t" + price + "\t" + isbn + "\t" + date;
    } // end method toString

    static class CompareLastName implements Comparator <MyBooks>{
           public int compare (MyBooks b1, MyBooks b2) {
                if((b1.lastname).compareTo(b2.lastname) > 0)
                    return 1;
                else
                    return 0;
          } // end compare
        } // end CompareLastName[ICODE][/ICODE] class

    static class CompareFirstName implements Comparator <MyBooks>{
       public int compare (MyBooks b1, MyBooks b2) {
            if((b1.firstname).compareTo(b2.firstname) > 0)
                return 1;
            else
                return 0;
      } // end compare
    } // end CompareFirstName class

    static class CompareTitle implements Comparator <MyBooks>{
        public int compare (MyBooks b1, MyBooks b2) {
            if(b1.title.compareTo(b2.title) > 0)
                return 1;
            else
                return 0;
      } // end compare
    } // end CompareTitle class

    static class ComparePrice implements Comparator <MyBooks>{
       public int compare (MyBooks b1, MyBooks b2) {
            if(b1.price.compareTo(b2.price) > 0)
                return 1;
            else
                return 0;
      } // end compare
    } // end ComparePrice class

    static class CompareISBN implements Comparator <MyBooks>{
       public int compare (MyBooks b1, MyBooks b2) {
            if(b1.isbn.compareTo(b2.isbn) > 0)
                return 1;
            else
                return 0;
      } // end compare
    } // end CompareISBN class

    static class CompareDate implements Comparator <MyBooks>{
       public int compare (MyBooks b1, MyBooks b2) {
            if(b1.isbn.compareTo(b2.isbn) > 0)
                return 1;
            else
                return 0;
      } // end compare
   } // end CompareDate class

}//end MyBooks class

Recommended Answers

All 3 Replies

Need Help with Comparable/Comparator.

Good for you. I, however, have no idea what that needed help might be, since you didn't say/ask.

Oh sorry, just have a problem with the following line and can't seem to figure out:

"class MyBooks implements Comparable{"

What problem? What is the complete compiler message?

P.S. That should be using Generics.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.