/*
 * Program to demonstrate using ArrayList to maintain
 * a list of Person objects.
 * File: ArrayListOfPersons.java
 * Aaron Tan
 */

import java.util.*;

class ArrayListOfPersons {

   public static void main(String[] args) {

      List<Person> friends = new ArrayList<Person>();
      Person person;

      // Add four friends
      person = new Person("Jane", 10, 'F');
      friends.add(person);

      person = new Person("Jack", 16, 'M');
      friends.add(person);

      person = new Person("Jill", 8, 'F');
      friends.add(person);

      person = new Person("John", 12, 'M');
      friends.add(person);

      System.out.println("There are " + friends.size() + 
                         (friends.size()<2 ? " friend" : " friends")
                         + " in the list.");
      displayList(friends);
      
      System.out.println();
      System.out.println("To add James in position 2 into list.");
      person = new Person("James", 14, 'M');
      friends.add(2, person);
      displayList(friends);

      System.out.println();
      System.out.println("To remove position 3 from list.");
      friends.remove(3);
      displayList(friends);

      System.out.println();
      System.out.println("To remove position 1 with Jessica.");
      person = new Person("Jessica", 10, 'F');
      friends.set(1, person);
      displayList(friends);
      
   }

   // Display all elements in the list.
   public static void displayList(List<Person> aList) {
      for (Person p: aList) {
         System.out.println(p.getName());
      }
   }

   // Display all elements in the list.
   // This version uses the iterator method.
   public static void displayListV2(List<Person> aList) {
      Person p;
      Iterator<Person> itr = aList.iterator();

      while (itr.hasNext()) {
         p = itr.next();
         System.out.println(p.getName());
      }

   }


}


