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

In Java please . Write a program that provides a way for you to store and retrie

ID: 3836968 • Letter: I

Question

In Java please. Write a program that provides a way for you to store and retrieve telephone numbers. It has three classes Person Class, PhoneBook class, and Menu Class. Design a console program that provides the following operations:

Add: Adds a person’s name and phone number to the phone book.

Delete: Deletes a given person’s name and phone number from the phone book, given only the name.

Find: Locates a person’s phone number, given only the person’s name.

Change: Changes a person’s phone number, given the person’s name and new phone number.

Quit: Quits the application, after first saving the phone book in a text file.

You can proceed as follows:

- Design and implement the class Person, which represents the name and phone number of a person. You will store instances of this class in the phone book.

-Design and implement the class PhoneBook, which represents the phone book. The class should contain a binary search tree as a data field. This tree contains the people in the book.

-Add methods that use a text file to save and restore the tree.

- Design and implement the class Menu, which provides the program’s user interface.

The program should read data from a text file when it begins and save data into the text file when the user quits the program

Explanation / Answer

TextReader in; // A stream for reading the data. try { // Try to create a stream for reading from the file. // If the file is not found, set the value of in to null. in = new TextReader( new FileReader(fileName) ); } catch (Exception e) { in = null; } if (in == null) { // The specified file could not be opened. Give the // user the option of creating a new, empty file. TextIO.putln(" The file "" + fileName + "" does not exist."); TextIO.put("Do you want to create the file? "); boolean create = TextIO.getlnBoolean(); if (create == false) { TextIO.putln("Program aborted."); System.exit(0); } directory = new PhoneDirectory(); // A new, empty phone directory. try { // Try to create the file. PrintWriter out = new PrintWriter( new FileWriter(fileName) ); directory.save(out); if (out.checkError()) throw new Exception(); TextIO.putln("Empty directory created."); } catch (Exception e) { TextIO.putln("Can't create file."); TextIO.putln("Program aborted."); System.exit(0); } } else { // The input stream was created successfully. Get the data. try { directory = new PhoneDirectory(); // A new, empty directory. directory.load(in); // Try to load it with data from the file. } catch (Exception e) { TextIO.putln("An error occurred while read data from "" + fileName + "":"); TextIO.putln(e.toString()); TextIO.putln("Program aborted."); System.exit(0); } } import java.io.*; public class PhoneDirectory { /* The data for the directory is stored in a pair of arrays. The phone number associated with the name names[i] is numbers[i]. These arrays will grow, as necessary, to accommodate as many entries as are added to the directory. The variable count keeps track of the number of entires in the directory. */ private String[] names = new String[1]; private String[] numbers = new String[1]; private int count = 0; public boolean changed; // This variable is set to true whenever a change // is made to the data in this directory. The value // is false when the object is created. The only time // that it is reset to false is if the load() method // is used to load a phone directory from a stream. // (Programs that use the directory can also set the // value of changed if they want, since it's public.) public void load(TextReader in) throws IOException { // Clears any entries currently in the directory, and loads // a new set of directory entries from the TextReader. The // data must consist of the following: a line containing the // number of entries in the directory; two lines for each // entry, with the name on the first line and the associated // number on the second line. Note that this method might // throw an IllegalArgumentException if the data in the file // is not valid -- for example if the same name occurs twice. // Note that if an error does occur, then the original // data in the directory remains. int newCount = in.getlnInt(); String[] newNames = new String[newCount + 5]; String[] newNumbers = new String[newCount + 5]; for (int i = 0; i
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