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

Add Delete Task Command #44

Merged
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package seedu.address.logic.commands.task;

import static java.util.Objects.requireNonNull;

import java.util.List;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.TaskCommand;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyTaskList;
import seedu.address.model.TaskList;
import seedu.address.model.task.Task;

public class DeleteTaskCommand extends TaskCommand {
public static final String COMMAND_WORD = "delete";
public static final String FULL_COMMAND_WORD = TaskCommand.COMMAND_WORD + " " + COMMAND_WORD;
public static final String MESSAGE_SUCCESS = "Task deleted: %1$s";
public static final String MESSAGE_USAGE = FULL_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;
}

/**
* Executes the command and returns the result message.
*
* @param model {@code Model} which the command should operate on
* @return feedback message of the operation result for display
* @throws CommandException If an error occurs during command execution.
*/
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

ReadOnlyTaskList taskList = model.getTaskList();

if (targetIndex.getZeroBased() >= taskList.getTasks().size()) {
throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}

Task deletedTask = model.getTaskAtIndex(targetIndex.getZeroBased());
model.deleteTask(deletedTask);

return new CommandResult(String.format(MESSAGE_DELETE_TASK_SUCCESS, deletedTask));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class DeleteCommandParser implements Parser<DeleteCommand> {
/**
* Parses the given {@code String} of arguments in the context of the DeleteCommand
wlren marked this conversation as resolved.
Show resolved Hide resolved
* and returns a DeleteCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
* @throws ParseException if the user input does not conform to the expected format
*/
public DeleteCommand parse(String args) throws ParseException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.TaskCommand;
import seedu.address.logic.commands.task.AddTaskCommand;
import seedu.address.logic.commands.task.DeleteTaskCommand;
import seedu.address.logic.commands.task.DoneTaskCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.logic.parser.task.AddTaskCommandParser;
import seedu.address.logic.parser.task.DeleteTaskCommandParser;
import seedu.address.logic.parser.task.DoneTaskCommandParser;


/**
* Parses all task-related commands (those starting with "task") and returns a TaskCommand.
*/
Expand All @@ -35,8 +38,13 @@ public TaskCommand parse(String userInput) throws ParseException {
switch (commandWord) {
case AddTaskCommand.COMMAND_WORD:
return new AddTaskCommandParser().parse(arguments);

case DeleteTaskCommand.COMMAND_WORD:
return new DeleteTaskCommandParser().parse(arguments);

case DoneTaskCommand.COMMAND_WORD:
return new DoneTaskCommandParser().parse(arguments);

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package seedu.address.logic.parser.task;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.task.DeleteTaskCommand;
import seedu.address.logic.parser.Parser;
import seedu.address.logic.parser.ParserUtil;
import seedu.address.logic.parser.exceptions.ParseException;

public class DeleteTaskCommandParser implements Parser<DeleteTaskCommand> {


/**
* Parses {@code userInput} into a new DeleteTaskCommand and returns it.
*
* @param userInput the input entered by the user
* @throws ParseException if {@code userInput} does not conform to the expected format
*/
@Override
public DeleteTaskCommand parse(String userInput) throws ParseException {
try {
Index index = ParserUtil.parseIndex(userInput);
return new DeleteTaskCommand(index);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE), pe);
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.model;

import java.nio.file.Path;
import java.util.List;
import java.util.function.Predicate;

import javafx.collections.ObservableList;
Expand Down Expand Up @@ -86,6 +87,20 @@ public interface Model {
*/
void updateFilteredPersonList(Predicate<Person> predicate);


/**
* Deletes the given task. The task must exist in the task list.
* @param deletedTask The task to delete
*/
void deleteTask(Task deletedTask);

/**
* Returns a list of the current tasks.
*
* @return the list of tasks
*/
List<Task> getTasks();

/**
* Adds the given task to the task list.
* @param task The task to be added.
Expand All @@ -103,4 +118,5 @@ public interface Model {
* @return Task corresponding to the index provided.
*/
Task getTaskAtIndex(int index) throws IndexOutOfBoundsException;

}
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ public void updateFilteredPersonList(Predicate<Person> predicate) {
filteredPersons.setPredicate(predicate);
}

@Override
public void deleteTask(Task deletedTask) {
taskList.removeTask(deletedTask);
}

//=========== TaskMaster2103 ============================================================================

@Override
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/model/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public void addTask(Task task) {
tasks.add(task);
}

public void removeTask(Task deletedTask) {
tasks.remove(deletedTask);
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/seedu/address/logic/commands/AddCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -150,6 +151,16 @@ public void updateFilteredPersonList(Predicate<Person> predicate) {
throw new AssertionError("This method should not be called.");
}

/**
* Returns a list of the current tasks.
*
* @return the list of tasks
*/
@Override
public List<Task> getTasks() {
return null;
}

@Override
public void addTask(Task task) {
throw new AssertionError("This method should not be called.");
Expand Down