Skip to content

Commit

Permalink
Added check for chronological events
Browse files Browse the repository at this point in the history
  • Loading branch information
ruiyangzh committed Sep 21, 2023
1 parent 80d2ff1 commit 132b5c9
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,26 @@ private static Executable parseEventParams(String paramString) throws InvalidVar
String endString = matcher.group(3);
LocalDate startTime = parseLocalDate(startString);
LocalDate endTime = parseLocalDate(endString);
ensureChronologicalTimes(startTime, endTime);
Event event = new Event(name, startTime, endTime);
return new AddTaskExecutable(event);
}

//Following method was added on friend's suggestion to ensure events make sense
//(rocketninja7 on GitHub).
private static void ensureChronologicalTimes(LocalDate... dates) throws InvalidVarException {
if (dates.length == 0 || dates[0] == null) {
return;
}
LocalDate currMax = dates[0];
for (LocalDate date : dates) {
if (date.isBefore(currMax)) {
throw new InvalidVarException("Dates must be in chronological order!");
} else {
currMax = date;
}
}
}
private static Executable parseDeleteParams(String paramString) throws InvalidVarException {
return new DeleteExecutable(parseIndex(paramString));
}
Expand Down

0 comments on commit 132b5c9

Please sign in to comment.