如何计算重复的联系人

时间:2018-04-10 01:10:24

标签: java arrays oop

我正在编写介绍编程类,我们当前的任务是创建一个包含三个类的联系人管理器(本质上是一个地址簿):name(添加first },middlelastcontact),contact(生成实际contact本身)和ContactManager(保留数组) contacts)。我在ContactManger类中的一个方法遇到问题,该方法应该检查数组中存在多少给定的contact对象,然后返回重复contacts的数量

以下是ContactManager类:

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;
    }
}

我知道我需要调用equalsname课程中的contact方法,但我不知道如何实施这些方法,因为我还在学习。此外,由于这是一个初学者课程,我们还不允许使用ArrayLists。任何有关如何使countEqualContacts方法工作的帮助将不胜感激。

感谢。

1 个答案:

答案 0 :(得分:0)

在Contacts类中,重写equals方法,仅​​当所有变量相等时才返回true,ex, if(this.fname.equals(fname)&amp;&amp; this.mName.equals(mname).. etc)  返回true; 其他  返回false;

在countEqualContyacts方法中,你可以调用它来比较和找到相等的对象。小心Null值(留给你检查空指针错误的方式和位置)