Skip to content

Commit

Permalink
Merge pull request AY2324S1-CS2103-F13-2#136 from nixonwidjaja/fix-month
Browse files Browse the repository at this point in the history
Fix bug invalid months
  • Loading branch information
sheryew committed Nov 2, 2023
2 parents 93923b8 + af9c2f9 commit 9abc1b3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ public BirthdayCommand parse(String args) throws ParseException {
* @param arg Months as strings
* @return a list of Months
*/
public List<Month> parseBirthday(String arg) {
public List<Month> parseBirthday(String arg) throws ParseException {
arg = arg.trim();
List<Month> 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;
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/seedu/address/model/person/Month.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package seedu.address.model.person;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;

/**
* Represents the month of the dob of the person.
*/
Expand All @@ -15,6 +18,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;

Expand All @@ -23,6 +27,8 @@ 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;
}

Expand Down Expand Up @@ -71,7 +77,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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
12 changes: 12 additions & 0 deletions src/test/java/seedu/address/model/person/MonthTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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;
Expand All @@ -17,6 +19,15 @@ 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
public void execute_monthFailure() {
assertThrows(Exception.class, () -> new Month(0));
assertThrows(Exception.class, () -> new Month(13));
assertThrows(Exception.class, () -> new Month(100000));
}

@Test
Expand Down Expand Up @@ -57,6 +68,7 @@ public void execute_doesNotContainsNegativeSuccess() {
@Test
public void execute_invalidMonth() {
assertFalse(Month.isValidMonth(INVALID_MONTH));
assertFalse(Month.isValidMonth(NEGATIVE_MONTH));
}

@Test
Expand Down

0 comments on commit 9abc1b3

Please sign in to comment.