Skip to content
This repository has been archived by the owner on Mar 5, 2023. It is now read-only.

Commit

Permalink
Logic: Implement RemarkCommand execute() logic
Browse files Browse the repository at this point in the history
. Replace the error message with an actual logic to modify the remarks
of a person.
  • Loading branch information
yamgent committed Feb 9, 2018
1 parent 23f9ebe commit 7491051
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 8 deletions.
43 changes: 40 additions & 3 deletions src/main/java/seedu/address/logic/commands/RemarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_REMARK;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.List;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.person.Person;
import seedu.address.model.person.Remark;
import seedu.address.model.person.exceptions.DuplicatePersonException;
import seedu.address.model.person.exceptions.PersonNotFoundException;

/**
* Changes the remark of an existing person in the address book.
Expand All @@ -22,7 +29,9 @@ public class RemarkCommand extends UndoableCommand {
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_REMARK + "Likes to swim.";

public static final String MESSAGE_ARGUMENTS = "Index: %1$d, Remark: %2$s";
public static final String MESSAGE_ADD_REMARK_SUCCESS = "Added remark to Person: %1$s";
public static final String MESSAGE_DELETE_REMARK_SUCCESS = "Removed remark from Person: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book.";

private final Index index;
private final Remark remark;
Expand All @@ -40,8 +49,36 @@ public RemarkCommand(Index index, Remark remark) {
}

@Override
protected CommandResult executeUndoableCommand() throws CommandException {
throw new CommandException(String.format(MESSAGE_ARGUMENTS, index.getOneBased(), remark));
public CommandResult executeUndoableCommand() throws CommandException {
List<Person> lastShownList = model.getFilteredPersonList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

Person personToEdit = lastShownList.get(index.getZeroBased());
Person editedPerson = new Person(personToEdit.getName(), personToEdit.getPhone(), personToEdit.getEmail(),
personToEdit.getAddress(), remark, personToEdit.getTags());

try {
model.updatePerson(personToEdit, editedPerson);
} catch (DuplicatePersonException dpe) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
} catch (PersonNotFoundException pnfe) {
throw new AssertionError("The target person cannot be missing");
}
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);

return new CommandResult(generateSuccessMessage(editedPerson));
}

/**
* Generates a command execution success message based on whether the remark is added to or removed from
* {@code personToEdit}.
*/
private String generateSuccessMessage(Person personToEdit) {
String message = (!remark.value.isEmpty()) ? MESSAGE_ADD_REMARK_SUCCESS : MESSAGE_DELETE_REMARK_SUCCESS;
return String.format(message, personToEdit);
}

@Override
Expand Down
78 changes: 73 additions & 5 deletions src/test/java/seedu/address/logic/commands/RemarkCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@
import static seedu.address.logic.commands.CommandTestUtil.VALID_REMARK_AMY;
import static seedu.address.logic.commands.CommandTestUtil.VALID_REMARK_BOB;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.RemarkCommand.MESSAGE_ARGUMENTS;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.logic.commands.CommandTestUtil.showFirstPersonOnly;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;
import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import org.junit.Test;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.CommandHistory;
import seedu.address.logic.UndoRedoStack;
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.person.Person;
import seedu.address.model.person.Remark;
import seedu.address.testutil.PersonBuilder;

/**
* Contains integration tests (interaction with the Model) and unit tests for RemarkCommand.
Expand All @@ -28,11 +33,74 @@ public class RemarkCommandTest {
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
public void execute() throws Exception {
final String remark = "Some remark";
public void execute_addRemarkUnfilteredList_success() throws Exception {
Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
Person editedPerson = new PersonBuilder(firstPerson).withRemark("Some remark").build();

assertCommandFailure(prepareCommand(INDEX_FIRST_PERSON, remark), model,
String.format(MESSAGE_ARGUMENTS, INDEX_FIRST_PERSON.getOneBased(), remark));
RemarkCommand remarkCommand = prepareCommand(INDEX_FIRST_PERSON, editedPerson.getRemark().value);

String expectedMessage = String.format(RemarkCommand.MESSAGE_ADD_REMARK_SUCCESS, editedPerson);

Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs());
expectedModel.updatePerson(firstPerson, editedPerson);

assertCommandSuccess(remarkCommand, model, expectedMessage, expectedModel);
}

@Test
public void execute_deleteRemarkUnfilteredList_success() throws Exception {
Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
Person editedPerson = new PersonBuilder(firstPerson).withRemark("").build();

RemarkCommand remarkCommand = prepareCommand(INDEX_FIRST_PERSON, editedPerson.getRemark().toString());

String expectedMessage = String.format(RemarkCommand.MESSAGE_DELETE_REMARK_SUCCESS, editedPerson);

Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs());
expectedModel.updatePerson(firstPerson, editedPerson);

assertCommandSuccess(remarkCommand, model, expectedMessage, expectedModel);
}

@Test
public void execute_filteredList_success() throws Exception {
showFirstPersonOnly(model);

Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
Person editedPerson = new PersonBuilder(model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()))
.withRemark("Some remark").build();
RemarkCommand remarkCommand = prepareCommand(INDEX_FIRST_PERSON, editedPerson.getRemark().value);

String expectedMessage = String.format(RemarkCommand.MESSAGE_ADD_REMARK_SUCCESS, editedPerson);

Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs());
expectedModel.updatePerson(firstPerson, editedPerson);

assertCommandSuccess(remarkCommand, model, expectedMessage, expectedModel);
}

@Test
public void execute_invalidPersonIndexUnfilteredList_failure() throws Exception {
Index outOfBoundIndex = Index.fromOneBased(model.getFilteredPersonList().size() + 1);
RemarkCommand remarkCommand = prepareCommand(outOfBoundIndex, VALID_REMARK_BOB);

assertCommandFailure(remarkCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

/**
* Edit filtered list where index is larger than size of filtered list,
* but smaller than size of address book
*/
@Test
public void execute_invalidPersonIndexFilteredList_failure() throws Exception {
showFirstPersonOnly(model);
Index outOfBoundIndex = INDEX_SECOND_PERSON;
// ensures that outOfBoundIndex is still in bounds of address book list
assertTrue(outOfBoundIndex.getZeroBased() < model.getAddressBook().getPersonList().size());

RemarkCommand remarkCommand = prepareCommand(outOfBoundIndex, VALID_REMARK_BOB);

assertCommandFailure(remarkCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

@Test
Expand Down

0 comments on commit 7491051

Please sign in to comment.