Home > Mobile >  Java - Modifying a Contact in AddressBook (beginner)
Java - Modifying a Contact in AddressBook (beginner)

Time:01-29

I'm new to Java and object-orient programming. This is for a class assignment in which we create a contact service to add/modify/delete contacts. I'm stuck on how to modify a contact. This is the Contact class that has already been given to us.

Contact Class:

public class Contact {

private String contactId;
private String firstName;
private String phone;

public Contact (String contactId, String firstName, String phone) {
    if (contactId == null || contactId.length() > 10) {
        throw new IllegalArgumentException("invalid contact ID");
    }
    if (firstName == null || firstName.length() > 10) {
        throw new IllegalArgumentException("invalid first name");
    }
    if (phone == null || phone.length() != 10) {
        throw new IllegalArgumentException("invalid phone number");
    }
    
    this.contactId = contactId;
    this.firstName = firstName;
    this.phone = phone;
}

public String getContactId() {
    return contactId;
}

public String getFirstName() {
    return firstName;
}

public String getPhone() {
    return phone;
}
}

In my contact service class, I have written the methods for creating and getting a contact

public class ContactService {

int Id = 000;

//create contactList array
private ArrayList<Contact> contactList = new ArrayList<>();

public Contact getContact(String ID) {
    
    //iterates through contact list
    Iterator<Contact> itr = contactList.iterator();
    
    while (itr.hasNext()) {
        Contact contact = itr.next();
        if (contact.getContactId().equals(ID)) {
            
            //returns contact object for matching ID
            return contact; 
        } 
    }
    return null;
    }

//adds a contact to the contactList array
public Contact addContact(String firstName, String Phone) {

    Id  ; //increment ID to make a unique ID
    
     //convert integer to string for ArrayList
    String contactId = Integer.toString(Id);
    //create contact object
    Contact contact = new Contact(contactId, firstName, Phone);
    //add contact to list
    contactList.add(contact);
    
    return contact;
    
}

For my updateContact() method, I want to call getContact() which should return a contact object but I'm not sure how to update the object from there. I'm sorry this is such a basic question but I've thoroughly twisted my brain into a knot and I would appreciate any pointers. Posting here was a last resort after a few days of researching and trying different things. This is probably this closest I've come:

public void updateContact(String contactId, String firstName, String phone) {
    
    getContact(contactId);
            
    contact.setFirstName(firstName);
    contact.setPhone(phone);
    
}

However, not only does this approach not work but it would require me to create setter methods. If I do that the new input does not go through the null and length checks contained in the Contact constructor.

CodePudding user response:

As @Atryom noted, you can add the same null and length checks that you have in your constructor to your setter methods. There's no restriction specifying that your setters cannot have those checks.

CodePudding user response:

Your update method does not work because you are calling getContact without storing return value of the method

public void updateContact(String contactId, String firstName, String phone) {
    Contact contact = getContact(contactId);
    contact.setFirstName(firstName);
    contact.setPhone(phone);
  }

For make sure your checks are called you can call setter methods in your constructor like this

public Contact(String contactId, String firstName, String phone) {
    setFirstName(firstName);
    setPhone(phone);
    setContactId(contactId);
  }

And check your values into setter methods

public void setFirstName(String firstName) {
    if (firstName == null || firstName.length() > 10) {
      throw new IllegalArgumentException("invalid first name");
    }
    this.firstName = firstName;
  }

  public void setPhone(String phone) {
    if (phone == null || phone.length() != 10) {
      throw new IllegalArgumentException("invalid phone number");
    }
    this.phone = phone;
  }

  public void setContactId(String contactId) {
    if (contactId == null || contactId.length() > 10) {
      throw new IllegalArgumentException("invalid contact ID");
    }
    this.contactId = contactId;
  }

Furthermore, you can improve your performance using an HashMap instead of ArrayList, so that you don't need to iterate whole the list every time you need a contact.

private final Map<String, Contact> contactList = new HashMap<>();

  public Contact getContact(String ID) {
    return contactList.get(ID);
  }

  //adds a contact to the contactList array
  public Contact addContact(String firstName, String Phone) {
    Id  ;
    String contactId = Integer.toString(Id);
    Contact contact = new Contact(contactId, firstName, Phone);
    return contactList.put(contactId, contact);
  }
  •  Tags:  
  • Related