import java.util.Scanner;
import java.util.InputMismatchException;

/**
 * Provides a user interface to a to-do list.
 *
 * @author Zach Tomaszewski
 * @version 17 Mar 2008
 */
public class SimpleToDoListUI {

  public static void main(String[] args) {

    SimpleToDoList todo = new SimpleToDoList();
    Scanner keybd = new Scanner(System.in);

    int choice = -1;
    //loop until user chooses to quit
    while (choice != 0) {
      //display list
      System.out.println("\nTO-DO LIST: ");
      System.out.println(todo);

      //print menu
      System.out.println("1. Add an item to the end of the list.");
      System.out.println("2. Remove first item from the list.");
      System.out.println("0. Quit.");

      //process user input
      do {
        System.out.print("Choose an option: ");
        try {
          choice = keybd.nextInt();
          keybd.nextLine(); //clear \n and any other garbage


          switch (choice) {
            case 0: //quit
              //do nothing, and let loop quit
              break;
            case 1: //add
              System.out.print("Enter an item to add to the list: ");
              todo.add(keybd.nextLine());
              break;
            case 2: //remove
              String removed = todo.remove();
              if (removed == null) {
                System.out.println("Could not remove entry because list is empty.");
              }else {
                System.out.println("Removed \"" + removed + "\" from list.");
              }
              break;
            default: //unrecognized
              System.out.println("That is not a valid choice.  Please try again.");
              break;
          }
        }catch (InputMismatchException ime) {
          System.out.println("Please enter a number " +
                             "corresponding to one of the options.");
          keybd.nextLine(); //clear stream
        }
      } while (choice < 0 || choice > 2);
    }

    //done, so goodbye message
    System.out.println();
    System.out.println("Thanks for using the To-Do list!");

  }//end main method
}