forked from nus-cs2103-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 4
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 #30 from jskimdev/branch-Model
Add sex field
- Loading branch information
Showing
3 changed files
with
128 additions
and
0 deletions.
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,73 @@ | ||
package seedu.address.model.person; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.util.AppUtil.checkArgument; | ||
|
||
/** | ||
* Represents a Person's sex in the patient list. | ||
* Guarantees: immutable; is valid as declared in {@link #isValidSex(String)} | ||
*/ | ||
public class Sex { | ||
public static final String MESSAGE_CONSTRAINTS = "Sex should only be Male or Female"; | ||
|
||
public final SexOption sex; | ||
|
||
/** | ||
* Constructus a {@code Sex}. | ||
* | ||
* @param sex A valid sex. | ||
*/ | ||
public Sex(String sex) { | ||
requireNonNull(sex); | ||
checkArgument(isValidSex(sex), MESSAGE_CONSTRAINTS); | ||
this.sex = assignSex(sex); | ||
} | ||
|
||
/** | ||
* Returns true if a given string is a valid sex. | ||
*/ | ||
public static boolean isValidSex(String test) { | ||
return test.equals("Male") || test.equals("Female") ? true : false; | ||
} | ||
|
||
/** | ||
* Assign one of SexOption, Male or Female, depending on the sex input. | ||
* | ||
* @param sex sex input. | ||
* @return one of SexOption | ||
*/ | ||
public SexOption assignSex(String sex) { | ||
if (sex.equals("Male")) { | ||
return SexOption.MALE; | ||
} else if (sex.equals("Female")) { | ||
return SexOption.FEMALE; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return sex.getLabel(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof Sex)) { | ||
return false; | ||
} | ||
|
||
Sex otherSex = (Sex) other; | ||
return sex.equals(otherSex.sex); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return sex.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,19 @@ | ||
package seedu.address.model.person; | ||
|
||
/** | ||
* Represents one of sex, Male or Female. | ||
*/ | ||
public enum SexOption { | ||
MALE("Male"), | ||
FEMALE("Female"); | ||
|
||
private final String label; | ||
|
||
SexOption(String label) { | ||
this.label = label; | ||
} | ||
|
||
public String getLabel() { | ||
return label; | ||
} | ||
} |
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,36 @@ | ||
package seedu.address.model.person; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static seedu.address.testutil.Assert.assertThrows; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class SexTest { | ||
@Test | ||
public void constructor_null_throwsNullPointerException() { | ||
assertThrows(NullPointerException.class, () -> new Sex(null)); | ||
} | ||
|
||
@Test | ||
public void constructor_invalidSex_throwsIllegalArgumentException() { | ||
String invalidSex = ""; | ||
assertThrows(IllegalArgumentException.class, () -> new Sex(invalidSex)); | ||
} | ||
|
||
@Test | ||
public void isValidSex() { | ||
// null sex | ||
assertThrows(NullPointerException.class, () -> Sex.isValidSex(null)); | ||
|
||
// invalid sex | ||
assertFalse(Sex.isValidSex("")); // empty string | ||
assertFalse(Sex.isValidSex(" ")); // spaces only | ||
assertFalse(Sex.isValidSex("akldjkldf")); // random string | ||
assertFalse(Sex.isValidSex("Malet")); // similar to valid string | ||
|
||
// valid sex | ||
assertTrue(Sex.isValidSex("Male")); | ||
assertTrue(Sex.isValidSex("Female")); | ||
} | ||
} |