forked from nus-cs2103-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Unit Number #59
Merged
felixchanyy
merged 9 commits into
AY2324S2-CS2103-F09-1:master
from
KhoonSun47:branch-add-unit-number
Mar 15, 2024
Merged
Add Unit Number #59
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6d7120b
Issue #46: Add Unit Number
KhoonSun47 b6f302b
Issue #46: Add Unit Number
KhoonSun47 8546447
Issue #46: Add Unit Number
KhoonSun47 8033660
Issue #46: Add Unit Number
KhoonSun47 2126005
Issue #46: Add Unit Number
KhoonSun47 8c0b7e6
Merge branch 'master' into branch-add-unit-number
felixchanyy 9addd30
Fix ParserUtil.java Error
felixchanyy 6d472e1
Merge branch 'master' into branch-add-unit-number
felixchanyy 2959dd0
Fix ParserUtilTest.java lexicographical order
felixchanyy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be formatted like 'u/' to maintain consistency with the rest of the syntax?