Skip to content

Commit

Permalink
Add Faculty class
Browse files Browse the repository at this point in the history
Add Faculty and its test class.
  • Loading branch information
JerryWang0000 committed Mar 13, 2024
1 parent f7c4297 commit c6d07f2
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/main/java/staffconnect/model/person/Faculty.java
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();
}
}
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 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"));
}
}

0 comments on commit c6d07f2

Please sign in to comment.