Skip to content

Commit

Permalink
Merge pull request #86 from m1oojv/megan/mark-unmark-tasks
Browse files Browse the repository at this point in the history
feat: Implement mark and unmark tasks
  • Loading branch information
jiakai-17 authored Oct 18, 2023
2 parents 2f98290 + 36be4a7 commit fc8ba87
Show file tree
Hide file tree
Showing 32 changed files with 931 additions and 67 deletions.
5 changes: 4 additions & 1 deletion src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Messages {
public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
public static final String MESSAGE_INVALID_TASK_DISPLAYED_INDEX = "The task index provided is invalid";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_TASKS_LISTED_OVERVIEW = "%1$d tasks listed!";
public static final String MESSAGE_DUPLICATE_FIELDS =
Expand Down Expand Up @@ -57,7 +58,9 @@ public static String format(Task task) {
final StringBuilder builder = new StringBuilder();
builder.append(task.getTitle())
.append("; Note: ")
.append(task.getNote());
.append(task.getNote())
.append("; Status: ")
.append(task.getStatus());
return builder.toString();
}

Expand Down
83 changes: 83 additions & 0 deletions src/main/java/seedu/address/logic/commands/MarkTaskCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.task.Status.STATUS_DONE;

import java.util.List;

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

/**
* Marks the status of an existing task in the task list as done.
*/
public class MarkTaskCommand extends Command {

public static final String COMMAND_WORD = "markTask";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Mark status as done for the task specified "
+ "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_MARK_TASK_SUCCESS = "Marked as Done: %1$s";
public static final String MESSAGE_HAS_BEEN_MARKED = "This task is already marked as done in the task list.";

private final Index index;

/**
* Constructs a MarkTaskCommand.
* @param index of the task in the filtered task list to mark as done
*/
public MarkTaskCommand(Index index) {
requireNonNull(index);

this.index = index;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Task> lastShownList = model.getFilteredTaskList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}

Task taskToMark = lastShownList.get(index.getZeroBased());

if (taskToMark.getStatus().equals(STATUS_DONE)) {
throw new CommandException(MESSAGE_HAS_BEEN_MARKED);
}

Task markedTask = model.markTask(taskToMark);
return new CommandResult(String.format(MESSAGE_MARK_TASK_SUCCESS, Messages.format(markedTask)));
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof MarkTaskCommand)) {
return false;
}

MarkTaskCommand otherMarkTaskCommand = (MarkTaskCommand) other;
return index.equals(otherMarkTaskCommand.index);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("targetIndex", index)
.toString();
}
}

82 changes: 82 additions & 0 deletions src/main/java/seedu/address/logic/commands/UnmarkTaskCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.task.Status.STATUS_NOT_DONE;

import java.util.List;

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

/**
* Marks the status of an existing task in the task list as not done.
*/
public class UnmarkTaskCommand extends Command {

public static final String COMMAND_WORD = "unmarkTask";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Mark status as not done for the task specified "
+ "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_UNMARK_TASK_SUCCESS = "Marked as Not Done: %1$s";
public static final String MESSAGE_HAS_BEEN_MARKED = "This task is already marked as not done in the task list.";

private final Index index;

/**
* @param index of the task in the filtered task list to mark as not done
*/
public UnmarkTaskCommand(Index index) {
requireNonNull(index);

this.index = index;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Task> lastShownList = model.getFilteredTaskList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}

Task taskToMark = lastShownList.get(index.getZeroBased());

if (taskToMark.getStatus().equals(STATUS_NOT_DONE)) {
throw new CommandException(MESSAGE_HAS_BEEN_MARKED);
}

Task unmarkedTask = model.unmarkTask(taskToMark);
return new CommandResult(String.format(MESSAGE_UNMARK_TASK_SUCCESS, Messages.format(unmarkedTask)));
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof UnmarkTaskCommand)) {
return false;
}

