forked from nus-cs2103-AY2021S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add prioritize and unprioritize commands #124
Merged
nevirmc
merged 4 commits into
AY2021S1-CS2103T-F11-3:master
from
hyngkng:add-priority-tag
Oct 20, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
86 changes: 86 additions & 0 deletions
86
src/main/java/seedu/address/logic/commands/PrioritizeCommand.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,86 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PRIORITY; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_ASSIGNMENT; | ||
|
||
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.assignment.Assignment; | ||
import seedu.address.model.assignment.Deadline; | ||
import seedu.address.model.assignment.ModuleCode; | ||
import seedu.address.model.assignment.Name; | ||
import seedu.address.model.assignment.Priority; | ||
import seedu.address.model.assignment.Remind; | ||
import seedu.address.model.assignment.Schedule; | ||
|
||
/** | ||
* Sets priority for an assignment identified using it's displayed index from the address book. | ||
*/ | ||
public class PrioritizeCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "prioritize"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Sets LOW, MEDIUM or HIGH priority for the assignment identified by the index number " | ||
+ "used in the displayed assignment list.\n" | ||
+ "Parameters: INDEX (must be a positive integer) " | ||
+ PREFIX_PRIORITY + "PRIORITY LEVEL\n" | ||
+ "Example: " + COMMAND_WORD + " 1 " | ||
+ PREFIX_PRIORITY + Priority.LOW_PRIORITY; | ||
|
||
public static final String MESSAGE_PRIORITIZE_ASSIGNMENT_SUCCESS = "Set priority for Assignment: %1$s"; | ||
|
||
private final Index targetIndex; | ||
private final Priority priority; | ||
|
||
/** | ||
* Constructs a PriorityCommand to set priority to the specified assignment. | ||
* @param targetIndex index of the assignment in the filtered assignment list to prioritise. | ||
* @param priority Priority to be tagged to the assignment. | ||
*/ | ||
public PrioritizeCommand(Index targetIndex, Priority priority) { | ||
requireNonNull(targetIndex); | ||
this.targetIndex = targetIndex; | ||
this.priority = priority; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
List<Assignment> lastShownList = model.getFilteredAssignmentList(); | ||
|
||
if (targetIndex.getZeroBased() >= lastShownList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_ASSIGNMENT_DISPLAYED_INDEX); | ||
} | ||
|
||
Assignment assignmentToPrioritise = lastShownList.get(targetIndex.getZeroBased()); | ||
|
||
Assignment prioritizedAssignment = createPrioritisedAssignment(assignmentToPrioritise, priority); | ||
|
||
model.setAssignment(assignmentToPrioritise, prioritizedAssignment); | ||
model.updateFilteredAssignmentList(PREDICATE_SHOW_ALL_ASSIGNMENT); | ||
return new CommandResult(String.format(MESSAGE_PRIORITIZE_ASSIGNMENT_SUCCESS, prioritizedAssignment)); | ||
} | ||
|
||
/** | ||
* Creates and returns a {@code Assignment} with the details of {@code assignmentToPrioritise}. | ||
*/ | ||
private static Assignment createPrioritisedAssignment(Assignment assignmentToPrioritize, Priority priority) { | ||
assert assignmentToPrioritize != null; | ||
|
||
Name updatedName = assignmentToPrioritize.getName(); | ||
Deadline updatedDeadline = assignmentToPrioritize.getDeadline(); | ||
ModuleCode updatedModuleCode = assignmentToPrioritize.getModuleCode(); | ||
Remind updatedRemind = assignmentToPrioritize.getRemind(); | ||
Schedule updatedSchedule = assignmentToPrioritize.getSchedule(); | ||
Priority updatedPriority = priority; | ||
|
||
return new Assignment(updatedName, updatedDeadline, updatedModuleCode, updatedRemind, updatedSchedule, | ||
updatedPriority); | ||
} | ||
} |
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
84 changes: 84 additions & 0 deletions
84
src/main/java/seedu/address/logic/commands/UnprioritizeCommand.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,84 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_ASSIGNMENT; | ||
|
||
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.assignment.Assignment; | ||
import seedu.address.model.assignment.Deadline; | ||
import seedu.address.model.assignment.ModuleCode; | ||
import seedu.address.model.assignment.Name; | ||
import seedu.address.model.assignment.Priority; | ||
import seedu.address.model.assignment.Remind; | ||
import seedu.address.model.assignment.Schedule; | ||
|
||
/** | ||
* Removes priority for an assignment identified using it's displayed index from the address book. | ||
*/ | ||
public class UnprioritizeCommand extends NegateCommand { | ||
|
||
public static final String COMMAND_WORD_SUFFIX = "prioritize"; | ||
public static final String COMMAND_WORD = NegateCommand.COMMAND_WORD + COMMAND_WORD_SUFFIX; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Removes the priority from the assignment identified by the index number " | ||
+ "used in the displayed assignment list.\n" | ||
+ "Parameters: INDEX (must be a positive integer)\n" | ||
+ "Example: " + COMMAND_WORD + " 1"; | ||
|
||
public static final String MESSAGE_UNPRIORITIZE_ASSIGNMENT_SUCCESS = "Removed priority for Assignment: %1$s"; | ||
public static final String MESSAGE_UNPRIORITIZE_ASSIGNMENT = "This assignment does not have priority set."; | ||
|
||
/** | ||
* Constructs an UnprioritiseCommand to remove priority from the specified assignment. | ||
* @param targetIndex index of the assignment in the filtered assignment list to remove priority. | ||
*/ | ||
public UnprioritizeCommand(Index targetIndex) { | ||
super(targetIndex); | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
List<Assignment> lastShownList = model.getFilteredAssignmentList(); | ||
|
||
if (getTargetIndex().getZeroBased() >= lastShownList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_ASSIGNMENT_DISPLAYED_INDEX); | ||
} | ||
|
||
Assignment assignmentToUnprioritize = lastShownList.get(getTargetIndex().getZeroBased()); | ||
|
||
if (!assignmentToUnprioritize.hasPriority() && model.hasAssignment(assignmentToUnprioritize)) { | ||
throw new CommandException(MESSAGE_UNPRIORITIZE_ASSIGNMENT); | ||
} | ||
|
||
assert(assignmentToUnprioritize.isReminded()); | ||
Assignment unprioritisedAssignment = createUnprioritizedAssignment(assignmentToUnprioritize); | ||
|
||
model.setAssignment(assignmentToUnprioritize, unprioritisedAssignment); | ||
model.updateFilteredAssignmentList(PREDICATE_SHOW_ALL_ASSIGNMENT); | ||
return new CommandResult(String.format(MESSAGE_UNPRIORITIZE_ASSIGNMENT_SUCCESS, unprioritisedAssignment)); | ||
} | ||
|
||
/** | ||
* Creates and returns an {@code Assignment} with the details of {@code assignmentToUnprioritise}. | ||
*/ | ||
private static Assignment createUnprioritizedAssignment(Assignment assignmentToUnprioritize) { | ||
assert assignmentToUnprioritize != null; | ||
|
||
Name updatedName = assignmentToUnprioritize.getName(); | ||
Deadline updatedDeadline = assignmentToUnprioritize.getDeadline(); | ||
ModuleCode updatedModuleCode = assignmentToUnprioritize.getModuleCode(); | ||
Remind updatedRemind = assignmentToUnprioritize.getRemind(); | ||
Schedule updatedSchedule = assignmentToUnprioritize.getSchedule(); | ||
Priority priority = new Priority(); | ||
|
||
return new Assignment(updatedName, updatedDeadline, updatedModuleCode, updatedRemind, updatedSchedule, | ||
priority); | ||
} | ||
} |
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
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
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
49 changes: 49 additions & 0 deletions
49
src/main/java/seedu/address/logic/parser/PrioritizeCommandParser.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,49 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PRIORITY; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.PrioritizeCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.assignment.Priority; | ||
|
||
public class PrioritizeCommandParser implements Parser<PrioritizeCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the PriorityCommand | ||
* and returns a PriorityCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public PrioritizeCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_PRIORITY); | ||
|
||
Index index; | ||
|
||
try { | ||
index = ParserUtil.parseIndex(argMultimap.getPreamble()); | ||
} catch (ParseException pe) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, PrioritizeCommand.MESSAGE_USAGE), | ||
pe); | ||
} | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_PRIORITY)) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, PrioritizeCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
Priority priority = ParserUtil.parsePriority(argMultimap.getValue(PREFIX_PRIORITY).get()); | ||
return new PrioritizeCommand(index, priority); | ||
} | ||
|
||
/** | ||
* Returns true if none of the prefixes contains empty {@code Optional} values in the given | ||
* {@code ArgumentMultimap}. | ||
*/ | ||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps priority can be named as updatedPriority for consistency? But current one is still fine!