/**
 * Constructs a few Bird objects and demonstrates their methods.
 *
 * @author Zach Tomaszewski
 * @version 22 Oct 2008
 */
public class AviaryRevised {

  public static void main(String[] args) {

    //seeing if limits work properly
    BirdRevised freeRange = new BirdRevised("Chicken", BirdRevised.SMALL,
                                             BirdRevised.MEDIUM, true);
    BirdRevised kfc = new BirdRevised("Chicken", 15, 27, false);
    BirdRevised egg = new BirdRevised("Chicken", BirdRevised.MEDIUM);

    System.out.println("A few birds: ");
    System.out.print("Free-range: ");
    freeRange.print();
    System.out.print("KFC: ");
    kfc.print();
    System.out.print("A brown egg: ");
    egg.print();

    System.out.println("\nAfter a feeding...");
    freeRange.feed();
    kfc.feed();
    //kfc.size++;   //can't do this thanks to encapsulation
    egg.feed();
    System.out.print("Free-range: ");
    freeRange.print();
    System.out.print("KFC: ");
    kfc.print();
    System.out.print("A brown egg: ");
    egg.print();

    System.out.println("\nAfter a hatching...");
    kfc.hatch();
    egg.hatch();
    System.out.print("Free-range: ");
    freeRange.print();
    System.out.print("KFC: ");
    kfc.print();
    System.out.print("A brown egg: ");
    egg.print();

    System.out.println("\nAfter another feeding...");
    freeRange.feed();
    kfc.feed();
    egg.feed();
    System.out.print("Free-range: ");
    freeRange.print();
    System.out.print("KFC: ");
    kfc.print();
    System.out.print("A brown egg: ");
    egg.print();


    //This next part demonstrates why you might need accessor methods
    // -- to see if you need to feed your bird or not.
    System.out.println("\nLife in the factory farm: ");
    java.util.ArrayList<BirdRevised> birds =
      new java.util.ArrayList<BirdRevised>();
    birds.add(new BirdRevised("Runty Chicken", BirdRevised.SMALL));
    birds.add(new BirdRevised("Giant Chicken", BirdRevised.LARGE));
    birds.add(new BirdRevised("Chicken", BirdRevised.MEDIUM));

    int fed = 0;
    boolean stillGrowing = true;
    while (stillGrowing) {

      //a round of bird care for all birds that need it
      for (int i = 0; i < birds.size(); i++) {
        BirdRevised bird = birds.get(i);
        if (!bird.hasHatched()) {
          bird.hatch();
          System.out.print("Hatched ");
        }else if (bird.getSize() < bird.getMaxSize()) {
          bird.feed();
          fed++;
        }
        if (bird.canFly()) {
          bird.clipWings();
        }
        //print effects
        System.out.print(i + ": ");
        bird.print();
      }

      //now see if any still need to grow
      stillGrowing = false;
      for (BirdRevised bird : birds) {  //the object/iterator form of for loop
        if (bird.getSize() < bird.getMaxSize()) {
          //at least one bird that still needs to grow
          stillGrowing = true;
          break;
        }
      }
    }
    System.out.println("All achieved with only " + fed + " feedings!");

  }
}
