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()
    1. Returns the number of Contacts that have already been added
  • boolean add(Contact newContact)
    1. check if the current size of the AddressBook is greater than or equal to the maximum size
      • return false if the AddressBook is full
    2. insert the new Contact in the next free element at the end of the Contacts array (myContacts[size] = newContact)
    3. increment size
    4. return true because the new Contact was successfully added
  • Contact remove(String fn, String ln)
    1. 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)
    2. Once you have found the Contact, you should remember its index (location) in the array
    3. Check if the Contact was found in the array
      1. If it was found, you must use a loop to shift all the array elements down one index
      2. HINT: myContacts[i] = myContacts[i+1] //where i is a loop counter
    4. 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;
    5. Decrement size
    6. Return the removed Contact
  • void print()
    1. traverse the Contact array and print out the entire AddressBook (Use a loop!)
  • Contact find(String fn, String ln)
    1. traverse the Contact array and find the Contact with the matching first name and last name
      • HINT: Use a loop and if statement
    2. 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())
    3. return null if the Contact is not found

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)
    1. Print out a message that explains the program (e.g. "Welcome to YOUR_NAME's address book!")
    2. 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.
    1. You should then create a new Contact
    2. 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.
    1. 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.
    1. Use the AddressBook's find() method to obtain the Contact.
    2. 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)