UnmarkTaskCommand otherUnmarkTaskCommand = (UnmarkTaskCommand) other;
return index.equals(otherUnmarkTaskCommand.index);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("targetIndex", index)
.toString();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import seedu.address.logic.commands.FindTaskCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListPersonCommand;
import seedu.address.logic.commands.MarkTaskCommand;
import seedu.address.logic.commands.UnmarkTaskCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -73,6 +75,12 @@ public Command parseCommand(String userInput) throws ParseException {
case ListPersonCommand.COMMAND_WORD:
return new ListPersonCommand();

case MarkTaskCommand.COMMAND_WORD:
return new MarkTaskCommandParser().parse(arguments);

case UnmarkTaskCommand.COMMAND_WORD:
return new UnmarkTaskCommandParser().parse(arguments);

case AddTaskCommand.COMMAND_WORD:
return new AddTaskCommandParser().parse(arguments);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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.MarkTaskCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new MarkTaskCommand object
*/
public class MarkTaskCommandParser implements Parser<MarkTaskCommand> {

/**
* Parses the given {@code String} of arguments in the context of the MarkTaskCommand
* and returns a MarkTaskCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public MarkTaskCommand parse(String args) throws ParseException {
try {
Index index = ParserUtil.parseIndex(args);
return new MarkTaskCommand(index);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, MarkTaskCommand.MESSAGE_USAGE), pe);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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.UnmarkTaskCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new UnmarkTaskCommand object
*/
public class UnmarkTaskCommandParser implements Parser<UnmarkTaskCommand> {

/**
* Parses the given {@code String} of arguments in the context of the UnmarkTaskCommand
* and returns a UnmarkTaskCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public UnmarkTaskCommand parse(String args) throws ParseException {
try {
Index index = ParserUtil.parseIndex(args);
return new UnmarkTaskCommand(index);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, UnmarkTaskCommand.MESSAGE_USAGE), pe);
}
}

}
11 changes: 11 additions & 0 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ public void addTask(Task t) {
tasks.add(t);
}

/**
* Replaces the given task {@code target} in the list with {@code editedTask}.
* {@code target} must exist in the address book.
* The task information of {@code editedTask} must not be the same as another existing task in the address book.
*/
public void setTask(Task target, Task editedTask) {
requireNonNull(editedTask);

tasks.setTask(target, editedTask);
}

//// util methods

@Override
Expand Down
29 changes: 26 additions & 3 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,24 @@ public interface Model {
//=========== Task-Level operations ======================================================================

/**
* Returns true if a task with the same title and note as {@code task} exists in the address book.
* Returns true if a task with the same title and note as {@code task} exists in
* the task list.
*/
boolean hasTask(Task task);

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

/**
* Replaces the given task {@code target} with {@code editedTask}.
* {@code target} must exist in the task list.
* The task information of {@code editedTask} must not be the same as another existing task in the task list.
*/
void setTask(Task target, Task editedTask);

/**
* Returns an unmodifiable view of the filtered task list
*/
Expand All @@ -126,4 +134,19 @@ public interface Model {
* @throws NullPointerException if {@code predicate} is null.
*/
void updateFilteredTaskList(Predicate<Task> predicate);

/**
* Updates the task of the filtered task list to mark it as done.
*
* @param task The specific task to mark in the filtered task list.
*/
Task markTask(Task task);

/**
* Updates the task of the filtered task list to mark it as not done.
*
* @param task The specific task to mark in the filtered task list.
*/
Task unmarkTask(Task task);

}
21 changes: 20 additions & 1 deletion src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,26 @@ public void addTask(Task task) {
updateFilteredTaskList(PREDICATE_SHOW_ALL_TASKS);
}

//=========== Filtered Task List Accessors ===============================================================
@Override
public void setTask(Task target, Task editedTask) {
requireAllNonNull(target, editedTask);

addressBook.setTask(target, editedTask);
}

@Override
public Task markTask(Task task) {
setTask(task, task.markDone());
return task.markDone();
}

@Override
public Task unmarkTask(Task task) {
setTask(task, task.unmarkDone());
return task.unmarkDone();
}

//=========== Filtered Task List Accessors =============================================================

/**
* Returns an unmodifiable view of the list of {@code Task} backed by the
Expand Down
Loading

0 comments on commit fc8ba87

Please sign in to comment.