Programming: Address book You will implement a Java console application that man
ID: 3564056 • Letter: P
Question
Programming: Address book
You will implement a Java console application that manages a simple address book. The address book consists of a list of contacts, and it provides methods to add, find, and print those contacts. Each contact consists of only a name and a phone number.
The address book initially contains no contacts, and the application never saves or loads any data to or from permanent storage.
You must implement the address book as a Java class named AddressBook, and you must implement individual contact information as a Java class named Contact. The AddressBook should contain a collection of Contact instances. The following class diagram depicts this relationship:
Address Book
-contacts: List
-----------------------------
+add (c: Contact) : void
+find (s: String): List < Contact>
+toString (): String
Contact
--------
-name: String
-phone: String
--------------
+to String (): String
The application operates in a loop. For each iteration, the application first shows a menu of available commands, then prompts the user to enter a command, and then finally processes that user-supplied command
Start > show menu > prompt user > quit? >end ( repeat)
The menu of available commands and the prompt presented to the user appear as follows:
~ Address Book Menu ~
+-------------------------+
ADD Adds a contact
FIND Finds contacts
PRINT Prints contacts
QUIT Quits
+-------------------------+
-->
The user may enter one of the following commands: add, find, print, or quit. The application ignores letter case when determining the command. If the user enters an invalid command, the application responds by writing "Invalid command." and looping. For example, if the user enters "upload", the application responds as follows:
Adding Contacts
If the user enters the command add, the application prompts the user for a name and a phone number. The user may enter any non-empty strings for these values. After the user enters the two values, the application displays a message indicating the successful addition of the contact to the address book. The following demonstrates this interaction:
--> add
NAME ---> Henry Ford
PHONE --> 808-544-0862
Added [Heny Ford; 808-544-0862]
Finding Contacts
If the user enters the command find, the application prompts the user for text it will use to search the address book. The user may enter any non-empty string for this value. After the user enters the text, the application displays a list of all contacts that contain that text in either the contact
Explanation / Answer
import java.util.ArrayList;
import java.util.List;
public class AddressBook {
private List<Contact> contacts;
public AddressBook(){
contacts = new ArrayList<Contact>(); // Initialize list in constructor
}
public void add(Contact c){
contacts.add(c);
}
public List<Contact> find(String s){
List<Contact> list = new ArrayList<Contact>();
for(Contact x : contacts){
if(x.toString().toLowerCase().contains(s)){ // to ignore case
list.add(x);
}
}
return list;
}
public String toString(){
String ret = "";
for(Contact c : contacts){
ret = ret+c+" ";
}
return ret;
}
}
public class Contact {
private String name;
private String phone;
public void setName(String name) {
this.name = name;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String toString(){
return "["+name+"; "+phone+"]";
}
}
import java.util.List;
import java.util.Scanner;
public class AddressBookDriver {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
AddressBook addressBook = new AddressBook();
String option;
do{
System.out.println("~Address Book Menu~");
System.out.println("+-----------------+");
System.out.println("ADD Adds a Contact");
System.out.println("FIND Finds Contacts");
System.out.println("PRINT Prints Contacts");
System.out.println("QUITS Quits");
System.out.print("-->");
option = in.nextLine();
option = option.toUpperCase(); // to ingnore case and make work in switch
switch (option) {
case "ADD":
Contact c = new Contact();
System.out.print("NAME--> ");
String name = in.nextLine();
c.setName(name);
System.out.print("PHONE--> ");
String phone = in.nextLine();
c.setPhone(phone);
addressBook.add(c);
System.out.println("Added: "+c);
break;
case "FIND":
System.out.print("WHAT--> ");
String f = in.nextLine().toLowerCase(); // to ingnore case
List<Contact> list = addressBook.find(f);
for(Contact x: list){
System.out.println(x);
}
break;
case "PRINT":
System.out.println(addressBook.toString());
break;
case "QUIT":
break;
default:
System.out.println("Invalid command, try again");
System.out.println(); // blank line
break;
}
}
while(!option.equalsIgnoreCase("QUIT"));
}
}
************************************OUTPUT****************************
~Address Book Menu~
+-----------------+
ADD Adds a Contact
FIND Finds Contacts
PRINT Prints Contacts
QUITS Quits
-->upload
Invalid command, try again
~Address Book Menu~
+-----------------+
ADD Adds a Contact
FIND Finds Contacts
PRINT Prints Contacts
QUITS Quits
-->add
NAME--> Henry Ford
PHONE--> 808-544-0862
Added: [Henry Ford; 808-544-0862]
~Address Book Menu~
+-----------------+
ADD Adds a Contact
FIND Finds Contacts
PRINT Prints Contacts
QUITS Quits
-->find
WHAT--> 808
[Henry Ford; 808-544-0862]
~Address Book Menu~
+-----------------+
ADD Adds a Contact
FIND Finds Contacts
PRINT Prints Contacts
QUITS Quits
-->print
[Henry Ford; 808-544-0862]
~Address Book Menu~
+-----------------+
ADD Adds a Contact
FIND Finds Contacts
PRINT Prints Contacts
QUITS Quits
-->quit
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.