import java.util.ArrayList;

/**
 * Provides a simple to-do list.
 *
 * A 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 04 Nov 2008
 */
public class SimpleToDoList {

  /*
   * This version of SimpleToDoList uses an array, rather than an ArrayList.
   * Also, remove throws an exception rather than returning null if there is
   * nothing to remove.
   */

  private String[] list;
  private int capacity;  //how many item the array can currently hold
  private int size;      //how many items are actually in the array right now


  public SimpleToDoList() {
    capacity = 5;
    size = 0;
    list = new String[capacity];
  }


  /**
   * Adds the given item to the end of this todo list.
   */
  public void add(String item) {
    if (size == capacity) {
      this.increaseCapacity();
    }
    list[size] = item;
    size++;
  }

  /**
   * Removes the first item from this todo list.
   * Returns the item so removed, or null if the list is empty.
   */
  public String remove() {
    if (this.size > 0) {
      String removed = list[0];
      size--;
      //but now need to shift everything down in the array
      for (int i = 0; i < this.size; i++) {
        list[i] = list[i+1];
      }
      return removed;
    }else {
      return null;
    }
  }

  /**
   * Returns a string representation of this todo list,
   * listing one item per line.  If the list is empty,
   * returns an empty string.
   */
  public String toString() {
    String output = "";
    for (int i = 0; i < this.size; i++) {
      output += "* " + list[i] + "\n";
    }
    return output;
  }


  /**
   * Increases the size of the underlying array used to store items.
   */
  private void increaseCapacity() {
    this.capacity *= 2;  //double the current capacity
    String[] larger = new String[this.capacity];
    //copy current list into new larger list
    for (int i = 0; i < size; i++) {
      larger[i] = this.list[i];
    }
    this.list = larger; //replace current list with new larger one
  }

}


