Question: Write a Java menu-driven program that creates and manipulates a directory of names, telephone numbers, and home addresses. The following information will be stored for
Write a Java menu-driven program that creates and manipulates a directory of names, telephone numbers, and home addresses. The following information will be stored for each person in the directory:
- Name (Last, First)
- Home address (street address, city, state, zip code)
- Home telephone number
Your program should use an Array data structure to store the directory entries and should be able to perform the following basic functions. The directory should remain an sorted after each of the operations below:
- Display the entire directory in sorted order by key value (combination of last and first names)
- Search and display the contents of a particular entry
- Delete an existing entry
- Insert an entry
These some tools it might help you
like this one: binarySearch Method
/** * Uses a binary search to search the directory for a combination of last name and first name * Returns the index at which it is found or -1 if it is not */ public int binarySearch(String last, String first) { int min = 0; int max = entryHolder.length; int mid = 0; while(min <= max) { mid = (min + max) / 2; if(entryHolder[mid].getLastName().equals(last)) { if(entryHolder[mid].getFirstName().equals(first)) { return mid; } } else { if(last.compareTo(entryHolder[mid].getLastName()) < 0) { max = mid - 1; } else { min = mid + 1; } } } return -1; }
Also this one:
import java.util.Scanner;
public class DirectoryRunner { public static void main(String[] args) { boolean cont = true; Scanner scan = new Scanner(System.in); System.out.println("How large is your directory?"); Directory dir = new Directory(scan.nextInt()); scan.close(); while(cont == true) { Scanner contScan = new Scanner(System.in); String input; System.out.println(" What would you like to do? '1' to add an entry, '2' to delete an entry, '3' to linearly search for an entry, '4' to find an entry using binary search, '5' to print out the directory, '6' to quit:"); input = contScan.nextLine(); if(input.equals("1")) { String first; String last; String phone; System.out.println("Please enter first name:"); first = contScan.nextLine(); System.out.println("Please enter last name:"); last = contScan.nextLine(); System.out.println("Please enter phone number:"); phone = contScan.nextLine(); dir.addEntry(last, first, phone); // if successfully added System.out.println("Added. "); } else if(input.equals("2")) { String first; String last; System.out.println("Please enter first name:"); first = contScan.nextLine(); System.out.println("Please enter last name:"); last = contScan.nextLine(); dir.deleteEntry(last, first); // if successfully deleted System.out.println("Deleted. "); } else if(input.equals("3")) { String first; String last; int found; System.out.println("Please enter first name:"); first = contScan.nextLine(); System.out.println("Please enter last name:"); last = contScan.nextLine(); found = dir.linearSearch(last, first); if(found != -1) System.out.println("Entry found at index " + found); else System.out.println("Entry not found: " + found); System.out.println("Thank you. "); } else if(input.equals("4")) { String first; String last; int found; System.out.println("Please enter first name:"); first = contScan.nextLine(); System.out.println("Please enter last name:"); last = contScan.nextLine(); found = dir.linearSearch(last, first); if(found != -1) System.out.println("Entry found at index " + found); else System.out.println("Entry not found: " + found); System.out.println("Thank you. "); } else if(input.equals("5")) { System.out.println(dir.toString() + " Printed. "); } else if(input.equals("6")) { System.out.println("Bye."); cont = false; } } }
}
And
public class Directory { // Data Fields private Entry[] entryHolder; /** * Creates a new directory to hold entries with a given size * The directory is initially blank */ public Directory(int numberOfEntries) { } /** * Adds an entry to the first empty spot */ public void addEntry(String last, String first, String phone) { } /** * Deletes an entry based on last name and first name */ public void deleteEntry(String last, String first) { } /** * Linearly searches the directory for a combination of last name and first name * Returns the index at which it is found or -1 if it is not */ public int linearSearch(String last, String first) { // just to showcase the runner working return 1; } /** * Uses a binary search to search the directory for a combination of last name and first name * Returns the index at which it is found or -1 if it is not */ public int binarySearch(String last, String first) { // just to showcase the runner working return 1; } /** * Creates a String consisting of each entry's names and phone numbers */ public String toString() { // just to showcase the runner working return "(this is the directory)"; } }
And
public class Entry { // Data Fields private String lastName; private String firstName; private String phoneNumber;
/** * Constructor for objects of class Entry * Holds a last name, a first name, and a phone number */ public Entry(String last, String first, String number) { } /** * Return first name */ public String getFirstName() { // just to showcase the runner working return "the first name of the entry"; } /** * Return last name */ public String getLastName() { // just to showcase the runner working return "the last name of the entry"; } /** * Return phone number */ public String getPhoneNumber() { // just to showcase the runner working return "the phone number of the entry"; }
}
These codes options for the user
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
