/**
 * Provides a simple ToDoList that holds up to 5 elements. 
 * Provides a simple menu-based text interface so the user can add and
 * remove elements.  
 * <p>
 * Demonstrates practical use of arrays.
 * 
 * @author Zach Tomaszewski
 */
public class ToDoListMain {

  public static void main(String[] args) {
    //todo list storage
    String[] todo = new String[5];
    int count = 0;  //how many items have been added to todo
    
    java.util.Scanner keybd = new java.util.Scanner(System.in);
    
    int choice = 1;
    while (choice != 0) {
      //print list
      System.out.println();
      System.out.println("TODO:");
      for (int i = 0; i < count; i++) {
        System.out.println((i + 1) + ". " + todo[i]);
      }
      System.out.println();
      
      //print menu choices
      System.out.println("MENU:");
      System.out.println("1 - Add item");
      System.out.println("2 - Remove last item");
      System.out.println("3 - Remove specific item");
      System.out.println("0 - Quit");
      System.out.print("Enter your menu choice: ");

      //process user's menu choice
      try {
        choice = keybd.nextInt();
        keybd.nextLine();  //clear input stream
        switch (choice) {
          case 1:  //ADD
            if (count == todo.length) {
              System.out.println("Sorry, but this to-do list is full! (Max: " +
                 todo.length + " items)");              
            }else { 
              System.out.print("Enter the thing you need to do: ");
              String task = keybd.nextLine();
              todo[count] = task;
              count++;
            }
            break;

          case 2:  //REMOVE LAST
            if (count > 0) {
              count--;
              System.out.println("Removed.");
            }else {
              System.out.println("The to-do list is already empty.");
            }
            break;

          case 3:  //REMOVE AT
            System.out.print("Enter the index of the item to remove: ");
            int index = keybd.nextInt();
            if (index < 1 || index > count) {              
              System.out.println("There is no item to remove at index " + 
                 index + " of this to-do list.");
            }else {
              index--;  //convert to 0-based indexing used by array
              //delete by shifting everything down into removed item's space
              for (int i = index; i < count - 1; i++) {
                todo[i] = todo[i + 1];
              }
              count--;  //removed an element
              System.out.println("Removed.");
            }
            break;
            
          case 0:
            System.out.println("Goodbye!");
            break;

          default:
            System.out.println("Sorry, but " + choice + " is not one of " +
                "the menu choices. Please try again.");
            break;
        }
      }catch (java.util.InputMismatchException ime) {
        System.out.println("Sorry, but you must enter a number.");
        keybd.nextLine();  //clear bad input from stream
      }
    }//end while
  
  }
}
