From a8d2a763dfac4d586713ca46262005bb170dda40 Mon Sep 17 00:00:00 2001 From: Aditi Gawande <> Date: Thu, 10 Oct 2024 21:35:10 +0800 Subject: [PATCH 1/3] Add spaces phone validation --- src/main/java/seedu/address/model/person/Phone.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/seedu/address/model/person/Phone.java b/src/main/java/seedu/address/model/person/Phone.java index d733f63d739..43376845b61 100644 --- a/src/main/java/seedu/address/model/person/Phone.java +++ b/src/main/java/seedu/address/model/person/Phone.java @@ -11,7 +11,7 @@ public class Phone { public static final String MESSAGE_CONSTRAINTS = - "Phone numbers should only contain numbers, and it should be at least 3 digits long"; + "Invalid phone number format. Enter a valid 8 digit phone number."; public static final String VALIDATION_REGEX = "\\d{3,}"; public final String value; @@ -21,9 +21,13 @@ public class Phone { * @param phone A valid phone number. */ public Phone(String phone) { + requireNonNull(phone); - checkArgument(isValidPhone(phone), MESSAGE_CONSTRAINTS); - value = phone; + String trimmedPhone = phone.replace(" ", ""); + checkArgument(isValidPhone(trimmedPhone), MESSAGE_CONSTRAINTS); + value = trimmedPhone; + + } /** From bea49f621dc7537d056900fa581d933099f9eac3 Mon Sep 17 00:00:00 2001 From: Aditi Gawande <> Date: Thu, 10 Oct 2024 22:12:26 +0800 Subject: [PATCH 2/3] Add phone test cases --- .../java/seedu/address/model/person/Phone.java | 4 ++-- .../seedu/address/model/person/PhoneTest.java | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/model/person/Phone.java b/src/main/java/seedu/address/model/person/Phone.java index 43376845b61..8b2158b224c 100644 --- a/src/main/java/seedu/address/model/person/Phone.java +++ b/src/main/java/seedu/address/model/person/Phone.java @@ -11,7 +11,7 @@ public class Phone { public static final String MESSAGE_CONSTRAINTS = - "Invalid phone number format. Enter a valid 8 digit phone number."; + "Invalid phone number format. Enter a valid phone number that is more than 3 digits."; public static final String VALIDATION_REGEX = "\\d{3,}"; public final String value; @@ -23,7 +23,7 @@ public class Phone { public Phone(String phone) { requireNonNull(phone); - String trimmedPhone = phone.replace(" ", ""); + String trimmedPhone = phone.replaceAll(" ", ""); checkArgument(isValidPhone(trimmedPhone), MESSAGE_CONSTRAINTS); value = trimmedPhone; diff --git a/src/test/java/seedu/address/model/person/PhoneTest.java b/src/test/java/seedu/address/model/person/PhoneTest.java index deaaa5ba190..8e754122e38 100644 --- a/src/test/java/seedu/address/model/person/PhoneTest.java +++ b/src/test/java/seedu/address/model/person/PhoneTest.java @@ -19,6 +19,22 @@ public void constructor_invalidPhone_throwsIllegalArgumentException() { assertThrows(IllegalArgumentException.class, () -> new Phone(invalidPhone)); } + + @Test + public void constructor_validPhone_withSpaces_createsPhone() { + Phone phoneWithSpaces = new Phone(" 911 "); + assertTrue(phoneWithSpaces.equals(new Phone("911"))); // Should equal the trimmed version + + Phone longPhoneWithSpaces = new Phone(" 93121534 "); + assertTrue(longPhoneWithSpaces.equals(new Phone("93121534"))); // Should equal the trimmed version + + Phone mixedSpaces1 = new Phone(" 1234 5678 "); + assertTrue(mixedSpaces1.equals(new Phone("12345678"))); + + Phone mixedSpaces2 = new Phone(" 98 76 54 32 "); + assertTrue(mixedSpaces2.equals(new Phone("98765432"))); + } + @Test public void isValidPhone() { // null phone number @@ -31,6 +47,7 @@ public void isValidPhone() { assertFalse(Phone.isValidPhone("phone")); // non-numeric assertFalse(Phone.isValidPhone("9011p041")); // alphabets within digits assertFalse(Phone.isValidPhone("9312 1534")); // spaces within digits + assertFalse(Phone.isValidPhone(" 9312 1534 ")); //spaces around digits // valid phone numbers assertTrue(Phone.isValidPhone("911")); // exactly 3 numbers From 68a1f07dd0a6ec7bf192c2555e5317cc8ea752d5 Mon Sep 17 00:00:00 2001 From: Aditi Gawande <> Date: Thu, 10 Oct 2024 22:27:29 +0800 Subject: [PATCH 3/3] Edit constructor name --- src/test/java/seedu/address/model/person/PhoneTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/seedu/address/model/person/PhoneTest.java b/src/test/java/seedu/address/model/person/PhoneTest.java index 8e754122e38..26c17123421 100644 --- a/src/test/java/seedu/address/model/person/PhoneTest.java +++ b/src/test/java/seedu/address/model/person/PhoneTest.java @@ -21,7 +21,7 @@ public void constructor_invalidPhone_throwsIllegalArgumentException() { @Test - public void constructor_validPhone_withSpaces_createsPhone() { + public void constructor_validPhone_withSpaces() { Phone phoneWithSpaces = new Phone(" 911 "); assertTrue(phoneWithSpaces.equals(new Phone("911"))); // Should equal the trimmed version