Write a program that simulates an address book. Driver main method should be as
ID: 3549247 • Letter: W
Question
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");
}
}
25%
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.
Explanation / Answer
________________________________________________________________________________________________
********************************************* LastFirstChapter10 .java ****************************************************
________________________________________________________________________________________________
import java.io.IOException;
import java.util.ArrayList;
public class LastFirstChapter10
// Replace LastFirst with your Last Name and First Name
{
public static void main(String[] args) throws NumberFormatException, IOException
{
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
{
LiFiAddressBook newEntry=new LiFiAddressBook();
aBook.add(newEntry);//Adding new entry into the array list
LiFiAddressBook.addEntry(newEntry);
System.out.println();
}
int foundIndex = LiFiAddressBook.search(aBook);
System.out.println();
if (foundIndex > -1)
aBook.get(foundIndex).display();
else
System.out.println("No Entry Found");
}
}
________________________________________________________________________________________________
********************************************* LiFiAddressBook .java *******************************************************
________________________________________________________________________________________________
import java.io.*;
import java.util.ArrayList;
public class LiFiAddressBook
{
String FirstName;
String LastName;
String StreetAddress;
String CityState;
String ZipCode;
public static void addEntry(LiFiAddressBook entry) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//Get the first name as a input and store it in the entry object
System.out.print("Please Enter First Name: ");
entry.FirstName=br.readLine();
//Get the last name as a input and store it in the entry object
System.out.print("Please Enter Last Name: ");
entry.LastName=br.readLine();
//Get the address as a input and store it in the entry object
System.out.print("Please Enter Street Address:");
entry.StreetAddress=br.readLine();
//Get the city,state as a input and store it in the entry object
System.out.print("Please Enter City, State: ");
entry.CityState=br.readLine();
//Get the zip code as a input and store it in the entry object
System.out.print("Please Enter Zip Code: ");
entry.ZipCode=br.readLine();
}
public static int search(ArrayList<LiFiAddressBook> aBook) throws NumberFormatException, IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("1. First Name");
System.out.println("2. Last Name");
System.out.println("3. Street Address");
System.out.println("4. City, State");
System.out.println("5. Zip Code");
System.out.print(" Please Enter Field to Search: ");
int choice=Integer.parseInt(br.readLine());
System.out.print(" Please enter value to search for: ");
String value=br.readLine();
//The array list is iterated through to search if the given value matches the field of any of the entries in the list
for(LiFiAddressBook e:aBook)
{
switch(choice){
case 1:
if(value.equalsIgnoreCase(e.FirstName))
return aBook.indexOf(e);
break;
case 2:
if(value.equalsIgnoreCase(e.LastName))
return aBook.indexOf(e);
break;
case 3:
if(value.equalsIgnoreCase(e.StreetAddress))
return aBook.indexOf(e);
break;
case 4:
if(value.equalsIgnoreCase(e.CityState))
return aBook.indexOf(e);
break;
case 5:
if(value.equalsIgnoreCase(e.ZipCode))
return aBook.indexOf(e);
break;
}
}
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("City, State: "+this.CityState);
System.out.println("Zip Code: "+this.ZipCode);
}
}
pls rate first with 5 star first otherwise i wont get points, thanks alot
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.