Skip to content

Commit

Permalink
Merge pull request AY2324S2-CS2103T-W13-4#37 from Yskie/branch-delete…
Browse files Browse the repository at this point in the history
…-command

The Delete Task Command
  • Loading branch information
chin-herng authored Mar 19, 2024
2 parents 3ff249a + 39f8b18 commit 7cf9ff9
Show file tree
Hide file tree
Showing 18 changed files with 439 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ src/test/data/sandbox/
.DS_Store
docs/_site/
docs/_markbind/logs/

build.gradle
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ task coverage(type: JacocoReport) {
}

dependencies {
String jUnitVersion = '5.4.0'
String jUnitVersion = '5.10.0'
String javaFxVersion = '17.0.7'

implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'win'
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/seedu/address/logic/commands/DeleteTaskCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package seedu.address.logic.commands;

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
+ ": 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 \"listtask\" to "
+ "display all tasks.";

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

private final Index taskIndexToDelete;

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

/**
* 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);

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

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

@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 taskIndexToDelete.equals(otherDeleteCommand.taskIndexToDelete);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("toDelete", taskIndexToDelete.getOneBased())
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.DeleteTaskCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindCommand;
Expand Down Expand Up @@ -61,6 +62,9 @@ public Command parseCommand(String userInput) throws ParseException {
case AddTaskCommand.COMMAND_WORD:
return new AddTaskCommandParser().parse(arguments);

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

case EditCommand.COMMAND_WORD:
return new EditCommandParser().parse(arguments);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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 {@code userInput} into a command and returns it.
*
* @param userInput
* @throws ParseException if {@code userInput} does not conform the expected format
*/
@Override
public DeleteTaskCommand parse(String userInput) throws ParseException {
try {
Index taskIndexToDelete = ParserUtil.parseIndex(userInput.trim());
return new DeleteTaskCommand(taskIndexToDelete);
} catch (ParseException pe) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteTaskCommand.MESSAGE_USAGE));
}

}

}
33 changes: 25 additions & 8 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.person.Person;
import seedu.address.model.person.UniquePersonList;
import seedu.address.model.task.Task;

/**
* Wraps all data at the address-book level
Expand All @@ -18,17 +19,21 @@ public class AddressBook implements ReadOnlyAddressBook {
private final UniquePersonList persons;

/*
* The 'unusual' code block below is a non-static initialization block, sometimes used to avoid duplication
* between constructors. See https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
* The 'unusual' code block below is a non-static initialization block,
* sometimes used to avoid duplication
* between constructors. See
* https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
*
* Note that non-static init blocks are not recommended to use. There are other ways to avoid duplication
* among constructors.
* Note that non-static init blocks are not recommended to use. There are other
* ways to avoid duplication
* among constructors.
*/
{
persons = new UniquePersonList();
}

public AddressBook() {}
public AddressBook() {
}

