/*
 * Improve compareTo to be robust implementation of Comparable,
 * by checking the dynamic type of the object.
 * 
 * Lecture 23, Thu Mar 30 2006
 */
public class NamedSortableBunny implements Comparable
{
    private int x;
    private int y;
    private int numCarrots;
    private String name;
 
    /**
     * Compare a Bunny based on its name. Robust version checks the dynamic type
     * of the object, and only does casts when safe.
     * 
     * @param other Object to compare
     * @return Negative if other object less than this, 0 if they're the same
     *         (or other object is not a Bunny), and positive if the object is
     *         greater.
     */ 
    public int compareTo(Object other) 
    {
        if (other instanceof NamedSortableBunny) { 
            return this.name.compareTo( ( (NamedSortableBunny) other ).getName() );
        } else {
            return 0;
        }
    }
   
    /** Return the Bunny's name */
    public String getName() {
        return name;
    }
    
    /** Return the state of the Bunny as one long string */
    public String toString() {
        return "x position "+x+" y position "+y+" numCarrots "
        +numCarrots+" name "+name;
    }
    
    public NamedSortableBunny(int x, int y, int numCarrots, String name) 
    {
        this.x = x;
        this.y = y;
        this.numCarrots = numCarrots;
        this.name = name;
    }


    public void hop(int direction) {
        if (numCarrots > 0)    {
            System.out.println("hop");
            if (direction == 12)
                y++;
            else if (direction == 3) 
        x++;
        else if (direction == 6) 
        y--;
        else 
        x--;
            numCarrots--;
        } else {
            System.out.println("This bunny can't hop");
        }
    }
    
    public void displayInfo()
    {
        System.out.println("This bunny is at position "+x+","+y);
        System.out.println("This bunny has "+numCarrots+" carrots remaining");
    }
 
}

