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

Write a program that simulates an address book. This program cannot use .IOExcep

ID: 3561008 • Letter: W

Question

Write a program that simulates an address book.

This program cannot use .IOException or Throws

Driver main method should be as shown below. Add comments to explain functionality

import java.util.ArrayList;
public class LastFirstChapter10
//Replace LastFirst with your Last Name and First Name {

     public static void main(String [] args)      {

ArrayList<LiFiAddressBook> aBook = new ArrayList<LiFiAddressBook>(); //Replace LiFi with Last Initial First Initial (for all instances) for (int count = 0; count < 1; count++) //change 1 to add more than 1 {

            //****************************             //add code here to add new entry to ArrayList             //call addEntry             //print blank line             //****************************

}
int foundIndex = LiFiAddressBook.search(aBook); System.out.println();
if (foundIndex > -1)

            aBook.get(foundIndex).display();       else

} }

System.out.println("No Entry Found");

LiFiAddressBook.java class

Instance variables:

First Name (string)

Last Name (string)

Street Address (string)

City State (string)

Zip Code (string)

addEntry method:
Get input for variables above. See sample in example at bottom.

search method:
Receive ArrayList as argument
Output Search Menu (see example at bottom)
Utilize a switch and search ArrayList for field specified. Return index number if entry found or -1 if not found

display method:
Print results as shown in example at bottom.

Sample Session:

Please Enter First Name: Ken
Please Enter Last Name: Dewey
Please Enter Street Address: 6420 SE 15th St.

Please Enter City, State: Midwest City, OK

Please Enter Zip Code: 73110

Please Enter First Name: Nick
Please Enter Last Name: Dewey
Please Enter Street Address: 3232 Longridge Rd.

Please Enter City, State: Del City, OK
Please Enter Zip Code: 73115

Please Enter First Name: Steve
Please Enter Last Name: Jones
Please Enter Street Address: 1205 Waynes Place

Please Enter City, State: Moore, OK
Please Enter Zip Code: 73024

Search Menu;
1. First Name
2. Last Name
3. Street Address 4. City, State

5. Zip Code

Please Enter Field to Search: 1

Please enter value to search for: Nick

First Name: Nick
Last Name: Dewey
Street Address: 3232 Longridge Rd. City, State: Del City, OK
Zip Code: 73115

If the item is not found:

No Entry Found

Explanation / Answer

import java.util.ArrayList;
import java.util.Scanner;

public class LastFirstChapter10

//Replace LastFirst with your Last Name and First Name

{

public static void main(String [] args)

{

ArrayList<LiFiAddressBook> aBook = new ArrayList<LiFiAddressBook>();

//Replace LiFi with Last Initial First Initial (for all instances)
String searchStr = null;
int field = 0;
Scanner recorder = new Scanner(System.in);

System.out.println("Enter the option: 1 - Add Address 2 - Search 99 - Quit ");
int option = 0; ;
while (option != 3) //change 1 to add more than 1

{
   System.out.println("Enter Your option: ");
   option = recorder.nextInt();
  
   switch(option) {
   case 1:
       LiFiAddressBook ab = new LiFiAddressBook();
       System.out.println("Enter Address details ");
       System.out.println("First Name:   ");
       ab.setFirstName(recorder.next());
       System.out.println("Last Name:   ");
       ab.setLastName(recorder.next());
       System.out.println("Street Address:   ");
       ab.setStreetAddress(recorder.next());
       System.out.println("City, State: ");
       ab.setCityState(recorder.next());
       System.out.println("Zip Code:   ");
       ab.setZipCode(recorder.next());
       aBook.add(ab);
   case 2:
       System.out.println("Enter Search Option: "+
               "1. First Name "+
               "2. Last Name "+
               "3. Street Address 4. City, State "+

               "5. Zip Code) ");
       option = recorder.nextInt();
       System.out.println("Enter your search string: ");
       searchStr = recorder.next();
      
       int bIndex = LiFiAddressBook.search(aBook, searchStr, field);
       if(bIndex == -1)
           System.out.println("No entry found");
       else {

System.out.println("An entry is found with the search"):
           LiFiAddressBook abc = aBook.get(bIndex);
           abc.display();
       }
   case 99:
       System.exit(0);
          
   }


}

}

}

class LiFiAddressBook {
   private String firstName;

   private String lastName;

   private String streetAddress;

   private String cityState;

   private String zipCode;
  
   public LiFiAddressBook() {
      
   }

   public LiFiAddressBook(String firstName,String lastName, String streetAddress,
           String cityState, String zipCode ) {
       this.firstName = firstName;
       this.lastName = lastName;
       this.streetAddress = streetAddress;
       this.cityState = cityState;
       this.zipCode = zipCode;
      
   }
  
   public static int search(ArrayList<LiFiAddressBook> aBook, String searchStr, int field) {
      
       if(aBook != null) {
           boolean isFound = false;
           for(int i=0; i < aBook.size(); i++) {
               LiFiAddressBook ab = aBook.get(i);
               if(field == 1)
                   isFound = ab.getFirstName().equals(searchStr.trim());
               if(field == 2)
                   isFound = ab.getLastName().equals(searchStr.trim());
               if(field == 3)
                   isFound = ab.getStreetAddress().equals(searchStr.trim());
               if(field == 4)
                   isFound = ab.getCityState().equals(searchStr.trim());
               if(field == 5)
                   isFound = ab.getZipCode().equals(searchStr.trim());
              
               if(isFound)
                   return i;
           }
       }
       return -1;
      
   }
  
   public void display() {
       System.out.println("First Name:   "+this.firstName);
       System.out.println("Last Name:   "+this.lastName);
       System.out.println("Street Address:   "+this.streetAddress);
       System.out.println("Zip Code:   "+this.zipCode);
       System.out.println("City, State:   "+this.cityState);
   }
  
   public String getFirstName() {
       return firstName;
   }

   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }

   public String getLastName() {
       return lastName;
   }

   public void setLastName(String lastName) {
       this.lastName = lastName;
   }

   public String getStreetAddress() {
       return streetAddress;
   }

   public void setStreetAddress(String streetAddress) {
       this.streetAddress = streetAddress;
   }

   public String getCityState() {
       return cityState;
   }

   public void setCityState(String cityState) {
       this.cityState = cityState;
   }

   public String getZipCode() {
       return zipCode;
   }

   public void setZipCode(String zipCode) {
       this.zipCode = zipCode;
   }
  
}

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