Skip to content

Commit

Permalink
Merge pull request #148 from wonyeji/add-mark-as-done-command
Browse files Browse the repository at this point in the history
Add `MarkAsDone` command
  • Loading branch information
thanwinnikki committed Oct 22, 2021
2 parents cf1db9d + 5f0dd26 commit e1f00a9
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TASK;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION;

import java.util.List;

Expand All @@ -22,9 +22,9 @@ public class AddTaskCommand extends Command {

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a task to the group. "
+ "Parameters: "
+ PREFIX_TASK + "TASK "
+ PREFIX_DESCRIPTION + "TASK_DESCRIPTION"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_TASK + "Read book";
+ PREFIX_DESCRIPTION + "Read book";

public static final String MESSAGE_SUCCESS = "New task added: %1$s";
public static final String MESSAGE_DUPLICATE_TASK = "This task already exists in the group";
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/seedu/address/logic/commands/MarkAsDoneCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import java.util.List;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.group.Group;
import seedu.address.model.task.Task;
import seedu.address.model.task.UniqueTaskList;

public class MarkAsDoneCommand extends Command {
public static final String COMMAND_WORD = "done";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Marks a task as done "
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";

public static final String MESSAGE_SUCCESS = "Task marked as done: %1$s";
public static final String MESSAGE_TASK_ALREADY_DONE = "This task has already been marked as done!";

private final Index targetIndex;

private final Index firstIndex = Index.fromZeroBased(0);

public MarkAsDoneCommand(Index targetIndex) {
this.targetIndex = targetIndex;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Group> lastShownGroupList = model.getFilteredGroupList();
if (lastShownGroupList.size() != 1) {
//todo
throw new CommandException(Messages.MESSAGE_INVALID_GROUP_DISPLAYED_INDEX);
}
Group group = lastShownGroupList.get(firstIndex.getZeroBased());
UniqueTaskList tasks = group.getTasks();

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

Task taskToMarkAsDone = tasks.getTask(targetIndex.getZeroBased());
if (taskToMarkAsDone.getDoneTask()) {
throw new CommandException(MESSAGE_TASK_ALREADY_DONE);
} else {
taskToMarkAsDone.setDoneTask();
}
return new CommandResult(String.format(MESSAGE_SUCCESS, taskToMarkAsDone));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof MarkAsDoneCommand // instanceof handles nulls
&& targetIndex.equals(((MarkAsDoneCommand) other).targetIndex)); // state check
}
}
4 changes: 3 additions & 1 deletion src/main/java/seedu/address/logic/commands/MatesCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public class MatesCommand extends Command {
public static final String COMMAND_WORD = "mates";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Lists members in group index indicated.";
+ ": Lists members in group index indicated."
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";

public static final String MESSAGE_SUCCESS = "Listed all members in group.";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import seedu.address.logic.commands.GroupsCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.MarkAsDoneCommand;
import seedu.address.logic.commands.MatesCommand;
import seedu.address.logic.commands.RemoveCommand;
import seedu.address.logic.parser.exceptions.ParseException;
Expand Down Expand Up @@ -102,6 +103,9 @@ public Command parseCommand(String userInput) throws ParseException {
case AddTaskCommand.COMMAND_WORD:
return new AddTaskCommandParser().parse(arguments);

case MarkAsDoneCommand.COMMAND_WORD:
return new MarkAsDoneCommandParser().parse(arguments);

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.MarkAsDoneCommand;
import seedu.address.logic.parser.exceptions.ParseException;

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

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

0 comments on commit e1f00a9

Please sign in to comment.