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 date exceptions #148

Merged
merged 5 commits into from
Oct 30, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ Format: `add n/NAME [d/DESCRIPTION] [start/START_DATE] [end/END_DATE]`

* Plans cannot have duplicate names.
* `START_DATE` and `END_DATE` should be in the format of `yyyy-MM-dd`
* `START_DATE` should be before or equal to `END_DATE`.

Examples:

Expand Down Expand Up @@ -297,6 +298,7 @@ Format: `editplan INDEX [n/NAME] [d/DESCRIPTION] [start/START_DATE] [end/END_DAT
* Edits the plan at the specified `INDEX`. The index refers to the index number shown in the displayed plan list. The index *must be a positive integer* 1, 2, 3, ...
* At least one of the optional fields must be provided.
* Existing values will be updated to the input values.
* `START_DATE` should be before or equal to `END_DATE`.

Examples:

Expand Down Expand Up @@ -328,6 +330,7 @@ Format: `findplan [n/NAME] [d/DESCRIPTION] [start/RANGE_START] [end/RANGE_END]`
* Start and end range
** is considered a match when there exist overlaps in time. (e.g. a plan with start date of `2019-01-01` and end date of `2019-03-03` matches a plan with start date of `2019-02-02` and end date of `2019-04-04`)
** both `RANGE_START` and `RANGE_END` should be specified.
** `RANGE_START` should be before or equal to `RANGE_END`.

=== Task Management

Expand All @@ -336,6 +339,8 @@ Format: `findplan [n/NAME] [d/DESCRIPTION] [start/RANGE_START] [end/RANGE_END]`
Adds a task to a specified plan. +
Format: `addtask plan/PLAN_INDEX prob/PROBLEM_INDEX [due/TARGET_DATE]`

* `DUE_DATE` should be after or equal to plan's `START_DATE` and before or equal to plan's `END_DATE`.

Examples:

* `addtask plan/1 prob/1 due/2019-12-12`
Expand All @@ -345,6 +350,8 @@ Examples:
Copies a specified task from one plan to another. +
Format: `copytask task/TASK_INDEX from/PLAN_INDEX to/PLAN_INDEX`

* `DUE_DATE` should be after or equal to plan's `START_DATE` and before or equal to plan's `END_DATE`.

Examples:

* `copytask task/1 from/1 to/2`
Expand Down Expand Up @@ -372,6 +379,8 @@ Examples:
Edits the due date of a specified task from a specified plan. +
Format: `edittask plan/PLAN_INDEX task/TASK_INDEX due/DUE_DATE`

* `DUE_DATE` should be after or equal to plan's `START_DATE` and before or equal to plan's `END_DATE`.

Examples:

* `edittask plan/1 task/1 due/2019-12-12`
Expand All @@ -390,6 +399,8 @@ Examples:
Moves a specified task from a specified plan to another. +
Format: `movetask task/TASK_INDEX from/PLAN_INDEX to/PLAN_INDEX`

* `DUE_DATE` should be after or equal to plan's `START_DATE` and before or equal to plan's `END_DATE`.

jiayushe marked this conversation as resolved.
Show resolved Hide resolved
Examples:

* `movetask task/1 from/1 to/2`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_START_DATE;
import static seedu.algobase.model.Model.PREDICATE_SHOW_ALL_PLANS;
import static seedu.algobase.model.searchrule.plansearchrule.TimeRange.ORDER_CONSTRAINTS;
import static seedu.algobase.model.searchrule.plansearchrule.TimeRange.isValidRange;

import java.time.LocalDate;
import java.util.Collections;
Expand Down Expand Up @@ -80,6 +82,10 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
Plan planToEdit = lastShownList.get(index.getZeroBased());
Plan editedPlan = createEditedPlan(planToEdit, editPlanDescriptor);

if (!isValidRange(editedPlan.getStartDate(), editedPlan.getEndDate())) {
throw new CommandException(ORDER_CONSTRAINTS);
}

if (!planToEdit.isSamePlan(editedPlan) && model.hasPlan(editedPlan)) {
throw new CommandException(String.format(MESSAGE_DUPLICATE_PLAN, editedPlan.getPlanName()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
model.updateFilteredPlanList(PREDICATE_SHOW_ALL_PLANS);

return new CommandResult(
String.format(MESSAGE_EDIT_TASK_SUCCESS,
taskToUpdate.getProblem().getName(),
editTaskDescriptor.targetDate,
updatedPlan.getPlanName()));
String.format(MESSAGE_EDIT_TASK_SUCCESS,
taskToUpdate.getProblem().getName(),
editTaskDescriptor.targetDate,
updatedPlan.getPlanName()));
}

@Override
Expand All @@ -112,10 +112,10 @@ public EditTaskDescriptor(Index planIndex, Index problemIndex, LocalDate targetD
@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof EditTaskDescriptor // instanceof handles nulls
&& planIndex.equals(((EditTaskDescriptor) other).planIndex)
&& taskIndex.equals(((EditTaskDescriptor) other).taskIndex)
&& targetDate.equals(((EditTaskDescriptor) other).targetDate));
|| (other instanceof EditTaskDescriptor // instanceof handles nulls
&& planIndex.equals(((EditTaskDescriptor) other).planIndex)
&& taskIndex.equals(((EditTaskDescriptor) other).taskIndex)
&& targetDate.equals(((EditTaskDescriptor) other).targetDate));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public CommandResult execute(Model model, CommandHistory history) throws Command

List<Plan> lastShownPlanList = model.getFilteredPlanList();
if (moveTaskDescriptor.planFromIndex.getZeroBased() >= lastShownPlanList.size()
|| moveTaskDescriptor.planToIndex.getZeroBased() >= lastShownPlanList.size()) {
|| moveTaskDescriptor.planToIndex.getZeroBased() >= lastShownPlanList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}
Plan planFrom = lastShownPlanList.get(moveTaskDescriptor.planFromIndex.getZeroBased());
Expand All @@ -77,7 +77,7 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
Set<Task> taskSetTo = new HashSet<>(taskListTo);
if (taskSetTo.contains(taskToMove)) {
throw new CommandException(
String.format(MESSAGE_DUPLICATE_TASK, taskToMove.getProblem().getName(), planTo.getPlanName()));
String.format(MESSAGE_DUPLICATE_TASK, taskToMove.getProblem().getName(), planTo.getPlanName()));
}
if (!planTo.checkWithinDateRange(taskToMove.getTargetDate())) {
throw new CommandException(Messages.MESSAGE_INVALID_TASK_DATE);
Expand All @@ -91,11 +91,11 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
model.updateFilteredPlanList(PREDICATE_SHOW_ALL_PLANS);

return new CommandResult(
String.format(MESSAGE_MOVE_TASK_SUCCESS,
taskToMove.getProblem().getName(),
updatedPlanFrom.getPlanName(),
updatedPlanTo.getPlanName()
)
String.format(MESSAGE_MOVE_TASK_SUCCESS,
taskToMove.getProblem().getName(),
updatedPlanFrom.getPlanName(),
updatedPlanTo.getPlanName()
)
);
}

Expand Down Expand Up @@ -123,10 +123,10 @@ public MoveTaskDescriptor(Index taskIndex, Index planFromIndex, Index planToInde
@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof MoveTaskDescriptor // instanceof handles nulls
&& taskIndex.equals(((MoveTaskDescriptor) other).taskIndex)
&& planFromIndex.equals(((MoveTaskDescriptor) other).planFromIndex)
&& planToIndex.equals(((MoveTaskDescriptor) other).planToIndex));
|| (other instanceof MoveTaskDescriptor // instanceof handles nulls
&& taskIndex.equals(((MoveTaskDescriptor) other).taskIndex)
&& planFromIndex.equals(((MoveTaskDescriptor) other).planFromIndex)
&& planToIndex.equals(((MoveTaskDescriptor) other).planToIndex));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_END_DATE;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_START_DATE;
import static seedu.algobase.model.searchrule.plansearchrule.TimeRange.ORDER_CONSTRAINTS;
import static seedu.algobase.model.searchrule.plansearchrule.TimeRange.isValidRange;

import java.time.LocalDate;
import java.util.HashSet;
Expand Down Expand Up @@ -61,6 +63,10 @@ public AddPlanCommand parse(String args) throws ParseException {
endDate = LocalDate.now().plusMonths(1);
}

if (!isValidRange(startDate, endDate)) {
throw new ParseException(ORDER_CONSTRAINTS);
}

Set<Task> tasks = new HashSet<>();

Plan plan = new Plan(name, description, startDate, endDate, tasks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_START_DATE;
import static seedu.algobase.logic.parser.ParserUtil.hasPrefixesPresent;
import static seedu.algobase.logic.parser.ParserUtil.parseDate;
import static seedu.algobase.model.searchrule.plansearchrule.TimeRange.ORDER_CONSTRAINTS;
import static seedu.algobase.model.searchrule.plansearchrule.TimeRange.isValidRange;

import java.time.LocalDate;
import java.util.Arrays;
Expand Down Expand Up @@ -56,13 +58,19 @@ public FindPlanCommand parse(String args) throws ParseException {

if (argumentMultimap.getValue(PREFIX_NAME).isPresent()) {
List<String> planNameKeywords = getArgumentValueAsList(argumentMultimap.getValue(PREFIX_NAME).get());
if (planNameKeywords.stream().allMatch(String::isBlank)) {
throw new ParseException(FindPlanCommand.MESSAGE_NO_CONSTRAINTS);
}
List<Keyword> keywords = planNameKeywords.stream().map(Keyword::new).collect(Collectors.toList());
findPlanDescriptor.setPlanNamePredicate(new PlanNameContainsKeywordsPredicate(keywords));
}

if (argumentMultimap.getValue(PREFIX_DESCRIPTION).isPresent()) {
List<String> planDescriptionKeywords =
getArgumentValueAsList(argumentMultimap.getValue(PREFIX_DESCRIPTION).get());
if (planDescriptionKeywords.stream().allMatch(String::isBlank)) {
throw new ParseException(FindPlanCommand.MESSAGE_NO_CONSTRAINTS);
}
List<Keyword> keywords = planDescriptionKeywords.stream().map(Keyword::new).collect(Collectors.toList());
findPlanDescriptor.setPlanDescriptionPredicate(
new PlanDescriptionContainsKeywordsPredicate(keywords));
Expand All @@ -76,16 +84,13 @@ public FindPlanCommand parse(String args) throws ParseException {
}
String start = argumentMultimap.getValue(PREFIX_START_DATE).get();
String end = argumentMultimap.getValue(PREFIX_END_DATE).get();
try {
LocalDate startDate = parseDate(start);
LocalDate endDate = parseDate(end);
TimeRange timeRange = new TimeRange(startDate, endDate);
System.out.print(timeRange);
findPlanDescriptor.setTimeRangePredicate(new TimeRangePredicate(timeRange));
} catch (ParseException e) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindPlanCommand.MESSAGE_USAGE), e);
LocalDate startDate = parseDate(start);
LocalDate endDate = parseDate(end);
if (!isValidRange(startDate, endDate)) {
throw new ParseException(ORDER_CONSTRAINTS);
}
TimeRange timeRange = new TimeRange(startDate, endDate);
findPlanDescriptor.setTimeRangePredicate(new TimeRangePredicate(timeRange));
}

if (!findPlanDescriptor.isAnyFieldProvided()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
*/
public class TimeRange {

public static final String MESSAGE_CONSTRAINTS = "Both start date and end date should be present.";
public static final String MESSAGE_CONSTRAINTS = "Both start date and end date should be present";
public static final String ORDER_CONSTRAINTS = "Starting time should not be after end time.";

public final LocalDate startDate;
public final LocalDate endDate;
Expand All @@ -31,7 +32,7 @@ public TimeRange(LocalDate startDate, LocalDate endDate) {
* Returns true if a given time range is valid.
*/
public static boolean isValidRange(LocalDate startDate, LocalDate endDate) {
return startDate != null && endDate != null;
return startDate != null && endDate != null && !startDate.isAfter(endDate);
}

@Override
Expand Down