Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[W5.7][F10-1]De Jesus Joshua Robert #447

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ Examples:
* `add John Doe p/98765432 e/[email protected] a/John street, block 123, #01-01`
* `add Betsy Crowe pp/1234567 e/[email protected] pa/Newgate Prison t/criminal t/friend`

== Edit details of a person: `edit`

Edits an existing person's details in the address book. +
Format: edit INDEX [n/NAME] [p/PHONE] [e/EMAIL] [a/ADDRESS] [t/TAG]...`

Examples:
* `edit 1 p/23423423 e/[email protected] a/There`
* `edit 2 p/23422342 e/[email protected] a/Here`

== Listing all persons : `list`

Shows a list of all persons, along with their non-private details, in the address book. +
Expand Down
69 changes: 69 additions & 0 deletions src/seedu/addressbook/commands/EditCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package seedu.addressbook.commands;

import java.util.HashSet;
import java.util.Set;

import seedu.addressbook.data.exception.IllegalValueException;
import seedu.addressbook.data.person.Address;
import seedu.addressbook.data.person.Email;
import seedu.addressbook.data.person.Name;
import seedu.addressbook.data.person.Person;
import seedu.addressbook.data.person.Phone;
import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.data.person.UniquePersonList;
import seedu.addressbook.data.tag.Tag;

/**
* Edits a person in the address book.
*/
public class EditCommand extends Command {
public static final String COMMAND_WORD = "edit";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits an existing person in the address book. "
+ "Contact details can be marked private by prepending 'p' to the prefix.\n"
+ "Parameters: NAME [p]p/PHONE [p]e/EMAIL [p]a/ADDRESS [t/TAG]...\n"
+ "Example: " + COMMAND_WORD
+ " John Doe p/98765432 e/[email protected] a/311, Clementi Ave 2, #02-25 t/friends t/owesMoney";

public static final String MESSAGE_SUCCESS = "Person edited: %1$s";

private final Person toEdit;

/**
* Convenience constructor using raw values.
*
* @throws IllegalValueException if any of the raw values are invalid
*/
public EditCommand(String name,
String phone, boolean isPhonePrivate,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation for wrapped lines should be 8 spaces <== from coding standard provided

String email, boolean isEmailPrivate,
String address, boolean isAddressPrivate,
Set<String> tags) throws IllegalValueException {
final Set<Tag> tagSet = new HashSet<>();
for (String tagName : tags) {
tagSet.add(new Tag(tagName));
}
this.toEdit = new Person(
new Name(name),
new Phone(phone, isPhonePrivate),
new Email(email, isEmailPrivate),
new Address(address, isAddressPrivate),
tagSet
);
}

public EditCommand(Person toEdit) {
this.toEdit = toEdit;
}

public ReadOnlyPerson getPerson() {
return toEdit;
}

@Override
public CommandResult execute() {
addressBook.editPerson(toEdit);
return new CommandResult(String.format(MESSAGE_SUCCESS, toEdit));
}

}
7 changes: 7 additions & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public void addPerson(Person toAdd) throws DuplicatePersonException {
allPersons.add(toAdd);
}

/**
* Edits a person in the address book.
*/
public void editPerson(Person toEdit) {
allPersons.edit(toEdit);
}

/**
* Returns true if an equivalent person exists in the address book.
*/
Expand Down
7 changes: 7 additions & 0 deletions src/seedu/addressbook/data/person/UniquePersonList.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ public void add(Person toAdd) throws DuplicatePersonException {
internalList.add(toAdd);
}

/**
* Edits a person in the list.
*/
public void edit(Person toEdit) {
internalList.add(toEdit);
}

/**
* Removes the equivalent person from the list.
*
Expand Down
46 changes: 46 additions & 0 deletions test/java/seedu/addressbook/commands/EditCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package seedu.addressbook.commands;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.junit.Test;

import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.exception.IllegalValueException;
import seedu.addressbook.data.person.Address;
import seedu.addressbook.data.person.Email;
import seedu.addressbook.data.person.Name;
import seedu.addressbook.data.person.Person;
import seedu.addressbook.data.person.Phone;
import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.data.person.UniquePersonList;
import seedu.addressbook.util.TestUtil;


public class EditCommandTest {
private static final List<ReadOnlyPerson> EMPTY_PERSON_LIST = Collections.emptyList();
private static final Set<String> EMPTY_STRING_SET = Collections.emptySet();

@Test
public void editCommand_validData_CorrectlyDone() {
Person p = TestUtil.generateTestPerson();
EditCommand command = new EditCommand(p);
AddressBook book = new AddressBook();
command.setData(book, EMPTY_PERSON_LIST);
CommandResult result = command.execute();
UniquePersonList people = book.getAllPersons();

assertTrue(people.contains(p));
assertEquals(1, people.immutableListView().size());
assertFalse(result.getRelevantPersons().isPresent());
assertEquals(String.format(EditCommand.MESSAGE_SUCCESS, p), result.feedbackToUser);
}
}