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

/**
 * Some experiments to better understand binary search trees
 */
public class TestBST
{
  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);

    BST bst = new BST(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("drop - delete an element");
        screen.println("dump - print the tree");
        screen.println("quit - quit already");
      }
      else if (command.equals("add")) {
        screen.print("Enter a string to add: "); screen.flush();
        String addMe = keyboard.readLine();
        bst.add(new Person(addMe, Math.round(1000* Math.random())));   
      }
      else if (command.equals("drop") || command.equals("delete")) {
        screen.print("Enter a string to delete: "); screen.flush();
        String dropMe = keyboard.readLine();
        bst.delete(new Person(dropMe, Math.round(1000*Math.random())));   
      }
      else if (command.equals("dump")) {
        bst.dump();
      }
      else {
        screen.println("Sorry, I didn't understand '" + command + "'");
      }
    } // while

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

} // class TestBST

