Contacts – Part 4
In this workbook, you will apply the Big 3 steps to the ContactManager
class
.
The contact manager is identified by the contacts that it manages. It also performs the actions of adding/removing contacts.
Task 1 – Adding the field
Add the contacts
field to the ContactManager
class
. The number of contacts can increase or decrease, so use an ArrayList
.
Task 2 – The Big 3
-
Constructor.
-
It receives no parameters.
-
It sets the field equal to a new object of the
ArrayList
class.
-
-
Getter:
getContact
-
It receives one parameter:
int index
. -
It returns an object at the requested
index
.
-
-
Setter:
setContact
-
It receives two parameters:
int index
,Contact contact
. -
It sets an element equal to a copy of the object being passed in.
-
As you're applying each step, careful not to fall into the reference trap.
Task 3 – addContact
.
-
This method is
void
-
Receives one parameter:
Contact contact
-
Adds a contact to the ArrayList.
Task 4 – removeContact
.
-
This method is
void
. -
Receives one parameter:
String contactName
. -
Removes the contact that matches the name passed in.
Task 5 – toString
.
-
Set a
String
variabletemp
that equals""
. -
Loop through every contact in the
contacts
field. -
During each run, add the
toString
of each contact. -
Then , add two new lines
\n\n
to temp. -
return temp;
Task 6 – Testing the code
-
Inside
main()
, create an object of theContactManager class
. -
Add three contacts to the
contacts
field. Use the following values:-
Ryan 6135012424 11/11/1992
-
Gio 6477092344 11/11/1993
-
Thomas 8192256979 11/11/1994
-
-
Print the
ContactManager
.
call removeContact
and remove "Gio".