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

For this program you will implement a phone directory using a vector and demonst

ID: 3647946 • Letter: F

Question

For this program you will implement a phone directory using a vector and demonstrate a variety of features. In general, you will need to search through entries of an office phone directory and return the specified information. A phone book entry consists of a person's name, his or her home phone number, work phone number, and cell phone number. The main method should load several entries into the book and demonstrate that your program works. In particular, demonstrate that the program can:

Search for a person's numbers based on his or her name.
Search for a person's contact information based on his or her work number.
Remove entries from your directory.
Print all the entries in the directory.
Return the entry at a specific index. If a name or work phone number does not exist, your program should print out that the name was not found.


PhoneDirectory.java

Explanation / Answer

code: import java.io.*; public class Phones { static final String DEFAULT_FILENAME = "phones.dat"; static PhoneDirectory directory; // Holds the data for // the phone directory. public static void main(String[] args) { String fileName; // Name of file that stores the directory data. boolean done; // Set to true when the user wants to exit the program. /* Get the file name from the command line, or use the DEFAULT_FILENAME if there is no command-line argument. */ if (args.length == 0) fileName = DEFAULT_FILENAME; else fileName = args[0]; /* Read the phone directory data from the file. This routine might terminate the program if an error occurs when the attempt is made to end the data. */ readPhoneData(fileName); /* Show user a menu of available operations, get the user's choice, and carry it out. Repeat until the user selects the "Exit from this program" operation. Each of the other four commands is carried out by calling a subroutine. */ done = false; while (done == false) { TextIO.putln(); TextIO.putln(); TextIO.putln("Select the operation you want to perform:"); TextIO.putln(); TextIO.putln(" 1. Look up a phone number"); TextIO.putln(" 2. Add an entry to the directory"); TextIO.putln(" 3. Delete an entry from the directory"); TextIO.putln(" 4. Change someone's phone number"); TextIO.putln(" 5. Exit form this program."); TextIO.putln(); TextIO.put("Enter the number of your choice: "); int menuOption = TextIO.getlnInt(); switch (menuOption) { case 1: doLookup(); break; case 2: doAddEntry(); break; case 3: doDeleteEntry(); break; case 4: doModifyEntry(); break; case 5: done = true; break; default: System.out.println("Illegal choice! Please try again."); } // end switch } // end while /* If the phone directory data has been modified, write the changed data back to the file. */ if (directory.changed == true) writePhoneData(fileName); TextIO.putln(" Exiting program."); } // end main() static void readPhoneData(String fileName) { // Get the data for the phone directory from the specified // file. Terminate the program if an error occurs. If the // file does not exist, give the user the option of creating // it. 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); } } } // end readPhoneData() static void writePhoneData(String fileName) { // Save the data from the phone directory to the specified file. PrintWriter out; try { out = new PrintWriter( new FileWriter(fileName) ); } catch (Exception e) { TextIO.putln(" Can't open file for output!"); TextIO.putln("Changes have not been saved."); return; } directory.save(out); if (out.checkError()) { TextIO.putln("Some error occurred while saving data to a file."); TextIO.putln("Sorry, but your phone directory might be ruined"); } } static void doLookup() { // Carry out the "Look up a phone number" command. Get // a name from the user, then find and print the associated // number if any. TextIO.putln(" Look up the name: "); String name = TextIO.getln(); String number = directory.numberFor(name); if (number == null) TextIO.putln(" No such name in the directory."); else TextIO.putln(" The number for " + name + " is " + number); } static void doAddEntry() { // Carry out the "Add an entry to the directory" command. // This will only work if the name that the user specifies // does not already exist in the directory. If it does, // print an error message and exit. Otherwise, get the // number for that person from the user and add the entry // to the directory. TextIO.putln(" Add entry for this name: "); String name = TextIO.getln(); if (directory.numberFor(name) != null) { TextIO.putln("That name is already in the directory."); TextIO.putln("Use command number 4 to change the entry for " + name); return; } TextIO.putln("What is the number for " + name + "? "); String number = TextIO.getln(); directory.addNewEntry(name,number); } static void doDeleteEntry() { // Carry out the "Delete an entry from the directory" command. // Get the name to be deleted from the user and delete it. // If the name doesn't exist in the directory, print a message. TextIO.putln(" Delete the entry for this name: "); String name = TextIO.getln(); if (directory.numberFor(name) == null) TextIO.putln("There is not entry for " + name); else { directory.deleteEntry(name); TextIO.putln("Entry deleted."); } } static void doModifyEntry() { // Carry out the "Change someone's phone number" command. // Get the name from the user. If the name does not exist // in the directory, print a message and exit. Otherwise, // get the new number for that person and make the change. TextIO.putln(" Change the number for this name: "); String name = TextIO.getln(); if (directory.numberFor(name) == null) { TextIO.putln("That name is not in the directory."); TextIO.putln("Use command number 2 to add an entry for " + name); return; } TextIO.putln("What is the new number for " + name + "? "); String number = TextIO.getln(); directory.updateEntry(name,number); } } // end class Phones

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