/*
 * PhoneBook.java      Authors: Koffman and Wolz
 * Represents a telephone directory.
 */
public class PhoneBook {
  // Data fields
  private Friend[] directory;        // array of friends
  private static int PAGE_SIZE = 5;  // friends on each page
  private int numPages;      // number of pages in directory
  private int numFriends;    // number of friends in directory

  // postcondition: Creates a new PhoneBook object with 1 page
  //   in its directory.
  public PhoneBook() {
    directory = new Friend[PAGE_SIZE];
    numPages = 1;
  }

  // postcondition: Adds its argument to the directory. 
  //   Adds a new page if current page is filled.
  //   Increments numFriends.
  public void addFriend(Friend aFriend) {
    if (numFriends >= numPages * PAGE_SIZE)
      addPage();
    directory[numFriends] = aFriend;
    numFriends++;
  }

  // postcondition: Returns true if its argument is in range.
  public boolean inDirectory(int index) {
    return index >= 0 && index < numFriends;
  }

  // postcondition: Returns the friend selected by its argument.
  public Friend getFriend(int index) {
    if (inDirectory(index))
      return directory[index];
    else
      return null;
  }

  // postcondition: Returns the number of the friend whose
  //   name is referenced by its argument.
  public String getNumber(String friendName) {
    int index = findFriend(friendName);
    if (index >= 0)
      return directory[index].getPhone();
    else
      return null;
  }

  // postcondition: Returns the index of the friend whose
  //   name is referenced by its argument.
  //   Returns -1 if not found.
  public int findFriend(String friendName) {
    for (int index = 0; index < numFriends; index++) {
      if (directory[index].getName().equals(friendName))
        return index;
    }

    // friendName not found.
    return -1;
  }

  // postcondition: Stores aFriend at position index.
  public void setFriend(int index, Friend aFriend) {
    if (inDirectory(index))
      directory[index] = aFriend;
  }

  // postcondition: Increments numPages and adds a page to
  //   the directory array.
  public void addPage() {
    numPages++;

    // Create a new array of friends with one more page.
    Friend[] tempDirectory = new Friend[numPages * PAGE_SIZE];

    // Copy all friends from the old array to the new array.
    System.arraycopy(directory, 0, tempDirectory, 0, numFriends);

    // Reset directory to reference the new array.
    directory = tempDirectory;
  }

  public int getNumPages() {
    return numPages;
  }

  public int getNumFriends() {
    return numFriends;
  }
  
  // postcondition: Returns a String representing the telephone
  //   directory. The page number is inserted at the beginning
  //   of each new page.
  public String toString() {
    String result = "";
    for (int index = 0; index < numFriends; index++) {
      if (index % PAGE_SIZE == 0)
         result = result + "\npage: " + (1 + index / PAGE_SIZE) + "\n";
      result = result + directory[index] + "\n";
    }
    return result;
  }

}

