forked from nus-cs2103-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")); | ||
} | ||
} |