Write a program that simulates an address book.

Physics
Tutor: None Selected Time limit: 1 Day

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

Sep 11th, 2014

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


Sep 11th, 2014

Did you know? You can earn $20 for every friend you invite to Studypool!
Click here to
Refer a Friend
...
Sep 11th, 2014
...
Sep 11th, 2014
Dec 3rd, 2016
check_circle
Mark as Final Answer
check_circle
Unmark as Final Answer
check_circle
Final Answer

Secure Information

Content will be erased after question is completed.

check_circle
Final Answer