Skip to content

Commit

Permalink
Refactor code for delete task feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Yskie committed Mar 17, 2024
1 parent 5dab680 commit 907aa93
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 25 deletions.
27 changes: 15 additions & 12 deletions src/main/java/seedu/address/logic/commands/DeleteTaskCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,35 @@

import static java.util.Objects.requireNonNull;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.task.Task;

/**
* Deletes a task in the task list.
*/
public class DeleteTaskCommand extends Command {

public static final String COMMAND_WORD = "deletetask";
public static final String MESSAGE_USAGE = COMMAND_WORD + "<index> : Deletes a task in the task list.";
public static final String MESSAGE_SUCCESS = "Task number %1$s has been deleted.";
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_SUCCESS = "Deleted Task: %1$s";
public static final String MESSAGE_INDEX_TOO_LARGE = "The index is not valid, use \"list task\" to "
+ "display all tasks.";


public static final String MESSAGE_INDEX_BELOW_ONE = "The index must be greater than 0.";

private final Integer taskIndexToDelete;
private final Index taskIndexToDelete;

/**
* Creates an DeleteTaskCommand to add the specified {@code index}
*/
public DeleteTaskCommand(int index) {
public DeleteTaskCommand(Index index) {
taskIndexToDelete = index;
}

Expand All @@ -38,22 +45,18 @@ public DeleteTaskCommand(int index) {
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

if (taskIndexToDelete <= 0) {
throw new CommandException(MESSAGE_INDEX_BELOW_ONE);
}

if (!model.isValidTaskIndex(taskIndexToDelete)) {
throw new CommandException(MESSAGE_INDEX_TOO_LARGE);
}
}

model.deleteTask(taskIndexToDelete);
return new CommandResult(String.format(MESSAGE_SUCCESS, taskIndexToDelete));
Task deletedTask = model.deleteTask(taskIndexToDelete);
return new CommandResult(String.format(MESSAGE_SUCCESS, deletedTask.getDescription()));
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("toDelete", taskIndexToDelete)
.add("toDelete", taskIndexToDelete.getOneBased())
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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;

Expand All @@ -22,9 +23,9 @@ public DeleteTaskCommand parse(String userInput) throws ParseException {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteTaskCommand.MESSAGE_USAGE));
}
try {
int taskIndexToDelete = Integer.parseInt(userInput.trim());
Index taskIndexToDelete = ParserUtil.parseIndex(userInput.trim());
return new DeleteTaskCommand(taskIndexToDelete);
} catch (NumberFormatException e) {
} catch (ParseException pe) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteTaskCommand.MESSAGE_USAGE));
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.index.Index;
import seedu.address.model.person.Person;
import seedu.address.model.task.Task;

Expand Down Expand Up @@ -107,7 +108,7 @@ static void setTaskList(TaskList taskList) {}
* Deletes the given task.
* {@code index} the index must exist in the task list.
*/
void deleteTask(int index);
Task deleteTask(Index index);

/**
* Returns true if a task has the same description as a {@code task} in the task list.
Expand All @@ -117,7 +118,7 @@ static void setTaskList(TaskList taskList) {}
/**
* Returns true if the {@code index} is within the task list.
*/
boolean isValidTaskIndex(int index);
boolean isValidTaskIndex(Index index);

/** Returns an unmodifiable view of the filtered person list */
ObservableList<Person> getFilteredPersonList();
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javafx.collections.transformation.FilteredList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.core.index.Index;
import seedu.address.model.person.Person;
import seedu.address.model.task.Task;

Expand Down Expand Up @@ -141,8 +142,8 @@ public void addTask(Task task) {
}

@Override
public void deleteTask(int index) {
taskList.deleteTask(index);
public Task deleteTask(Index index) {
return taskList.deleteTask(index);
}

@Override
Expand All @@ -152,7 +153,7 @@ public boolean hasTask(Task task) {
}

@Override
public boolean isValidTaskIndex(int index) {
public boolean isValidTaskIndex(Index index) {
return taskList.isValidTaskIndex(index);
}
/**
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/seedu/address/model/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import seedu.address.commons.core.index.Index;
import seedu.address.model.task.Task;

/**
Expand Down Expand Up @@ -34,13 +35,25 @@ public void addTask(Task task) {
taskList.add(task);
}

/**
* Returns a task based on the index of list.
*
* @param index The index of the task to be returned.
*/
public Task getTask(Index index) {
Task task = taskList.get(index.getZeroBased());
return task;
}

/**
* Deletes a task based on the index of list.
*
* @param index The index of the task to be deleted in the list.
*/
public void deleteTask(int index) {
taskList.remove(index - 1);
public Task deleteTask(Index index) {
Task task = taskList.get(index.getZeroBased());
taskList.remove(index.getZeroBased());
return task;
}

public ObservableList<Task> getSerializeTaskList() {
Expand All @@ -51,8 +64,8 @@ public boolean hasTask(Task task) {
return taskList.contains(task);
}

public boolean isValidTaskIndex(int index) {
return taskList.size() > index;
public boolean isValidTaskIndex(Index index) {
return index.getZeroBased() < taskList.size();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.AddressBook;
Expand Down Expand Up @@ -193,7 +194,7 @@ public void addTask(Task task) {
* @param index The index of the task to be deleted in the list.
*/
@Override
public void deleteTask(int index) {
public Task deleteTask(Index index) {
throw new AssertionError("This method should not be called.");
}

Expand All @@ -214,7 +215,7 @@ public boolean hasTask(Task task) {
* @param index
*/
@Override
public boolean isValidTaskIndex(int index) {
public boolean isValidTaskIndex(Index index) {
throw new AssertionError("This method should not be called.");
}

Expand Down

0 comments on commit 907aa93

Please sign in to comment.