Skip to content

Commit

Permalink
Merge pull request #56 from lokidoki102/branch-add-block-class
Browse files Browse the repository at this point in the history
Add Block Class
  • Loading branch information
felixchanyy authored Mar 15, 2024
2 parents 373f139 + 993fc34 commit 084fe75
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +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_BLOCK = new Prefix("blk/");
public static final Prefix PREFIX_POSTALCODE = new Prefix("postal/");

}
14 changes: 14 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.house.PostalCode;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
Expand Down Expand Up @@ -125,6 +126,19 @@ public static Set<Tag> parseTags(Collection<String> tags) throws ParseException
}

/**
* 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);

* Parses a {@code String postalCode} into a {@code postalCode}.
* Leading and trailing whitespaces will be trimmed.
*
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/seedu/address/model/house/Block.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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 static final String ZERO_REGEX = "^0+$";
public static final String ZERO_AND_ALPHABET_REGEX = "^0*[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) && !test.matches(ZERO_REGEX) && !test.matches(ZERO_AND_ALPHABET_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();
}
}
26 changes: 26 additions & 0 deletions src/test/java/seedu/address/logic/parser/ParserUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.house.PostalCode;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
Expand All @@ -27,6 +28,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 INVALID_POSTALCODE = "5678990";

private static final String VALID_NAME = "Rachel Walker";
Expand All @@ -36,6 +38,7 @@ public class ParserUtilTest {
private static final String VALID_POSTALCODE = "654321";
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";

Expand Down Expand Up @@ -219,4 +222,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));
}
}
69 changes: 69 additions & 0 deletions src/test/java/seedu/address/model/house/BlockTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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 BlockTest {

@Test
public void constructor_null_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new Block(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("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
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")));
}
}

0 comments on commit 084fe75

Please sign in to comment.