CSC212 Program Assignment #2 Sorting Phone Records


Overview

Create an application that displays information about a phone list and lets the user lookup a phone number for a name

This is a sample application with a minimum functionality so that you have a few relevant tasks to do, but do not have to provide other features that would make the program more usable.

However, you will need to create 3 classes for this assignment: Entry, PhoneBook, and PhoneBookApp.

In particular, your application, PhoneBookApp, should do the following:

  1. Create a PhoneBook instance
  2. Prompt for a file name containing name, phonenumber pairs. For simplicity, the name should be a single String (e.g., a first name only).
  3. Use a PhoneBook method, readData, to read the data from the file and store it in an Entry array member of the PhoneBook instance.
  4. Use a PhoneBook method, sort, to sort the Entry array by name.
  5. Print each element in the sorted Entry array in order by name.
  6. Prompt the user for a name and read the reply. If the reply is "quit", the program should end.

    While the reply is not "quit"

    1. Search the array for the phone number for that name.
    2. If the name is not found print "Name not found", otherwise, print the phone number.
    3. Prompt the user again for a name (or "quit").

Classes

You should have the following classes:

All data members of Entry and PhoneBook should be private.

PhoneBookApp should contain the main method for your program.

You should provide a public String toString() method for Entry that returns a string of the form:

  Name: Bob, Phone: 773-111-222

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 have an Entry[] member which can be initialized to be size 10 by the default PhoneBook constructor.

    The other constructor allows the user to specify the size of this array.

    PhoneBook
    - entries: Entry[]
    - count: int
    + PhoneBook()
    + PhoneBook(int)
    + getCount(): int
    + getCapacity(): int
    + getEntries(): Entry[]
    + find(String): Entry
    + readData(String)
    + sort()

    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.
    
    // Constructors and methods
    Public() - initialize entries to be an array of length 10, set count = 0
    
    Public(int sz) - initialize entries to be an array of length sz, 
               set count = 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
    
    getEntries(): returns the whole Entry array. Remember that it does not
    make a copy of the array, it returns a reference to the one and only
    Entry array.
    
     Precondition: none
     Postcondition: Does not change the state of the PhoneBook
    
    find(String name): Entry
      Precondition: none
      Postcondition: No change to the PhoneBook
      Return value: null of name was not found in any Entry in the
                    PhoneBook Entry array, entries. Otherwise it returns a
                    reference to an Entry containing the name and the
                    corresponding phone number.
    
    readData(String filename)
      Precondition: filename should be the name of an existing file. If
                    the file cannot be located a FileNotFoundException
                    will be thrown 
      Postcondition: The name, phone number pairs from the file will be
                     read. For each pair an Entry instance will be created
                     and stored in the next available position in the
                     entries array. The count should be incremented for
                     each Entry instance stored. 
    
    sort()
      Precondition: none
      Postcondition: The entries array will be sorted in increasing order
                     by name.
    
    
    

Sample Input File



Susannah  123-555-6666
Bob 202-505-6879
Wilma 202-555-5466
Graham 850-555-6654
Alex 919-505-4356
Barbara 123-555-6643 
Sam 850-555-5866
Fred 919-505-5843
Betty 602-505-6658

Sample Run



java PhoneBookApp
Enter name of file containing name/phone pairs: phone.txt

PhoneBook sorted by name
Name: Alex, Phone: 919-505-4356
Name: Barbara, Phone: 123-555-6643
Name: Betty, Phone: 602-505-6658
Name: Bob, Phone: 202-505-6879
Name: Fred, Phone: 919-505-5843
Name: Graham, Phone: 850-555-6654
Name: Sam, Phone: 850-555-5866
Name: Susannah, Phone: 123-555-6666
Name: Wilma, Phone: 202-555-5466


Enter name to lookup or 'quit': Barney
Barney not found in phone book


Enter name to lookup or 'quit': Graham
Phone number for Graham is 850-555-6654


Enter name to lookup or 'quit': Betty
Phone number for Betty is 602-505-6658


Enter name to lookup or 'quit': quit

Shutting down now!

What to turn in

Be sure to add your name in a comment at the beginning of each of the three .java files.

Use zip (or other compression utility) to create a single file containing the three source files: Entry.java, PhoneBook.java, and PhoneBookApp.java

Submit the single file to the Course Online Site for program 2.