Skip to content

Commit

Permalink
Add phone validator
Browse files Browse the repository at this point in the history
Ensure all phone numbers have 8 digits according to Singapore phone number format
  • Loading branch information
alphajae11 committed Apr 14, 2024
1 parent 6ec5fe3 commit 2504e8f
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/main/java/seedu/address/model/person/Phone.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ public class Phone {


public static final String MESSAGE_CONSTRAINTS =
"Phone numbers should only contain numbers, and it should be at least 3 digits long";
public static final String VALIDATION_REGEX = "\\d{3,}";
"Phone numbers should only contain numbers, and it should be 8 digits long "
+ "according to Singapore mobile number format";
public static final String VALIDATION_REGEX = "\\d{8}";
public final String value;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"metadata": "I like to play basketball."
}, {
"name" : "Elle Meyer",
"phone" : "9482224",
"phone" : "94822242",
"email" : "[email protected]",
"address" : "michegan ave",
"roles" : [ ],
Expand All @@ -68,7 +68,7 @@
"metadata": "I support Manchester United."
}, {
"name" : "Fiona Kunz",
"phone" : "9482427",
"phone" : "94824272",
"email" : "[email protected]",
"address" : "little tokyo",
"roles" : [ ],
Expand All @@ -81,7 +81,7 @@
"metadata": "I support Manchester United."
}, {
"name" : "George Best",
"phone" : "9482442",
"phone" : "94824422",
"email" : "[email protected]",
"address" : "4th street",
"roles" : [ ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ParserUtilTest {
private static final String INVALID_ROLE = "#friend";

private static final String VALID_NAME = "Rachel Walker";
private static final String VALID_PHONE = "123456";
private static final String VALID_PHONE = "12345678";
private static final String VALID_ADDRESS = "123 Main Street #0505";
private static final String VALID_EMAIL = "[email protected]";
private static final String VALID_ROLE_1 = "friend";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public void test_nameDoesNotContainKeywords_returnsFalse() {
assertFalse(predicate.test(new PersonBuilder().withName("Alice Bob").build()));

// Keywords match phone, email and address, but does not match name
predicate = new NameContainsKeywordsPredicate(Arrays.asList("12345", "[email protected]", "Main", "Street"));
assertFalse(predicate.test(new PersonBuilder().withName("Alice").withPhone("12345")
predicate = new NameContainsKeywordsPredicate(Arrays.asList("12345678", "[email protected]", "Main", "Street"));
assertFalse(predicate.test(new PersonBuilder().withName("Alice").withPhone("12345678")
.withEmail("[email protected]").withAddress("Main Street").build()));
}

Expand Down
13 changes: 6 additions & 7 deletions src/test/java/seedu/address/model/person/PhoneTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,22 @@ public void isValidPhone() {
// invalid phone numbers
assertFalse(Phone.isValidPhone("")); // empty string
assertFalse(Phone.isValidPhone(" ")); // spaces only
assertFalse(Phone.isValidPhone("91")); // less than 3 numbers
assertFalse(Phone.isValidPhone("9190137")); // less than 38 numbers
assertFalse(Phone.isValidPhone("phone")); // non-numeric
assertFalse(Phone.isValidPhone("9011p041")); // alphabets within digits
assertFalse(Phone.isValidPhone("9312 1534")); // spaces within digits
assertFalse(Phone.isValidPhone("17803682394326478")); // More than 8 number

// valid phone numbers
assertTrue(Phone.isValidPhone("911")); // exactly 3 numbers
assertTrue(Phone.isValidPhone("93121534"));
assertTrue(Phone.isValidPhone("124293842033123")); // long phone numbers
assertTrue(Phone.isValidPhone("93121534")); // exactly 8 numbers
}

@Test
public void equals() {
Phone phone = new Phone("999");
Phone phone = new Phone("99171577");

// same values -> returns true
assertTrue(phone.equals(new Phone("999")));
assertTrue(phone.equals(new Phone("99171577")));

// same object -> returns true
assertTrue(phone.equals(phone));
Expand All @@ -55,6 +54,6 @@ public void equals() {
assertFalse(phone.equals(5.0f));

// different values -> returns false
assertFalse(phone.equals(new Phone("995")));
assertFalse(phone.equals(new Phone("99580770")));
}
}
10 changes: 5 additions & 5 deletions src/test/java/seedu/address/testutil/TypicalPersons.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,23 @@ public class TypicalPersons {
.withAtt("0")
.withSess("1")
.withMetadata("I like to play basketball.").build();
public static final Person ELLE = new PersonBuilder().withName("Elle Meyer").withPhone("9482224")
public static final Person ELLE = new PersonBuilder().withName("Elle Meyer").withPhone("94822242")
.withEmail("[email protected]").withAddress("michegan ave").withMetadata("I support Manchester United.")
.build();
public static final Person FIONA = new PersonBuilder().withName("Fiona Kunz").withPhone("9482427")
public static final Person FIONA = new PersonBuilder().withName("Fiona Kunz").withPhone("94824272")
.withEmail("[email protected]").withAddress("little tokyo")
.withAmount("50.00")
.withAtt("0")
.withSess("1")
.withMetadata("I support Manchester United.").build();
public static final Person GEORGE = new PersonBuilder().withName("George Best").withPhone("9482442")
public static final Person GEORGE = new PersonBuilder().withName("George Best").withPhone("94824422")
.withEmail("[email protected]").withAddress("4th street").withMetadata("I support Manchester United.")
.build();

// Manually added
public static final Person HOON = new PersonBuilder().withName("Hoon Meier").withPhone("8482424")
public static final Person HOON = new PersonBuilder().withName("Hoon Meier").withPhone("84824242")
.withEmail("[email protected]").withAddress("little india").build();
public static final Person IDA = new PersonBuilder().withName("Ida Mueller").withPhone("8482131")
public static final Person IDA = new PersonBuilder().withName("Ida Mueller").withPhone("84821312")
.withEmail("[email protected]").withAddress("chicago ave").build();

// Manually added - Person's details found in {@code CommandTestUtil}
Expand Down

0 comments on commit 2504e8f

Please sign in to comment.