import java.util.*;

/**
 * Provides a simple to-do list.
 *
 * This to-do list is just a list of Strings.  New items can be added
 * to the end of the list.  Completed items can be removed from
 * the beginning of the list.
 *
 * @author Zach Tomaszewski
 * @version 19 Oct 2009
 */
public class ToDoList {

  public static void main(String[] args) {

    String[] todo = new String[5];
    int count = 0;  //number of items in todo
    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: ");
      for (int i = 0; i < count; i++) {
        System.out.println("* " + todo[i]);
      }
      System.out.println();

      //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: ");
              String item = keybd.nextLine();
              if (count == todo.length) {
                //todo is full so double in size before adding
                String[] temp = new String[todo.length * 2];
                for (int i = 0; i < todo.length; i++) {
                  temp[i] = todo[i];
                }
                todo = temp;
              }
              todo[count] = item;
              count++;
              break;

            case 2: //remove
              if (count == 0) {
                System.out.println("Could not remove entry because list is empty.");
              }else {
                String removed = todo[0];
                //shift everything down
                for (int i = 1; i < count; i++) {
                  todo[i - 1] = todo[i];
                }
                count--;
                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

}