/* ListIterGrader.java */

import java.util.*;

/**
 * Runs a number of tests on a MyListIterator, 
 * which extends java.util.ListIterator.
 *
 * @author Zach Tomaszewski
 * @version 01 Feb 2005
 */
public class ListIterGrader {

  /**
   * Tests each MyListIterator method, printing expected 
   * and actual results to the screen.
   */
  public static void main(String[] args) {

    pln("");
    pln("Creating a iterator of an empty list.");
    //next line will work if MyListIterator implements ListIterator
    //and if the MyListIterator constuctor takes a List (as it should)
    ListIterator iter = new MyListIterator(new LinkedList());    
    
    p("hasNext [false]: ");
    pln("" + iter.hasNext());
    p("hasPrevious [false]: ");
    pln("" + iter.hasPrevious());
    p("nextIndex [0]: ");
    pln("" + iter.nextIndex());
    p("nextPrevious [-1]: ");
    pln("" + iter.previousIndex());
    try {
      p("next [NSEException]: ");
      pln(iter.next());
    }catch (NoSuchElementException nsee) {
      pln("NoSuchElementException");
    } 
    try {
      p("previous [NSEException]: ");
      pln(iter.previous());
    }catch (NoSuchElementException nsee) {
      pln("NoSuchElementException");
    } 
    
    pln("");
    pln("Add \"item\" to iterator.");
    iter.add("item");
    p("nextIndex [1]: ");
    pln("" + iter.nextIndex());
    p("hasNext [false]: ");
    pln("" + iter.hasNext());
    p("hasPrevious [true]: ");
    pln("" + iter.hasPrevious());
    p("previous [item]: ");
    pln("" + iter.previous());
    p("hasPrevious [false]: ");
    pln("" + iter.hasPrevious());
    pln("Setting last returned item to \"reset\".");
    iter.set("reset");
    p("next [reset]: ");
    pln(iter.next());
    pln("Removing last returned item. (Leaves an empty list.)");
    iter.remove();
    p("hasNext [false]: ");
    pln("" + iter.hasNext());
    p("hasPrevious [false]: ");
    pln("" + iter.hasPrevious());
    
    pln("");

    p("Creating a new list [two, four, six, eight, last]: ");
    Vector v = new Vector();
    v.add("two");
    v.add("four");
    v.add("six");
    v.add("eight");
    v.add("last");
    pln(v);

    pln("Creating a new MyListIterator with above list.");
    iter = new MyListIterator(v);
    pln("Attempting a series of iterations to test state management.");
    try {
      iter.add("zero");
      iter.add("one");
      try {
        iter.remove();
        pln("OOPS: Removed when you shouldn't have (after an add).");
      }catch (IllegalStateException ise) {
        pln("GOOD: Won't remove immediately after add.");
      }      
      while (iter.nextIndex() <= 3) {
        iter.next();
      }
      iter.set("3"); //changing four to 3...
      iter.set("three");  //... and 3 to three
      iter.next();  //now on six
      iter.remove(); //six gone
      iter.next(); //now on eight
      iter.remove(); //eight gone
      try {
        iter.remove();
        pln("OOPS: Removed when you shouldn't have (after a remove).");
      }catch (IllegalStateException ise) {
        pln("GOOD: Won't remove twice in a row.");
      }      
    }catch (IllegalStateException ise) {
       pln("State management too uptight -- threw ISE when not needed.");
    }
    p("Finished list [zero, one, two, three, last]: ");
    pln(v);
    pln("DONE!");
  }


  /* quick and dirty print helpers */
  private static void pln(Object str) {
    System.out.println(str);
  }

  private static void p(Object str) {
    System.out.print(str);
  }

}//end class

