forked from nus-cs2103-AY1920S1/addressbook-level3
-
Notifications
You must be signed in to change notification settings - Fork 7
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 #70 from jiayushe/add-tasks
Add logics for deleting tasks and marking tasks as done/undone
- Loading branch information
Showing
16 changed files
with
603 additions
and
82 deletions.
There are no files selected for viewing
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
94 changes: 94 additions & 0 deletions
94
src/main/java/seedu/algobase/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,94 @@ | ||
package seedu.algobase.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_PLAN; | ||
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_TASK; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import seedu.algobase.commons.core.Messages; | ||
import seedu.algobase.commons.core.index.Index; | ||
import seedu.algobase.logic.commands.exceptions.CommandException; | ||
import seedu.algobase.model.Model; | ||
import seedu.algobase.model.plan.Plan; | ||
import seedu.algobase.model.task.Task; | ||
|
||
/** | ||
* Deletes a Task identified using its index in the Plan and the Plan index. | ||
*/ | ||
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 in the plan.\n" | ||
+ "Parameters: " | ||
+ PREFIX_PLAN + "PLAN" | ||
+ PREFIX_TASK + "TASK\n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ PREFIX_PLAN + "1 " | ||
+ PREFIX_TASK + "10"; | ||
|
||
public static final String MESSAGE_DELETE_TASK_SUCCESS = "Deleted Task: %1$s"; | ||
|
||
private final DeleteTaskDescriptor deleteTaskDescriptor; | ||
|
||
/** | ||
* Creates a DeleteTaskCommand to delete a {@code Task} in the specified {@code Plan} | ||
* | ||
* @param deleteTaskDescriptor details of the plan and problem involved | ||
*/ | ||
public DeleteTaskCommand(DeleteTaskDescriptor deleteTaskDescriptor) { | ||
this.deleteTaskDescriptor = deleteTaskDescriptor; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
List<Plan> lastShownPlanList = model.getFilteredPlanList(); | ||
|
||
if (deleteTaskDescriptor.planIndex.getZeroBased() >= lastShownPlanList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX); | ||
} | ||
|
||
Plan planToUpdate = lastShownPlanList.get(deleteTaskDescriptor.planIndex.getZeroBased()); | ||
List<Task> taskList = new ArrayList<>(planToUpdate.getTasks()); | ||
Task task = taskList.get(deleteTaskDescriptor.taskIndex.getZeroBased()); | ||
taskList.remove(deleteTaskDescriptor.taskIndex.getZeroBased()); | ||
Set<Task> taskSet = new HashSet<>(taskList); | ||
Plan updatedPlan = Plan.createUpdatedPlan(planToUpdate, taskSet); | ||
model.setPlan(planToUpdate, updatedPlan); | ||
return new CommandResult(String.format(MESSAGE_DELETE_TASK_SUCCESS, task)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof DeleteTaskCommand // instanceof handles nulls | ||
&& deleteTaskDescriptor.equals(((DeleteTaskCommand) other).deleteTaskDescriptor)); // state check | ||
} | ||
|
||
/** | ||
* Stores the details of the plan and problem involved. | ||
*/ | ||
public static class DeleteTaskDescriptor { | ||
private Index planIndex; | ||
private Index taskIndex; | ||
|
||
public DeleteTaskDescriptor(Index planIndex, Index problemIndex) { | ||
this.planIndex = planIndex; | ||
this.taskIndex = problemIndex; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof DeleteTaskDescriptor // instanceof handles nulls | ||
&& planIndex.equals(((DeleteTaskDescriptor) other).planIndex) | ||
&& taskIndex.equals(((DeleteTaskDescriptor) other).taskIndex)); | ||
} | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
src/main/java/seedu/algobase/logic/commands/DoneTaskCommand.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,95 @@ | ||
package seedu.algobase.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_PLAN; | ||
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_TASK; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import seedu.algobase.commons.core.Messages; | ||
import seedu.algobase.commons.core.index.Index; | ||
import seedu.algobase.logic.commands.exceptions.CommandException; | ||
import seedu.algobase.model.Model; | ||
import seedu.algobase.model.plan.Plan; | ||
import seedu.algobase.model.task.Task; | ||
|
||
/** | ||
* Marks a Task identified using its index in the Plan and the Plan index as done. | ||
*/ | ||
public class DoneTaskCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "donetask"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Marks the Task identified by the index as done in the plan.\n" | ||
+ "Parameters: " | ||
+ PREFIX_PLAN + "PLAN" | ||
+ PREFIX_TASK + "TASK\n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ PREFIX_PLAN + "1 " | ||
+ PREFIX_TASK + "10"; | ||
|
||
public static final String MESSAGE_DONE_TASK_SUCCESS = "Marked Task as done: %1$s"; | ||
|
||
private final DoneTaskDescriptor doneTaskDescriptor; | ||
|
||
/** | ||
* Creates a DoneTaskCommand to mark a {@code Task} as done in the specified {@code Plan} | ||
* | ||
* @param doneTaskDescriptor details of the plan and problem involved | ||
*/ | ||
public DoneTaskCommand(DoneTaskDescriptor doneTaskDescriptor) { | ||
this.doneTaskDescriptor = doneTaskDescriptor; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
List<Plan> lastShownPlanList = model.getFilteredPlanList(); | ||
|
||
if (doneTaskDescriptor.planIndex.getZeroBased() >= lastShownPlanList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX); | ||
} | ||
|
||
Plan planToUpdate = lastShownPlanList.get(doneTaskDescriptor.planIndex.getZeroBased()); | ||
List<Task> taskList = new ArrayList<>(planToUpdate.getTasks()); | ||
Task task = taskList.get(doneTaskDescriptor.taskIndex.getZeroBased()); | ||
taskList.remove(doneTaskDescriptor.taskIndex.getZeroBased()); | ||
Set<Task> taskSet = new HashSet<>(taskList); | ||
taskSet.add(new Task(task.getProblem(), true)); | ||
Plan updatedPlan = Plan.createUpdatedPlan(planToUpdate, taskSet); | ||
model.setPlan(planToUpdate, updatedPlan); | ||
return new CommandResult(String.format(MESSAGE_DONE_TASK_SUCCESS, task)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof DoneTaskCommand // instanceof handles nulls | ||
&& doneTaskDescriptor.equals(((DoneTaskCommand) other).doneTaskDescriptor)); // state check | ||
} | ||
|
||
/** | ||
* Stores the details of the plan and problem involved. | ||
*/ | ||
public static class DoneTaskDescriptor { | ||
private Index planIndex; | ||
private Index taskIndex; | ||
|
||
public DoneTaskDescriptor(Index planIndex, Index problemIndex) { | ||
this.planIndex = planIndex; | ||
this.taskIndex = problemIndex; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof DoneTaskDescriptor // instanceof handles nulls | ||
&& planIndex.equals(((DoneTaskDescriptor) other).planIndex) | ||
&& taskIndex.equals(((DoneTaskDescriptor) other).taskIndex)); | ||
} | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
src/main/java/seedu/algobase/logic/commands/UndoneTaskCommand.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,95 @@ | ||
package seedu.algobase.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_PLAN; | ||
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_TASK; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import seedu.algobase.commons.core.Messages; | ||
import seedu.algobase.commons.core.index.Index; | ||
import seedu.algobase.logic.commands.exceptions.CommandException; | ||
import seedu.algobase.model.Model; | ||
import seedu.algobase.model.plan.Plan; | ||
import seedu.algobase.model.task.Task; | ||
|
||
/** | ||
* Marks a Task identified using its index in the Plan and the Plan index as undone. | ||
*/ | ||
public class UndoneTaskCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "undonetask"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Marks the Task identified by the index as undone in the plan.\n" | ||
+ "Parameters: " | ||
+ PREFIX_PLAN + "PLAN" | ||
+ PREFIX_TASK + "TASK\n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ PREFIX_PLAN + "1 " | ||
+ PREFIX_TASK + "10"; | ||
|
||
public static final String MESSAGE_UNDONE_TASK_SUCCESS = "Marked Task as undone: %1$s"; | ||
|
||
private final UndoneTaskDescriptor undoneTaskDescriptor; | ||
|
||
/** | ||
* Creates a UndoneTaskCommand to mark a {@code Task} as undone in the specified {@code Plan} | ||
* | ||
* @param undoneTaskDescriptor details of the plan and problem involved | ||
*/ | ||
public UndoneTaskCommand(UndoneTaskDescriptor undoneTaskDescriptor) { | ||
this.undoneTaskDescriptor = undoneTaskDescriptor; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
List<Plan> lastShownPlanList = model.getFilteredPlanList(); | ||
|
||
if (undoneTaskDescriptor.planIndex.getZeroBased() >= lastShownPlanList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX); | ||
} | ||
|
||
Plan planToUpdate = lastShownPlanList.get(undoneTaskDescriptor.planIndex.getZeroBased()); | ||
List<Task> taskList = new ArrayList<>(planToUpdate.getTasks()); | ||
Task task = taskList.get(undoneTaskDescriptor.taskIndex.getZeroBased()); | ||
taskList.remove(undoneTaskDescriptor.taskIndex.getZeroBased()); | ||
Set<Task> taskSet = new HashSet<>(taskList); | ||
taskSet.add(new Task(task.getProblem(), false)); | ||
Plan updatedPlan = Plan.createUpdatedPlan(planToUpdate, taskSet); | ||
model.setPlan(planToUpdate, updatedPlan); | ||
return new CommandResult(String.format(MESSAGE_UNDONE_TASK_SUCCESS, task)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof UndoneTaskCommand // instanceof handles nulls | ||
&& undoneTaskDescriptor.equals(((UndoneTaskCommand) other).undoneTaskDescriptor)); // state check | ||
} | ||
|
||
/** | ||
* Stores the details of the plan and problem involved. | ||
*/ | ||
public static class UndoneTaskDescriptor { | ||
private Index planIndex; | ||
private Index taskIndex; | ||
|
||
public UndoneTaskDescriptor(Index planIndex, Index problemIndex) { | ||
this.planIndex = planIndex; | ||
this.taskIndex = problemIndex; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof UndoneTaskDescriptor // instanceof handles nulls | ||
&& planIndex.equals(((UndoneTaskDescriptor) other).planIndex) | ||
&& taskIndex.equals(((UndoneTaskDescriptor) other).taskIndex)); | ||
} | ||
} | ||
} |
Oops, something went wrong.