From 2089efb11db1d55fc477ab43d39c1ff52b49b478 Mon Sep 17 00:00:00 2001 From: whitesnowx Date: Tue, 12 Mar 2024 21:53:45 +0800 Subject: [PATCH 1/4] add Module.java class and ModuleTest.java --- .../staffconnect/model/person/Module.java | 62 +++++++++++++++++ .../staffconnect/model/person/ModuleTest.java | 66 +++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 src/main/java/staffconnect/model/person/Module.java create mode 100644 src/test/java/staffconnect/model/person/ModuleTest.java diff --git a/src/main/java/staffconnect/model/person/Module.java b/src/main/java/staffconnect/model/person/Module.java new file mode 100644 index 00000000000..0bee435b19e --- /dev/null +++ b/src/main/java/staffconnect/model/person/Module.java @@ -0,0 +1,62 @@ +package staffconnect.model.person; + +import static java.util.Objects.requireNonNull; +import static staffconnect.commons.util.AppUtil.checkArgument; + +/** + * Represents a Person's phone number in the staff book. + * Guarantees: immutable; is valid as declared in {@link #isValidModule(String)} + */ +public class Module { + + + public static final String MESSAGE_CONSTRAINTS = + "Module code should contain 2-4 capital letters followed by 4 digits long and at most 1 capitalised suffix"; + public static final String VALIDATION_REGEX = "[A-Z]{2,5}\\d{4}[A-Z]{0,2}"; + + public final String value; + + /** + * Constructs a {@code Module}. + * + * @param module A valid module code. + */ + public Module(String module) { + requireNonNull(module); + checkArgument(isValidModule(module), MESSAGE_CONSTRAINTS); + value = module; + } + + /** + * Returns true if a given string is a valid module code. + */ + public static boolean isValidModule(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 Module)) { + return false; + } + + Module otherModule = (Module) other; + return value.equals(otherModule.value); + } + + @Override + public int hashCode() { + return value.hashCode(); + } + +} diff --git a/src/test/java/staffconnect/model/person/ModuleTest.java b/src/test/java/staffconnect/model/person/ModuleTest.java new file mode 100644 index 00000000000..ace8b8d2658 --- /dev/null +++ b/src/test/java/staffconnect/model/person/ModuleTest.java @@ -0,0 +1,66 @@ +package staffconnect.model.person; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static staffconnect.testutil.Assert.assertThrows; + +import org.junit.jupiter.api.Test; + +public class ModuleTest { + + @Test + public void constructor_null_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> new Module(null)); + } + + @Test + public void constructor_invalidModule_throwsIllegalArgumentException() { + String invalidModule = ""; + assertThrows(IllegalArgumentException.class, () -> new Module(invalidModule)); + } + + @Test + public void isValidModule() { + // null phone number + assertThrows(NullPointerException.class, () -> Module.isValidModule(null)); + + // invalid phone numbers + assertFalse(Module.isValidModule("")); // empty string + assertFalse(Module.isValidModule(" ")); // spaces only + assertFalse(Module.isValidModule("ABCD")); // letters only + assertFalse(Module.isValidModule("1234")); // numbers only + assertFalse(Module.isValidModule("2103T")); // missing prefix + assertFalse(Module.isValidModule("C2103T")); // only 1 prefix + assertFalse(Module.isValidModule("CS2103TTTT")); // too many suffix + assertFalse(Module.isValidModule("CSCSCS2103T")); // too many prefix + assertFalse(Module.isValidModule("CS210310101010T")); // too many numbers + assertFalse(Module.isValidModule("CS21T")); // too little numbers + assertFalse(Module.isValidModule("cs2103t")); // suffix not capitalised + assertFalse(Module.isValidModule("cs2103T")); // prefix not capitalised + + // valid phone numbers + assertTrue(Module.isValidModule("CS2103")); // 2 prefix, 4 letters without 1 optional suffix + assertTrue(Module.isValidModule("CS2103T")); // 2 prefix, 4 letters with 1 optional suffix + assertTrue(Module.isValidModule("GEN2050")); // 3 prefix, 4 letters without 1 optional suffix + assertTrue(Module.isValidModule("GEN2050Y")); // 3 prefix, 4 letters with 1 optional suffix + assertTrue(Module.isValidModule("GESS1035")); // 4 prefix, 4 letters without 1 optional suffix + assertTrue(Module.isValidModule("GESS1035X")); // 4 prefix, 4 letters with 1 optional suffix + } + + @Test + public void equals() { + Module module = new Module("CS2103"); + + // same values -> returns true + assertTrue(module.equals(new Module("CS2103"))); + + // same object -> returns true + assertTrue(module.equals(module)); + + // null -> returns false + assertFalse(module.equals(null)); + + // different values -> returns false + assertFalse(module.equals(new Module("MS2345"))); + } +} From c7fe269507a07b4d70a9b128af7e090aacbebf4a Mon Sep 17 00:00:00 2001 From: whitesnowx Date: Tue, 12 Mar 2024 22:17:25 +0800 Subject: [PATCH 2/4] Change class description to module code --- src/main/java/staffconnect/model/person/Module.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/staffconnect/model/person/Module.java b/src/main/java/staffconnect/model/person/Module.java index 0bee435b19e..b0c1cfbc839 100644 --- a/src/main/java/staffconnect/model/person/Module.java +++ b/src/main/java/staffconnect/model/person/Module.java @@ -4,7 +4,7 @@ import static staffconnect.commons.util.AppUtil.checkArgument; /** - * Represents a Person's phone number in the staff book. + * Represents a Person's Module code in the staff book. * Guarantees: immutable; is valid as declared in {@link #isValidModule(String)} */ public class Module { From 19ec67cf92c132ba370225b125c5cac6c23790c7 Mon Sep 17 00:00:00 2001 From: whitesnowx Date: Tue, 12 Mar 2024 22:35:08 +0800 Subject: [PATCH 3/4] fix validation_regex for module codes in Module.java --- src/main/java/staffconnect/model/person/Module.java | 2 +- src/test/java/staffconnect/model/person/ModuleTest.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/staffconnect/model/person/Module.java b/src/main/java/staffconnect/model/person/Module.java index b0c1cfbc839..a8dbddc72ed 100644 --- a/src/main/java/staffconnect/model/person/Module.java +++ b/src/main/java/staffconnect/model/person/Module.java @@ -12,7 +12,7 @@ public class Module { public static final String MESSAGE_CONSTRAINTS = "Module code should contain 2-4 capital letters followed by 4 digits long and at most 1 capitalised suffix"; - public static final String VALIDATION_REGEX = "[A-Z]{2,5}\\d{4}[A-Z]{0,2}"; + public static final String VALIDATION_REGEX = "[A-Z]{2,4}\\d{4}[A-Z]{0,1}"; public final String value; diff --git a/src/test/java/staffconnect/model/person/ModuleTest.java b/src/test/java/staffconnect/model/person/ModuleTest.java index ace8b8d2658..da903f8668d 100644 --- a/src/test/java/staffconnect/model/person/ModuleTest.java +++ b/src/test/java/staffconnect/model/person/ModuleTest.java @@ -31,8 +31,8 @@ public void isValidModule() { assertFalse(Module.isValidModule("1234")); // numbers only assertFalse(Module.isValidModule("2103T")); // missing prefix assertFalse(Module.isValidModule("C2103T")); // only 1 prefix - assertFalse(Module.isValidModule("CS2103TTTT")); // too many suffix - assertFalse(Module.isValidModule("CSCSCS2103T")); // too many prefix + assertFalse(Module.isValidModule("CS2103TT")); // too many suffix + assertFalse(Module.isValidModule("CSCSC2103T")); // too many prefix assertFalse(Module.isValidModule("CS210310101010T")); // too many numbers assertFalse(Module.isValidModule("CS21T")); // too little numbers assertFalse(Module.isValidModule("cs2103t")); // suffix not capitalised From 779c1b70177f6462c022857997f799124d3931da Mon Sep 17 00:00:00 2001 From: whitesnowx Date: Tue, 12 Mar 2024 23:01:56 +0800 Subject: [PATCH 4/4] added different types for equals() test --- src/test/java/staffconnect/model/person/ModuleTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/java/staffconnect/model/person/ModuleTest.java b/src/test/java/staffconnect/model/person/ModuleTest.java index da903f8668d..dc1cf7613c3 100644 --- a/src/test/java/staffconnect/model/person/ModuleTest.java +++ b/src/test/java/staffconnect/model/person/ModuleTest.java @@ -60,6 +60,9 @@ public void equals() { // null -> returns false assertFalse(module.equals(null)); + // different types -> returns false + assertFalse(module.equals(5.0f)); + // different values -> returns false assertFalse(module.equals(new Module("MS2345"))); }