Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
howenc committed Nov 6, 2023
1 parent b2f8fea commit 5ec7187
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public FindMeetingCommand parse(String args) throws ParseException {
LocalDateTime start = LocalDateTime.MIN;
LocalDateTime end = LocalDateTime.MAX;

if (argMultimap.getValue(PREFIX_START).isPresent() && argMultimap.getValue(PREFIX_END).isEmpty()
|| argMultimap.getValue(PREFIX_START).isEmpty() && argMultimap.getValue(PREFIX_END).isPresent()) {
throw new ParseException("Please input both start and end times");
}

if (argMultimap.getValue(PREFIX_START).isPresent() && argMultimap.getValue(PREFIX_END).isPresent()) {
start = ParserUtil.parseMeetingTime(argMultimap.getValue(PREFIX_START).get());
end = ParserUtil.parseMeetingTime(argMultimap.getValue(PREFIX_END).get());
Expand Down
40 changes: 38 additions & 2 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import static seedu.address.logic.Messages.MESSAGE_TOO_MANY_INDEXES;

import java.time.LocalDateTime;
import java.time.Month;
import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
Expand Down Expand Up @@ -191,7 +193,12 @@ public static LocalDateTime parseContactTime(String time) throws ParseException
if (!LastContactedTime.isValidLastContactedTime(preppedTime)) {
throw new ParseException(LastContactedTime.MESSAGE_CONSTRAINTS);
}
return preppedTime;
if (checkCorrectDay(preppedTime, time)) {
return preppedTime;
} else {
throw new ParseException(preppedTime.getMonth().toString()
+ " does not have " + time.substring(0, 2) + " days.");
}
} catch (DateTimeParseException e) {
throw new ParseException(LastContactedTime.MESSAGE_CONSTRAINTS);
}
Expand All @@ -207,12 +214,41 @@ public static LocalDateTime parseMeetingTime(String time) throws ParseException
requireNonNull(time);
String trimmedStart = time.trim();
try {
return LocalDateTime.parse(trimmedStart, FORMAT);
LocalDateTime result = LocalDateTime.parse(trimmedStart, FORMAT);
if (checkCorrectDay(result, time)) {
return result;
} else {
throw new ParseException(result.getMonth().toString()
+ "does not have " + time.substring(0, 2) + " days.");
}
} catch (DateTimeParseException e) {
throw new ParseException(MeetingTime.MESSAGE_CONSTRAINTS);
}
}

private static boolean checkCorrectDay(LocalDateTime localDateTime, String string) {
if (Year.isLeap(localDateTime.getYear())
&& (localDateTime.getMonth() == Month.FEBRUARY && string.startsWith("29"))) {
return true;
}
Month thisMonth = localDateTime.getMonth();
if (!has31Days(thisMonth) && string.startsWith("31")) {
return false;
}
return thisMonth != Month.FEBRUARY
|| (!string.startsWith("29") && !string.startsWith("30"));
}

private static boolean has31Days(Month month) {
requireNonNull(month);
switch (month) {
case FEBRUARY: case APRIL: case JUNE: case SEPTEMBER: case NOVEMBER:
return false;
default:
return true;
}
}

/**
* Parses a {@code String tag} into a {@code Tag}.
* Leading and trailing whitespaces will be trimmed.
Expand Down

0 comments on commit 5ec7187

Please sign in to comment.