import Array;
import IncomparableException;
import Searcher;
import CompareComparable;
import SimpleOutput;
import java.util.Comparator;

public class TestSearcher {
  /**
   * Display an array.
   */
  public static void showArray(SimpleOutput out, String name, Array stuff) {
    int length = stuff.size();
    for (int i = 0; i < length; i++) {
      out.println(name + "[" + i + "]: " + stuff.get(i));
    }
  } // showArray(stuff)

  /**
   * Try sequential search and return an appropriate string.
   * "found value: XXX" or "not found"
   */
  public static String sequentialSearch(Object value,
                                        Array stuff,
                                        Comparator order) {
    try {
      Object result = Searcher.sequentialSearch(value, stuff, order);
      return ("found value (" + result.toString() + ")");
    }
    catch (NotFoundException e) {
      return "not found";
    }
    catch (IncomparableException e) {
      return "bad comparison";
    }
  } // sequentialSearch(Object, Array, Comparator)
  
  public static void main(String[] args) {
    Array ordered = new Array(new int[] {1, 2, 3, 6, 7, 10});
    Array random = new Array(new int[] {2, 10, 3, 7, 1, 6});
    Comparator order = new CompareComparable();
    SimpleOutput out = new SimpleOutput();

    showArray(out, "ordered", ordered);
    showArray(out, "random", random);

    out.println("inorder(ordered): " + Searcher.inorder(ordered, order));
    out.println("inorder(random): " + Searcher.inorder(random, order));

    out.println("sequentialSearch(0,ordered): " + 
                sequentialSearch(new Integer(0), ordered, order));
    out.println("sequentialSearch(1,ordered): " + 
                sequentialSearch(new Integer(1), ordered, order));
    out.println("sequentialSearch(0,random): " + 
                sequentialSearch(new Integer(0), random, order));
    out.println("sequentialSearch(1,random): " + 
                sequentialSearch(new Integer(1), random, order));
              
  }
} // class TestSearcher

