Programming Assignment 2

A Simple Address Book

Objective

To develop a deeper understanding of classes and objects.

Requirements

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:

Class Diagram

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.

Flowchart

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:

--> upload Invalid command.

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 ---> Carl Farrell PHONE --> 808-544-0813 Added [Carl Farrell; 808-544-0813]

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’s name or phone number. The application ignores letter case when performing this search. After displaying all matching contacts, the application displays the number of contacts found. The following demonstrates this interaction:

--> find WHAT ---> 808 [Carl Farrell; 808-544-0813] [Curt Powley; 808-544-1145] Number of contacts found: 2

Printing the Address Book

If the user enters the command print, the application prints a list of all contacts in the address book, followed by the number of contacts it printed. The following demonstrates this interaction:

--> print [Jade Cheng; 206-5059] [Carl Farrell; 808-544-0813] [Curt Powley; 808-544-1145] Number of contacts: 3

Quitting

When the user enters the command quit, the application terminates without displaying any additional messages. The following demonstrates this interaction:

--> quit

Examples

Output

$ java ChengJade2 ~ Address Book Menu ~ +-------------------------+ ADD Adds a contact FIND Finds contacts PRINT Prints contacts QUIT Quits +-------------------------+ --> add NAME ---> Jade Cheng PHONE --> 206-5059 Added [Jade Cheng; 206-5059] ~ Address Book Menu ~ +-------------------------+ ADD Adds a contact FIND Finds contacts PRINT Prints contacts QUIT Quits +-------------------------+ --> add NAME ---> Carl Farrell PHONE --> 808-544-0813 Added [Carl Farrell; 808-544-0813] ~ Address Book Menu ~ +-------------------------+ ADD Adds a contact FIND Finds contacts PRINT Prints contacts QUIT Quits +-------------------------+ --> add NAME ---> Curt Powley PHONE --> 808-544-1145 Added [Curt Powley; 808-544-1145] ~ Address Book Menu ~ +-------------------------+ ADD Adds a contact FIND Finds contacts PRINT Prints contacts QUIT Quits +-------------------------+ --> print [Jade Cheng; 206-5059] [Carl Farrell; 808-544-0813] [Curt Powley; 808-544-1145] Number of contacts: 3 ~ Address Book Menu ~ +-------------------------+ ADD Adds a contact FIND Finds contacts PRINT Prints contacts QUIT Quits +-------------------------+ --> find WHAT --> pow [Curt Powley; 808-544-1145] Number of contacts found: 1 ~ Address Book Menu ~ +-------------------------+ ADD Adds a contact FIND Finds contacts PRINT Prints contacts QUIT Quits +-------------------------+ --> find WHAT --> 808 [Carl Farrell; 808-544-0813] [Curt Powley; 808-544-1145] Number of contacts found: 2 ~ Address Book Menu ~ +-------------------------+ ADD Adds a contact FIND Finds contacts PRINT Prints contacts QUIT Quits +-------------------------+ --> quit

Resources