I need help to get the right output the below program Project: For this program
ID: 3649439 • Letter: I
Question
I need help to get the right output the below programProject:
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.
import java.io.*;
import java.util.Scanner;
import java.io.FileReader;
import java.io.PrintWriter;
public class PhoneDirectory {
static final String DEFAULT_FILENAME = "phoneDirectory.dat";
static PhoneDirectory directory; // Holds the data for the phone directory.
private boolean changed;
static Scanner sc = new Scanner(System.in);
public static void main(String[] args)
{
directory = new PhoneDirectory(); // A new, empty phone directory.
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)
{
System.out.println();
System.out.println();
System.out.println("Select the operation you want to perform:");
System.out.println();
System.out.println(" 1. Look up a phone number");
System.out.println(" 2. Add an entry to the directory");
System.out.println(" 3. Delete an entry from the directory");
System.out.println(" 4. Change someone's phone number");
System.out.println(" 5. Exit form this program.");
System.out.println();
System.out.println("Enter the number of your choice: ");
int menuOption = sc.nextInt();
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);
System.out.println(" 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.
Scanner reader = null;
// 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.
reader = new Scanner(new FileReader(fileName));
}
catch (Exception e)
{
reader = null;
}
if (reader == null)
{
// The specified file could not be opened. Give the
// user the option of creating a new, empty file.
System.out.println(" The file "" + fileName + "" does not exist.");
System.out.println("Do you want to create the file? ");
boolean create = sc.nextBoolean();
if (create == false) {
System.out.println("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();
System.out.println("Empty directory created.");
}
catch (Exception e)
{
System.out.println("Can't create file.");
System.out.println("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(reader); // Try to load it with data from the file.
}
catch (Exception e) {
System.out.println("An error occurred while read data from "" + fileName + "":");
System.out.println(e.toString());
System.out.println("Program aborted.");
System.exit(0);
}
}
} // end readPhoneData()
private void save(PrintWriter out) {
// TODO Auto-generated method stub
}
private void load(Scanner reader) {
// TODO Auto-generated method stub
}
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) {
System.out.println(" Can't open file for output!");
System.out.println("Changes have not been saved.");
return;
}
directory.save(out);
if (out.checkError()) {
System.out.println("Some error occurred while saving data to a file.");
System.out.println("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.
System.out.println(" Look up the name: ");
String name = sc.next();
String number = directory.numberFor(name);
if (number == null)
System.out.println(" No such name in the directory.");
else
System.out.println(" The number for " + name + " is " + number);
}
private String numberFor(String name) {
// TODO Auto-generated method stub
return null;
}
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.
System.out.println(" Add entry for this name: ");
String name = sc.next();
if (directory.numberFor(name) != null) {
System.out.println("That name is already in the directory.");
System.out.println("Use command number 4 to change the entry for " + name);
return;
}
System.out.println("What is the number for " + name + "? ");
String number = sc.next();
directory.addNewEntry(name,number);
System.out.println("What is the number for " + name + "? ");
}
private void addNewEntry(String name, String number) {
// TODO Auto-generated method stub
}
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.
System.out.println(" Delete the entry for this name: ");
String name = sc.next();
if (directory.numberFor(name) == null)
System.out.println("There is no entry for " + name);
else {
directory.deleteEntry(name);
System.out.println("Entry deleted.");
}
}
private void deleteEntry(String name) {
// TODO Auto-generated method stub
}
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.
System.out.println(" Change the number for this name: ");
String name = sc.next();
if (directory.numberFor(name) == null) {
System.out.println("That name is not in the directory.");
System.out.println("Use command number 2 to add an entry for " + name);
return;
}
System.out.println("What is the new number for " + name + "? ");
String number = sc.next();
directory.updateEntry(name,number);
}
private void updateEntry(String name, String number) {
// TODO Auto-generated method stub
}
} // end class PhoneDirectory
Explanation / Answer
The PhoneDirectory class: /* An object of type PhoneDirectory holds a list of names and associated phone numbers. In this simple implementation, both the names and the numbers are stored as strings. The names and numbers must be non-null strings, but no attempt is made to ensure that the values make sense. Comparison of names is in all cases case-insensitive. A given name cannot occur more than once in the directory. The instance methods throw IllegalArgumentExceptions when the rules are violated. The instance methods load() and save() are provided for loading the data for the directory from a stream and for saving the data to a stream. */ 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; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.