/**
 * TestArrayList.java -- This is an application/main class that illustrates
 * how to use an ArrayList.
 * It shows a way to access elements of an ArrayList object through try-catch
 * and type-casting.  Hint: Think about why we need to have try-catch.
 *
 * JDEP 183H Fall 2006
 * @author Leen-Kiat Soh
 * @version 1.0
 */

import java.util.*;

class TestArrayList  {

   public static void main(String[] args)  {

      ArrayList council;
      Jedi jedi;
      Sith sith;
      Wookie wookie;

      // creating the council variable
      council = new ArrayList();

      // creating 5 objects and adding them to council
      jedi= new Jedi(10,"Anakin");
      council.add(jedi);

      jedi = new Jedi(5,"Luke");
      council.add(jedi);

      sith = new Sith(6,"Maul");
      council.add(sith);

      sith = new Sith(10,"Palpatin");
      council.add(sith);

      wookie = new Wookie(0,"Chewbacca");
      council.add(wookie);

      System.out.println("The total number of elements in the arraylist is " + 
         council.size());

      // how to use type cast to access the elements in the arraylist
      for (int i = 0; i < council.size(); i++)  {
	 try {
            jedi = (Jedi) council.get(i);
            jedi.printInfo();
	 }  catch (Exception e)  {
	    try {
   	       sith = (Sith) council.get(i);
	       sith.printInfo();
	    }  catch (Exception d)  {
	       wookie = (Wookie) council.get(i);
	       wookie.printInfo();
	       }
            }
      }

      council.remove(0);

      System.out.println("The total number of elements in the arraylist is " + 
         council.size());

   }  // end main

}