/**
* Creates an AddressBook using the Persons in the {@code toBeCopied}
Expand Down Expand Up @@ -60,7 +65,8 @@ public void resetData(ReadOnlyAddressBook newData) {
//// person-level operations

/**
* Returns true if a person with the same identity as {@code person} exists in the address book.
* Returns true if a person with the same identity as {@code person} exists in
* the address book.
*/
public boolean hasPerson(Person person) {
requireNonNull(person);
Expand All @@ -76,9 +82,11 @@ public void addPerson(Person p) {
}

/**
* Replaces the given person {@code target} in the list with {@code editedPerson}.
* Replaces the given person {@code target} in the list with
* {@code editedPerson}.
* {@code target} must exist in the address book.
* The person identity of {@code editedPerson} must not be the same as another existing person in the address book.
* The person identity of {@code editedPerson} must not be the same as another
* existing person in the address book.
*/
public void setPerson(Person target, Person editedPerson) {
requireNonNull(editedPerson);
Expand All @@ -94,6 +102,15 @@ public void removePerson(Person key) {
persons.remove(key);
}

/**
* Removes {@code task} from the {@code person} in {@code AddressBook}.
* {@code task} must exist in the task list.
*/
public void deleteAssignedTask(Task task) {
requireNonNull(task);
persons.deleteAssignedTask(task);
}

//// util methods

@Override
Expand Down
21 changes: 20 additions & 1 deletion 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 @@ -99,15 +100,33 @@ static void setTaskList(TaskList taskList) {}

/**
* Adds the given task.
* {@code task} must not already exist in the address book.
* {@code task} must not already exist in the task list.
*/
void addTask(Task task);

/**
* Deletes the given task.
* {@code task} the task must exist in the task list.
*/
void deleteTask(Task task);

/**
* Gets the task based on the given index.
* {@code index} the index in the task list.
*/
Task getTask(Index index);


/**
* Returns true if a task has the same description as a {@code task} in the task list.
*/
boolean hasTask(Task task);

/**
* Returns true if the {@code index} is within the task list.
*/
boolean isValidTaskIndex(Index index);

/** Returns an unmodifiable view of the filtered person list */
ObservableList<Person> getFilteredPersonList();

Expand Down
32 changes: 27 additions & 5 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 @@ -43,7 +44,8 @@ public ModelManager() {
this(new AddressBook(), new TaskList(), new UserPrefs());
}

//=========== UserPrefs ==================================================================================
// =========== UserPrefs
// ==================================================================================

@Override
public void setUserPrefs(ReadOnlyUserPrefs userPrefs) {
Expand Down Expand Up @@ -97,7 +99,8 @@ public void setTaskListFilePath(Path taskListFilePath) {
userPrefs.setTaskListFilePath(taskListFilePath);
}

//=========== AddressBook ================================================================================
// =========== AddressBook
// ================================================================================

@Override
public void setAddressBook(ReadOnlyAddressBook addressBook) {
Expand Down Expand Up @@ -133,19 +136,36 @@ public void setPerson(Person target, Person editedPerson) {
addressBook.setPerson(target, editedPerson);
}

//=========== Task Manager ===============================================================================
// =========== Task Manager
// ===============================================================================

@Override
public void addTask(Task task) {
taskList.addTask(task);
}

@Override
public void deleteTask(Task task) {
addressBook.deleteAssignedTask(task);
taskList.deleteTask(task);
}

@Override
public Task getTask(Index index) {
return taskList.getTask(index);
}

@Override
public boolean hasTask(Task task) {
requireNonNull(task);
return taskList.hasTask(task);
}

@Override
public boolean isValidTaskIndex(Index index) {
return taskList.isValidTaskIndex(index);
}

/**
* Replaces task list data with the data in {@code taskList}.
*
Expand All @@ -163,10 +183,12 @@ public TaskList getTaskList() {
return taskList;
}

//=========== Filtered Person List Accessors =============================================================
// =========== Filtered Person List Accessors
// =============================================================

/**
* Returns an unmodifiable view of the list of {@code Person} backed by the internal list of
* Returns an unmodifiable view of the list of {@code Person} backed by the
* internal list of
* {@code versionedAddressBook}
*/
@Override
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/seedu/address/model/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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 @@ -43,6 +44,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 task The task to be deleted.
*/
public void deleteTask(Task task) {
taskList.remove(task);
}

public ObservableList<Task> getSerializeTaskList() {
return FXCollections.unmodifiableObservableList(FXCollections.observableArrayList(taskList));
}
Expand All @@ -51,6 +71,10 @@ public boolean hasTask(Task task) {
return taskList.contains(task);
}

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

@Override
public boolean equals(Object other) {
if (other == this) {
Expand All @@ -65,4 +89,5 @@ public boolean equals(Object other) {
TaskList otherTaskList = (TaskList) other;
return taskList.equals(otherTaskList.taskList);
}

}
Loading

0 comments on commit 7cf9ff9

Please sign in to comment.