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 #59 from KhoonSun47/branch-add-unit-number
Add Unit Number
- Loading branch information
Showing
5 changed files
with
169 additions
and
0 deletions.
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,64 @@ | ||
package seedu.address.model.house; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.util.AppUtil.checkArgument; | ||
|
||
/** | ||
* 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, it should be at least 1 digit " | ||
+ "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; | ||
|
||
/** | ||
* 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. | ||
* | ||
* @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) && !test.matches(ZERO_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(); | ||
} | ||
} |
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
64 changes: 64 additions & 0 deletions
64
src/test/java/seedu/address/model/house/UnitNumberTest.java
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,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; | ||
|
||
public class UnitNumberTest { | ||
|
||
@Test | ||
public void constructor_null_throwsNullPointerException() { | ||
assertThrows(NullPointerException.class, () -> new UnitNumber(null)); | ||
} | ||
|
||
@Test | ||
public void constructor_invalidUnitNumber_throwsIllegalArgumentException() { | ||
String invalidUnitNumber = "abcd"; | ||
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("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 | ||
assertFalse(UnitNumber.isValidUnitNumber("ab")); // non-numeric | ||
assertFalse(UnitNumber.isValidUnitNumber("1a2")); // alphabets within digits | ||
|
||
// valid unit numbers | ||
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 | ||
} | ||
|
||
@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"))); | ||
} | ||
} |