Skip to content

Commit

Permalink
Add date exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Seris370 committed Oct 30, 2019
1 parent 4a253d4 commit 238b8d4
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 13 deletions.
9 changes: 9 additions & 0 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,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 @@ -298,6 +299,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 All @@ -316,6 +318,8 @@ Format: `listplan`
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 Down Expand Up @@ -343,6 +347,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 @@ -361,6 +367,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`.

Examples:

* `movetask task/1 from/1 to/2`
Expand All @@ -383,6 +391,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 after or equal to `RANGE_END`.

=== Tabs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_PLAN;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_PROBLEM;
import static seedu.algobase.model.Model.PREDICATE_SHOW_ALL_PLANS;
import static seedu.algobase.model.task.Task.MESSAGE_DATE_EXCEPTION;

import java.time.LocalDate;
import java.util.HashSet;
Expand Down Expand Up @@ -78,6 +79,11 @@ public CommandResult execute(Model model) throws CommandException {
task = new Task(problem, planToUpdate.getEndDate(), false);
}

if (task.getTargetDate().isAfter(planToUpdate.getEndDate())
|| task.getTargetDate().isBefore(planToUpdate.getStartDate())) {
throw new CommandException(MESSAGE_DATE_EXCEPTION);
}

Set<Task> taskSet = new HashSet<>(planToUpdate.getTasks());
if (taskSet.contains(task)) {
return new CommandResult(
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_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) throws CommandException {
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 @@ -5,6 +5,7 @@
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_PLAN;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_TASK;
import static seedu.algobase.model.Model.PREDICATE_SHOW_ALL_PLANS;
import static seedu.algobase.model.task.Task.MESSAGE_DATE_EXCEPTION;

import java.time.LocalDate;
import java.util.ArrayList;
Expand Down Expand Up @@ -63,6 +64,12 @@ public CommandResult execute(Model model) throws CommandException {
Plan planToUpdate = lastShownPlanList.get(editTaskDescriptor.planIndex.getZeroBased());
List<Task> taskList = new ArrayList<>(planToUpdate.getTasks());
Task taskToUpdate = taskList.get(editTaskDescriptor.taskIndex.getZeroBased());

if (taskToUpdate.getTargetDate().isAfter(planToUpdate.getEndDate())
|| taskToUpdate.getTargetDate().isBefore(planToUpdate.getStartDate())) {
throw new CommandException(MESSAGE_DATE_EXCEPTION);
}

taskList.remove(editTaskDescriptor.taskIndex.getZeroBased());
Set<Task> taskSet = new HashSet<>(taskList);
taskSet.add(Task.updateDueDate(taskToUpdate, editTaskDescriptor.targetDate));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_PLAN_TO;
import static seedu.algobase.logic.parser.CliSyntax.PREFIX_TASK;
import static seedu.algobase.model.Model.PREDICATE_SHOW_ALL_PLANS;
import static seedu.algobase.model.task.Task.MESSAGE_DATE_EXCEPTION;

import java.util.ArrayList;
import java.util.HashSet;
Expand Down Expand Up @@ -64,11 +65,18 @@ public CommandResult execute(Model model) throws CommandException {
Plan planFrom = lastShownPlanList.get(moveTaskDescriptor.planFromIndex.getZeroBased());
List<Task> taskListFrom = new ArrayList<>(planFrom.getTasks());
Task taskToMove = taskListFrom.get(moveTaskDescriptor.taskIndex.getZeroBased());

Plan planTo = lastShownPlanList.get(moveTaskDescriptor.planToIndex.getZeroBased());

if (taskToMove.getTargetDate().isAfter(planTo.getEndDate())
|| taskToMove.getTargetDate().isBefore(planTo.getStartDate())) {
throw new CommandException(MESSAGE_DATE_EXCEPTION);
}

taskListFrom.remove(moveTaskDescriptor.taskIndex.getZeroBased());
Set<Task> taskSetFrom = new HashSet<>(taskListFrom);
Plan updatedPlanFrom = Plan.updateTasks(planFrom, taskSetFrom);

Plan planTo = lastShownPlanList.get(moveTaskDescriptor.planToIndex.getZeroBased());
List<Task> taskListTo = new ArrayList<>(planTo.getTasks());
Set<Task> taskSetTo = new HashSet<>(taskListTo);
if (taskSetTo.contains(taskToMove)) {
Expand Down
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
1 change: 0 additions & 1 deletion src/main/java/seedu/algobase/model/plan/Plan.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public Plan(Id id, PlanName planName, PlanDescription planDescription, LocalDate
*/
public static Plan updateTasks(Plan planToUpdate, Set<Task> taskSet) {
assert planToUpdate != null;

Id id = planToUpdate.id;
PlanName name = planToUpdate.planName;
PlanDescription description = planToUpdate.planDescription;
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
2 changes: 2 additions & 0 deletions src/main/java/seedu/algobase/model/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
public class Task {

public static final String MESSAGE_DATE_EXCEPTION = "Task's due date should be within plan's time range.";

private final Id id;
private final Problem problem;
private final Boolean isSolved;
Expand Down

0 comments on commit 238b8d4

Please sign in to comment.