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 #57 from lokidoki102/branch-add-street-class
Add Street Class
- Loading branch information
Showing
5 changed files
with
161 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,60 @@ | ||
package seedu.address.model.house; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.util.AppUtil.checkArgument; | ||
|
||
/** | ||
* Represents a House's street. | ||
* Guarantees: immutable; is valid as declared in {@link #isValidStreet(String)} | ||
*/ | ||
public class Street { | ||
|
||
public static final String MESSAGE_CONSTRAINTS = | ||
"Street should only contain alphanumeric characters and hyphens."; | ||
public static final String VALIDATION_REGEX = "^(?=.*[a-zA-Z0-9])[a-zA-Z0-9\\s-]+$"; | ||
public final String value; | ||
|
||
/** | ||
* Constructs a {@code Street}. | ||
* | ||
* @param street A valid street. | ||
*/ | ||
public Street(String street) { | ||
requireNonNull(street); | ||
checkArgument(isValidStreet(street), MESSAGE_CONSTRAINTS); | ||
value = street; | ||
} | ||
|
||
/** | ||
* Returns true if a given string is a valid street. | ||
*/ | ||
public static boolean isValidStreet(String test) { | ||
return test.matches(VALIDATION_REGEX); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof Street)) { | ||
return false; | ||
} | ||
|
||
Street otherStreet = (Street) other; | ||
return value.equals(otherStreet.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,60 @@ | ||
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 StreetTest { | ||
@Test | ||
public void constructor_null_throwsNullPointerException() { | ||
assertThrows(NullPointerException.class, () -> new Street(null)); | ||
} | ||
|
||
@Test | ||
public void constructor_invalidBlock_throwsIllegalArgumentException() { | ||
String invalidStreet = ""; | ||
assertThrows(IllegalArgumentException.class, () -> new Street(invalidStreet)); | ||
} | ||
|
||
@Test | ||
public void isValidStreet() { | ||
// null street | ||
assertThrows(NullPointerException.class, () -> Street.isValidStreet(null)); | ||
|
||
// invalid street | ||
assertFalse(Street.isValidStreet("")); // empty string | ||
assertFalse(Street.isValidStreet(" ")); // spaces only | ||
assertFalse(Street.isValidStreet("-")); // hyphen only | ||
assertFalse(Street.isValidStreet("@!^&*%&^")); // non-alphanumeric | ||
assertFalse(Street.isValidStreet("qwe!@#ert$%")); // non-alphanumeric + alphabets | ||
assertFalse(Street.isValidStreet("!@#123")); // non-alphanumeric + numbers | ||
assertFalse(Street.isValidStreet("t3$t!ng")); // non-alphanumeric + alphanumerics | ||
|
||
// valid street | ||
assertTrue(Street.isValidStreet("292A East Coast Rd")); | ||
assertTrue(Street.isValidStreet("350 Orchard Rd Sshaw House 13-01")); // with dash | ||
assertTrue(Street.isValidStreet("46 PANDAN LOOP BLK 3 PANDAN LIGHT IND PARK")); // all caps | ||
} | ||
|
||
@Test | ||
public void equals() { | ||
Street street = new Street("15 Kaki Bukit View 01-00"); | ||
|
||
// same values -> returns true | ||
assertTrue(street.equals(new Street("15 Kaki Bukit View 01-00"))); | ||
|
||
// same object -> returns true | ||
assertTrue(street.equals(street)); | ||
|
||
// null -> returns false | ||
assertFalse(street.equals(null)); | ||
|
||
// different types -> returns false | ||
assertFalse(street.equals(5.0f)); | ||
|
||
// different values -> returns false | ||
assertFalse(street.equals(new Street("3 Sungei Kadut Street 6 Sungei Kadut Industrial Estate"))); | ||
} | ||
} |