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

Write a Java menu-driven program that creates and manipulates a directory of nam

ID: 3575780 • Letter: W

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 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

The user should enter the size of the array, N. An error message should be displayed if inserting into the array makes the size larger than N.

You must use three classes: entry class, directory class, and a driver.

Explanation / Answer

import java.util.*;
class Entry
{
   String[] name = new String[2];
   String[] homeAddress = new String[4];
   int telNum;
   Entry(String names[],String homeAddresses[],int telNums){
       this.name[0]=names[0];
       this.name[1]=names[1];
       this.homeAddress[0]=homeAddresses[0];
       this.homeAddress[1]=homeAddresses[1];
       this.homeAddress[2]=homeAddresses[2];
       this.homeAddress[3]=homeAddresses[3];
       this.telNum=telNums;
   }

}

class Driver
{
   public static void main(String args[])
   {
       Directory dir = new Directory(10);
       while(true){
       System.out.println(" Enter Choice-: ");
       System.out.println("1. insert an Entry");
       System.out.println("2. display");
       System.out.println("3. search");
       System.out.println("4. delete");
       Scanner sc = new Scanner(System.in);
       int choice = sc.nextInt();
       String[] name = new String[2];
       String[] homeAddresses = new String[4];
       int telNum;
       sc.nextLine();
       switch(choice)
       {
           case 1:{
                       System.out.println("Enter Last name: ");
                       name[0] = sc.nextLine();
                       System.out.println("Enter First Name: ");
                       name[1] = sc.nextLine();
                       System.out.println("Enter street address: ");
                       homeAddresses[0] = sc.nextLine();
                       System.out.println("Enter city: ");
                       homeAddresses[1] = sc.nextLine();
                       System.out.println("Enter state: ");
                       homeAddresses[2] = sc.nextLine();
                       System.out.println("Enter zipcode: ");
                       homeAddresses[3] = sc.nextLine();
                       System.out.println("Enter telephone number: ");
                       telNum = sc.nextInt();
                       Entry e = new Entry(name,homeAddresses,telNum);
                       dir.insert(e);
                       dir.sort();
                       break;
                    }
           case 2:{
                   dir.display();
                   break;

                    }
           case 3:   {
                   System.out.println("Enter Last Name : -");
                   String lastName = sc.nextLine();
                   System.out.println("Enter first Name :-");
                   String firstName = sc.nextLine();
                   dir.search( lastName, firstName );
                   break;  
                   }
            case 4:{
                   System.out.println("Enter Last Name : -");
                   String lastName = sc.nextLine();
                   System.out.println("Enter first Name :-");
                   String firstName = sc.nextLine();
                   dir.delete( lastName, firstName );
                   break;  
            }
       }
   }

   }
}
class Directory{
   int max;
   Entry[] dir;
   int index = 0;
   Directory(int max){
       this.max = max;
       this.dir = new Entry[max];
   }
   public void insert(Entry newentry){
       dir[index] = newentry;
       index++;
   }
   public void delete(String last, String first){
       int i;
       for(i=0; i< index;i++){
           Entry tempEntry = dir[i];
           if(tempEntry.name[0].equals(last) && tempEntry.name[1].equals(first) ) {
               break;
           }
       }
       if( i < index ){
           if(i == 0){
               dir[0] = null;
           }
           for( int j=i; j <= index-1; j++ ) {
           dir[j] = dir[j + 1];
           }
           index = index - 1;
       }
       else{
           System.out.println("not found");
       }
      
   }
   public void display(){
       if(index ==0){
           System.out.println("Directory Empty !!");
           System.exit(0);
       }
       for(int i=0;i<index;i++){
           Entry tempEntry = dir[i];
           System.out.println("Last Name : " + tempEntry.name[0]);
           System.out.println("First Name : " + tempEntry.name[1]);
           System.out.println("street address : " + tempEntry.homeAddress[0]);
           System.out.println("city : " + tempEntry.homeAddress[1]);
           System.out.println("state : " + tempEntry.homeAddress[2]);
           System.out.println("zip code : " + tempEntry.homeAddress[3]);
           System.out.println("tel number : " + tempEntry.telNum);
       }
   }
   public void sort(){
       Entry tempi,tempj;
       for(int i=0; i < index; i++){
           tempi = dir[i];
           for(int j = 0; j < index; j++){
               tempj = dir[j];
               int choice = (tempi.name[0]).compareTo(tempj.name[0]);
               if(choice == 1 ){
                   dir[i] = tempj;
                   dir[j] = tempi;
               }
           }
       }
   }
   public int search(String last, String first){
       Entry tempEntry;
       for(int i=0; i < index; i++){
           tempEntry = dir[i];
           if((tempEntry.name[0]).equals(last) && (tempEntry.name[1]).equals(first)){
               System.out.println(" Element found !!! ");
           System.out.println("Last Name : " + tempEntry.name[0]);
           System.out.println("First Name : " + tempEntry.name[1]);
           System.out.println("street address : " + tempEntry.homeAddress[0]);
           System.out.println("city : " + tempEntry.homeAddress[1]);
           System.out.println("state : " + tempEntry.homeAddress[2]);
           System.out.println("zip code : " + tempEntry.homeAddress[3]);
           System.out.println("tel number : " + tempEntry.telNum);
           return 0;  
           }
       }
       System.out.println(" not found ");
       return 1;
   }
}

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