From ac1ed4b58dd878b5f797d2c725b65273c5a3cdf5 Mon Sep 17 00:00:00 2001 From: nixonwidjaja Date: Thu, 2 Nov 2023 23:46:30 +0800 Subject: [PATCH 1/5] Fix bug invalid months --- .../address/logic/parser/BirthdayCommandParser.java | 10 +++++++--- src/main/java/seedu/address/model/person/Month.java | 6 +++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/java/seedu/address/logic/parser/BirthdayCommandParser.java b/src/main/java/seedu/address/logic/parser/BirthdayCommandParser.java index 6c513e5d37d..6187a214c90 100644 --- a/src/main/java/seedu/address/logic/parser/BirthdayCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/BirthdayCommandParser.java @@ -49,13 +49,17 @@ public BirthdayCommand parse(String args) throws ParseException { * @param arg Months as strings * @return a list of Months */ - public List parseBirthday(String arg) { + public List parseBirthday(String arg) throws ParseException { arg = arg.trim(); List monthList = new ArrayList<>(); String[] args = arg.split(","); for (String s : args) { - int month = Integer.parseInt(s); - monthList.add(new Month(month)); + try { + int month = Integer.parseInt(s); + monthList.add(new Month(month)); + } catch (Exception e) { + throw new ParseException(Month.INVALID_MONTH); + } } return monthList; } diff --git a/src/main/java/seedu/address/model/person/Month.java b/src/main/java/seedu/address/model/person/Month.java index 752090c5165..97c2de6c0a3 100644 --- a/src/main/java/seedu/address/model/person/Month.java +++ b/src/main/java/seedu/address/model/person/Month.java @@ -1,5 +1,7 @@ package seedu.address.model.person; +import static seedu.address.commons.util.AppUtil.checkArgument; + /** * Represents the month of the dob of the person. */ @@ -15,6 +17,7 @@ public class Month { "Month provided cannot be 0."; public static final String MESSAGE_CONSTRAINTS_MONTH_INVALID_CHARACTERS = "Month provided cannot be a decimal nor contain alphabets."; + public static final String INVALID_MONTH = "Invalid month(s) provided."; public final int month; @@ -23,6 +26,7 @@ public class Month { * @param monthValue the integer representation of the intended month. */ public Month(int monthValue) { + checkArgument(monthValue > 0 && monthValue < 13, INVALID_MONTH); month = monthValue; } @@ -71,7 +75,7 @@ public static boolean isNegativeMonth(String monthString) { public static boolean isValidMonth(String monthString) { String monthTrimmed = monthString.trim(); int value = Integer.parseInt(monthTrimmed); - return value < 13; + return value > 0 && value < 13; } public int getMonthValue() { From cfaff4096428729bac6cb895009d2ae448a886d8 Mon Sep 17 00:00:00 2001 From: nixonwidjaja Date: Fri, 3 Nov 2023 00:03:12 +0800 Subject: [PATCH 2/5] Require non null --- src/main/java/seedu/address/model/person/Month.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/seedu/address/model/person/Month.java b/src/main/java/seedu/address/model/person/Month.java index 97c2de6c0a3..3495f384077 100644 --- a/src/main/java/seedu/address/model/person/Month.java +++ b/src/main/java/seedu/address/model/person/Month.java @@ -1,5 +1,6 @@ package seedu.address.model.person; +import static java.util.Objects.requireNonNull; import static seedu.address.commons.util.AppUtil.checkArgument; /** @@ -26,6 +27,7 @@ public class Month { * @param monthValue the integer representation of the intended month. */ public Month(int monthValue) { + requireNonNull(monthValue); checkArgument(monthValue > 0 && monthValue < 13, INVALID_MONTH); month = monthValue; } From 0839cfd1cb736fe352416f1c37d1cd677f6e8d04 Mon Sep 17 00:00:00 2001 From: nixonwidjaja Date: Fri, 3 Nov 2023 00:06:32 +0800 Subject: [PATCH 3/5] Add test --- .../seedu/address/logic/parser/BirthdayCommandParserTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/seedu/address/logic/parser/BirthdayCommandParserTest.java b/src/test/java/seedu/address/logic/parser/BirthdayCommandParserTest.java index cf034915a85..62bd176c173 100644 --- a/src/test/java/seedu/address/logic/parser/BirthdayCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/BirthdayCommandParserTest.java @@ -46,5 +46,6 @@ public void parse_noArgs_returnsBirthdayCommand() throws ParseException { @Test public void parse_invalidArgs_exceptionThrown() throws ParseException { assertThrows(ParseException.class, () -> parser.parse(" r/2")); + assertThrows(ParseException.class, () -> parser.parse(" m/a")); } } From 5686dac42bf4ea711c765c8087df8c372ef58563 Mon Sep 17 00:00:00 2001 From: nixonwidjaja Date: Fri, 3 Nov 2023 00:11:04 +0800 Subject: [PATCH 4/5] Add tests --- src/test/java/seedu/address/model/person/MonthTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/java/seedu/address/model/person/MonthTest.java b/src/test/java/seedu/address/model/person/MonthTest.java index 68d05daf5b3..85ed5a71b33 100644 --- a/src/test/java/seedu/address/model/person/MonthTest.java +++ b/src/test/java/seedu/address/model/person/MonthTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; @@ -17,6 +18,8 @@ public class MonthTest { public void execute_monthEqualsSuccess() { Month testMonth = new Month(1); assertEquals(new Month(Integer.parseInt(VALID_MONTH)), testMonth); + assertEquals(testMonth, testMonth); + assertNotEquals(testMonth, null); } @Test From af9c2f937c7f93465ba41f3bda3433151f4f7e3f Mon Sep 17 00:00:00 2001 From: nixonwidjaja Date: Fri, 3 Nov 2023 00:17:34 +0800 Subject: [PATCH 5/5] Add tests --- src/test/java/seedu/address/model/person/MonthTest.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/test/java/seedu/address/model/person/MonthTest.java b/src/test/java/seedu/address/model/person/MonthTest.java index 85ed5a71b33..f52eb3b6996 100644 --- a/src/test/java/seedu/address/model/person/MonthTest.java +++ b/src/test/java/seedu/address/model/person/MonthTest.java @@ -3,6 +3,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; @@ -22,6 +23,13 @@ public void execute_monthEqualsSuccess() { assertNotEquals(testMonth, null); } + @Test + public void execute_monthFailure() { + assertThrows(Exception.class, () -> new Month(0)); + assertThrows(Exception.class, () -> new Month(13)); + assertThrows(Exception.class, () -> new Month(100000)); + } + @Test public void execute_containsAlphabetsSuccess() { assertTrue(Month.containsAlphabetsOrDecimals(MONTH_WITH_CHARACTERS)); @@ -60,6 +68,7 @@ public void execute_doesNotContainsNegativeSuccess() { @Test public void execute_invalidMonth() { assertFalse(Month.isValidMonth(INVALID_MONTH)); + assertFalse(Month.isValidMonth(NEGATIVE_MONTH)); } @Test