From 5ec7187c37810b3de07877c05aaf8d2ed05201e7 Mon Sep 17 00:00:00 2001 From: howenc Date: Mon, 6 Nov 2023 18:47:05 +0800 Subject: [PATCH] Fix bug #122 & #133 --- .../parser/FindMeetingCommandParser.java | 5 +++ .../address/logic/parser/ParserUtil.java | 40 ++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/logic/parser/FindMeetingCommandParser.java b/src/main/java/seedu/address/logic/parser/FindMeetingCommandParser.java index b5fc29e4b07..90fb0f27c72 100644 --- a/src/main/java/seedu/address/logic/parser/FindMeetingCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/FindMeetingCommandParser.java @@ -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()); diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index 723f114a510..d092fb86653 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -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; @@ -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); } @@ -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.