forked from nus-cs2103-AY2122S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
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 #148 from wonyeji/add-mark-as-done-command
Add `MarkAsDone` command
- Loading branch information
Showing
5 changed files
with
101 additions
and
4 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
63 changes: 63 additions & 0 deletions
63
src/main/java/seedu/address/logic/commands/MarkAsDoneCommand.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,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 | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
src/main/java/seedu/address/logic/parser/MarkAsDoneCommandParser.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,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); | ||
} | ||
} | ||
} |