From d157a74b0da4d76398c2ee68b591dbca0adb0387 Mon Sep 17 00:00:00 2001 From: Carl Date: Wed, 13 Mar 2024 01:43:09 +0800 Subject: [PATCH 1/7] Add new Block class This class is to be used by house class later on. --- .../java/seedu/address/model/house/Block.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/main/java/seedu/address/model/house/Block.java diff --git a/src/main/java/seedu/address/model/house/Block.java b/src/main/java/seedu/address/model/house/Block.java new file mode 100644 index 00000000000..0e84a1770be --- /dev/null +++ b/src/main/java/seedu/address/model/house/Block.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's block number. + * Guarantees: immutable; is valid as declared in {@link #isValidBlock(String)} + */ +public class Block { + public static final String MESSAGE_CONSTRAINTS = + "Block numbers should only contain numbers, with or without a letter as the end. " + + "There can only be at most 3 digits long."; + public static final String VALIDATION_REGEX = "^\\d{1,3}[a-zA-Z]?$"; + public final String value; + + /** + * Constructs a {@code Block}. + * + * @param block A valid block number. + */ + public Block(String block) { + requireNonNull(block); + checkArgument(isValidBlock(block), MESSAGE_CONSTRAINTS); + value = block; + } + + /** + * Returns true if a given string is a valid block number. + */ + public static boolean isValidBlock(String test) { + return test.matches(VALIDATION_REGEX); + } + + @Override + public String toString() { + return value; + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof Block)) { + return false; + } + + Block otherBlock = (Block) other; + return value.equals(otherBlock.value); + } + + @Override + public int hashCode() { + return value.hashCode(); + } +} From 70759d4aa2f2ab6ff5668fe1ded9788c32ae6d27 Mon Sep 17 00:00:00 2001 From: Carl Date: Wed, 13 Mar 2024 01:50:55 +0800 Subject: [PATCH 2/7] Add test cases for Block class The test cases are responsible for checking null entries, various invalid entries, and equals() method for Block class. --- .../seedu/address/model/house/BlockTest.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/test/java/seedu/address/model/house/BlockTest.java diff --git a/src/test/java/seedu/address/model/house/BlockTest.java b/src/test/java/seedu/address/model/house/BlockTest.java new file mode 100644 index 00000000000..c7c64f70e0d --- /dev/null +++ b/src/test/java/seedu/address/model/house/BlockTest.java @@ -0,0 +1,64 @@ +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; +import seedu.address.model.person.Phone; + +public class BlockTest { + + @Test + public void constructor_null_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> new Phone(null)); + } + + @Test + public void constructor_invalidBlock_throwsIllegalArgumentException() { + String invalidBlock = ""; + assertThrows(IllegalArgumentException.class, () -> new Block(invalidBlock)); + } + + @Test + public void isValidBlock() { + // null block number + assertThrows(NullPointerException.class, () -> Block.isValidBlock(null)); + + // invalid block numbers + assertFalse(Block.isValidBlock("")); // empty string + assertFalse(Block.isValidBlock(" ")); // spaces only + assertFalse(Block.isValidBlock("A")); // letter only + assertFalse(Block.isValidBlock("blocknumber")); // non-numeric + assertFalse(Block.isValidBlock("10A9")); // alphabet within digits + assertFalse(Block.isValidBlock("A109")); // alphabet at the start + assertFalse(Block.isValidBlock("14 1")); // spaces within digits + assertFalse(Block.isValidBlock("9999")); // more than 3 digits + + // valid phone numbers + assertTrue(Block.isValidBlock("777")); + assertTrue(Block.isValidBlock("777A")); // block number with alphabet at the end + assertTrue(Block.isValidBlock("1A")); // short block number with alphabet at the end + assertTrue(Block.isValidBlock("1")); // short block number + } + + @Test + public void equals() { + Block block = new Block("234A"); + + // same values -> returns true + assertTrue(block.equals(new Block("234A"))); + + // same object -> returns true + assertTrue(block.equals(block)); + + // null -> returns false + assertFalse(block.equals(null)); + + // different types -> returns false + assertFalse(block.equals(5.0f)); + + // different values -> returns false + assertFalse(block.equals(new Block("995"))); + } +} From fdfba81b548a7f2bb2355975a62927cccd605783 Mon Sep 17 00:00:00 2001 From: Carl Date: Wed, 13 Mar 2024 01:51:47 +0800 Subject: [PATCH 3/7] Add prefix for block field --- src/main/java/seedu/address/logic/parser/CliSyntax.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/logic/parser/CliSyntax.java b/src/main/java/seedu/address/logic/parser/CliSyntax.java index 75b1a9bf119..33880a583ef 100644 --- a/src/main/java/seedu/address/logic/parser/CliSyntax.java +++ b/src/main/java/seedu/address/logic/parser/CliSyntax.java @@ -11,5 +11,5 @@ 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_BLOCK = new Prefix("blk/"); } From 37f49713d9886683cedd1e72ad4f86ad0f40e8a7 Mon Sep 17 00:00:00 2001 From: Carl Date: Wed, 13 Mar 2024 01:54:14 +0800 Subject: [PATCH 4/7] Fix checkstyle errors --- src/main/java/seedu/address/model/house/Block.java | 4 ++-- src/test/java/seedu/address/model/house/BlockTest.java | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/seedu/address/model/house/Block.java b/src/main/java/seedu/address/model/house/Block.java index 0e84a1770be..f8120dc7a26 100644 --- a/src/main/java/seedu/address/model/house/Block.java +++ b/src/main/java/seedu/address/model/house/Block.java @@ -9,8 +9,8 @@ */ public class Block { public static final String MESSAGE_CONSTRAINTS = - "Block numbers should only contain numbers, with or without a letter as the end. " + - "There can only be at most 3 digits long."; + "Block numbers should only contain numbers, with or without a letter as the end. " + + "There can only be at most 3 digits long."; public static final String VALIDATION_REGEX = "^\\d{1,3}[a-zA-Z]?$"; public final String value; diff --git a/src/test/java/seedu/address/model/house/BlockTest.java b/src/test/java/seedu/address/model/house/BlockTest.java index c7c64f70e0d..8bc40989895 100644 --- a/src/test/java/seedu/address/model/house/BlockTest.java +++ b/src/test/java/seedu/address/model/house/BlockTest.java @@ -5,13 +5,12 @@ import static seedu.address.testutil.Assert.assertThrows; import org.junit.jupiter.api.Test; -import seedu.address.model.person.Phone; public class BlockTest { @Test public void constructor_null_throwsNullPointerException() { - assertThrows(NullPointerException.class, () -> new Phone(null)); + assertThrows(NullPointerException.class, () -> new Block(null)); } @Test From a8a7bd7744d95987a163737d360a51201aac067a Mon Sep 17 00:00:00 2001 From: Carl Date: Thu, 14 Mar 2024 21:03:31 +0800 Subject: [PATCH 5/7] Fix block validation input Currently, the validation accepts inputs that's only consisting of zeroes or zeroes + alphabet. However, those kind of block numbers do not exist in Singapore. --- src/main/java/seedu/address/model/house/Block.java | 4 +++- src/test/java/seedu/address/model/house/BlockTest.java | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/model/house/Block.java b/src/main/java/seedu/address/model/house/Block.java index f8120dc7a26..75086202621 100644 --- a/src/main/java/seedu/address/model/house/Block.java +++ b/src/main/java/seedu/address/model/house/Block.java @@ -12,6 +12,8 @@ public class Block { "Block numbers should only contain numbers, with or without a letter as the end. " + "There can only be at most 3 digits long."; public static final String VALIDATION_REGEX = "^\\d{1,3}[a-zA-Z]?$"; + public static final String ZERO_REGEX = "^0+$"; + public static final String ZERO_AND_ALPHABET_REGEX = "^0*[a-zA-Z]$"; public final String value; /** @@ -29,7 +31,7 @@ public Block(String block) { * Returns true if a given string is a valid block number. */ public static boolean isValidBlock(String test) { - return test.matches(VALIDATION_REGEX); + return test.matches(VALIDATION_REGEX) && !test.matches(ZERO_REGEX) && !test.matches(ZERO_AND_ALPHABET_REGEX); } @Override diff --git a/src/test/java/seedu/address/model/house/BlockTest.java b/src/test/java/seedu/address/model/house/BlockTest.java index 8bc40989895..ba6b49fd346 100644 --- a/src/test/java/seedu/address/model/house/BlockTest.java +++ b/src/test/java/seedu/address/model/house/BlockTest.java @@ -25,6 +25,12 @@ public void isValidBlock() { assertThrows(NullPointerException.class, () -> Block.isValidBlock(null)); // invalid block numbers + assertFalse(Block.isValidBlock("0")); // 1 zero only + assertFalse(Block.isValidBlock("00")); // 2 zeroes only + assertFalse(Block.isValidBlock("000")); // 3 zeroes only + assertFalse(Block.isValidBlock("0A")); // 1 zero + alphabet + assertFalse(Block.isValidBlock("00A")); // 2 zeroes + alphabet + assertFalse(Block.isValidBlock("000A")); // 3 zeroes + alphabet assertFalse(Block.isValidBlock("")); // empty string assertFalse(Block.isValidBlock(" ")); // spaces only assertFalse(Block.isValidBlock("A")); // letter only From 1efe568c6283d6b8f4337a4fc294b79080bd8da8 Mon Sep 17 00:00:00 2001 From: Carl Date: Thu, 14 Mar 2024 21:17:06 +0800 Subject: [PATCH 6/7] Update ParserUtil to parse Block inputs --- .../seedu/address/logic/parser/ParserUtil.java | 16 ++++++++++++++++ 1 file changed, 16 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..da7ecf96fa6 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.Block; import seedu.address.model.person.Address; import seedu.address.model.person.Email; import seedu.address.model.person.Name; @@ -121,4 +122,19 @@ public static Set parseTags(Collection tags) throws ParseException } return tagSet; } + + /** + * Parses a {@code String block} into an {@code Block}. + * Leading and trailing whitespaces will be trimmed. + * + * @throws ParseException if the given {@code block} is invalid. + */ + public static Block parseBlock(String block) throws ParseException { + requireNonNull(block); + String trimmedBlock = block.trim(); + if (!Block.isValidBlock(trimmedBlock)) { + throw new ParseException(Block.MESSAGE_CONSTRAINTS); + } + return new Block(trimmedBlock); + } } From 13947bb12bfee1db196ab6913d707a6cb3191de1 Mon Sep 17 00:00:00 2001 From: Carl Date: Thu, 14 Mar 2024 21:17:53 +0800 Subject: [PATCH 7/7] Add tests for parsing Block inputs --- .../address/logic/parser/ParserUtilTest.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/test/java/seedu/address/logic/parser/ParserUtilTest.java b/src/test/java/seedu/address/logic/parser/ParserUtilTest.java index 4256788b1a7..47b7ac63b23 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.Block; 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_BLOCK = "12a34"; 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_BLOCK = "205A"; private static final String WHITESPACE = " \t\r\n"; @@ -193,4 +196,27 @@ public void parseTags_collectionWithValidTags_returnsTagSet() throws Exception { assertEquals(expectedTagSet, actualTagSet); } + + @Test + public void parseBlock_null_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> ParserUtil.parseBlock((String) null)); + } + + @Test + public void parseBlock_invalidValue_throwsParseException() { + assertThrows(ParseException.class, () -> ParserUtil.parseBlock(INVALID_BLOCK)); + } + + @Test + public void parseBlock_validValueWithoutWhitespace_returnsBlock() throws Exception { + Block expectedBlock = new Block(VALID_BLOCK); + assertEquals(expectedBlock, ParserUtil.parseBlock(VALID_BLOCK)); + } + + @Test + public void parseBlock_validValueWithWhitespace_returnsTrimmedBlock() throws Exception { + String blockWithWhitespace = WHITESPACE + VALID_BLOCK + WHITESPACE; + Block expectedBlock = new Block(VALID_BLOCK); + assertEquals(expectedBlock, ParserUtil.parseBlock(blockWithWhitespace)); + } }