Thursday, July 29, 2010

package mysorting;
public class Person implements Comparable
{
    // fields
    private String name; // the person's name
    private int maximumBooks;
  
    // constructor
    public Person(String n)
    {
        name = n;
        maximumBooks = 3;
    }
   
    // getters and setters
    public String getName()
    {
        return name;
    }
  
    public void setName(String n)
    {
        name = n;
    }

    public int getMaximumBooks() {
        return maximumBooks;
    }

    public void setMaximumBooks(int maxBooks) {
        maximumBooks = maxBooks;
    }
   
    public String toString()
    {
        return getName() + " (" + getMaximumBooks() + " books)";
    }

    @Override
    public int compareTo(Object arg0) {
        Person p1 = (Person)arg0;
        if (name.compareTo(p1.getName()) == 0 )
            return 0;
        else if (name.compareTo(p1.getName()) < 0)
            return -1;
        else if (name.compareTo(p1.getName()) > 0)
            return +1;
        return 0;
    }
}
  

No comments:

Post a Comment