CSC212 Program Assignment #2 PhoneBook


  1. Overview

    Create an application that lets the user manage a phonebook similar to the capability of most cell phones.

    You will present a simple text menu that will allow the user to do any one of the following:

  2. Classes

    You should have the following classes:

    All data members of these classes should be private.

    You will need to provide "get" and "set" methods in classes to manage most (or possibly all) private data members of each class.

    You should provide a public String toString() method for Entry.

    The PhoneBookApp class should create an empty PhoneBook and provide the menu, but able to hold the number of entries specified by the parameter. The PhoneBookApp will provide the menu and methods for menu choice. However, these methods should use the PhoneBook and Entry instance methods to do most of the work.

    Here is a UML class diagram showing the relationship among these classes:

    1. The dashed arrow means PhoneBookApp creates an instance of PhoneBook (e.g. in the application's main method), but we will not create lots of instances of PhoneBookApp; there will only be one PhoneBookApp. So PhoneBookApp doesn't need any instance data members.

    2. The arrow with the diamond shape at one end means PhoneBook has Entry members. The n at the other end of the arrow means a PhoneBook as 1 or more Entry members (e.g. an Entry array)

    3. The arrow (vertical) with the open triangle head indicates that EntryPlusAddr is a subclass of Entry

  3. Class Details

    1. Entry.

      The Entry class should contain a name and a phone number.

      Entry
      - name: String
      - phone: String
      + Entry()
      + Entry(String, String)
      + getName(): String
      + getPhone(): String
      + toString(): String
    2. The PhoneBook class should use an ArrayList of Entrys.

      An ArrayList has all the same properties of a Vector except that it doesn't check that only one thread is modifying the ArrayList. It isn't "thread-safe" for applications that are "multi-threaded." On the other hand, Vector is "thread-safe". This means ArrayList is "slightly" more efficient than Vector for programs that don't have multiple threads making modifications since ArrayList doesn't have to execute the overhead enforce the "one thread at a time" rule.

      PhoneBook
      - entries: Entry[]
      - count: int
      - position: int
      + PhoneBook()
      + PhoneBook(int)
      + getCount(): int
      + getCapacity(): int
      + start()
      + hasNext(): boolean
      + next(): Entry
      + add(String, String)
      + add(Entry)

      Here is a more detailed description of the members of PhoneBook:

      // Data members
      Entry[] entries; A member array to hold Entry instances.
      int count; The number of Entry instances actually stored in entries.
      int position; The index into entries of the "next" Entry to return by
         the next() method.
      
      // Constructors and methods
      Public() - initialize entries to be an array of length 10, set count =
                 0 and position = 0.
      
      Public(int sz) - initialize entries to be an array of length sz, 
                 set count = 0 and position = 0.
      
      getCount: returns the number of Entry instances stored in entries.
       Precondition: none
       Postcondition: Does not change the state of the PhoneBook
      
      getCapacity(): returns the length of the entries array. This is the
       maximum number of Entries that can be in the PhoneBook.
       Precondition: none
       Postcondition: Does not change the state of the PhoneBook
      
      start(): Sets (or resets) the position to 0 so that a subsequent call
       to next() will return the first Entry in entries (unless the PhoneBook
       is empty.
       Pre
      
  4. Sample Run

    
    1. Add one Entry
    2. Add Entries from File
    3. List All
    4. quit
    
    > 1
    Name: Barry
    Phone: 222-333-4444
    
    1. Add one Entry
    2. Add Entries from File
    3. List All
    4. quit
    
    > 1
    Name: Barbara
    Phone: 323-232-2222
    
    1. Add one Entry
    2. Add Entries from File
    3. List All
    4. quit
    
    > 1
    Name: Bill
    Phone: 555-555-1111
    
    1. Add one Entry
    2. Add Entries from File
    3. List All
    4. quit
    
    > 1
    Name: Carol
    Phone: 919-999-4444
    
    1. Add one Entry
    2. Add Entries from File
    3. List All
    4. quit
    
    > 1
    Name: Cathy
    Phone: 888-123-4567
    
    1. Add one Entry
    2. Add Entries from File
    3. List All
    4. quit
    
    > 2
    File Name: phonerecs.txt
    
    2 records added
    
    
    1. Add one Entry
    2. Add Entries from File
    3. List All
    4. quit
    
    > 3
    Name: Barry
    Phone: 222-333-4444
    n(ext) or q(uit)> n
    Name: Barbara
    Phone: 323-232-2222
    n(ext) or q(uit)> n
    Name: Bill
    Phone: 555-555-1111
    n(ext) or q(uit)> n
    Name: Carol
    Phone: 919-999-4444
    n(ext) or q(uit)> n
    Name: Cathy
    Phone: 888-123-4567
    n(ext) or q(uit)> n
    Name: Sam
    Phone: 666-123-7654
    n(ext) or q(uit)> n
    
    No more entries.
    
    1. Add one Entry
    2. Add Entries from File
    3. List All
    4. quit
    
    > q
    
    INVALID input q
    Enter an integer 1 to 4
    
    1. Add one Entry
    2. Add Entries from File
    3. List All
    4. quit
    
    > 7
    
    INVALID input 7
    Enter an integer 1 to 4
    
    1. Add one Entry
    2. Add Entries from File
    3. List All
    4. quit
    
    > 4