Skip to content

Commit

Permalink
Merge pull request #57 from lokidoki102/branch-add-street-class
Browse files Browse the repository at this point in the history
Add Street Class
  • Loading branch information
felixchanyy authored Mar 15, 2024
2 parents 935d411 + 9ef61aa commit e731de7
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class CliSyntax {
public static final Prefix PREFIX_EMAIL = new Prefix("e/");
public static final Prefix PREFIX_ADDRESS = new Prefix("a/");
public static final Prefix PREFIX_TAG = new Prefix("t/");
public static final Prefix PREFIX_STREET = new Prefix("street/");
public static final Prefix PREFIX_LEVEL = new Prefix("level/");
public static final Prefix PREFIX_UNITNUMBER = new Prefix("unitNo/");
public static final Prefix PREFIX_BLOCK = new Prefix("blk/");
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import seedu.address.model.house.Block;
import seedu.address.model.house.Level;
import seedu.address.model.house.PostalCode;
import seedu.address.model.house.Street;
import seedu.address.model.house.UnitNumber;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
Expand Down Expand Up @@ -127,6 +128,21 @@ public static Set<Tag> parseTags(Collection<String> tags) throws ParseException
return tagSet;
}

/**
* Parses a {@code String street} into a {@code Street}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code street} is invalid.
*/
public static Street parseStreet(String street) throws ParseException {
requireNonNull(street);
String trimmedStreet = street.trim();
if (!Street.isValidStreet(trimmedStreet)) {
throw new ParseException(Street.MESSAGE_CONSTRAINTS);
}
return new Street(trimmedStreet);
}

/**
* Parses a {@code String level} into a {@code Level}.
* Leading and trailing whitespaces will be trimmed.
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/seedu/address/model/house/Street.java
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();
}

}
25 changes: 24 additions & 1 deletion src/test/java/seedu/address/logic/parser/ParserUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import seedu.address.model.house.Block;
import seedu.address.model.house.Level;
import seedu.address.model.house.PostalCode;
import seedu.address.model.house.Street;
import seedu.address.model.house.UnitNumber;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
Expand All @@ -30,6 +31,7 @@ public class ParserUtilTest {
private static final String INVALID_ADDRESS = " ";
private static final String INVALID_EMAIL = "example.com";
private static final String INVALID_TAG = "#friend";
private static final String INVALID_STREET = "t3$t!ng";
private static final String INVALID_LEVEL = "aa";
private static final String INVALID_UNIT_NUMBER = "1234";
private static final String INVALID_BLOCK = "12a34";
Expand All @@ -42,10 +44,10 @@ public class ParserUtilTest {
private static final String VALID_POSTALCODE = "654321";
private static final String VALID_TAG_1 = "friend";
private static final String VALID_TAG_2 = "neighbour";
private static final String VALID_STREET = "292A East Coast Rd";
private static final String VALID_LEVEL = "10";
private static final String VALID_UNIT_NUMBER = "123";
private static final String VALID_BLOCK = "205A";

private static final String WHITESPACE = " \t\r\n";

@Test
Expand Down Expand Up @@ -230,6 +232,27 @@ public void parseTags_collectionWithValidTags_returnsTagSet() throws Exception {
}

@Test
public void parseStreet_null_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> ParserUtil.parseStreet((String) null));
}

@Test
public void parseStreet_invalidValue_throwsParseException() {
assertThrows(ParseException.class, () -> ParserUtil.parseStreet(INVALID_STREET));
}

@Test
public void parseStreet_validValueWithoutWhitespace_returnsStreet() throws Exception {
Street expectedStreet = new Street(VALID_STREET);
assertEquals(expectedStreet, ParserUtil.parseStreet(VALID_STREET));
}

@Test
public void parseStreet_validValueWithWhitespace_returnsTrimmedStreet() throws Exception {
String streetWithWhitespace = WHITESPACE + VALID_STREET + WHITESPACE;
Street expectedStreet = new Street(VALID_STREET);
assertEquals(expectedStreet, ParserUtil.parseStreet(streetWithWhitespace));
}
public void parseLevel_null_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> ParserUtil.parseLevel((String) null));
}
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/seedu/address/model/house/StreetTest.java
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")));
}
}

0 comments on commit e731de7

Please sign in to comment.