Skip to content
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
merged 4 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;

Expand Down Expand Up @@ -91,8 +92,10 @@ private static Assignment createEditedAssignment(Assignment assignmentToEdit,
.orElse(assignmentToEdit.getModuleCode());
Remind updatedRemind = assignmentToEdit.getRemind();
Schedule updatedSchedule = assignmentToEdit.getSchedule();
Priority priority = assignmentToEdit.getPriority();

return new Assignment(updatedName, updatedDeadline, updatedModuleCode, updatedRemind, updatedSchedule);
return new Assignment(updatedName, updatedDeadline, updatedModuleCode, updatedRemind, updatedSchedule,
priority);
}

@Override
Expand Down
86 changes: 86 additions & 0 deletions src/main/java/seedu/address/logic/commands/PrioritizeCommand.java
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;

Expand Down Expand Up @@ -78,8 +79,10 @@ private static Assignment createRemindedAssignment(Assignment assignmentToRemind
ModuleCode updatedModuleCode = assignmentToRemind.getModuleCode();
Remind updatedRemind = assignmentToRemind.getRemind().setReminder();
Schedule updatedSchedule = assignmentToRemind.getSchedule();
Priority priority = assignmentToRemind.getPriority();

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!


return new Assignment(updatedName, updatedDeadline, updatedModuleCode, updatedRemind, updatedSchedule);
return new Assignment(updatedName, updatedDeadline, updatedModuleCode, updatedRemind, updatedSchedule,
priority);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
import seedu.address.model.assignment.Task;
Expand Down Expand Up @@ -158,8 +159,10 @@ private Assignment createScheduledAssignment(Assignment assignmentToSchedule, Sc
Deadline updatedDeadline = assignmentToSchedule.getDeadline();
ModuleCode updatedModuleCode = assignmentToSchedule.getModuleCode();
Remind updatedRemind = assignmentToSchedule.getRemind();
Priority priority = assignmentToSchedule.getPriority();

return new Assignment(updatedName, updatedDeadline, updatedModuleCode, updatedRemind, schedule);
return new Assignment(updatedName, updatedDeadline, updatedModuleCode, updatedRemind, schedule,
priority);
}

@Override
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;

Expand Down Expand Up @@ -77,8 +78,10 @@ private static Assignment createUnremindedAssignment(Assignment assignmentToUnre
ModuleCode updatedModuleCode = assignmentToUnremind.getModuleCode();
Remind updatedRemind = new Remind();
Schedule updatedSchedule = assignmentToUnremind.getSchedule();
Priority priority = assignmentToUnremind.getPriority();

return new Assignment(updatedName, updatedDeadline, updatedModuleCode, updatedRemind, updatedSchedule);
return new Assignment(updatedName, updatedDeadline, updatedModuleCode, updatedRemind, updatedSchedule,
priority);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;

Expand Down Expand Up @@ -54,12 +55,13 @@ public AddCommand parse(String args) throws ParseException {
ModuleCode moduleCode = ParserUtil.parseModuleCode(argMultimap.getValue(PREFIX_MODULE_CODE).get());
Remind remind = new Remind();
Schedule schedule = new Schedule();
Priority priority = new Priority();

if (isRemindPresent) {
remind = remind.setReminder();
}

Assignment assignment = new Assignment(name, deadline, moduleCode, remind, schedule);
Assignment assignment = new Assignment(name, deadline, moduleCode, remind, schedule, priority);
return new AddCommand(assignment);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ImportCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.PrioritizeCommand;
import seedu.address.logic.commands.RemindCommand;
import seedu.address.logic.commands.ScheduleCommand;
import seedu.address.logic.commands.UndoCommand;
import seedu.address.logic.commands.UnprioritizeCommand;
import seedu.address.logic.commands.UnremindCommand;
import seedu.address.logic.parser.exceptions.ParseException;

Expand Down Expand Up @@ -88,6 +90,12 @@ public Command parseCommand(String userInput) throws ParseException {
case ScheduleCommand.COMMAND_WORD:
return new ScheduleCommandParser().parse(arguments);

case PrioritizeCommand.COMMAND_WORD:
return new PrioritizeCommandParser().parse(arguments);

case UnprioritizeCommand.COMMAND_WORD:
return new UnprioritizeCommandParser().parse(arguments);

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public class CliSyntax {
public static final Prefix PREFIX_TIMETABLE_URL = new Prefix("url/");
public static final Prefix PREFIX_EXPECTED_HOURS = new Prefix("expected/");
public static final Prefix PREFIX_DO_BEFORE = new Prefix("before/");
public static final Prefix PREFIX_PRIORITY = new Prefix("priority/");
}
22 changes: 22 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import seedu.address.model.assignment.Deadline;
import seedu.address.model.assignment.ModuleCode;
import seedu.address.model.assignment.Name;
import seedu.address.model.assignment.Priority;

/**
* Contains utility methods used for parsing strings in the various *Parser classes.
Expand Down Expand Up @@ -112,4 +113,25 @@ public static ModuleCode parseModuleCode(String moduleCode) throws ParseExceptio
return new ModuleCode(trimmedModuleCode);
}

/**
* Parses a {@code String priority} into an {@code Priority}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code Priority} is invalid.
*/
public static Priority parsePriority(String priority) throws ParseException {
requireNonNull(priority);
String priorityLevel = priority.trim().toUpperCase();
if (priorityLevel.equals(Priority.LOW_PRIORITY)) {
return new Priority(Priority.LOW_PRIORITY);
}
if (priorityLevel.equals(Priority.MEDIUM_PRIORITY)) {
return new Priority(Priority.MEDIUM_PRIORITY);
}
if (priorityLevel.equals(Priority.HIGH_PRIORITY)) {
return new Priority(Priority.HIGH_PRIORITY);
}
throw new ParseException(Priority.MESSAGE_CONSTRAINTS);
}

}
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());
}
}