Search code examples
javaobjectunique

Creating an addContact() method that will create unique contacts each time


A school assignment (in beginner Java) is asking me to create a small contact manager program, which I'm having trouble with.

It asks us to create a few classes - Address, PhoneNumber, Contact, and ContactManager.

In ContactManager, we're asked to create a method called addContact() which will add a brand new unique contact to an object array within ContactManager.

However I cannot figure out how to make this method do what I want it to do since each time it creates a new Contact, it always has the same name. How do I make the object it creates have a unique name (i.e. Contact001, Contact002 etc) each time?

Also, how do I feed through all the relevant information it needs to create it, assuming I can enter all the data in instance variables to test it? This is my code class:

public class Contact {
//Contact Instance Variables
private String lastName;
private String firstName;
private String middleName;
private Address completeAddress[];
private PhoneNumber phoneNumer[];
private SocialNetworkAccount socialNetworkInfo[];



public Contact(String lastName, String firstName, String middleName,
        Address[] completeAddress, PhoneNumber[] phoneNumer,
        SocialNetworkAccount[] socialNetworkInfo) {
    this.lastName = lastName;
    this.firstName = firstName;
    this.middleName = middleName;
    this.completeAddress = completeAddress;
    this.phoneNumer = phoneNumer;
    this.socialNetworkInfo = socialNetworkInfo;
}

Solution

  • "private List contacts;" is a declaration of an instance variable called contacts.

    The variable's type is a List, which is a specific kind of Collection object found in the java.util package.

    List<Contact> is a way of stating to the compiler that this list contains only Contact objects. See "Generics" in the java tutorial.