forked from nus-cs2103-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from pra-navi/branch-deleteTask
feat: Implement deleteTask
- Loading branch information
Showing
11 changed files
with
299 additions
and
1 deletion.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/main/java/seedu/address/logic/commands/DeleteTaskCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.List; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.task.Task; | ||
|
||
/** | ||
* Deletes a task identified using it's displayed index from the address book. | ||
*/ | ||
public class DeleteTaskCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "deleteTask"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Deletes the task identified by the index number used in the displayed task list.\n" | ||
+ "Parameters: INDEX (must be a positive integer)\n" | ||
+ "Example: " + COMMAND_WORD + " 1"; | ||
|
||
public static final String MESSAGE_DELETE_TASK_SUCCESS = "Deleted Task: %1$s"; | ||
|
||
private final Index targetIndex; | ||
|
||
public DeleteTaskCommand(Index targetIndex) { | ||
this.targetIndex = targetIndex; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
List<Task> lastShownList = model.getFilteredTaskList(); | ||
|
||
if (targetIndex.getZeroBased() >= lastShownList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX); | ||
} | ||
|
||
Task toDelete = lastShownList.get(targetIndex.getZeroBased()); | ||
model.deleteTask(toDelete); | ||
return new CommandResult(String.format(MESSAGE_DELETE_TASK_SUCCESS, Messages.format(toDelete))); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof DeleteTaskCommand)) { | ||
return false; | ||
} | ||
|
||
DeleteTaskCommand otherDeleteCommand = (DeleteTaskCommand) other; | ||
return targetIndex.equals(otherDeleteCommand.targetIndex); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("targetIndex", targetIndex) | ||
.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/seedu/address/logic/parser/DeleteTaskCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.DeleteTaskCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses input arguments and creates a new DeleteTaskCommand object | ||
*/ | ||
public class DeleteTaskCommandParser implements Parser<DeleteTaskCommand> { | ||
/** | ||
* Parses the given {@code String} of arguments in the context of the DeleteTaskCommand | ||
* and returns a DeleteTaskCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public DeleteTaskCommand parse(String args) throws ParseException { | ||
try { | ||
Index index = ParserUtil.parseIndex(args); | ||
return new DeleteTaskCommand(index); | ||
} catch (ParseException pe) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteTaskCommand.MESSAGE_USAGE), pe); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
src/test/java/seedu/address/logic/commands/DeleteTaskCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; | ||
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; | ||
import static seedu.address.logic.commands.CommandTestUtil.showTaskAtIndex; | ||
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST; | ||
import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND; | ||
import static seedu.address.testutil.TypicalTasks.getTypicalAddressBook; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.ModelManager; | ||
import seedu.address.model.UserPrefs; | ||
import seedu.address.model.task.Task; | ||
|
||
/** | ||
* Contains integration tests (interaction with the Model) and unit tests for | ||
* {@code DeleteTaskCommand}. | ||
*/ | ||
public class DeleteTaskCommandTest { | ||
|
||
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); | ||
|
||
@Test | ||
public void execute_validIndexUnfilteredList_success() { | ||
Task taskToDelete = model.getFilteredTaskList().get(INDEX_FIRST.getZeroBased()); | ||
DeleteTaskCommand deleteTaskCommand = new DeleteTaskCommand(INDEX_FIRST); | ||
|
||
String expectedMessage = String.format(DeleteTaskCommand.MESSAGE_DELETE_TASK_SUCCESS, | ||
Messages.format(taskToDelete)); | ||
|
||
ModelManager expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs()); | ||
expectedModel.deleteTask(taskToDelete); | ||
|
||
assertCommandSuccess(deleteTaskCommand, model, expectedMessage, expectedModel); | ||
} | ||
|
||
@Test | ||
public void execute_invalidIndexUnfilteredList_throwsCommandException() { | ||
Index outOfBoundIndex = Index.fromOneBased(model.getFilteredTaskList().size() + 1); | ||
DeleteTaskCommand deleteTaskCommand = new DeleteTaskCommand(outOfBoundIndex); | ||
|
||
assertCommandFailure(deleteTaskCommand, model, Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX); | ||
} | ||
|
||
@Test | ||
public void execute_validIndexFilteredList_success() { | ||
showTaskAtIndex(model, INDEX_FIRST); | ||
|
||
Task taskToDelete = model.getFilteredTaskList().get(INDEX_FIRST.getZeroBased()); | ||
DeleteTaskCommand deleteTaskCommand = new DeleteTaskCommand(INDEX_FIRST); | ||
|
||
String expectedMessage = String.format(DeleteTaskCommand.MESSAGE_DELETE_TASK_SUCCESS, | ||
Messages.format(taskToDelete)); | ||
|
||
Model expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs()); | ||
expectedModel.deleteTask(taskToDelete); | ||
showNoTask(expectedModel); | ||
|
||
assertCommandSuccess(deleteTaskCommand, model, expectedMessage, expectedModel); | ||
} | ||
|
||
@Test | ||
public void execute_invalidIndexFilteredList_throwsCommandException() { | ||
showTaskAtIndex(model, INDEX_FIRST); | ||
|
||
Index outOfBoundIndex = INDEX_SECOND; | ||
// ensures that outOfBoundIndex is still in bounds of address book list | ||
assertTrue(outOfBoundIndex.getZeroBased() < model.getAddressBook().getTaskList().size()); | ||
|
||
DeleteTaskCommand deleteTaskCommand = new DeleteTaskCommand(outOfBoundIndex); | ||
|
||
assertCommandFailure(deleteTaskCommand, model, Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX); | ||
} | ||
|
||
@Test | ||
public void equals() { | ||
DeleteTaskCommand deleteTaskFirstCommand = new DeleteTaskCommand(INDEX_FIRST); | ||
DeleteTaskCommand deleteTaskSecondCommand = new DeleteTaskCommand(INDEX_SECOND); | ||
|
||
// same object -> returns true | ||
assertTrue(deleteTaskFirstCommand.equals(deleteTaskFirstCommand)); | ||
|
||
// same values -> returns true | ||
DeleteTaskCommand deleteTaskFirstCommandCopy = new DeleteTaskCommand(INDEX_FIRST); | ||
assertTrue(deleteTaskFirstCommand.equals(deleteTaskFirstCommandCopy)); | ||
|
||
// different types -> returns false | ||
assertFalse(deleteTaskFirstCommand.equals(1)); | ||
|
||
// null -> returns false | ||
assertFalse(deleteTaskFirstCommand.equals(null)); | ||
|
||
// different task -> returns false | ||
assertFalse(deleteTaskFirstCommand.equals(deleteTaskSecondCommand)); | ||
} | ||
|
||
@Test | ||
public void toStringMethod() { | ||
Index targetIndex = Index.fromOneBased(1); | ||
DeleteTaskCommand deleteTaskCommand = new DeleteTaskCommand(targetIndex); | ||
String expected = DeleteTaskCommand.class.getCanonicalName() + "{targetIndex=" + targetIndex + "}"; | ||
assertEquals(expected, deleteTaskCommand.toString()); | ||
} | ||
|
||
/** | ||
* Updates {@code model}'s filtered list to show no task. | ||
*/ | ||
private void showNoTask(Model model) { | ||
model.updateFilteredTaskList(p -> false); | ||
|
||
assertTrue(model.getFilteredTaskList().isEmpty()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/test/java/seedu/address/logic/parser/DeleteTaskCommandParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; | ||
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; | ||
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.logic.commands.DeleteTaskCommand; | ||
|
||
/** | ||
* As we are only doing white-box testing, our test cases do not cover path variations | ||
* outside of the DeleteTaskCommand code. For example, inputs "1" and "1 abc" take the | ||
* same path through the DeleteTaskCommand, 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 DeleteTaskCommandParserTest { | ||
|
||
private DeleteTaskCommandParser parser = new DeleteTaskCommandParser(); | ||
|
||
@Test | ||
public void parse_validArgs_returnsDeleteTaskCommand() { | ||
assertParseSuccess(parser, "1", new DeleteTaskCommand(INDEX_FIRST)); | ||
} | ||
|
||
@Test | ||
public void parse_invalidArgs_throwsParseException() { | ||
assertParseFailure(parser, "a", String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
DeleteTaskCommand.MESSAGE_USAGE)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters