forked from nus-cs2103-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Faculty
class
#68
Merged
Pluiexo
merged 15 commits into
AY2324S2-CS2103-F08-3:master
from
JerryWang0000:branch-Faculty
Mar 15, 2024
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
df81f7c
Merge branch 'master' of https://github.com/JerryWang0000/tp
JerryWang0000 5b0fc0a
Merge branch 'AY2324S2-CS2103-F08-3:master' into master
JerryWang0000 c10d74b
Merge branch 'master' of https://github.com/JerryWang0000/tp
JerryWang0000 b870db1
Merge branch 'AY2324S2-CS2103-F08-3:master' into master
JerryWang0000 f7c4297
Merge branch 'master' of https://github.com/JerryWang0000/tp
JerryWang0000 c6d07f2
Add Faculty class
JerryWang0000 ef39cb0
Add JavaDoc
JerryWang0000 bab057d
Make minor changes
JerryWang0000 ae96be6
Modify the euqals() method
JerryWang0000 d11121f
Modify Faculty to use enums
JerryWang0000 727040a
Make minor changes
JerryWang0000 077c6fe
Change Type of value in Faculty.java
JerryWang0000 d98567e
Fix minor typos in JavaDoc
JerryWang0000 326f20f
Change value type of Faculty to Enum
JerryWang0000 e1b7be0
Rename the name of FacultyEnum
JerryWang0000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,113 @@ | ||
package staffconnect.model.person; | ||
|
||
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 | ||
* {@link #isValidFaculty(String)} | ||
*/ | ||
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 faculty, | ||
* or the value is invalid. The enum class serves as the purpose for parsing user strings into the faculty name. | ||
*/ | ||
public enum FacultyName { | ||
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 facultyNameValue; | ||
|
||
FacultyName(String facultyName) { | ||
this.facultyNameValue = facultyName; | ||
} | ||
|
||
/** | ||
* Links enum member to its name. | ||
* | ||
* @return name of the faculty | ||
*/ | ||
public String getFacultyName() { | ||
return facultyNameValue; | ||
} | ||
} | ||
public final FacultyName value; | ||
|
||
/** | ||
* Constructs a {@code Faculty}. | ||
* | ||
* @param faculty A valid faculty. | ||
*/ | ||
public Faculty(String faculty) { | ||
requireNonNull(faculty); | ||
checkArgument(isValidFaculty(faculty), MESSAGE_CONSTRAINTS); | ||
value = fromString(faculty); // can be extended | ||
} | ||
|
||
/** | ||
* 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(FacultyName.values()) | ||
.anyMatch(faculty -> faculty.getFacultyName().equalsIgnoreCase(test)); | ||
} | ||
|
||
private static FacultyName fromString(String name) { | ||
for (FacultyName faculty : FacultyName.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.getFacultyName(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == this) { | ||
return true; | ||
} | ||
|
||
if (!(obj instanceof Faculty)) { | ||
return false; | ||
} | ||
|
||
Faculty otherFaculty = (Faculty) obj; | ||
return this.value.equals(otherFaculty.value); | ||
} | ||
|
||
@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 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() { | ||
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("science")); // ignore capitals | ||
assertTrue(Faculty.isValidFaculty("Computing")); | ||
assertTrue(Faculty.isValidFaculty("Arts and Social Sciences")); // long faculty | ||
} | ||
|
||
@Test | ||
public void equals() { | ||
Faculty faculty = new Faculty("Computing"); | ||
|
||
// same values -> returns true | ||
assertEquals(faculty, new Faculty("Computing")); | ||
|
||
// 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("Science")); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some may consider this magic string, but you don't have to fix it in this commit