SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Generics And The Arraylist Class
Exercise:
Programming Projects
Question:2 | ISBN:9780132830317 | Edition: 5

Question

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 to 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 any 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 the “for-each” loop to iterate through the ArrayList .


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

//Contact.java
public class Contact 
{
	private String fName;
	private String lName;
	private String phNum;
	private String emailAdd;

	public Contact(String fn, String ln, String pn, String e)
	{
		fName = fn;
		lName = ln;
		phNum = pn;
		emailAdd = e;
	}
	public void setfName(String newFirst)
	{
		fName = newFirst;
	}
	public void setlName(String newLast)
	{
		lName = newLast;
	}
	public void setphNum(String newNumber)
	{
		phNum = newNumber;
	}
	public void setemailAdd(String newemailAdd)
	{
		emailAdd = newemailAdd;
	}
	public String getfName()
	{
		return fName;
	}
	public String getlName()
	{
		return lName;
	}
	public String getphNum()
	{
		return phNum;
	}
	public String getemailAdd()
	{
		return emailAdd;
	}
}

 

//ContactsTest.java for run program
import java.util.*;
public class ContactsTest
{
	public static void main(String args[])
	{

		ArrayList<Contact> contacts = 
				new ArrayList<Contact>();
		Scanner in = new Scanner(System.in);
		while(true)
		{

			
			System.out.println("1)Add a new contact");
			System.out.println("2)Display all contacts");
			System.out.println("3)Display specific contact");
			System.out.println("4)Delete a contact");
			System.out.println("-1) Quit");
			System.out.print("Enter Your choice: ");

					int choice = in.nextInt();
					in.nextLine();
					switch(choice)
					{

					case 1:
						System.out.print("Enter first name: ");
						String fn = in.nextLine();
						System.out.print("Enter last name: ");
						String ln = in.nextLine();
				System.out.print("Enter phone number: ");
						String pn = in.nextLine();
						System.out.print("Enter Email address: ");
						String ea = in.nextLine();
						contacts.add(new Contact(fn, ln, pn, ea));
						break;
					case 2:
						for(int i = 0; i < contacts.size(); i++)
						{
							Contact c = contacts.get(i);
					System.out.print(c.getlName() + "  " + c.getfName() + "; ");
					System.out.print("Phone number: " + c.getphNum());
					System.out.print("; Email: " + 	c.getemailAdd());
							System.out.println();
						}
						break;
					case 3:
						System.out.println("Enter an information piece about person:");
						String info = in.nextLine();
						Contact c = findPerson(contacts, info);	
						if(c != null)
						{
				System.out.print(c.getlName() + ", " + c.getfName() + "; ");
				System.out.print("Phone number: " + c.getphNum());
				System.out.print("; Email: " + c.getemailAdd());
							System.out.println();
						}
						else
							System.out.println("Contact not found.");

						break;

					case 4:
						System.out.println("Enter an information piece about person:");
									String someInfo = in.nextLine();
						Contact contact = 	findPerson(contacts, someInfo);
						if(contact != null)
						{
			System.out.print(contact.getlName() + ", " + contact.getfName() + "; 				");
					System.out.print("Phone number: " + 	contact.getphNum());
					System.out.print("; Email: " + 		contact.getemailAdd());
							System.out.println();
					System.out.println("Are you sure to delete contact? (Y/N)");
									String delete = in.nextLine();
									if(delete.equalsIgnoreCase("y"))
									{
										contacts.remove(contact);
							System.out.println("Contact deleted successfully.");
									}
						}
						else
							System.out.println("Contact not found.");

						break;
					}

					if(choice == -1)
						break;

					System.out.println("___________________________________________");
		}

	}

	private static Contact findPerson(ArrayList<Contact> contacts, String info)
	{
		Contact c = null;
		for(int i = 0; i < contacts.size(); i++)
		{
			if(contacts.get(i).getfName().
					indexOf(info) != -1)
				c = contacts.get(i);

			if(contacts.get(i).getlName().
					indexOf(info) != -1)
				c = contacts.get(i);

			if(contacts.get(i).getphNum().
					indexOf(info) != -1)
				c = contacts.get(i);

			if(contacts.get(i).getemailAdd().
					indexOf(info) != -1)
				c = contacts.get(i);
		}
		return c;
	}
}

OUTPUT:

1)Add a new contact
2)Display all contacts
3)Display specific contact
4)Delete a contact
-1) Quit
Enter Your choice: 1
Enter first name: Don
Enter last name: Tom
Enter phone number: 123-456-7890
Enter Email address: DonTom@gmail.com
___________________________________________
1)Add a new contact
2)Display all contacts
3)Display specific contact
4)Delete a contact
-1) Quit
Enter Your choice: 1
Enter first name: Josh
Enter last name: Krush
Enter phone number: 980-765-54321
Enter Email address: JoshKrush@yahoo.com
___________________________________________
1)Add a new contact
2)Display all contacts
3)Display specific contact
4)Delete a contact
-1) Quit
Enter Your choice: 2
Tom  Don; Phone number: 123-456-7890; Email: DonTom@gmail.com
Krush  Josh; Phone number: 980-765-54321; Email: JoshKrush@yahoo.com
___________________________________________
1)Add a new contact
2)Display all contacts
3)Display specific contact
4)Delete a contact
-1) Quit
Enter Your choice: 3
Enter an information piece about person:
Josh
Krush, Josh; Phone number: 980-765-54321; Email: JoshKrush@yahoo.com
___________________________________________
1)Add a new contact
2)Display all contacts
3)Display specific contact
4)Delete a contact
-1) Quit
Enter Your choice: 4
Enter an information piece about person:
Josh
Krush, Josh; 				Phone number: 980-765-54321; Email: JoshKrush@yahoo.com
Are you sure to delete contact? (Y/N)
y
Contact deleted successfully.
___________________________________________
1)Add a new contact
2)Display all contacts
3)Display specific contact
4)Delete a contact
-1) Quit
Enter Your choice: 2
Tom  Don; Phone number: 123-456-7890; Email: DonTom@gmail.com
___________________________________________
1)Add a new contact
2)Display all contacts
3)Display specific contact
4)Delete a contact
-1) Quit
Enter Your choice: -1

 

0 0

Discussions

Post the discussion to improve the above solution.