Skip to content

Commit

Permalink
Merge pull request #70 from jiayushe/add-tasks
Browse files Browse the repository at this point in the history
Add logics for deleting tasks and marking tasks as done/undone
  • Loading branch information
jiayushe committed Oct 15, 2019
2 parents efaaffa + 76560cd commit 8b042fb
Show file tree
Hide file tree
Showing 16 changed files with 603 additions and 82 deletions.
1 change: 1 addition & 0 deletions src/main/java/seedu/algobase/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Messages {
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_PLAN_DISPLAYED_INDEX = "The Plan index provided is invalid";
public static final String MESSAGE_INVALID_PROBLEM_DISPLAYED_INDEX = "The Problem index provided is invalid";
public static final String MESSAGE_INVALID_TASK_DISPLAYED_INDEX = "The Task index provided is invalid";
public static final String MESSAGE_PROBLEMS_LISTED_OVERVIEW = "%1$d problems listed!";
public static final String MESSAGE_PLANS_LISTED_OVERVIEW = "%1$d plans listed!";
}
11 changes: 8 additions & 3 deletions src/main/java/seedu/algobase/logic/commands/AddTaskCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_PROBLEM;

import java.util.List;
import java.util.Set;

import seedu.algobase.commons.core.Messages;
import seedu.algobase.commons.core.index.Index;
Expand All @@ -21,7 +22,8 @@ public class AddTaskCommand extends Command {

public static final String COMMAND_WORD = "addtask";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a task to a training plan. "
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Adds a task to a training plan. "
+ "Parameters: "
+ PREFIX_PLAN + "PLAN "
+ PREFIX_PROBLEM + "PROBLEM\n"
Expand Down Expand Up @@ -61,7 +63,10 @@ public CommandResult execute(Model model) throws CommandException {
Plan plan = lastShownPlanList.get(addTaskDescriptor.planIndex.getZeroBased());
Problem problem = lastShownProblemList.get(addTaskDescriptor.problemIndex.getZeroBased());
Task task = new Task(problem);
plan.getTasks().add(task);
Set<Task> taskSet = plan.getTasks();
taskSet.add(task);
Plan updatedPlan = Plan.createUpdatedPlan(plan, taskSet);
model.setPlan(plan, updatedPlan);

return new CommandResult(String.format(MESSAGE_SUCCESS, task, plan));
}
Expand All @@ -74,7 +79,7 @@ public boolean equals(Object other) {
}

/**
* Stores the details of the plan and problem involced.
* Stores the details of the plan and problem involved.
*/
public static class AddTaskDescriptor {
private Index planIndex;
Expand Down
94 changes: 94 additions & 0 deletions src/main/java/seedu/algobase/logic/commands/DeleteTaskCommand.java
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 src/main/java/seedu/algobase/logic/commands/DoneTaskCommand.java
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 src/main/java/seedu/algobase/logic/commands/UndoneTaskCommand.java
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));
}
}
}
Loading

0 comments on commit 8b042fb

Please sign in to comment.