Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a project to maintain telephone directory of several people. The d. re

ID: 3690062 • Letter: I

Question

Implement a project to maintain telephone directory of several people. The d. rectory should maintain record of each person associated with following information: Name Contact no Age State Zipcode The project which you have developed should contain following features: Add a record of a specific person to the telephone directory. Delete a record of a specific person to the telephone directory. Search a contact number of a person n the directory based on the name. Print the details of a person based on the name. Update the contact no of a person based on the name. Sort the information of each person in the directory based on their last names. Display the size of the telephone directory. For your convenience create an input text file and store the information of each person in a systematic manner. Then use file i|o concepts to read the input text file an store the contents. The results should be printed to console and output text file.

Explanation / Answer

My main program uses a simple text-based interface to give the user access to the directory. (A GUI program might be preferable, but is much harder to write.) In a while loop, the program presents the user with a menu of options:

The user is asked to select one of the options by typing in its number. For option 5, the program responds by breaking out of the while loop and exiting from the program. In the case of each of the other four cases, a subroutine is called to process the option. The subroutine gets the necessary data from the user and calls an instance method from the PhoneDirectory class to perform the requested operation. All this is fairly straightforward.

If this is done, then writing the directory a file named fileName is pretty simple. If directory is a variable that refers to the PhoneDirectory object, we could just say:

Reading the data from a file is almost as easy. It does require a type-cast to cast the Object returned by the readObject() method into a PhoneDirectory object. (This type-cast is one possible source of error, if the file contains a valid object but not an object of type PhoneDirectory.)

I didn't take this approach in my program. One possible objection is that it produces a file in machine-readable form instead of human-readable. It's nice to be able to read the data in the file directly. It also ties the file very tightly to the PhoneDirectory class. It might be nice to be able to use the same data in other programs that use different classes to store the directory data. Instead, I designed a text-file format. The first line of the data file specifies the number of entries in the phone directory. After that, there are two lines for each entry. The first line is the name and the second is the associated number.

My PhoneDirectory class contains two instance methods for reading and saving data. (It's not clear that the PhoneDirectory class is the best place for these methods. Maybe the saving and reading should be done entirely in the main program. However, my PhoneDirectory class provides no way for the main program to get a list of all entries from the directory, so there is no way for the main program to save that list in a file. Arguably, it might have been preferable to add such a method to the PhoneDirectory class.) Here are the methods. The essential point is that the load() method must be able to read data in the exact format that is written by the save() method.

Using these methods, the following code could be used to load a phone directory from a file named fileName:

In my program, the data is stored in a file named "phone.dat", but the user has the option of specifying a different file name as a command line argument when the program is run. If the file does not exist, the program offers to create a new, empty phone directory. Otherwise, it tries to read the phone directory data from the file. this makes the procedure for creating the directory somewhat more complicated. Here is the code that I actually use in my program to load the phone directory:

The PhoneDirectory class:

The main program:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote