Skip to content
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
merged 15 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/main/java/staffconnect/model/person/Faculty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package staffconnect.model.person;

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.";
/*
* 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].*";
tsulim marked this conversation as resolved.
Show resolved Hide resolved
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;

Check warning on line 37 in src/main/java/staffconnect/model/person/Faculty.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/staffconnect/model/person/Faculty.java#L37

Added line #L37 was not covered by tests
Copy link

@Pluiexo Pluiexo Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When storing value as a enum, you may choose call a toString in the enum class to return the value inside the enums instead

}

@Override
public boolean equals(Object obj) {
if (obj instanceof Faculty) {
return ((Faculty) obj).value.equals(this.value);
}
return false;
}
tsulim marked this conversation as resolved.
Show resolved Hide resolved

@Override
public int hashCode() {
return value.hashCode();

Check warning on line 50 in src/main/java/staffconnect/model/person/Faculty.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/staffconnect/model/person/Faculty.java#L50

Added line #L50 was not covered by tests
}
}
57 changes: 57 additions & 0 deletions src/test/java/staffconnect/model/person/FacultyTest.java
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("~")); // 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"));
}
}
Loading