forked from nus-cs2103-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from lokidoki102/branch-add-block-class
Add Block Class
- Loading branch information
Showing
5 changed files
with
171 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))); | ||
} | ||
} |