From 6d7120b175817fdded0a1ff87c74ce4ef2afdf4e Mon Sep 17 00:00:00 2001 From: KhoonSun47 Date: Wed, 13 Mar 2024 10:36:11 +0800 Subject: [PATCH 1/7] Issue #46: Add Unit Number Update CLISyntax.java: Add prefix for unit number Add UnitNumber.java: Add functionality to add unit number to add command Add UnitNumberTest.java: Add test cases to test UnitNumber.java --- .../seedu/address/logic/parser/CliSyntax.java | 1 + .../seedu/address/model/house/UnitNumber.java | 59 ++++++++++++++++++ .../address/model/house/UnitNumberTest.java | 61 +++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 src/main/java/seedu/address/model/house/UnitNumber.java create mode 100644 src/test/java/seedu/address/model/house/UnitNumberTest.java diff --git a/src/main/java/seedu/address/logic/parser/CliSyntax.java b/src/main/java/seedu/address/logic/parser/CliSyntax.java index 75b1a9bf119..0ba0e476308 100644 --- a/src/main/java/seedu/address/logic/parser/CliSyntax.java +++ b/src/main/java/seedu/address/logic/parser/CliSyntax.java @@ -11,5 +11,6 @@ public class CliSyntax { public static final Prefix PREFIX_EMAIL = new Prefix("e/"); public static final Prefix PREFIX_ADDRESS = new Prefix("a/"); public static final Prefix PREFIX_TAG = new Prefix("t/"); + public static final Prefix PREFIX_UNITNUMBER = new Prefix("unitNo/"); } diff --git a/src/main/java/seedu/address/model/house/UnitNumber.java b/src/main/java/seedu/address/model/house/UnitNumber.java new file mode 100644 index 00000000000..c1ff33b2e34 --- /dev/null +++ b/src/main/java/seedu/address/model/house/UnitNumber.java @@ -0,0 +1,59 @@ +package seedu.address.model.house; + +import static java.util.Objects.requireNonNull; +import static seedu.address.commons.util.AppUtil.checkArgument; + +/** + * Represents a House Address's unit number in the address book. + * Guarantees: immutable; is valid as declared in {@link #isValidUnitNumber(String)} + */ +public class UnitNumber { + + public static final String MESSAGE_CONSTRAINTS = + "The unit number should only contain numbers, and it should only be 2 digits long."; + public static final String VALIDATION_REGEX = "\\d{2}"; + public final String value; + + /** + * Constructs a {@code UnitNumber}. + * + * @param unitNumber A valid unit number. + */ + public UnitNumber(String unitNumber) { + requireNonNull(unitNumber); + checkArgument(isValidUnitNumber(unitNumber), MESSAGE_CONSTRAINTS); + value = unitNumber; + } + + /** + * Returns true if a given string is a valid unit number. + */ + public static boolean isValidUnitNumber(String test) { + return test.matches(VALIDATION_REGEX); + } + + @Override + public String toString() { + return value; + } + + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + + if (!(other instanceof UnitNumber)) { + return false; + } + + UnitNumber otherUnitNumber = (UnitNumber) other; + return value.equals(otherUnitNumber.value); + } + + @Override + public int hashCode() { + return value.hashCode(); + } + +} diff --git a/src/test/java/seedu/address/model/house/UnitNumberTest.java b/src/test/java/seedu/address/model/house/UnitNumberTest.java new file mode 100644 index 00000000000..05f9af8f1d1 --- /dev/null +++ b/src/test/java/seedu/address/model/house/UnitNumberTest.java @@ -0,0 +1,61 @@ +package seedu.address.model.house; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.testutil.Assert.assertThrows; + +import org.junit.jupiter.api.Test; + +public class UnitNumberTest { + + @Test + public void constructor_null_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> new UnitNumber(null)); + } + + @Test + public void constructor_invalidUnitNumber_throwsIllegalArgumentException() { + String invalidUnitNumber = "1"; // Not 2 digits + assertThrows(IllegalArgumentException.class, () -> new UnitNumber(invalidUnitNumber)); + } + + @Test + public void isValidUnitNumber() { + // null unit number + assertThrows(NullPointerException.class, () -> UnitNumber.isValidUnitNumber(null)); + + // invalid unit numbers + assertFalse(UnitNumber.isValidUnitNumber("")); // empty string + assertFalse(UnitNumber.isValidUnitNumber(" ")); // spaces only + assertFalse(UnitNumber.isValidUnitNumber("9")); // less than 2 digits + assertFalse(UnitNumber.isValidUnitNumber("123")); // more than 2 digits + assertFalse(UnitNumber.isValidUnitNumber("ab")); // non-numeric + assertFalse(UnitNumber.isValidUnitNumber("1a")); // alphabets within digits + assertFalse(UnitNumber.isValidUnitNumber(" 12")); // spaces before digits + assertFalse(UnitNumber.isValidUnitNumber("12 ")); // spaces after digits + + // valid unit numbers + assertTrue(UnitNumber.isValidUnitNumber("10")); // exactly 2 digits + assertTrue(UnitNumber.isValidUnitNumber("99")); // exactly 2 digits + } + + @Test + public void equals() { + UnitNumber unitNumber = new UnitNumber("12"); + + // same values -> returns true + assertTrue(unitNumber.equals(new UnitNumber("12"))); + + // same object -> returns true + assertTrue(unitNumber.equals(unitNumber)); + + // null -> returns false + assertFalse(unitNumber.equals(null)); + + // different types -> returns false + assertFalse(unitNumber.equals(5.0f)); + + // different unit number -> returns false + assertFalse(unitNumber.equals(new UnitNumber("34"))); + } +} From b6f302ba6bdba02bd40372e5ca61dde40b19f230 Mon Sep 17 00:00:00 2001 From: KhoonSun47 Date: Wed, 13 Mar 2024 10:50:56 +0800 Subject: [PATCH 2/7] Issue #46: Add Unit Number Update UnitNumberTest.java: Remove two test cases --- src/test/java/seedu/address/model/house/UnitNumberTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/java/seedu/address/model/house/UnitNumberTest.java b/src/test/java/seedu/address/model/house/UnitNumberTest.java index 05f9af8f1d1..6fd31dbde5b 100644 --- a/src/test/java/seedu/address/model/house/UnitNumberTest.java +++ b/src/test/java/seedu/address/model/house/UnitNumberTest.java @@ -31,8 +31,6 @@ public void isValidUnitNumber() { assertFalse(UnitNumber.isValidUnitNumber("123")); // more than 2 digits assertFalse(UnitNumber.isValidUnitNumber("ab")); // non-numeric assertFalse(UnitNumber.isValidUnitNumber("1a")); // alphabets within digits - assertFalse(UnitNumber.isValidUnitNumber(" 12")); // spaces before digits - assertFalse(UnitNumber.isValidUnitNumber("12 ")); // spaces after digits // valid unit numbers assertTrue(UnitNumber.isValidUnitNumber("10")); // exactly 2 digits From 85464473572706410dae9084a5892550cfdb9e55 Mon Sep 17 00:00:00 2001 From: KhoonSun47 Date: Wed, 13 Mar 2024 11:17:40 +0800 Subject: [PATCH 3/7] Issue #46: Add Unit Number Update UnitNumber.java: Change the requirement such that unit number must be at least 1 digit, and at most 3 digits (but cannot be 0) Update UnitNumberTest.java: Change the test cases according to the new requirements --- .../java/seedu/address/model/house/UnitNumber.java | 14 +++++++++----- .../seedu/address/model/house/UnitNumberTest.java | 14 ++++++++------ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main/java/seedu/address/model/house/UnitNumber.java b/src/main/java/seedu/address/model/house/UnitNumber.java index c1ff33b2e34..77e448b0967 100644 --- a/src/main/java/seedu/address/model/house/UnitNumber.java +++ b/src/main/java/seedu/address/model/house/UnitNumber.java @@ -4,14 +4,16 @@ import static seedu.address.commons.util.AppUtil.checkArgument; /** - * Represents a House Address's unit number in the address book. + * Represents a House's unit number in the address book. * Guarantees: immutable; is valid as declared in {@link #isValidUnitNumber(String)} */ public class UnitNumber { public static final String MESSAGE_CONSTRAINTS = - "The unit number should only contain numbers, and it should only be 2 digits long."; - public static final String VALIDATION_REGEX = "\\d{2}"; + "The unit number should only contain numbers, it should be at least 1 digit " + + "and at most 3 digits long, and cannot be 0."; + public static final String VALIDATION_REGEX = "\\d{1,3}"; + public final String value; /** @@ -27,9 +29,12 @@ public UnitNumber(String unitNumber) { /** * Returns true if a given string is a valid unit number. + * + * @param test The string to test. + * @return true if the test matches the VALIDATION_REGEX and is not "0". */ public static boolean isValidUnitNumber(String test) { - return test.matches(VALIDATION_REGEX); + return test.matches(VALIDATION_REGEX) && !test.equals("0"); } @Override @@ -55,5 +60,4 @@ public boolean equals(Object other) { public int hashCode() { return value.hashCode(); } - } diff --git a/src/test/java/seedu/address/model/house/UnitNumberTest.java b/src/test/java/seedu/address/model/house/UnitNumberTest.java index 6fd31dbde5b..5a0dcc60d18 100644 --- a/src/test/java/seedu/address/model/house/UnitNumberTest.java +++ b/src/test/java/seedu/address/model/house/UnitNumberTest.java @@ -15,7 +15,7 @@ public void constructor_null_throwsNullPointerException() { @Test public void constructor_invalidUnitNumber_throwsIllegalArgumentException() { - String invalidUnitNumber = "1"; // Not 2 digits + String invalidUnitNumber = "abcd"; assertThrows(IllegalArgumentException.class, () -> new UnitNumber(invalidUnitNumber)); } @@ -25,16 +25,18 @@ public void isValidUnitNumber() { assertThrows(NullPointerException.class, () -> UnitNumber.isValidUnitNumber(null)); // invalid unit numbers + assertFalse(UnitNumber.isValidUnitNumber("0")); // '0' is invalid assertFalse(UnitNumber.isValidUnitNumber("")); // empty string assertFalse(UnitNumber.isValidUnitNumber(" ")); // spaces only - assertFalse(UnitNumber.isValidUnitNumber("9")); // less than 2 digits - assertFalse(UnitNumber.isValidUnitNumber("123")); // more than 2 digits + assertFalse(UnitNumber.isValidUnitNumber("1234")); // more than 3 digits assertFalse(UnitNumber.isValidUnitNumber("ab")); // non-numeric - assertFalse(UnitNumber.isValidUnitNumber("1a")); // alphabets within digits + assertFalse(UnitNumber.isValidUnitNumber("1a2")); // alphabets within digits // valid unit numbers - assertTrue(UnitNumber.isValidUnitNumber("10")); // exactly 2 digits - assertTrue(UnitNumber.isValidUnitNumber("99")); // exactly 2 digits + assertTrue(UnitNumber.isValidUnitNumber("1")); // minimum valid number + assertTrue(UnitNumber.isValidUnitNumber("01")); // leading 0 is allowed + assertTrue(UnitNumber.isValidUnitNumber("10")); // 2 digits + assertTrue(UnitNumber.isValidUnitNumber("999")); // maximum valid number } @Test From 80336602b901ae22f0519c0355ee3fc3eb9865cf Mon Sep 17 00:00:00 2001 From: KhoonSun47 Date: Wed, 13 Mar 2024 11:29:27 +0800 Subject: [PATCH 4/7] Issue #46: Add Unit Number Update UnitNumber.java: Add another regex to test for the following input '0', '00', '000', which should be invalid since a unit number cannot be '0's Update UnitNumberTest.java: Update the test cases according to the new requirements --- src/main/java/seedu/address/model/house/UnitNumber.java | 5 +++-- src/test/java/seedu/address/model/house/UnitNumberTest.java | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/model/house/UnitNumber.java b/src/main/java/seedu/address/model/house/UnitNumber.java index 77e448b0967..2e2713f7d8e 100644 --- a/src/main/java/seedu/address/model/house/UnitNumber.java +++ b/src/main/java/seedu/address/model/house/UnitNumber.java @@ -11,8 +11,9 @@ public class UnitNumber { public static final String MESSAGE_CONSTRAINTS = "The unit number should only contain numbers, it should be at least 1 digit " - + "and at most 3 digits long, and cannot be 0."; + + "and at most 3 digits long, and cannot be '0', '00' or '000'."; public static final String VALIDATION_REGEX = "\\d{1,3}"; + public static final String ZERO_REGEX = "^0+$"; public final String value; @@ -34,7 +35,7 @@ public UnitNumber(String unitNumber) { * @return true if the test matches the VALIDATION_REGEX and is not "0". */ public static boolean isValidUnitNumber(String test) { - return test.matches(VALIDATION_REGEX) && !test.equals("0"); + return test.matches(VALIDATION_REGEX) && !test.matches(ZERO_REGEX); } @Override diff --git a/src/test/java/seedu/address/model/house/UnitNumberTest.java b/src/test/java/seedu/address/model/house/UnitNumberTest.java index 5a0dcc60d18..eaeb7b3df5e 100644 --- a/src/test/java/seedu/address/model/house/UnitNumberTest.java +++ b/src/test/java/seedu/address/model/house/UnitNumberTest.java @@ -26,6 +26,8 @@ public void isValidUnitNumber() { // invalid unit numbers assertFalse(UnitNumber.isValidUnitNumber("0")); // '0' is invalid + assertFalse(UnitNumber.isValidUnitNumber("00")); // '00' is invalid + assertFalse(UnitNumber.isValidUnitNumber("000")); // '000' is invalid assertFalse(UnitNumber.isValidUnitNumber("")); // empty string assertFalse(UnitNumber.isValidUnitNumber(" ")); // spaces only assertFalse(UnitNumber.isValidUnitNumber("1234")); // more than 3 digits @@ -36,6 +38,7 @@ public void isValidUnitNumber() { assertTrue(UnitNumber.isValidUnitNumber("1")); // minimum valid number assertTrue(UnitNumber.isValidUnitNumber("01")); // leading 0 is allowed assertTrue(UnitNumber.isValidUnitNumber("10")); // 2 digits + assertTrue(UnitNumber.isValidUnitNumber("001")); // 3 digits assertTrue(UnitNumber.isValidUnitNumber("999")); // maximum valid number } From 2126005384b4d8abb38d8db212bf039ac4e6f578 Mon Sep 17 00:00:00 2001 From: KhoonSun47 Date: Thu, 14 Mar 2024 22:44:15 +0800 Subject: [PATCH 5/7] Issue #46: Add Unit Number Update ParserUtil.java: Add parsing of unit numbers Update ParserUtilTest.java: Add test cases for parsing of unit numbers --- .../address/logic/parser/ParserUtil.java | 17 ++++++++++++ .../address/logic/parser/ParserUtilTest.java | 27 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index b117acb9c55..6707a1fef9f 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -9,6 +9,7 @@ import seedu.address.commons.core.index.Index; import seedu.address.commons.util.StringUtil; import seedu.address.logic.parser.exceptions.ParseException; +import seedu.address.model.house.UnitNumber; import seedu.address.model.person.Address; import seedu.address.model.person.Email; import seedu.address.model.person.Name; @@ -25,6 +26,7 @@ public class ParserUtil { /** * Parses {@code oneBasedIndex} into an {@code Index} and returns it. Leading and trailing whitespaces will be * trimmed. + * * @throws ParseException if the specified index is invalid (not non-zero unsigned integer). */ public static Index parseIndex(String oneBasedIndex) throws ParseException { @@ -121,4 +123,19 @@ public static Set parseTags(Collection tags) throws ParseException } return tagSet; } + + /** + * Parses a {@code String unitNumber} into a {@code unitNumber}. + * Leading and trailing whitespaces will be trimmed. + * + * @throws ParseException if the given {@code unitNumber} is invalid. + */ + public static UnitNumber parseUnitNumber(String unitNumber) throws ParseException { + requireNonNull(unitNumber); + String trimmedLevel = unitNumber.trim(); + if (!UnitNumber.isValidUnitNumber(trimmedLevel)) { + throw new ParseException(UnitNumber.MESSAGE_CONSTRAINTS); + } + return new UnitNumber(trimmedLevel); + } } diff --git a/src/test/java/seedu/address/logic/parser/ParserUtilTest.java b/src/test/java/seedu/address/logic/parser/ParserUtilTest.java index 4256788b1a7..76705f1496d 100644 --- a/src/test/java/seedu/address/logic/parser/ParserUtilTest.java +++ b/src/test/java/seedu/address/logic/parser/ParserUtilTest.java @@ -14,6 +14,7 @@ import org.junit.jupiter.api.Test; import seedu.address.logic.parser.exceptions.ParseException; +import seedu.address.model.house.UnitNumber; import seedu.address.model.person.Address; import seedu.address.model.person.Email; import seedu.address.model.person.Name; @@ -26,6 +27,7 @@ public class ParserUtilTest { private static final String INVALID_ADDRESS = " "; private static final String INVALID_EMAIL = "example.com"; private static final String INVALID_TAG = "#friend"; + private static final String INVALID_UNIT_NUMBER = "1234"; private static final String VALID_NAME = "Rachel Walker"; private static final String VALID_PHONE = "123456"; @@ -33,6 +35,7 @@ public class ParserUtilTest { private static final String VALID_EMAIL = "rachel@example.com"; private static final String VALID_TAG_1 = "friend"; private static final String VALID_TAG_2 = "neighbour"; + private static final String VALID_UNIT_NUMBER = "123"; private static final String WHITESPACE = " \t\r\n"; @@ -193,4 +196,28 @@ public void parseTags_collectionWithValidTags_returnsTagSet() throws Exception { assertEquals(expectedTagSet, actualTagSet); } + + @Test + public void parseUnitNumber_null_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> ParserUtil.parseUnitNumber((String) null)); + } + + @Test + public void parseUnitNumber_invalidValue_throwsParseException() { + assertThrows(ParseException.class, () -> ParserUtil.parseUnitNumber(INVALID_UNIT_NUMBER)); + } + + @Test + public void parseUnitNumber_validValueWithoutWhitespace_returnsUnitNumber() throws Exception { + UnitNumber expectedUnitNumber = new UnitNumber(VALID_UNIT_NUMBER); + assertEquals(expectedUnitNumber, ParserUtil.parseUnitNumber(VALID_UNIT_NUMBER)); + } + + @Test + public void parseUnitNumber_validValueWithWhitespace_returnsTrimmedUnitNumber() throws Exception { + String unitNumberWithWhitespace = WHITESPACE + VALID_UNIT_NUMBER + WHITESPACE; + UnitNumber expectedUnitNumber = new UnitNumber(VALID_UNIT_NUMBER); + assertEquals(expectedUnitNumber, ParserUtil.parseUnitNumber(unitNumberWithWhitespace)); + } + } From 9addd309d2ef6b4bd0c3d4bffc3f0f031f9bd0b0 Mon Sep 17 00:00:00 2001 From: Felix Chan You Yuan <111300022+felixchanyy@users.noreply.github.com> Date: Fri, 15 Mar 2024 12:44:10 +0800 Subject: [PATCH 6/7] Fix ParserUtil.java Error --- src/main/java/seedu/address/logic/parser/ParserUtil.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index 5e9495b2306..efc5055d4d4 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -9,9 +9,9 @@ import seedu.address.commons.core.index.Index; import seedu.address.commons.util.StringUtil; import seedu.address.logic.parser.exceptions.ParseException; -import seedu.address.model.house.UnitNumber; import seedu.address.model.house.Block; import seedu.address.model.house.PostalCode; +import seedu.address.model.house.UnitNumber; import seedu.address.model.person.Address; import seedu.address.model.person.Email; import seedu.address.model.person.Name; @@ -139,6 +139,8 @@ public static UnitNumber parseUnitNumber(String unitNumber) throws ParseExceptio throw new ParseException(UnitNumber.MESSAGE_CONSTRAINTS); } return new UnitNumber(trimmedLevel); + } + /** * Parses a {@code String block} into an {@code Block}. * Leading and trailing whitespaces will be trimmed. * @@ -151,7 +153,8 @@ public static Block parseBlock(String block) throws ParseException { throw new ParseException(Block.MESSAGE_CONSTRAINTS); } return new Block(trimmedBlock); - + } + /** * Parses a {@code String postalCode} into a {@code postalCode}. * Leading and trailing whitespaces will be trimmed. * From 2959dd0daadce502d9fe452cfa6a0e948d7ef306 Mon Sep 17 00:00:00 2001 From: Felix Chan You Yuan <111300022+felixchanyy@users.noreply.github.com> Date: Fri, 15 Mar 2024 12:48:37 +0800 Subject: [PATCH 7/7] Fix ParserUtilTest.java lexicographical order --- src/test/java/seedu/address/logic/parser/ParserUtilTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/seedu/address/logic/parser/ParserUtilTest.java b/src/test/java/seedu/address/logic/parser/ParserUtilTest.java index 0d422a4e677..f363378c13b 100644 --- a/src/test/java/seedu/address/logic/parser/ParserUtilTest.java +++ b/src/test/java/seedu/address/logic/parser/ParserUtilTest.java @@ -14,9 +14,9 @@ import org.junit.jupiter.api.Test; import seedu.address.logic.parser.exceptions.ParseException; -import seedu.address.model.house.UnitNumber; import seedu.address.model.house.Block; import seedu.address.model.house.PostalCode; +import seedu.address.model.house.UnitNumber; import seedu.address.model.person.Address; import seedu.address.model.person.Email; import seedu.address.model.person.Name;