AddressBook class lab assignment
From WLCS
AddressBook class
- The objective of this assignment will be to design and implement an address book
- Your address book will allow you to add, remove, and search for contacts
- The first version of our address book will use a Contact array, but future versions of AddressBook will not
Attributes
- a Contact array named myContacts
- a size variable to keep track of the size
- a MAX_SIZE variable to set the maximum size
Constructors
- a default constructor
- assign MAX_SIZE to 20
- assign the Contact array to a new Contact array of MAX_SIZE elements
- set the size variable to 0
Methods
- int getSize()
- Returns the number of Contacts that have already been added
- boolean add(Contact newContact)
- check if the current size of the AddressBook is greater than or equal to the maximum size
- return false if the AddressBook is full
- insert the new Contact in the next free element at the end of the Contacts array (myContacts[size] = newContact)
- increment size
- return true because the new Contact was successfully added
- check if the current size of the AddressBook is greater than or equal to the maximum size
- Contact remove(String fn, String ln)
- Using a for loop, find the Contact that matches fn and ln, and save it in a variable
- In order to check if two strings match, you have to use the .equals() method
- Example: str1.equals(str2)
- Once you have found the Contact, you should remember its index (location) in the array
- Check if the Contact was found in the array
- If it was found, you must use a loop to shift all the array elements down one index
- HINT: myContacts[i] = myContacts[i+1] //where i is a loop counter
- Be sure to set the last element to null (so that there isn't a duplicate Contact in the end)
- HINT: myContacts[size-1] = null;
- Decrement size
- Return the removed Contact
- Using a for loop, find the Contact that matches fn and ln, and save it in a variable
- void print()
- traverse the Contact array and print out the entire AddressBook (Use a loop!)
- Contact find(String fn, String ln)
- traverse the Contact array and find the Contact with the matching first name and last name
- HINT: Use a loop and if statement
- return the Contact with the matching first name and last name
- To match the first name parameter with the Contact's firstname, you'll need to do something like this: fn.equals(myContacts[i].getFirstName())
- return null if the Contact is not found
- traverse the Contact array and find the Contact with the matching first name and last name
AddressBook Main
- Create a new file named AddressBookMain
- Create a new main method to test your AddressBook
- Your AddressBook demo should use a menu system (see AddressBook Menu System below)
- Print out a message that explains the program (e.g. "Welcome to YOUR_NAME's address book!")
- Print out a menu with the following options:
(a)dd to address book (r)emove from address book (f)ind a contact (p)rint address book (q)uit What would you like to do?
- When the user selects to ADD a Contact, you should prompt them for a first name, last name, phone, and address.
- You should then create a new Contact
- Add the Contact to your AddressBook using the AddressBook's add() method
- When the user selects to REMOVE a Contact, you should prompt them for a first name and last name.
- Remove the Contact to your AddressBook using the AddressBook's remove() method
- When the user selects to FIND a Contact, you should prompt them for a first name and last name.
- Use the AddressBook's find() method to obtain the Contact.
- Make sure that the Contact found is not null and then print it out
- The PRINT menu choice should print the entire AddressBook using the print() method
- Make sure your menu runs again after the user makes a choice (HINT: use a loop somewhere)
- The QUIT menu choice should exit the loop (HINT: use break)