Skip to content

Commit

Permalink
Merge pull request AY2324S2-CS2103T-T08-1#52 from NatLeong/update-jso…
Browse files Browse the repository at this point in the history
…nadaptedperson-optional

Update JsonAdaptedPerson for optional fields
  • Loading branch information
jovantanyk authored Mar 21, 2024
2 parents 03498c4 + e743c02 commit b274fc5
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 52 deletions.
27 changes: 14 additions & 13 deletions src/main/java/seedu/address/model/person/BloodType.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@
* Guarantees: immutable;
*/
public class BloodType {
public static final String MESSAGE_CONSTRAINTS =
"BloodType should be A, B, AB, O. Rh should be either + or -";
private enum Type { A, B, AB, O };
private enum Rh { POSITIVE, NEGATIVE };
public static final String MESSAGE_CONSTRAINTS = "BloodType should be A, B, AB, O. Rh should be either + or -";
private final Type type;

private final Rh rh;

/**
* Constructs a {@code BloodType}.
*
* @param type A valid blood type.
* @param rh A valid Rh factor.
* @param rh A valid Rh factor.
*/
public BloodType(String type, String rh) {
requireNonNull(type, rh);
Expand All @@ -33,32 +31,31 @@ public BloodType(String type, String rh) {

/**
* A method for testing if two strings form a valid BloodType
*
* @param testType String for storing testing type
* @param testRh String for storing testing rh
* @param testRh String for storing testing rh
* @return Boolean whether test passes or fails
*/
public static boolean isValidBloodType(String testType, String testRh) {
try {
Type type = Type.valueOf(testType);
if (testRh == "+" || testRh == "-") {
return true;
}
return false;
return testRh == "+" || testRh == "-";
} catch (IllegalArgumentException e) {
return false;
}
}

public String getType() {
return type.toString();
return this.type.toString();
}

public String getRh() {
return rh.toString();
return this.rh == Rh.POSITIVE ? "+" : "-";
}

@Override
public String toString() {
return this.type.toString() + this.rh.toString();
return this.type.toString() + this.getRh();
}

@Override
Expand All @@ -77,4 +74,8 @@ public boolean equals(Object other) {
BloodType otherBloodType = (BloodType) other;
return type.equals(otherBloodType.type) && rh.equals(otherBloodType.rh);
}

private enum Type { A, B, AB, O }

private enum Rh { POSITIVE, NEGATIVE }
}
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/person/DateOfBirth.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class DateOfBirth {
public static final String MESSAGE_CONSTRAINTS =
"Date of birth should be in the format of YYYY-MM-DD, and it should not be blank.";

public static final String VALIDATION_REGEX = "^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$";
public static final String VALIDATION_REGEX = "^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])";

private final LocalDate dateOfBirth;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/person/Nric.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Nric {
* The first character of the address must not be a whitespace,
* otherwise " " (a blank string) becomes a valid input.
*/
public static final String VALIDATION_REGEX = "^[ST]\\\\d{7}[A-Z]$";
public static final String VALIDATION_REGEX = "^[ST]\\d{7}[A-Z]$";

private final String nric;

Expand Down
1 change: 0 additions & 1 deletion src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ public boolean equals(Object other) {
&& phone.equals(otherPerson.phone)
&& address.equals(otherPerson.address)
&& dateOfBirth.equals(otherPerson.dateOfBirth)
&& dateOfAdmission.equals(otherPerson.dateOfAdmission)
&& sex.equals(otherPerson.sex);
}

Expand Down
104 changes: 94 additions & 10 deletions src/main/java/seedu/address/storage/JsonAdaptedPerson.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package seedu.address.storage;

import java.util.Optional;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.person.Address;
import seedu.address.model.person.Allergies;
import seedu.address.model.person.BloodType;
import seedu.address.model.person.Condition;
import seedu.address.model.person.Country;
import seedu.address.model.person.DateOfAdmission;
import seedu.address.model.person.DateOfBirth;
import seedu.address.model.person.Diagnosis;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Nric;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.person.Sex;
import seedu.address.model.person.Status;
import seedu.address.model.person.Symptom;

/**
* Jackson-friendly version of {@link Person}.
Expand All @@ -25,22 +35,42 @@ class JsonAdaptedPerson {
private final String status;
private final String address;
private final String dateOfBirth;
private final Optional<String> email;
private final Optional<String> country;
private final Optional<String> allergies;
private final Optional<String> bloodType;
private final Optional<String> condition;
private final Optional<String> dateOfAdmission;
private final Optional<String> diagnosis;
private final Optional<String> symptom;

/**
* Constructs a {@code JsonAdaptedPerson} with the given person details.
*/
@JsonCreator
public JsonAdaptedPerson(@JsonProperty("nric") String nric, @JsonProperty("name") String name,
@JsonProperty("phone") String phone, @JsonProperty("dateOfBirth") String dob,
@JsonProperty("sex") String sex, @JsonProperty("address") String address,
@JsonProperty("status") String status) {
@JsonProperty("phone") String phone, @JsonProperty("address") String address,
@JsonProperty("dateOfBirth") String dob, @JsonProperty("sex") String sex,
@JsonProperty("status") String status, @JsonProperty("email") String email,
@JsonProperty("country") String country, @JsonProperty("allergies") String allergies,
@JsonProperty("bloodType") String bloodType, @JsonProperty("condition") String condition,
@JsonProperty("dateOfAdmission") String doa, @JsonProperty("diagnosis") String diagnosis,
@JsonProperty("symptom") String symptom) {
this.nric = nric;
this.name = name;
this.phone = phone;
this.address = address;
this.dateOfBirth = dob;
this.sex = sex;
this.status = status;
this.email = Optional.ofNullable(email);
this.country = Optional.ofNullable(country);
this.allergies = Optional.ofNullable(allergies);
this.bloodType = Optional.ofNullable(bloodType);
this.condition = Optional.ofNullable(condition);
this.dateOfAdmission = Optional.ofNullable(doa);
this.diagnosis = Optional.ofNullable(diagnosis);
this.symptom = Optional.ofNullable(symptom);
}

/**
Expand All @@ -52,9 +82,16 @@ public JsonAdaptedPerson(Person source) {
this.phone = source.getPhone().toString();
this.address = source.getAddress().toString();
this.dateOfBirth = source.getDateOfBirth().toString();
this.sex = source.getEmail().toString();
this.sex = source.getSex().toString();
this.status = source.getStatus().toString();

this.email = Optional.ofNullable(source.getEmail()).map(Email::toString);
this.country = Optional.ofNullable(source.getCountry()).map(Country::toString);
this.allergies = Optional.ofNullable(source.getAllergies()).map(Allergies::toString);
this.bloodType = Optional.ofNullable(source.getBloodType()).map(BloodType::toString);
this.condition = Optional.ofNullable(source.getCondition()).map(Condition::toString);
this.dateOfAdmission = Optional.ofNullable(source.getDateOfAdmission()).map(DateOfAdmission::toString);
this.diagnosis = Optional.ofNullable(source.getDiagnosis()).map(Diagnosis::toString);
this.symptom = Optional.ofNullable(source.getSymptom()).map(Symptom::toString);
}

/**
Expand All @@ -63,6 +100,9 @@ public JsonAdaptedPerson(Person source) {
* @throws IllegalValueException if there were any data constraints violated in the adapted person.
*/
public Person toModelType() throws IllegalValueException {

Person person;

// NRIC Check
if (nric == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Nric.class.getSimpleName()));
Expand Down Expand Up @@ -97,11 +137,11 @@ public Person toModelType() throws IllegalValueException {
final Address modelAddress = new Address(address);
// Date of Birth Check
if (dateOfBirth == null) {
throw new IllegalValueException(
String.format(MISSING_FIELD_MESSAGE_FORMAT, DateOfBirth.class.getSimpleName()));
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT,
DateOfBirth.class.getSimpleName()));
}
if (!DateOfBirth.isValidDateOfBirth(dateOfBirth)) {
throw new IllegalValueException(Phone.MESSAGE_CONSTRAINTS);
throw new IllegalValueException(DateOfBirth.MESSAGE_CONSTRAINTS);
}
final DateOfBirth modelDateOfBirth = new DateOfBirth(dateOfBirth);
// Sex Check
Expand All @@ -117,10 +157,54 @@ public Person toModelType() throws IllegalValueException {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Status.class.getSimpleName()));
}
if (!Status.isValidStatus(status)) {
throw new IllegalValueException(Sex.MESSAGE_CONSTRAINTS);
throw new IllegalValueException(Status.MESSAGE_CONSTRAINTS);
}
final Status modelStatus = new Status(status);
return new Person(modelNric, modelName, modelPhone, modelAddress, modelDateOfBirth, modelSex, modelStatus);

person = new Person(modelNric, modelName, modelPhone, modelAddress, modelDateOfBirth, modelSex, modelStatus);

// Email check
if (email.isPresent()) {
final Email modelEmail = new Email(email.get());
person.setEmail(modelEmail);
}
// Country check
if (country.isPresent()) {
final Country modelCountry = new Country(country.get());
person.setCountry(modelCountry);
}
// Allergies check
if (allergies.isPresent()) {
final Allergies modelAllergies = new Allergies(allergies.get());
person.setAllergies(modelAllergies);
}
// BloodType check
if (bloodType.isPresent()) {
String[] parts = bloodType.get().split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
final BloodType modelBloodType = new BloodType(parts[0], parts[1]);
person.setBloodType(modelBloodType);
}
//Condition check
if (condition.isPresent()) {
final Condition modelCondition = new Condition(condition.get());
person.setCondition(modelCondition);
}
//Date of Admission check
if (dateOfAdmission.isPresent()) {
final DateOfAdmission modelDoa = new DateOfAdmission(dateOfAdmission.get());
person.setDateOfAdmission(modelDoa);
}
//Diagnosis check
if (diagnosis.isPresent()) {
final Diagnosis modelDiagnosis = new Diagnosis(diagnosis.get());
person.setDiagnosis(modelDiagnosis);
}
//Symptom check
if (symptom.isPresent()) {
final Symptom modelSymptom = new Symptom(symptom.get());
person.setSymptom(modelSymptom);
}
return person;
}

}
Loading

0 comments on commit b274fc5

Please sign in to comment.