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 #58 from lokidoki102/branch-add-level-class
Add Level class
- Loading branch information
Showing
5 changed files
with
173 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,63 @@ | ||
package seedu.address.model.house; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.util.AppUtil.checkArgument; | ||
|
||
|
||
/** | ||
* Represents a House's level. | ||
* Guarantees: immutable; is valid as declared in {@link #isValidLevel(String)} | ||
*/ | ||
public class Level { | ||
|
||
public static final String MESSAGE_CONSTRAINTS = | ||
"Level should only contain numbers, and it should only be at most 2 digits long"; | ||
public static final String VALIDATION_REGEX = "\\d{1,2}"; | ||
public static final String ZERO_REGEX = "^0+$"; | ||
|
||
public final String value; | ||
|
||
/** | ||
* Constructs a {@code Level}. | ||
* | ||
* @param level A valid level. | ||
*/ | ||
public Level(String level) { | ||
requireNonNull(level); | ||
checkArgument(isValidLevel(level), MESSAGE_CONSTRAINTS); | ||
value = level; | ||
} | ||
|
||
/** | ||
* Returns true if a given string is a valid level. | ||
*/ | ||
public static boolean isValidLevel(String test) { | ||
return test.matches(VALIDATION_REGEX) && !test.matches(ZERO_REGEX); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof Level)) { | ||
return false; | ||
} | ||
|
||
Level otherLevel = (Level) other; | ||
return value.equals(otherLevel.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 LevelTest { | ||
|
||
@Test | ||
public void constructor_null_throwsNullPointerException() { | ||
assertThrows(NullPointerException.class, () -> new Level(null)); | ||
} | ||
|
||
@Test | ||
public void constructor_invalidLevel_throwsIllegalArgumentException() { | ||
String invalidLevel = ""; | ||
assertThrows(IllegalArgumentException.class, () -> new Level(invalidLevel)); | ||
} | ||
|
||
@Test | ||
public void isValidLevel() { | ||
// null level | ||
assertThrows(NullPointerException.class, () -> Level.isValidLevel(null)); | ||
|
||
// blank levels | ||
assertFalse(Level.isValidLevel("")); // empty string | ||
assertFalse(Level.isValidLevel(" ")); // spaces only | ||
|
||
// invalid levels | ||
assertFalse(Level.isValidLevel("0")); // 1 zero only | ||
assertFalse(Level.isValidLevel("00")); // 2 zeroes only | ||
assertFalse(Level.isValidLevel("a")); // non-digit only | ||
assertFalse(Level.isValidLevel("aa")); // non-digit only | ||
assertFalse(Level.isValidLevel("###")); // non-digit only | ||
assertFalse(Level.isValidLevel("!@#")); // non-digit only | ||
assertFalse(Level.isValidLevel("a1")); // digit + non-digit | ||
assertFalse(Level.isValidLevel("1a")); // non-digit + digit | ||
assertFalse(Level.isValidLevel("111")); // number of digits > 2 | ||
assertFalse(Level.isValidLevel("44aa")); // number of digits >= 2 with letters | ||
assertFalse(Level.isValidLevel("44aaaaa")); // number of digits >= 2 with letters | ||
|
||
// valid levels | ||
assertTrue(Level.isValidLevel("1")); // one digit | ||
assertTrue(Level.isValidLevel("11")); // two digits | ||
assertTrue(Level.isValidLevel("01")); // two digits | ||
} | ||
|
||
@Test | ||
public void equals() { | ||
Level level = new Level("99"); | ||
|
||
// same values -> returns true | ||
assertTrue(level.equals(new Level("99"))); | ||
|
||
// same object -> returns true | ||
assertTrue(level.equals(level)); | ||
|
||
// null -> returns false | ||
assertFalse(level.equals(null)); | ||
|
||
// different types -> returns false | ||
assertFalse(level.equals(5.0f)); | ||
|
||
// different values -> returns false | ||
assertFalse(level.equals(new Level("11"))); | ||
} | ||
} |