diff --git a/src/main/java/staffconnect/model/availability/Availability.java b/src/main/java/staffconnect/model/availability/Availability.java new file mode 100644 index 00000000000..dc8df4673c6 --- /dev/null +++ b/src/main/java/staffconnect/model/availability/Availability.java @@ -0,0 +1,87 @@ +package staffconnect.model.availability; + +import static java.util.Objects.requireNonNull; +import static staffconnect.commons.util.AppUtil.checkArgument; + +import java.time.DayOfWeek; +/** + * Represents a Person's availability in the staff book. + * Guarantees: immutable; is valid as declared in {@link #isValidAvailability(String)} + */ +public class Availability { + + + public static final String MESSAGE_CONSTRAINTS = + "Should be a week of the day, the full word or the first syllable of the word"; + public static final String VALIDATION_REGEX = "(?i)((mon|tue(s)?|wed(nes)?|thu(r)?(rs)?|fri|sat(ur)?|sun)(day)?)"; + + public final String value; + + /** + * Constructs a {@code Availability}. + * + * @param availability A valid availability. + */ + public Availability(String availability) { + requireNonNull(availability); + checkArgument(isValidAvailability(availability), MESSAGE_CONSTRAINTS); + value = strToDayOfWeek(availability); + } + + /** + * Returns true if a given string is a valid availability. + */ + public static boolean isValidAvailability(String test) { + return test.matches(VALIDATION_REGEX); + } + + /** + * Returns a standardised Day of the Week. + * + * @param validAvailability validatedAvailability to be translated. + * @return Corresponding Day of Week. + */ + public static String strToDayOfWeek(String validAvailability) { + if (validAvailability.matches("(?i)(mon)(day)?")) { + return DayOfWeek.MONDAY.name(); + } else if (validAvailability.matches("(?i)(tue)(s)?(sday)?")) { + return DayOfWeek.TUESDAY.name(); + } else if (validAvailability.matches("(?i)(wed)(nes)?(nesday)?")) { + return DayOfWeek.WEDNESDAY.name(); + } else if (validAvailability.matches("(?i)(thu)(r)?(rs)?(rsday)?")) { + return DayOfWeek.THURSDAY.name(); + } else if (validAvailability.matches("(?i)(fri)(day)?")) { + return DayOfWeek.FRIDAY.name(); + } else if (validAvailability.matches("(?i)(sat)(ur)?(urday)?")) { + return DayOfWeek.SATURDAY.name(); + } else { + return DayOfWeek.SUNDAY.name(); + } + } + + @Override + public String toString() { + return '[' + value + ']'; + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof Availability)) { + return false; + } + + Availability otherAvailability = (Availability) other; + return value.equals(otherAvailability.value); + } + + @Override + public int hashCode() { + return value.hashCode(); + } + +} diff --git a/src/test/java/staffconnect/model/availability/AvailabilityTest.java b/src/test/java/staffconnect/model/availability/AvailabilityTest.java new file mode 100644 index 00000000000..d10ba39d211 --- /dev/null +++ b/src/test/java/staffconnect/model/availability/AvailabilityTest.java @@ -0,0 +1,111 @@ +package staffconnect.model.availability; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static staffconnect.testutil.Assert.assertThrows; + +import java.time.DayOfWeek; + +import org.junit.jupiter.api.Test; + +public class AvailabilityTest { + + @Test + public void constructor_null_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> new Availability(null)); + } + + @Test + public void constructor_invalidAvailability_throwsIllegalArgumentException() { + String invalidAvailability = ""; + assertThrows(IllegalArgumentException.class, () -> new Availability(invalidAvailability)); + } + + @Test + public void isValidAvailability() { + // null availability + assertThrows(NullPointerException.class, () -> Availability.isValidAvailability(null)); + + // invalid availability + assertFalse(Availability.isValidAvailability("")); // empty string + assertFalse(Availability.isValidAvailability(" ")); // spaces only + assertFalse(Availability.isValidAvailability("m")); // 1 letter only + assertFalse(Availability.isValidAvailability("1")); // 1 number only + assertFalse(Availability.isValidAvailability("m o n")); // spaces between letters + assertFalse(Availability.isValidAvailability("birthday")); // incorrect type of day + assertFalse(Availability.isValidAvailability("moon")); // 1 letter duplicated + assertFalse(Availability.isValidAvailability("mon123")); // trailing number behind day + assertFalse(Availability.isValidAvailability("#@$!")); // special characters + assertFalse(Availability.isValidAvailability("mon#@$!")); // prefix and special characters + assertFalse(Availability.isValidAvailability("123#@$!")); // numbers and special characters + assertFalse(Availability.isValidAvailability("day")); // day without prefix + assertFalse(Availability.isValidAvailability("sytaudrh")); // scrambled spelling + assertFalse(Availability.isValidAvailability("chewsday")); // misspelling + assertFalse(Availability.isValidAvailability("th")); // uncompleted syllable + assertFalse(Availability.isValidAvailability("sun day")); // space between syllable + assertFalse(Availability.isValidAvailability("sat day")); // missing middle syllable + assertFalse(Availability.isValidAvailability("satuday")); // missing middle letter + + // valid availability + assertTrue(Availability.isValidAvailability("mon")); // first syllable tuesday + assertTrue(Availability.isValidAvailability("tue")); // first syllable tuesday without s + assertTrue(Availability.isValidAvailability("tues")); // first syllable tuesday with s + assertTrue(Availability.isValidAvailability("wed")); // first syllable wednesday + assertTrue(Availability.isValidAvailability("wednes")); // two syllable wednesday + assertTrue(Availability.isValidAvailability("thu")); // first syllable thursday without rs + assertTrue(Availability.isValidAvailability("thur")); // first syllable thursday without s + assertTrue(Availability.isValidAvailability("thurs")); // first syllable thursday with s + assertTrue(Availability.isValidAvailability("sat")); // first syllable saturday + assertTrue(Availability.isValidAvailability("satur")); // two syllable saturday + assertTrue(Availability.isValidAvailability("sun")); // first syllable sunday + assertTrue(Availability.isValidAvailability("Sun")); // first syllable with capitalised first letter + assertTrue(Availability.isValidAvailability("sAtUr")); // two syllable mixed case + assertTrue(Availability.isValidAvailability("monday")); // full word lowercase monday + assertTrue(Availability.isValidAvailability("tuesday")); // full word lowercase tuesday + assertTrue(Availability.isValidAvailability("wednesday")); // full word lowercase wednesday + assertTrue(Availability.isValidAvailability("thursday")); // full word lowercase thursday + assertTrue(Availability.isValidAvailability("friday")); // full word lowercase friday + assertTrue(Availability.isValidAvailability("saturday")); // full word lowercase saturday + assertTrue(Availability.isValidAvailability("sunday")); // full word lowercase sunday + assertTrue(Availability.isValidAvailability("MONDAY")); // full word uppercase monday + assertTrue(Availability.isValidAvailability("Tuesday")); // full word with capitalised first letter + assertTrue(Availability.isValidAvailability("wEdNeSdAy")); // full word mixed case + } + + @Test + public void equals() { + Availability monAvailability = new Availability("monday"); + + // same values -> returns true + assertTrue(monAvailability.equals(new Availability("monday"))); + + // same day -> return true + assertTrue(monAvailability.equals(new Availability("mOn"))); + + // same object -> returns true + assertTrue(monAvailability.equals(monAvailability)); + + // null -> returns false + assertFalse(monAvailability.equals(null)); + + // different types -> returns false + assertFalse(monAvailability.equals(5.0f)); + + // different values -> returns false, mon and tues + assertFalse(monAvailability.equals(new Availability("tues"))); + + } + + @Test + public void strToDayOfWeekTest() { + assertEquals(DayOfWeek.MONDAY.toString(), Availability.strToDayOfWeek(new String("mon"))); + assertEquals(DayOfWeek.TUESDAY.toString(), Availability.strToDayOfWeek(new String("tues"))); + assertEquals(DayOfWeek.WEDNESDAY.toString(), Availability.strToDayOfWeek(new String("wednes"))); + assertEquals(DayOfWeek.THURSDAY.toString(), Availability.strToDayOfWeek(new String("thursday"))); + assertEquals(DayOfWeek.FRIDAY.toString(), Availability.strToDayOfWeek(new String("FRIDAY"))); + assertEquals(DayOfWeek.SATURDAY.toString(), Availability.strToDayOfWeek(new String("SATurday"))); + assertEquals(DayOfWeek.SUNDAY.toString(), Availability.strToDayOfWeek(new String("sunDAY"))); + } + +} diff --git a/src/test/java/staffconnect/model/person/ModuleTest.java b/src/test/java/staffconnect/model/person/ModuleTest.java index dc1cf7613c3..a9c205e3fe0 100644 --- a/src/test/java/staffconnect/model/person/ModuleTest.java +++ b/src/test/java/staffconnect/model/person/ModuleTest.java @@ -21,10 +21,10 @@ public void constructor_invalidModule_throwsIllegalArgumentException() { @Test public void isValidModule() { - // null phone number + // null module code assertThrows(NullPointerException.class, () -> Module.isValidModule(null)); - // invalid phone numbers + // invalid module code assertFalse(Module.isValidModule("")); // empty string assertFalse(Module.isValidModule(" ")); // spaces only assertFalse(Module.isValidModule("ABCD")); // letters only @@ -38,7 +38,7 @@ public void isValidModule() { assertFalse(Module.isValidModule("cs2103t")); // suffix not capitalised assertFalse(Module.isValidModule("cs2103T")); // prefix not capitalised - // valid phone numbers + // valid module code assertTrue(Module.isValidModule("CS2103")); // 2 prefix, 4 letters without 1 optional suffix assertTrue(Module.isValidModule("CS2103T")); // 2 prefix, 4 letters with 1 optional suffix assertTrue(Module.isValidModule("GEN2050")); // 3 prefix, 4 letters without 1 optional suffix