I am taking an intro to programming class and our current assignment is to create a contact manager (essentially an address book) with three classes: name
(adds a first
, middle
, and last
to a contact
), contact
(generates the actual contact
itself), and ContactManager
(keeps an array of contacts
). I'm having trouble with one of the methods in the ContactManger
class which is supposed to check how many of the given contact
objects exist in the array and then returns the number of repeat contacts
.
Here is the ContactManager
class:
public class ContactManager
{
private int nContacts;
private Contact[] contacts;
public ContactManager (int nMaxContact)
{
this.nContacts = 0;
contacts = new Contact[nMaxContact];
}
/**
* Given 4 String parameters, add a contact to the contacts array. Before adding
* a contact to the array, do a parameter sanity check in this method.
* If the array is full, or if first name or last name is null or an empty string, do
* not add a contact but return false instead.
*
* Middle name can be left as null
* or an empty String. Note that we allow adding duplicate contacts.
*
* If the name is acceptable, create a new contact and add the phone number to
* the contact by calling the addPhoneNumber method of the Contact object. If
* the method returns false, do not add the contact to the array and return
* false (i.e., we discard the contact instantiated.) Otherwise, add the contact
* to the array and return true.
*
* @param fName
* @param lName
* @param mName
* @param phoneNumber
*
* @return a boolean value. See the description above
*/
public boolean addContact (String fName, String lName, String mName, String phoneNumber)
{
if (fName == null || fName == "" || lName == null || lName == "" || contacts[contacts.length - 1] != null)
{
return false;
}
Name name;
name = new Name(fName, lName, mName);
Contact entry;
entry = new Contact(name);
Contact phone = new Contact(name);
phone.addPhoneNumber(phoneNumber);
nContacts++;
return true;
}
/**
* Given a Contact object, return the number of contacts with the identical name
* (i.e., they have identical first, last, and middle name. If the input
* parameter is null, return -1.
*
* For example, if the contacts array contains 10 contacts and there are 3
* elements have the same name, return 3.
*
* @return an int value. See the description above.
*/
public int countEqualContacts (Contact c)
{
int equal = 0;
if (c == null)
{
equal = -1;
}
for (int i = 0; i < contacts.length; i++)
{
if (Contact.equals(c) == true)
{
equal++;
}
}
return equal;
}
/**
* This method returns the number of contacts (int)
*/
public int countContacts ()
{
return nContacts;
}
}
I know that I need to call the equals
method from the name
and contact
class but I don't know how to implement those since I'm still learning. Also since this is a beginner class we are not yet allowed to use ArrayLists
. Any help on how to get the countEqualContacts
method to work would be appreciated.
Thanks.