/* OrderedListTester.java
 * 
 * Lukasz Supronik, Nov 14, 2004
 * edited by Betty O'Neil, Apr. 2006
 * 
 */
import java.util.Iterator;
/**
 * @author Lukasz Supronik
 * 
 * This class is a tester for class implementing OrderedList
 *	
 */
public class OrderedListTester<T extends Comparable<? super T>>
{
    // list under test
    private OrderedList<T> list;

    // construct with the list to test
    public OrderedListTester( OrderedList<T> x)
    {
	list = x;
    }

    // caller supplies an array of T objects, in any order, to put 
    // in the list under test
    public void doTest(T[] arr)
    {
	addElements(arr);
        printElements();
        System.out.println("size: " + list.size());
	System.out.println("Checking order");
	checkOrder();
        System.out.println("removing first, last and two middle elements");
        remElements();
	System.out.println("Checking order");
	checkOrder();
        System.out.println("printing what is left");
        printElements();
        System.out.println("checking if object exists");
        checkIfObjectExists();
        System.out.println("getting an index");
        checkObjectsIndex();
        System.out.println("getting the second element");
        getSecondElement();
    }

    public void addElements(T[] elts) 
    {
	for (T x: elts)
	    list.add(x);
    }

    public void checkOrder()
    {
	for (int i=0; i< list.size()-1; i++) {
	    if (list.get(i).compareTo(list.get(i+1)) > 0)
		System.out.println("Elements out of order at position "+i);
	}
    }

    /*
     * removes the first, last and the middle elements
     */
    public void remElements()
    {
        
        list.remove(list.get(0));
        list.remove(list.get(list.size()-1));
        list.remove(list.get(list.size()/2));
        list.remove(list.get(list.size()/2));   
    }
    
    /*
     * prints current elements inside a collection
     */
    public void printElements()
    {
        Iterator<T> itr = list.iterator();
        
        while(itr.hasNext())
            System.out.println(itr.next());
            
    }
    
    /*
     * gets the second element and prints it
     */
    public void getSecondElement()
    {
        System.out.println("Second element is: " + list.get(1));
    }
    
    /*
     * checking if object exists
     */
    public void checkIfObjectExists()
    {
        if(list.contains(list.get(3)) == false)
            System.out.println("x not found");
        else
            System.out.println("object found");
        
    }
    
    /*
     * gets the index of the third element inside the collection
     */
    public void checkObjectsIndex()
    {
        System.out.println(list.indexOf(list.get(3)));
    }
}
