Skip to content

Commit

Permalink
Modify Faculty to use enums
Browse files Browse the repository at this point in the history
Restrict the valid input values to currently existing NUS faculties. Change the test class.
  • Loading branch information
JerryWang0000 committed Mar 14, 2024
1 parent ae96be6 commit d11121f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
65 changes: 57 additions & 8 deletions src/main/java/staffconnect/model/person/Faculty.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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}.
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/staffconnect/model/person/FacultyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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"));
}
}

0 comments on commit d11121f

Please sign in to comment.