CSc 2310 Principles of Computer Programming I
Spring 1999
Programming Assignment #6
Due: 19 April 1999 (Monday)

  1. Write a menu-driven program for maintaining a personal phone book. The phone book should contain names and numbers of people and should be maintained in sorted order at all times. The name of a person will serve as a key for the data, i.e., a person can have at most one phone number. The main menu should contain the following options:

    (A) Add an entry
    (D) Delete an entry
    (C) Change a phone number
    (P) Display the phone book
    (L) Look up a phone number
    (I) Import phone book from a file
    (E) Export phone book to a file
    (Q) Quit Program
    
    Enter Your Choice:

    Add an entry:
    In this option the program should ask the user for the name and phone number and add the entry. If the name already exists, the entry should not be made and a message should be sent to the user. Also, remember to add the entry at the appropriate place in the array to maintain sorted order.

    Delete an entry:
    In this option the program should ask the user for the name of the person and then delete the entry. If the name does not exist, an appropriate message should be sent to the user.

    Change a phone number:
    In this option, the program will ask the user for the name of the person. Then, it should display the old phone number and ask the user for the new phone number. Then the change should be made. Again, any error situations should be reported to the user.

    Print the phone book:
    In this option, the program should display the phone book entries in a neatly formatted way on the display screen.

    Lookup a phone number:
    In this option, the program should ask the user for the name of a person and should print the person's phone number. Error handling should be done, in case the person does not exist in the database.

    Import phonebook from file:
    In this option, the program should ask the user for the name of the data file that contains the phone book data, then it should load the array with the data.

    Export phonebook to file:
    In this option, the program should ask the user for the name of the data file to which the phone book should be written, then it should write the phone book into the file (in the same format when the data was imported).

Remarks:

  1. You must develop this program by creating three separate Java classes: PhoneEntry, PhoneBook, and Phone.
  2. The PhoneEntry objects correspond to individual phone entries, which contain the person's name and their phone number. The PhoneBook object corresponds to a collection of PhoneEntry objects. This collection should be implemented by an array. The Phone class contains the main method and implements the menu options.
  3. To write to a file, use the OutputFile class. Include this class in the ohjava package. The steps involved in writing to a file with name fname are:
      OutputFile fout;
      fout = new OutputFile();
    
      fout.openFile(fname);
      fout.writeLine(line);
      ...
      ...
      fout.writeLine(line);
      fout.close();
  4. To read from a file, use the InputFile class. Include this class in the ohjava package. The steps involved in reading from a file with name fname are:
      InputFile fin;
      fin = new InputFile();
    
      boolean openSuccess = fin.openFile(fname);
      line = fin.readLine();
      ...
      ...
      line = fin.readLine();
      fin.close();
  5. You must include the throws Exception clause after the method specification for each method that directly or indirectly is involved in reading or writing files, as shown below:
    public static void main(String args[]) throws Exception {
    
  6. Since the phone book must be maintained in the sorted order by person name at all times, you must take extra care while inserting and deleting entries.
    • While inserting an entry, first, the program should locate the exact position in the array where this entry should go, then it should shift all entries beyond that position to make space for the new entry and then insert the new entry in the vacated space.
    • While deleting an entry, the program should shift all entries beyond the entry to be deleted up by one position so that after the deletion, all entries are in continuous locations in the array.



Raj Sunderraman
Sun Apr 4 18:06:24 EDT 1999