import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Some experiments to better understand binary search trees
 */
public class TestHeap
{
  public static void main(String[] args)
    throws Exception
  {
    // Prepare input and screenput.
    BufferedReader keyboard = 
      new BufferedReader(new InputStreamReader(System.in));
    PrintWriter screen = new PrintWriter(System.out, true);

    Heap heap = new Heap(new ComparePersonByName());
    boolean done = false;

    while (!done) {
      screen.print("Command: "); screen.flush();
      String command = keyboard.readLine();
      if (command.equals("quit")) {
        done = true;
      }
      else if (command.equals("help") || command.equals("?")) {
        screen.println("add - add an element");
        screen.println("dump - print the tree");
        screen.println("get - get the smallest");
        screen.println("quit - quit already");
      }
      else if (command.equals("add")) {
        screen.print("Enter a string to add: "); screen.flush();
        String addMe = keyboard.readLine();
        heap.add(new Person(addMe, Math.round(1000* Math.random())));   
      }
      else if (command.equals("dump")) {
        heap.dump();
      }
      else if (command.equals("get")) {
        screen.println("The smallest element is: " + heap.deleteSmallest());
      }
      else {
        screen.println("Sorry, I didn't understand '" + command + "'");
      }
    } // while

    // That's it, we're done.
    System.exit(0);
  } // main(String[])

} // class TestHeap

