IMPORTANT Note ****import java.io.IOException, import java.io.*, throws and Buff
ID: 3549324 • Letter: I
Question
IMPORTANT Note ****import java.io.IOException, import java.io.*, throws and BufferedReader cannot be used in program*****
Write a program that simulates an address book.
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 aBook = new ArrayList();
//Replace LiFi with Last Initial First Initial (for all instances)
for (int count = 0; count < 1; count++)
{
//****************************
//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");
}
}
Output should be as shown in example at bottom.
-----------------------------------------
LiFiAddressBook.java class
Instance variables:
First Name (string)
Last Name (integer)
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.
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
IMPORTANT Note ****import java.io.IOException, import java.io.*, throws and BufferedReader cannot be used in program*****
Explanation / Answer
Hi Please Find below the answer.
Follow are the two programs.
import java.util.ArrayList;
public class LastFirstChapter10
// Replace LastFirst with your Last Name and First Name
{
public static void main(String[] args)
{
ArrayList<LiFiAddressBook> lifi = new ArrayList<LiFiAddressBook>();
LiFiAddressBook abook = null;
for (int count =0;count <3;count ++)
{
abook = new LiFiAddressBook();
lifi.add(abook.addEntry());
}
int foundIndex = abook.search(lifi);
System.out.println();
if (foundIndex > -1)
lifi.get(foundIndex).display();
else
System.out.println("No Entry Found");
}
}
//*****//
import java.util.List;
import java.util.Scanner;
public class LiFiAddressBook {
String firstName;
String lastName;
String streetAddress;
String cityState;
String zipCode;
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;
}
LiFiAddressBook addEntry() {
LiFiAddressBook aBook = new LiFiAddressBook();
Scanner sc = new Scanner(System.in);
System.out.println("Please Enter First Name: ");
aBook.setFirstName(sc.nextLine());
System.out.println("Please Enter Last Name: ");
aBook.setLastName(sc.nextLine());
System.out.println("Please Enter Street Address: ");
aBook.setStreetAddress(sc.nextLine());
System.out.println("Please Enter City State: ");
aBook.setCityState(sc.nextLine());
System.out.println("Please Enter Zip Code: ");
aBook.setZipCode(sc.nextLine());
return aBook;
}
int search(List<LiFiAddressBook> adressBook) {
Scanner sc1 = new Scanner(System.in);
System.out.println("Please enter Field to Search");
System.out
.println(" Search Menu; 1. First Name 2. Last Name 3. Street Address 4. City, State 5. Zip Code ");
String menu = sc1.next();
System.out.println("Please enter Value to Search");
String value = sc1.next();
sc1.close();
int count = 0;
for (LiFiAddressBook address : adressBook) {
if (menu.equalsIgnoreCase("1"))
if (address.getFirstName().equalsIgnoreCase(value)) {
return count;
}
if (menu.equalsIgnoreCase("2"))
if (address.getLastName().equalsIgnoreCase(value)) {
return count;
}
if (menu.equalsIgnoreCase("3"))
if (address.getStreetAddress().equalsIgnoreCase(value)) {
return count;
}
if (menu.equalsIgnoreCase("4"))
if (address.getCityState().equalsIgnoreCase(value)) {
return count;
}
if (menu.equalsIgnoreCase("5"))
if (address.getZipCode().equalsIgnoreCase(value)) {
return count;
}
count++;
}
return -1;
}
void display() {
System.out.println("First Name: " + this.getFirstName());
System.out.println("Last Name: " + this.getLastName());
System.out.println("Street Address: " + this.getStreetAddress());
System.out.println("City State Name: " + this.getCityState());
System.out.println("Zip Code: " + this.getZipCode());
}
}
Output:
Please Enter First Name:
Ken
Please Enter Last Name:
Dewey
Please Enter Street Address:
6420 SE
Please Enter City State:
OK
Please Enter Zip Code:
73110
Please Enter First Name:
Nick
Please Enter Last Name:
Dewey
Please Enter Street Address:
3232 LRoad
Please Enter City State:
OK
Please Enter Zip Code:
73115
Please Enter First Name:
Steve
Please Enter Last Name:
Jones
Please Enter Street Address:
1205 Waynes
Please Enter City State:
ok
Please Enter Zip Code:
Moore
Please enter Field to Search
Search Menu;
1. First Name
2. Last Name
3. Street Address
4. City, State
5. Zip Code
1
Please enter Value to Search
Nick
First Name: Nick
Last Name: Dewey
Street Address: 3232 LRoad
City State Name: OK
Zip Code: 73115
Hope it helps.
Thanks