Skip to content

Commit

Permalink
Merge pull request #78 from pra-navi/branch-deletePerson
Browse files Browse the repository at this point in the history
feat: Implement deletePerson command
  • Loading branch information
freshcabbage123 authored Oct 13, 2023
2 parents c7a1b91 + 7b19480 commit fac3e36
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
/**
* Deletes a person identified using it's displayed index from the address book.
*/
public class DeleteCommand extends Command {
public class DeletePersonCommand extends Command {

public static final String COMMAND_WORD = "delete";
public static final String COMMAND_WORD = "deletePerson";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Deletes the person identified by the index number used in the displayed person list.\n"
Expand All @@ -27,7 +27,7 @@ public class DeleteCommand extends Command {

private final Index targetIndex;

public DeleteCommand(Index targetIndex) {
public DeletePersonCommand(Index targetIndex) {
this.targetIndex = targetIndex;
}

Expand All @@ -52,11 +52,11 @@ public boolean equals(Object other) {
}

// instanceof handles nulls
if (!(other instanceof DeleteCommand)) {
if (!(other instanceof DeletePersonCommand)) {
return false;
}

DeleteCommand otherDeleteCommand = (DeleteCommand) other;
DeletePersonCommand otherDeleteCommand = (DeletePersonCommand) other;
return targetIndex.equals(otherDeleteCommand.targetIndex);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import seedu.address.logic.commands.AddPersonCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.DeletePersonCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindCommand;
Expand Down Expand Up @@ -59,8 +59,8 @@ public Command parseCommand(String userInput) throws ParseException {
case EditCommand.COMMAND_WORD:
return new EditCommandParser().parse(arguments);

case DeleteCommand.COMMAND_WORD:
return new DeleteCommandParser().parse(arguments);
case DeletePersonCommand.COMMAND_WORD:
return new DeletePersonCommandParser().parse(arguments);

case ClearCommand.COMMAND_WORD:
return new ClearCommand();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.DeletePersonCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new DeleteCommand object
* Parses input arguments and creates a new DeletePersonCommand object
*/
public class DeleteCommandParser implements Parser<DeleteCommand> {
public class DeletePersonCommandParser implements Parser<DeletePersonCommand> {

/**
* Parses the given {@code String} of arguments in the context of the DeleteCommand
* and returns a DeleteCommand object for execution.
* Parses the given {@code String} of arguments in the context of the DeletePersonCommand
* and returns a DeletePersonCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public DeleteCommand parse(String args) throws ParseException {
public DeletePersonCommand parse(String args) throws ParseException {
try {
Index index = ParserUtil.parseIndex(args);
return new DeleteCommand(index);
return new DeletePersonCommand(index);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE), pe);
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeletePersonCommand.MESSAGE_USAGE), pe);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/seedu/address/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public void execute_invalidCommandFormat_throwsParseException() {

@Test
public void execute_commandExecutionError_throwsCommandException() {
String deleteCommand = "delete 9";
assertCommandException(deleteCommand, MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
String deletePersonCommand = "deletePerson 9";
assertCommandException(deletePersonCommand, MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,49 @@

/**
* Contains integration tests (interaction with the Model) and unit tests for
* {@code DeleteCommand}.
* {@code DeletePersonCommand}.
*/
public class DeleteCommandTest {
public class DeletePersonCommandTest {

private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
public void execute_validIndexUnfilteredList_success() {
Person personToDelete = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
DeleteCommand deleteCommand = new DeleteCommand(INDEX_FIRST_PERSON);
DeletePersonCommand deletePersonCommand = new DeletePersonCommand(INDEX_FIRST_PERSON);

String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS,
String expectedMessage = String.format(DeletePersonCommand.MESSAGE_DELETE_PERSON_SUCCESS,
Messages.format(personToDelete));

ModelManager expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs());
expectedModel.deletePerson(personToDelete);

assertCommandSuccess(deleteCommand, model, expectedMessage, expectedModel);
assertCommandSuccess(deletePersonCommand, model, expectedMessage, expectedModel);
}

@Test
public void execute_invalidIndexUnfilteredList_throwsCommandException() {
Index outOfBoundIndex = Index.fromOneBased(model.getFilteredPersonList().size() + 1);
DeleteCommand deleteCommand = new DeleteCommand(outOfBoundIndex);
DeletePersonCommand deletePersonCommand = new DeletePersonCommand(outOfBoundIndex);

assertCommandFailure(deleteCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
assertCommandFailure(deletePersonCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

@Test
public void execute_validIndexFilteredList_success() {
showPersonAtIndex(model, INDEX_FIRST_PERSON);

Person personToDelete = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
DeleteCommand deleteCommand = new DeleteCommand(INDEX_FIRST_PERSON);
DeletePersonCommand deletePersonCommand = new DeletePersonCommand(INDEX_FIRST_PERSON);

String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS,
String expectedMessage = String.format(DeletePersonCommand.MESSAGE_DELETE_PERSON_SUCCESS,
Messages.format(personToDelete));

Model expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs());
expectedModel.deletePerson(personToDelete);
showNoPerson(expectedModel);

assertCommandSuccess(deleteCommand, model, expectedMessage, expectedModel);
assertCommandSuccess(deletePersonCommand, model, expectedMessage, expectedModel);
}

@Test
Expand All @@ -74,39 +74,39 @@ public void execute_invalidIndexFilteredList_throwsCommandException() {
// ensures that outOfBoundIndex is still in bounds of address book list
assertTrue(outOfBoundIndex.getZeroBased() < model.getAddressBook().getPersonList().size());

DeleteCommand deleteCommand = new DeleteCommand(outOfBoundIndex);
DeletePersonCommand deletePersonCommand = new DeletePersonCommand(outOfBoundIndex);

assertCommandFailure(deleteCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
assertCommandFailure(deletePersonCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

@Test
public void equals() {
DeleteCommand deleteFirstCommand = new DeleteCommand(INDEX_FIRST_PERSON);
DeleteCommand deleteSecondCommand = new DeleteCommand(INDEX_SECOND_PERSON);
DeletePersonCommand deletePersonFirstCommand = new DeletePersonCommand(INDEX_FIRST_PERSON);
DeletePersonCommand deletePersonSecondCommand = new DeletePersonCommand(INDEX_SECOND_PERSON);

// same object -> returns true
assertTrue(deleteFirstCommand.equals(deleteFirstCommand));
assertTrue(deletePersonFirstCommand.equals(deletePersonFirstCommand));

// same values -> returns true
DeleteCommand deleteFirstCommandCopy = new DeleteCommand(INDEX_FIRST_PERSON);
assertTrue(deleteFirstCommand.equals(deleteFirstCommandCopy));
DeletePersonCommand deletePersonFirstCommandCopy = new DeletePersonCommand(INDEX_FIRST_PERSON);
assertTrue(deletePersonFirstCommand.equals(deletePersonFirstCommandCopy));

// different types -> returns false
assertFalse(deleteFirstCommand.equals(1));
assertFalse(deletePersonFirstCommand.equals(1));

// null -> returns false
assertFalse(deleteFirstCommand.equals(null));
assertFalse(deletePersonFirstCommand.equals(null));

// different person -> returns false
assertFalse(deleteFirstCommand.equals(deleteSecondCommand));
assertFalse(deletePersonFirstCommand.equals(deletePersonSecondCommand));
}

@Test
public void toStringMethod() {
Index targetIndex = Index.fromOneBased(1);
DeleteCommand deleteCommand = new DeleteCommand(targetIndex);
String expected = DeleteCommand.class.getCanonicalName() + "{targetIndex=" + targetIndex + "}";
assertEquals(expected, deleteCommand.toString());
DeletePersonCommand deletePersonCommand = new DeletePersonCommand(targetIndex);
String expected = DeletePersonCommand.class.getCanonicalName() + "{targetIndex=" + targetIndex + "}";
assertEquals(expected, deletePersonCommand.toString());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import seedu.address.logic.commands.AddPersonCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.DeletePersonCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditCommand.EditPersonDescriptor;
import seedu.address.logic.commands.ExitCommand;
Expand Down Expand Up @@ -47,10 +47,10 @@ public void parseCommand_clear() throws Exception {
}

@Test
public void parseCommand_delete() throws Exception {
DeleteCommand command = (DeleteCommand) parser.parseCommand(
DeleteCommand.COMMAND_WORD + " " + INDEX_FIRST_PERSON.getOneBased());
assertEquals(new DeleteCommand(INDEX_FIRST_PERSON), command);
public void parseCommand_deletePerson() throws Exception {
DeletePersonCommand command = (DeletePersonCommand) parser.parseCommand(
DeletePersonCommand.COMMAND_WORD + " " + INDEX_FIRST_PERSON.getOneBased());
assertEquals(new DeletePersonCommand(INDEX_FIRST_PERSON), command);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,27 @@

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.DeletePersonCommand;

/**
* As we are only doing white-box testing, our test cases do not cover path variations
* outside of the DeleteCommand code. For example, inputs "1" and "1 abc" take the
* same path through the DeleteCommand, and therefore we test only one of them.
* outside of the DeletePersonCommand code. For example, inputs "1" and "1 abc" take the
* same path through the DeletePersonCommand, and therefore we test only one of them.
* The path variation for those two cases occur inside the ParserUtil, and
* therefore should be covered by the ParserUtilTest.
*/
public class DeleteCommandParserTest {
public class DeletePersonCommandParserTest {

private DeleteCommandParser parser = new DeleteCommandParser();
private DeletePersonCommandParser parser = new DeletePersonCommandParser();

@Test
public void parse_validArgs_returnsDeleteCommand() {
assertParseSuccess(parser, "1", new DeleteCommand(INDEX_FIRST_PERSON));
public void parse_validArgs_returnsDeletePersonCommand() {
assertParseSuccess(parser, "1", new DeletePersonCommand(INDEX_FIRST_PERSON));
}

@Test
public void parse_invalidArgs_throwsParseException() {
assertParseFailure(parser, "a", String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE));
assertParseFailure(parser, "a", String.format(MESSAGE_INVALID_COMMAND_FORMAT,
DeletePersonCommand.MESSAGE_USAGE));
}
}

0 comments on commit fac3e36

Please sign in to comment.