Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/KhoonSun47/tp
Browse files Browse the repository at this point in the history
* 'master' of https://github.com/KhoonSun47/tp: (24 commits)
  Fix ParserUtilTest.java lexicographical order
  Fix ParserUtil.java Error
  Fix checkstyle of ParserUtilTest.java
  Update ParserUtilTest.java
  Fix ParserUtilTest.java
  Fix checkstyle in ParserUtil.java
  Fix ParserUtil.java
  Issue #46: Add Unit Number
  Add test cases for street parser
  Update ParserUtil to parse Street inputs
  Add test cases for level parser
  Update ParserUtil to parse Level inputs
  Fix level input validation
  Issue #46: Add Unit Number
  Issue #46: Add Unit Number
  Issue #46: Add Unit Number
  Issue #46: Add Unit Number
  Fix typo in method name
  Add a prefix for street field
  Add test cases for Street class
  ...
  • Loading branch information
KhoonSun47 committed Mar 15, 2024
2 parents 785ef8d + 772493e commit 9b992b6
Show file tree
Hide file tree
Showing 9 changed files with 502 additions and 1 deletion.
3 changes: 3 additions & 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,9 @@ 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/");
public static final Prefix PREFIX_POSTALCODE = new Prefix("postal/");
}
47 changes: 47 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import seedu.address.commons.util.StringUtil;
import seedu.address.logic.parser.exceptions.ParseException;
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;
import seedu.address.model.person.Name;
Expand Down Expand Up @@ -125,6 +128,50 @@ 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.
*
* @throws ParseException if the given {@code level} is invalid.
*/
public static Level parseLevel(String level) throws ParseException {
requireNonNull(level);
String trimmedLevel = level.trim();
if (!Level.isValidLevel(trimmedLevel)) {
throw new ParseException(Level.MESSAGE_CONSTRAINTS);
}
return new Level(trimmedLevel);
}

/**
* Parses a {@code String unitNumber} into a {@code unitNumber}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code unitNumber} is invalid.
*/
public static UnitNumber parseUnitNumber(String unitNumber) throws ParseException {
requireNonNull(unitNumber);
String trimmedLevel = unitNumber.trim();
if (!UnitNumber.isValidUnitNumber(trimmedLevel)) {
throw new ParseException(UnitNumber.MESSAGE_CONSTRAINTS);
}
return new UnitNumber(trimmedLevel);
}
/**
* Parses a {@code String block} into an {@code Block}.
* Leading and trailing whitespaces will be trimmed.
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/seedu/address/model/house/Level.java
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();
}

}
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();
}

}
64 changes: 64 additions & 0 deletions src/main/java/seedu/address/model/house/UnitNumber.java
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();
}
}
73 changes: 72 additions & 1 deletion src/test/java/seedu/address/logic/parser/ParserUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

import seedu.address.logic.parser.exceptions.ParseException;
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;
import seedu.address.model.person.Name;
Expand All @@ -28,6 +31,9 @@ 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";
private static final String INVALID_POSTALCODE = "5678990";

Expand All @@ -38,8 +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 @@ -224,6 +232,69 @@ 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));
}

@Test
public void parseLevel_invalidValue_throwsParseException() {
assertThrows(ParseException.class, () -> ParserUtil.parseLevel(INVALID_LEVEL));
}

@Test
public void parseLevel_validValueWithoutWhitespace_returnsLevel() throws Exception {
Level expectedLevel = new Level(VALID_LEVEL);
assertEquals(expectedLevel, ParserUtil.parseLevel(VALID_LEVEL));
}

@Test
public void parseLevel_validValueWithWhitespace_returnsTrimmedLevel() throws Exception {
String levelWithWhitespace = WHITESPACE + VALID_LEVEL + WHITESPACE;
Level expectedLevel = new Level(VALID_LEVEL);
assertEquals(expectedLevel, ParserUtil.parseLevel(levelWithWhitespace));
}
public void parseUnitNumber_null_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> ParserUtil.parseUnitNumber((String) null));
}

@Test
public void parseUnitNumber_invalidValue_throwsParseException() {
assertThrows(ParseException.class, () -> ParserUtil.parseUnitNumber(INVALID_UNIT_NUMBER));
}

@Test
public void parseUnitNumber_validValueWithoutWhitespace_returnsUnitNumber() throws Exception {
UnitNumber expectedUnitNumber = new UnitNumber(VALID_UNIT_NUMBER);
assertEquals(expectedUnitNumber, ParserUtil.parseUnitNumber(VALID_UNIT_NUMBER));
}

@Test
public void parseUnitNumber_validValueWithWhitespace_returnsTrimmedUnitNumber() throws Exception {
String unitNumberWithWhitespace = WHITESPACE + VALID_UNIT_NUMBER + WHITESPACE;
UnitNumber expectedUnitNumber = new UnitNumber(VALID_UNIT_NUMBER);
assertEquals(expectedUnitNumber, ParserUtil.parseUnitNumber(unitNumberWithWhitespace));
}
public void parseBlock_null_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> ParserUtil.parseBlock((String) null));
}
Expand Down
Loading

0 comments on commit 9b992b6

Please sign in to comment.