Here is the assignment- Write a program that uses an ArrayList of parameter type
ID: 3534722 • Letter: H
Question
Here is the assignment-
Write a program that uses an ArrayList of parameter type Contact to store a database of contacts. The Contact class should store the contact's first and last name, phone number, and email address. Add appropriate accessor and mutator methods. Your database program should present a menu that allows the user add a contact, display all contacts, search for a specific contact and display it, or search for a specific contact and give the user the option to delete it. The searches should find any contact where an instance variable contains a target search string. For example, if "elmore" is the search target then any contact where the first name, last name, phone number, or email address contains "elmore" should be returned for display or deletion. Use a "for each" loop to iterate through the ArrayList.
The portion I need help with: Figuring out how to search for the name to display, and the name to delete, as well as how I should go about in deleting the contact.
Here is my code-
public class Contact
{
String firstName;
String lastName;
String phoneNumber;
String email;
public Contact()
{
this.firstName = "";
this.lastName = "";
this.phoneNumber = "";
this.email = "";
}
public Contact(String fName, String lName, String phone, String email)
{
this.firstName = fName;
this.lastName = lName;
this.phoneNumber = phone;
this.email = email;
}
public void setFirstName(String fName)
{
this.firstName = fName;
}
public void setLastName(String lName)
{
this.lastName = lName;
}
public void setNumber(String phone)
{
this.phoneNumber = phone;
}
public void setEmail(String email)
{
this.email = email;
}
public String getFirstName()
{
return this.firstName;
}
public String getLastName()
{
return this.lastName;
}
public String getNumber()
{
return this.phoneNumber;
}
public String getEmail()
{
return this.email;
}
}
public static void main(String args[])
{
ArrayList<Contact> Book = new ArrayList<Contact>();
while(true)
{
System.out.println("Contact book menu");
System.out.println("-------------------------------------------");
System.out.println("Press 1: Add a new contact.");
System.out.println("Press 2: Display all contacts.");
System.out.println("Press 3: Seach contacts.");
System.out.println("Press 4: Delete a contact.");
System.out.println("Press 5: Exit menu.");
Scanner keyboard = new Scanner(System.in);
int input = keyboard.nextInt();
switch(input)
{
case 1: Contact A = new Contact();
System.out.println("Enter the first name: ");
A.firstName = keyboard.next();
System.out.println("Enter the last name: ");
A.lastName = keyboard.next();
System.out.println("Enter the phone number: ");
A.phoneNumber = keyboard.next();
System.out.println("Enter the e-mail address: "); A.email = keyboard.next();
break;
case 2: for(Contact A: Book)
{
System.out.println("Name: " + A.firstName + " " + A.lastName + " Phone: " + A.phoneNumber + " E-mail: " A.email);
break;
case 3: System.out.println("Enter the contact name you wish to search for: ");
String str = keyboard.next();
for (Contact A : Book)
{
if (A.firstName.indexOf(str) != -1)
{
System.out.println(A);
}
else if(A.lastName.indexOf(str) != -1)
{
System.out.println(A);
}
else if(A.email.indexOf(str) != -1)
{
System.out.println(A);
}
else
{
System.out.println("No results.");
}
}
break;
case 4: System.out.println("Enter the contact name you wish to delete: ");
for (Contact A : Book)
{
if (A.firstName.indexOf(str) != -1)
{
System.out.println(A);
}
else if(A.lastName.indexOf(str) != -1)
{
System.out.println(A);
}
else if(A.email.indexOf(str) != -1)
{
System.out.println(A);
}
else
{
System.out.println("No results.");
}
System.out.println("Are you sure you want to delete? (y/n)");
String deletion = keyboard.next();
if(deletion == "y")
{
Book.remove(A);
}
else
{
System.out.println("The contact(s) was not removed.");
}
}
break;
case 5: break;
}
}
Explanation / Answer
//here is the modified version of your code
import java.util.ArrayList;
import java.util.Scanner;
public class Contact
{
String firstName;
String lastName;
String phoneNumber;
String email;
public Contact()
{
this.firstName = "";
this.lastName = "";
this.phoneNumber = "";
this.email = "";
}
public Contact(String fName, String lName, String phone, String email)
{
this.firstName = fName;
this.lastName = lName;
this.phoneNumber = phone;
this.email = email;
}
public void setFirstName(String fName)
{
this.firstName = fName;
}
public void setLastName(String lName)
{
this.lastName = lName;
}
public void setNumber(String phone)
{
this.phoneNumber = phone;
}
public void setEmail(String email)
{
this.email = email;
}
public String getFirstName()
{
return this.firstName;
}
public String getLastName()
{
return this.lastName;
}
public String getNumber()
{
return this.phoneNumber;
}
public String getEmail()
{
return this.email;
}
public String toString()
{
String str="Name: " + firstName + " " + lastName + " Phone: " + phoneNumber + " E-mail: " +email;
return str;
}
public static void main(String args[])
{
ArrayList<Contact> Book = new ArrayList<Contact>();
while(true)
{
System.out.println("Contact book menu");
System.out.println("-------------------------------------------");
System.out.println("Press 1: Add a new contact.");
System.out.println("Press 2: Display all contacts.");
System.out.println("Press 3: Seach contacts.");
System.out.println("Press 4: Delete a contact.");
System.out.println("Press 5: Exit menu.");
Scanner keyboard = new Scanner(System.in);
int input = keyboard.nextInt();
if(input==5)
break;
switch(input)
{
case 1:
Contact B = new Contact();
System.out.println("Enter the first name: ");
B.firstName = keyboard.next();
System.out.println("Enter the last name: ");
B.lastName = keyboard.next();
System.out.println("Enter the phone number: ");
B.phoneNumber = keyboard.next();
System.out.println("Enter the e-mail address: ");
B.email = keyboard.next();
Book.add(B);
break;
case 2:
for(Contact A: Book)
{
System.out.println("Name: " + A.firstName + " " + A.lastName + " Phone: " + A.phoneNumber + " E-mail: " +A.email);
}
break;
case 3:
System.out.println("Enter the contact name or phone number or email address of person you wish to search for: ");
String str = keyboard.next();
int found=0;
for (Contact A : Book)
{
if (A.firstName.indexOf(str) != -1)
{
System.out.println(A);
found=1;
break;
}
else if(A.lastName.indexOf(str) != -1)
{
System.out.println(A);
found=1;
break;
}
else if(A.email.indexOf(str) != -1)
{
System.out.println(A);
found=1;
break;
}
else if(A.phoneNumber.indexOf(str) != -1)
{
System.out.println(A);
found=1;
break;
}
}
if(found==0)
System.out.println("No results.");
break;
case 4:
System.out.println("Enter the contact name or phone number or email address of person you wish to delete: ");
str = keyboard.next();
found=0;
for ( Contact A : Book)
{
if (A.firstName.indexOf(str) != -1)
{
System.out.println(A);
found=1;
}
else if(A.lastName.indexOf(str) != -1)
{
System.out.println(A);
found=1;
}
else if(A.email.indexOf(str) != -1)
{
System.out.println(A);
found=1;
}
else if(A.phoneNumber.indexOf(str) != -1)
{
System.out.println(A);
found=1;
}
if(found==1)
{
System.out.println("Are you sure you want to delete? (y/n)");
String deletion = keyboard.next();
if(deletion.compareToIgnoreCase("y")==0)
{
Book.remove(A);
}
else
{
System.out.println("The contact(s) was not removed.");
}
break;
}
}
if(found==0)
System.out.println("No results.");
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.