From c6d07f269ec044e61d67e5dd60ea69c82ab352e1 Mon Sep 17 00:00:00 2001 From: JerryWang0000 Date: Thu, 14 Mar 2024 00:10:42 +0800 Subject: [PATCH 1/7] Add Faculty class Add Faculty and its test class. --- .../staffconnect/model/person/Faculty.java | 47 +++++++++++++++ .../model/person/FacultyTest.java | 57 +++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 src/main/java/staffconnect/model/person/Faculty.java create mode 100644 src/test/java/staffconnect/model/person/FacultyTest.java diff --git a/src/main/java/staffconnect/model/person/Faculty.java b/src/main/java/staffconnect/model/person/Faculty.java new file mode 100644 index 00000000000..c045eb66c3a --- /dev/null +++ b/src/main/java/staffconnect/model/person/Faculty.java @@ -0,0 +1,47 @@ +package staffconnect.model.person; + +import static java.util.Objects.requireNonNull; +import static staffconnect.commons.util.AppUtil.checkArgument; + +public class Faculty { + public static final String MESSAGE_CONSTRAINTS = "This can be any non-blank String content."; + /* + * The first character of the faculty must not be a whitespace, + * otherwise " " (a blank string) becomes a valid input. + */ + public static final String VALIDATION_REGEX = "[^\\s].*"; + private final String value; + + /** + * Constructs a {@code Faculty}. + * + * @param faculty A valid faculty. + */ + public Faculty(String faculty) { + requireNonNull(faculty); + checkArgument(isValidFaculty(faculty), MESSAGE_CONSTRAINTS); + value = faculty; + } + + public static boolean isValidFaculty(String test) { + return test.matches(VALIDATION_REGEX); + } + + @Override + public String toString() { + return value; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof Faculty) { + return ((Faculty) obj).value.equals(this.value); + } + return false; + } + + @Override + public int hashCode() { + return value.hashCode(); + } +} diff --git a/src/test/java/staffconnect/model/person/FacultyTest.java b/src/test/java/staffconnect/model/person/FacultyTest.java new file mode 100644 index 00000000000..d6f247c202e --- /dev/null +++ b/src/test/java/staffconnect/model/person/FacultyTest.java @@ -0,0 +1,57 @@ +package staffconnect.model.person; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static staffconnect.testutil.Assert.assertThrows; + +public class FacultyTest { + @Test + public void constructor_null_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> new Faculty(null)); + } + + @Test + public void constructor_invalidFaculty_throwsIllegalArgumentException() { + String invalidFaculty = ""; + assertThrows(IllegalArgumentException.class, () -> new Faculty(invalidFaculty)); + } + + @Test + public void isValidFaculty() { + // null Faculty + assertThrows(NullPointerException.class, () -> Faculty.isValidFaculty(null)); + + // invalid faculties + assertFalse(Faculty.isValidFaculty("")); // empty string + assertFalse(Faculty.isValidFaculty(" ")); // spaces only + + // valid Faculties + assertTrue(Faculty.isValidFaculty("~"));// one character + assertTrue(Faculty.isValidFaculty("School of Computing"));// long faculty + assertTrue(Faculty.isValidFaculty("abcdefg")); + } + + @Test + public void equals() { + Faculty faculty = new Faculty("a faculty"); + + // same values -> returns true + assertEquals(faculty, new Faculty("a faculty")); + + // same object -> returns true + assertEquals(faculty, faculty); + + // null -> returns false + assertNotEquals(null, faculty); + + // different types -> returns false + assertFalse(faculty.equals(5.0f)); + + // different values -> returns false + assertNotEquals(faculty, new Faculty("Other Faculty")); + } +} From ef39cb0539a652249c1f39e67c3356b3ef8cb01b Mon Sep 17 00:00:00 2001 From: JerryWang0000 Date: Thu, 14 Mar 2024 00:18:48 +0800 Subject: [PATCH 2/7] Add JavaDoc --- src/main/java/staffconnect/model/person/Faculty.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/staffconnect/model/person/Faculty.java b/src/main/java/staffconnect/model/person/Faculty.java index c045eb66c3a..84c17f24be7 100644 --- a/src/main/java/staffconnect/model/person/Faculty.java +++ b/src/main/java/staffconnect/model/person/Faculty.java @@ -3,6 +3,11 @@ import static java.util.Objects.requireNonNull; import static staffconnect.commons.util.AppUtil.checkArgument; +/** + * Represents a Person's faculty in the staff book. + * Guarantees: immutable; is valid as declared in + * {@link #isValidFaculty(String)} + */ public class Faculty { public static final String MESSAGE_CONSTRAINTS = "This can be any non-blank String content."; /* From bab057d070f3d09d1ed418160e2d1694685af6e2 Mon Sep 17 00:00:00 2001 From: JerryWang0000 Date: Thu, 14 Mar 2024 00:28:17 +0800 Subject: [PATCH 3/7] Make minor changes --- .../java/staffconnect/model/person/FacultyTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/java/staffconnect/model/person/FacultyTest.java b/src/test/java/staffconnect/model/person/FacultyTest.java index d6f247c202e..e60c6654ade 100644 --- a/src/test/java/staffconnect/model/person/FacultyTest.java +++ b/src/test/java/staffconnect/model/person/FacultyTest.java @@ -1,13 +1,13 @@ package staffconnect.model.person; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static staffconnect.testutil.Assert.assertThrows; +import org.junit.jupiter.api.Test; + public class FacultyTest { @Test public void constructor_null_throwsNullPointerException() { @@ -30,8 +30,8 @@ public void isValidFaculty() { assertFalse(Faculty.isValidFaculty(" ")); // spaces only // valid Faculties - assertTrue(Faculty.isValidFaculty("~"));// one character - assertTrue(Faculty.isValidFaculty("School of Computing"));// long faculty + assertTrue(Faculty.isValidFaculty("~")); // one character + assertTrue(Faculty.isValidFaculty("School of Computing")); // long faculty assertTrue(Faculty.isValidFaculty("abcdefg")); } From ae96be67a9e90ec987e4e522e5e80f624cdc873d Mon Sep 17 00:00:00 2001 From: JerryWang0000 Date: Thu, 14 Mar 2024 22:15:10 +0800 Subject: [PATCH 4/7] Modify the euqals() method --- src/main/java/staffconnect/model/person/Faculty.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/java/staffconnect/model/person/Faculty.java b/src/main/java/staffconnect/model/person/Faculty.java index 84c17f24be7..cb511fc5cf6 100644 --- a/src/main/java/staffconnect/model/person/Faculty.java +++ b/src/main/java/staffconnect/model/person/Faculty.java @@ -39,10 +39,16 @@ public String toString() { @Override public boolean equals(Object obj) { - if (obj instanceof Faculty) { - return ((Faculty) obj).value.equals(this.value); + if (obj == this) { + return true; } - return false; + + if (!(obj instanceof Faculty)) { + return false; + } + + Faculty otherFaculty = (Faculty) obj; + return this.value.equals(otherFaculty.value); } @Override From d11121f5b0f1e7b799d63d1532ec54112cdb0843 Mon Sep 17 00:00:00 2001 From: JerryWang0000 Date: Fri, 15 Mar 2024 00:33:08 +0800 Subject: [PATCH 5/7] Modify Faculty to use enums Restrict the valid input values to currently existing NUS faculties. Change the test class. --- .../staffconnect/model/person/Faculty.java | 65 ++++++++++++++++--- .../model/person/FacultyTest.java | 12 ++-- 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/src/main/java/staffconnect/model/person/Faculty.java b/src/main/java/staffconnect/model/person/Faculty.java index cb511fc5cf6..58d753e2908 100644 --- a/src/main/java/staffconnect/model/person/Faculty.java +++ b/src/main/java/staffconnect/model/person/Faculty.java @@ -1,5 +1,8 @@ package staffconnect.model.person; +import java.lang.reflect.Array; +import java.util.Arrays; + import static java.util.Objects.requireNonNull; import static staffconnect.commons.util.AppUtil.checkArgument; @@ -9,13 +12,47 @@ * {@link #isValidFaculty(String)} */ public class Faculty { - public static final String MESSAGE_CONSTRAINTS = "This can be any non-blank String content."; + public static final String MESSAGE_CONSTRAINTS = + "The content should be a String representing a real faculty of NUS"; /* - * The first character of the faculty must not be a whitespace, - * otherwise " " (a blank string) becomes a valid input. + * For this version, a valid faculty value should match exactly the full name of the facult, + * or the value is invalid */ - public static final String VALIDATION_REGEX = "[^\\s].*"; - private final String value; + public enum NUSFaculty { + ARTS_AND_SOCIAL_SCIENCES("Arts and Social Sciences"), + BUSINESS("Business"), + COMPUTING("Computing"), + CONTINUING_AND_LIFELONG_EDUCATION("Continuing and Lifelong Education"), + DENTISTRY("Dentistry"), + DESIGN_AND_ENVIRONMENT("Design and Environment"), + DUKE_NUS_MEDICAL_SCHOOL("Duke-NUS Medical School"), + ENGINEERING("Engineering"), + INTEGRATIVE_SCIENCES_AND_ENGINEERING("Integrative Sciences and Engineering"), + LAW("Law"), + MEDICINE("Medicine"), + MUSIC("Music"), + PUBLIC_HEALTH("Public Health"), + PUBLIC_POLICY("Public Policy"), + SCIENCE("Science"), + UNIVERSITY_SCHOLARS_PROGRAMME("University Scholars Programme"), + YALE_NUS_COLLEGE("Yale-NUS College"); + + private final String facultyName; + + NUSFaculty(String facultyName) { + this.facultyName = facultyName; + } + + /** + * Links enum member to its name. + * + * @return name of the faculty + */ + public String getFacultyName() { + return facultyName; + } + } + private final NUSFaculty value; /** * Constructs a {@code Faculty}. @@ -25,16 +62,28 @@ public class Faculty { public Faculty(String faculty) { requireNonNull(faculty); checkArgument(isValidFaculty(faculty), MESSAGE_CONSTRAINTS); - value = faculty; + value = fromString(faculty); } public static boolean isValidFaculty(String test) { - return test.matches(VALIDATION_REGEX); + requireNonNull(test, "faculty cannot be null"); + + return Arrays.stream(NUSFaculty.values()) + .anyMatch(faculty -> faculty.getFacultyName().equalsIgnoreCase(test)); + } + + private static NUSFaculty fromString(String name) { + for (NUSFaculty faculty : NUSFaculty.values()) { + if (faculty.getFacultyName().equalsIgnoreCase(name)) { + return faculty; + } + } + throw new IllegalArgumentException("No enum constant matches the provided name: " + name); } @Override public String toString() { - return value; + return value.getFacultyName(); } @Override diff --git a/src/test/java/staffconnect/model/person/FacultyTest.java b/src/test/java/staffconnect/model/person/FacultyTest.java index e60c6654ade..c8d63aeccf4 100644 --- a/src/test/java/staffconnect/model/person/FacultyTest.java +++ b/src/test/java/staffconnect/model/person/FacultyTest.java @@ -30,17 +30,17 @@ public void isValidFaculty() { assertFalse(Faculty.isValidFaculty(" ")); // spaces only // valid Faculties - assertTrue(Faculty.isValidFaculty("~")); // one character - assertTrue(Faculty.isValidFaculty("School of Computing")); // long faculty - assertTrue(Faculty.isValidFaculty("abcdefg")); + assertTrue(Faculty.isValidFaculty("science")); // ignore capitals + assertTrue(Faculty.isValidFaculty("Computing")); + assertTrue(Faculty.isValidFaculty("Arts and Social Sciences")); // long faculty } @Test public void equals() { - Faculty faculty = new Faculty("a faculty"); + Faculty faculty = new Faculty("Computing"); // same values -> returns true - assertEquals(faculty, new Faculty("a faculty")); + assertEquals(faculty, new Faculty("Computing")); // same object -> returns true assertEquals(faculty, faculty); @@ -52,6 +52,6 @@ public void equals() { assertFalse(faculty.equals(5.0f)); // different values -> returns false - assertNotEquals(faculty, new Faculty("Other Faculty")); + assertNotEquals(faculty, new Faculty("Science")); } } From 727040a27fdc4ef3c127113d7c90647f5f866083 Mon Sep 17 00:00:00 2001 From: JerryWang0000 Date: Fri, 15 Mar 2024 00:49:48 +0800 Subject: [PATCH 6/7] Make minor changes --- .../staffconnect/model/person/Faculty.java | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main/java/staffconnect/model/person/Faculty.java b/src/main/java/staffconnect/model/person/Faculty.java index 58d753e2908..8c83337e3c4 100644 --- a/src/main/java/staffconnect/model/person/Faculty.java +++ b/src/main/java/staffconnect/model/person/Faculty.java @@ -1,11 +1,10 @@ package staffconnect.model.person; -import java.lang.reflect.Array; -import java.util.Arrays; - import static java.util.Objects.requireNonNull; import static staffconnect.commons.util.AppUtil.checkArgument; +import java.util.Arrays; + /** * Represents a Person's faculty in the staff book. * Guarantees: immutable; is valid as declared in @@ -14,11 +13,12 @@ public class Faculty { public static final String MESSAGE_CONSTRAINTS = "The content should be a String representing a real faculty of NUS"; - /* + + /** * For this version, a valid faculty value should match exactly the full name of the facult, * or the value is invalid */ - public enum NUSFaculty { + public enum FacultyEnum { ARTS_AND_SOCIAL_SCIENCES("Arts and Social Sciences"), BUSINESS("Business"), COMPUTING("Computing"), @@ -39,7 +39,7 @@ public enum NUSFaculty { private final String facultyName; - NUSFaculty(String facultyName) { + FacultyEnum(String facultyName) { this.facultyName = facultyName; } @@ -52,7 +52,7 @@ public String getFacultyName() { return facultyName; } } - private final NUSFaculty value; + private final FacultyEnum value; /** * Constructs a {@code Faculty}. @@ -65,15 +65,21 @@ public Faculty(String faculty) { value = fromString(faculty); } + /** + * Verifies if a faculty name is valid. + * + * @param test string representing the faculty name + * @return True if the input matches any faculty name. + */ public static boolean isValidFaculty(String test) { requireNonNull(test, "faculty cannot be null"); - return Arrays.stream(NUSFaculty.values()) + return Arrays.stream(FacultyEnum.values()) .anyMatch(faculty -> faculty.getFacultyName().equalsIgnoreCase(test)); } - private static NUSFaculty fromString(String name) { - for (NUSFaculty faculty : NUSFaculty.values()) { + private static FacultyEnum fromString(String name) { + for (FacultyEnum faculty : FacultyEnum.values()) { if (faculty.getFacultyName().equalsIgnoreCase(name)) { return faculty; } From 077c6fedff47fadaec57fd89fde88254699927da Mon Sep 17 00:00:00 2001 From: JerryWang0000 Date: Fri, 15 Mar 2024 01:56:42 +0800 Subject: [PATCH 7/7] Change Type of value in Faculty.java Change the type of value to String. --- src/main/java/staffconnect/model/person/Faculty.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/staffconnect/model/person/Faculty.java b/src/main/java/staffconnect/model/person/Faculty.java index 8c83337e3c4..2e10dc3f6c4 100644 --- a/src/main/java/staffconnect/model/person/Faculty.java +++ b/src/main/java/staffconnect/model/person/Faculty.java @@ -52,7 +52,7 @@ public String getFacultyName() { return facultyName; } } - private final FacultyEnum value; + public final String value; /** * Constructs a {@code Faculty}. @@ -62,7 +62,7 @@ public String getFacultyName() { public Faculty(String faculty) { requireNonNull(faculty); checkArgument(isValidFaculty(faculty), MESSAGE_CONSTRAINTS); - value = fromString(faculty); + value = fromString(faculty).getFacultyName(); // can be extended } /** @@ -89,7 +89,7 @@ private static FacultyEnum fromString(String name) { @Override public String toString() { - return value.getFacultyName(); + return value; } @Override