Skip to content

Commit

Permalink
Merge pull request #30 from jskimdev/branch-Model
Browse files Browse the repository at this point in the history
Add sex field
  • Loading branch information
Vision-2000 authored Mar 18, 2024
2 parents 692bf3d + d2f117a commit fe2183d
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/main/java/seedu/address/model/person/Sex.java
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();
}
}
19 changes: 19 additions & 0 deletions src/main/java/seedu/address/model/person/SexOption.java
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;
}
}
36 changes: 36 additions & 0 deletions src/test/java/seedu/address/model/person/SexTest.java
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"));
}
}

0 comments on commit fe2183d

Please sign in to comment.