
/**
 * BunnyHerd keeps track of many Bunny (actually, NamedBunny) objects
 * using an array.
 * 
 * Lecture 19, Thu Mar 16 2006
 *
 */
public class BunnyHerd
{
    private int currentHerdPosition;
    /** the number of bunnies that have been created so far. It's not
     * the case that all array positions smaller than this number are
     * valid, since deleting bunnies will leave a null entry. */

    private String herdName;
    private int maxBunnies;
    /** the maximum possible number of bunnies, as opposed to the
     * number of bunnies that have been created so far. */

    //private int x;
    //private int y;
    //private int numCarrots;
    //private String bunnyName;
    /* No! Don't want information for a single bunny. Some people had
     * fields like this for BunnyHerd, but these are the fields for an
     * individual Bunny. We should store this sort of information in
     * the individual Bunny objects that we're keeping in the array. 
     * The idea here is to reuse the Bunny object we created before */
    
    NamedBunny[] bunnies;
    /* Yes! Create an array of bunnies. Store information about each
     * bunny in its own object */
    
    public BunnyHerd(int maxBunnies, String herdName)
    {
        this.maxBunnies = maxBunnies;
        this.herdName = herdName;
        bunnies = new NamedBunny[maxBunnies];
        // Note that the constructor is the right place to initialize
        // the bunnies array.

        currentHerdPosition = 0;
    }
    
    /** Add a bunny to the herd */
    public void addBunny(int xPos, int yPos, int numCarrots, String name)
    {
        
        // We need to check if there's room before adding the bunny!
        if (currentHerdPosition < maxBunnies) {
            bunnies[currentHerdPosition] = new NamedBunny(xPos, yPos, numCarrots, name);
            currentHerdPosition++;
        }
    }
    
    /** Delete a bunny given its name. The deletion will leave an
     * array element set to null 
     */
    public void deleteBunny(String name)
    {
        //bunnies = new Bunny[bunnies.length -1];
        /* No! If we had a line like this here, we would lose
         * information about all previously created bunnies by
         * overwriting old array. */


        // for (int i = 0; i < maxBunnies; i++)
        /* No! We don't need to loop through whole array, since we
         * keep track of how many have been created so far */

        //for (int i = 0; i <= currentNumBunnies-1; i++)
	/* Loop guard above is correct, but the one below is somewhat clearer */

        for (int i = 0; i < currentHerdPosition; i++)
        {
            //if ( bunnies[i].getName().equals(name)) ) {
            /* Either the line above or the line below is legal, the
             * order doesn't matter for which String object makes the
             * equal method call. */
            if ( name.equals(bunnies[i].getName()) ) {
		bunnies[i] = null;
            }
        }
    }
    
    /** Print out the entire herd, using the toString method */
    public void printHerd()
    {
        for (int i = 0; i < currentHerdPosition; i++)
        {
            if (bunnies[i] != null)
                System.out.println(bunnies[i].toString());
        }
    }

    /** Driver class to test BunnyHerd */
    public static void main( String[] args )
    {
        BunnyHerd myHerd = new BunnyHerd(5, "angelbunnies");
        myHerd.addBunny(1,2,5,"Jerry");
        myHerd.printHerd();
        myHerd.addBunny(2,3,10,"Tamara");
        myHerd.addBunny(2,4,1,"Steve");
        myHerd.printHerd();
        BunnyHerd davidHerd = new BunnyHerd(2,"davidsHerd");
        davidHerd.addBunny(3,4,0,"David");
        davidHerd.addBunny(30,40,600,"Kermit");
        davidHerd.deleteBunny("David");
        davidHerd.printHerd();
        myHerd.printHerd();
        myHerd.deleteBunny("Tamara");
        myHerd.printHerd();
    }
    
}

