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.
Merge pull request #62 from whitesnowx/add-availability-attribute
Add Availability attribute & Availability Test
- Loading branch information
Showing
3 changed files
with
201 additions
and
3 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
src/main/java/staffconnect/model/availability/Availability.java
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,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(); | ||
} | ||
|
||
} |
111 changes: 111 additions & 0 deletions
111
src/test/java/staffconnect/model/availability/AvailabilityTest.java
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,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"))); | ||
} | ||
|
||
} |
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