UCL Logo

OneClassPhoneBook

The one class version of phone book is essentially a procedural program (a collection of methods that call one another) placed inside a Java class. It uses the KeyboardInput, FileInput and FileOutput classes (copies of these should be placed in the same directory as OneClassPhoneBook in order to compile and run it).

This is the class:

import java.util.ArrayList;


public class OneClassPhoneBook  
{
  private static final int SEARCH = 1;
  private static final int LIST = 2;
  private static final int ADD = 3;
  private static final int REMOVE = 4;
  private static final int SAVE = 5;
  private static final int LOAD = 6;
  private static final int QUIT = 7;
 
  private KeyboardInput in = new KeyboardInput();
  private ArrayList<String> names = new ArrayList<String>();
  private ArrayList<String> numbers = new ArrayList<String>();

  public void go()
  {
    boolean quit = false;
    while (!quit)
    {
      displayMenu();
      int item = in.readInteger();
      if (item == QUIT)
      {
        quit = true;
      }
      else
      {
        doOption(item);
      }
    }
  }

  private void displayMenu()
  {
    System.out.println("\nPhone Book");
    System.out.println(SEARCH + ". Search for a person's phone number");
    System.out.println(LIST + ". List all entries in the phone book");
    System.out.println(ADD + ". Add a new person and phone number");
    System.out.println(REMOVE + ". Remove a person and phone number");
    System.out.println(SAVE + ". Save phone list to a file");
    System.out.println(LOAD + ". Add a phone list from a file");
    System.out.println(QUIT + ". Quit");
    System.out.print("\nEnter selection: ");
  }
  
  private void doOption(int item)
  {
    switch (item)
    {
      case SEARCH :
        searchForEntry();
        break;
      case LIST :
        listAllEntries();
        break;
      case ADD :
        addEntry();
        break;
      case REMOVE :
        removeEntry();
        break;
      case SAVE :
        saveList();
        break;
      case LOAD :
        loadList();
        break;
      default :
        System.out.println("\nSorry - don't recognise that selection, try again");
    }
  }

  private void searchForEntry()
  {
    System.out.println("\nSearch phone list");
    System.out.print("Enter the person's name: ");
    String name = in.readString();
    int index = names.indexOf(name);
    if (index != -1)
    {      
      System.out.println("The phone number is: " + numbers.get(index));
    }
    else
    {
      System.out.println("Sorry - nothing found");
    }
  }
    
  private void listAllEntries()
  {
    if (names.size() == 0)
    {
      System.out.println("There are no entries in the phone book");
      return;
    }
    for (int i = 0 ; i < names.size(); i++)
    {
      String name = names.get(i);
      String number = numbers.get(i);
      System.out.println("Entry " + (i + 1) + ": ");
      System.out.println("  Name: " + name);
      System.out.println("  Number: " + number);      
    }
  }

  private void addEntry()
  {
    System.out.println("\nAdd entry to phone list");
    System.out.print("Enter the person's name: ");
    String name = in.readString();
    System.out.print("Enter the person's phone number: ");
    String phone = in.readString();
    names.add(name);
    numbers.add(phone);
  }

  private void removeEntry()
  {
    System.out.println("\nRemove entry from phone list");
    System.out.print("Enter the person's name: ");
    String name = in.readString();
    remove(name);
  }
  
  private void remove(String name)
  {
    int index = names.indexOf(name);
    if (index != -1)
    {
      names.remove(index);
      numbers.remove(index);
      System.out.println("The entry for: " + name + " has been removed");    
    }
    else
    {
      System.out.println("Name not found - nothing removed");
    }
  }

  private void saveList()
  {
    System.out.println("\nSave phone list to a file");
    System.out.print("Enter the file name: ");
    String fileName = in.readString();
    FileOutput out = new FileOutput(fileName);
    writeToFile(out);
    out.close();
  }
  
  private void writeToFile(FileOutput file)
  {
    for (int i = 0; i < names.size(); i++)
    {
      file.writeString(names.get(i));
      file.writeNewline();
      file.writeString(numbers.get(i));
      file.writeNewline();
    }
  }

  private void loadList()
  {
    System.out.println("\nAppend phone list from a file");
    System.out.print("Enter the file name: ");
    String fileName = in.readString();
    FileInput fileIn = new FileInput(fileName);
    readFromFile(fileIn);
    fileIn.close();
  }
  
  private void readFromFile(FileInput file)
  {
    String name = file.readString();
    String phone = file.readString();
    while (!file.eof())
    {
      names.add(name);
      numbers.add(phone);
      name = file.readString();
      phone = file.readString();
    }
  }  

  public static void main(String[] args)
  {
    new OneClassPhoneBook().go();
  }
}
Last updated: August 31, 2006

Computer Science Department - University College London - Gower Street - London - WC1E 6BT - Telephone: +44 (0)20 7679 7214 - Copyright 1999-2006 UCL


 Search by Google