Contact Manager — Editing and Deleting Contacts

Yesterday I finished up my editing a contact story; as I anticipated this had been a bit more involved than some of the other stories. I created an EditContact class that takes the ContactList as an argument. The edit contact class searches through the ArrayList of contacts for the Contact by index, and then calls a setter method in the Contacts class to change it.

I then did a similar thing to Delete contacts. I created a DeleteContact class, which also takes the ContactList as an argument. It deletes it based on the index.

Then I added in some validations ie. what happens if a user chooses a negative number, what happens if a user chooses a number that isn’t in the array. I did some big refactors/redesigns as well.

I’ve had a really long addContact method in the Command Line Interface for a few days, and I didn’t know how to refactor it.

Here’s what it looked like:

private void addNewContact() {
printToConsole("First Name: ");
String firstName = input.nextLine();
printToConsole("Last Name: ");
String lastName = input.nextLine();
printToConsole("Address (One Line):");
String address = input.nextLine();
printToConsole("City: ");
String city = input.nextLine();
printToConsole("Postcode:");
String postCode = input.nextLine();
printToConsole("Phone number:");
String phoneNumber = input.nextLine();
Contact newContact = new Contact(firstName, lastName, address, city, postCode, phoneNumber);
printToConsole(newContact.getFirstName() + " " + newContact.getLastName() + " has been added as a contact.");
contactList.addContact(newContact);
}

Here’s what I changed it to:

private void addNewContact() {
printToConsole("Add a new contact");
Contact newContact =
new Contact(
addFirstName(),
addLastName(),
addAddress(),
addCity(),
addPostCode(),
addPhoneNumber());
contactList.addContact(newContact);
printToConsole(
newContact.getFirstName()
+ " "
+ newContact.getLastName()
+ " has been added as a contact.");
}

private String addFirstName() {
printToConsole("First Name: ");
return input.nextLine().trim();
}

private String addLastName() {
printToConsole("Last Name: ");
return input.nextLine().trim();
}

private String addAddress() {
printToConsole("Address (One Line):");
return input.nextLine().trim();
}

private String addCity() {
printToConsole("City: ");
return input.nextLine().trim();
}

private String addPostCode() {
printToConsole("Postcode:");
return input.nextLine().trim();
}

private String addPhoneNumber() {
printToConsole("Phone number:");
return input.nextLine().trim();
}

I think that’s a bit cleaner, though I’d still like to get the outputs out of those methods.

Today I have my IPM and I am looking forward to getting feedback on this project.

Originally published on Jekyll

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.