Science
Write a program that simulates an address book.

Question Description

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

Final 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:\n 1 - Add Address \n 2 - Search \n 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: \n "+
        "1. First Name \n "+
        "2. Last Name \n "+
        "3. Street Address\n 4. City, State \n "+

        "5. Zip Code) \n ");
    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;
  }
 
}


khakaan (1456)
Rice University

Anonymous
I was on a very tight deadline but thanks to Studypool I was able to deliver my assignment on time.

Anonymous
The tutor was pretty knowledgeable, efficient and polite. Great service!

Anonymous
I did not know how to approach this question, Studypool helped me a lot.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors