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

The aim of this homework assignment is to write a ContactBook class that will st

ID: 3807774 • Letter: T

Question

The aim of this homework assignment is to write a ContactBook class that will store a list of Contacts in an array. A ContactBook is identified by an owner (last name and first name of the owner). This homework assignment will also help build towards project 1, which will be to write a program implementing a Management System for a Personal Contact Book. For this assignment write a ContactBook class. This class will store a list of Contacts in a stack allocated array of default capacity 10. (For now it will be of fixed capacity partially filled array. Later we will modify it to make it growable). You must be able to add new contacts to a list, delete old contacts, update existing contacts, search for a contact, and display the entire contact list. All of these will be implemented as member functions. In addition you will have functions that will give you size of the list, get particular contact at a particular index location and any other necessary functions. Write a driver program that will be a menu driven application. The program will create a single ContactBook which is empty in the main program. A menu will have the following options: 1) Add New Contact 2) Delete Old Contact 3) Display Contact Info (contact is identified by last name and first name) 4) Update Contact Info (contact is identified by last name and first name) 5) Display Entire Contact List 6) Exit

Explanation / Answer

package animat;

import java.util.Scanner;
public class ContactBook{
   static Scanner read=new Scanner(System.in);

   String firstname;
   String lastname;
   Scanner s = new Scanner(System.in);
   //Nested class for each entry

   public ContactBook(String f,String l)
   {
       this.firstname=f;
       this.lastname=l;
   }

   static class Entry{
       private String first;
       private String last;
       private String address;
       private String email;
       Entry(String first, String last, String address, String email){
           this.first = first;
           this.last = last;
           this.address = address;
           this.email = email;
       }
       Entry(){
           first = "";
           last = "";
           address = "";
           email = "";
       }
      
   }


   //Keeps track of how many entries are in the book
   private int entries = 0;
   Entry[] contents;
   public void initEntries(){
       contents = new Entry[10];
       for (int i = 0;i<contents.length;i++){ //Initializes an array of entries, then loops through to initialize each individual entry
           contents[i] = new Entry();
       }
   }
   public int getEntries(){
       return contents.length;
   }
   //Adds an entry to the book
   public void add(String first, String last, String address, String email){
       if (entries<contents.length){
           contents[entries] = new Entry(first, last, address, email);
           entries++;
       }
       else System.out.println("Error: book is full");
   }

   //Removes an entry from the book
   public void remove(int entry){
       if (entries>0){
           contents[entry] = new Entry();
           for (int i = 0;i<entries-entry;i++){
               if (entry+1==entries) contents[entry] = new Entry();
               else{
                   Entry temp = contents[entry+i];
                   contents[entry+i] = contents[entry+i+1]; //Removes an entry end moves each entry after it one backwards.
                   contents[entry+i+1] = temp;
               }
           }
           entries--;
       }
       else System.out.println("Error: book is empty.");
   }

   //Changes the values of an entry
   public void edit(String first, String last){
     
   int index=   this.SearchContact(first, last);
       if(index==-1)
       {
           System.out.println("not found");
       return;
       }
      
       System.out.println("enter new address");
       contents[index].address=read.next();
      
       System.out.println("enter new email");
       contents[index].email=read.next();

      
   }
   public void DisplayContactInfo(String firstname,String lastname)
   {
       int index=this.SearchContact(firstname, lastname);
       if(index==-1)
       {
           System.out.println("not found");
           return;
       }
       this.displaycontact(contents[index]);
     
   }
  
   private void displaycontact(Entry e)
   {
       System.out.println(e.first);
       System.out.println(e.last);
       System.out.println(e.address);
       System.out.println(e.email);

   }

   public void DisplayList()
   {
       for(int i=0;i<contents.length;i++)
       {
           displaycontact(contents[i]);
       }
   }
  
   //returns index if contact found else -1;
   private int SearchContact(String firstname,String lastname)
   {
      
       for(int i=0;i<contents.length;i++)
       {
           if(contents[i].first.equals(firstname)&&contents[i].last.equals(lastname))
           {         

return i;
           }
       }
     
  
     
   return -1;
   }
   public static void main(String args[])
   {


   ContactBook book=new ContactBook("John","kenedy");

   System.out.println(" 1) Add New Contact 2) Delete Old Contact 3) Display Contact Info 4) Update Contact Info 5) Display Entire Contact List 6) Exit)");
   int option;
  
   option=read.nextInt();

   switch (option) {
   case 1: //first read details of new contact and then add using book.add();
       break;
   case 2: //read the index and then delete using book.remove();
       break;
   case 3: //first read details of contact and then display using book.displayContact();;
   break;
   case 4: ;
   break;
   case 5: ;
   break;
   case 6: ;
   break;
   default: System.out.println( "Invalid option");
   break;
   }
   }

}

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