Skip to content

Commit

Permalink
Merge pull request AY2324S2-CS2103T-T08-1#45 from laney0808/add-new-p…
Browse files Browse the repository at this point in the history
…arsers

Add optional field setters, key words and parsers
  • Loading branch information
jovantanyk authored Mar 19, 2024
2 parents e186106 + 2bd9a96 commit 983fc0e
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 32 deletions.
52 changes: 44 additions & 8 deletions src/main/java/seedu/address/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DOB;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ALLERGIES;
import static seedu.address.logic.parser.CliSyntax.PREFIX_BLOODTYPE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CONDITION;
import static seedu.address.logic.parser.CliSyntax.PREFIX_COUNTRY;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATEOFADMISSION;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATEOFBIRTH;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DIAGNOSIS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NRIC;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SEX;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STATUS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SYMPTOM;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import java.util.stream.Stream;
Expand All @@ -17,6 +24,7 @@
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Address;
import seedu.address.model.person.DateOfBirth;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Nric;
import seedu.address.model.person.Person;
Expand All @@ -36,24 +44,52 @@ public class AddCommandParser implements Parser<AddCommand> {
*/
public AddCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NRIC, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS,
PREFIX_DOB, PREFIX_SEX, PREFIX_TAG);

if (!arePrefixesPresent(argMultimap, PREFIX_NRIC, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_DOB,
PREFIX_SEX) || !argMultimap.getPreamble().isEmpty()) {
ArgumentTokenizer.tokenize(args, PREFIX_NRIC, PREFIX_NAME, PREFIX_PHONE, PREFIX_ADDRESS,
PREFIX_DATEOFBIRTH, PREFIX_SEX, PREFIX_STATUS, PREFIX_TAG, PREFIX_EMAIL, PREFIX_COUNTRY,
PREFIX_DATEOFADMISSION, PREFIX_ALLERGIES, PREFIX_BLOODTYPE, PREFIX_CONDITION, PREFIX_SYMPTOM,
PREFIX_DIAGNOSIS);
if (!arePrefixesPresent(argMultimap, PREFIX_NRIC, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_DATEOFBIRTH,
PREFIX_SEX, PREFIX_STATUS) || !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS);
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NRIC, PREFIX_NAME, PREFIX_PHONE, PREFIX_ADDRESS,
PREFIX_DATEOFBIRTH, PREFIX_SEX);
Nric nric = ParserUtil.parseNric(argMultimap.getValue(PREFIX_NRIC).get());
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
DateOfBirth dob = ParserUtil.parseDateOfBirth(argMultimap.getValue(PREFIX_DOB).get());
DateOfBirth dob = ParserUtil.parseDateOfBirth(argMultimap.getValue(PREFIX_DATEOFBIRTH).get());
Sex sex = ParserUtil.parseSex(argMultimap.getValue(PREFIX_SEX).get());
Status status = ParserUtil.parseStatus(argMultimap.getValue(PREFIX_STATUS).get());
//TODO: add optional fields
Person person = new Person(nric, name, phone, address, dob, sex, status);
if (argMultimap.getValue(PREFIX_EMAIL).isPresent()) {
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
person.setEmail(email);
}
if (argMultimap.getValue(PREFIX_COUNTRY).isPresent()) {
person.setCountry(ParserUtil.parseCountry(argMultimap.getValue(PREFIX_COUNTRY).get()));
}
if (argMultimap.getValue(PREFIX_DATEOFADMISSION).isPresent()) {
person.setDateOfAdmission(ParserUtil.parseDateOfAdmission(argMultimap
.getValue(PREFIX_DATEOFADMISSION).get()));
}
if (argMultimap.getValue(PREFIX_ALLERGIES).isPresent()) {
person.setAllergies(ParserUtil.parseAllergies(argMultimap.getValue(PREFIX_ALLERGIES).get()));
}
if (argMultimap.getValue(PREFIX_BLOODTYPE).isPresent()) {
person.setBloodType(ParserUtil.parseBloodType(argMultimap.getValue(PREFIX_BLOODTYPE).get()));
}
if (argMultimap.getValue(PREFIX_CONDITION).isPresent()) {
person.setCondition(ParserUtil.parseCondition(argMultimap.getValue(PREFIX_CONDITION).get()));
}
if (argMultimap.getValue(PREFIX_SYMPTOM).isPresent()) {
person.setSymptom(ParserUtil.parseSymptom(argMultimap.getValue(PREFIX_SYMPTOM).get()));
}
if (argMultimap.getValue(PREFIX_DIAGNOSIS).isPresent()) {
person.setDiagnosis(ParserUtil.parseDiagnosis(argMultimap.getValue(PREFIX_DIAGNOSIS).get()));
}

return new AddCommand(person);
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ public class CliSyntax {
/* Prefix definitions */
public static final Prefix PREFIX_NAME = new Prefix("n/");
public static final Prefix PREFIX_NRIC = new Prefix("ic/");
public static final Prefix PREFIX_DOB = new Prefix("dob/");
public static final Prefix PREFIX_DATEOFBIRTH = new Prefix("dob/");
public static final Prefix PREFIX_SEX = new Prefix("s/");
public static final Prefix PREFIX_PHONE = new Prefix("hp/");
public static final Prefix PREFIX_ADDRESS = new Prefix("a/");
public static final Prefix PREFIX_EMAIL = new Prefix("e/");
public static final Prefix PREFIX_COUNTRY = new Prefix("c/");
public static final Prefix PREFIX_DOA = new Prefix("doa/");
public static final Prefix PREFIX_DATEOFADMISSION = new Prefix("doa/");
public static final Prefix PREFIX_BLOODTYPE = new Prefix("bt/");
public static final Prefix PREFIX_ALLERGIES = new Prefix("al/");
public static final Prefix PREFIX_CONDITION = new Prefix("con/");
public static final Prefix PREFIX_SYMPTOM = new Prefix("sym/");
public static final Prefix PREFIX_DIAGNOSIS = new Prefix("d/");
public static final Prefix PREFIX_TAG = new Prefix("t/");
public static final Prefix PREFIX_STATUS = new Prefix("st/");

Expand Down
91 changes: 88 additions & 3 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@
import seedu.address.commons.util.StringUtil;
import seedu.address.logic.parser.exceptions.ParseException;
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.Phone;
import seedu.address.model.person.Sex;
import seedu.address.model.person.Status;
import seedu.address.model.person.Symptom;
import seedu.address.model.tag.Tag;

/**
* Contains utility methods used for parsing strings in the various *Parser classes.
*/
public class ParserUtil {
//TODO: add parser for new fields

public static final String MESSAGE_INVALID_INDEX = "Index is not a non-zero unsigned integer.";

Expand Down Expand Up @@ -145,18 +152,96 @@ public static Status parseStatus(String status) throws ParseException {
}
return new Status(status);
}
/**
* Parses a {@code String country} into an {@code Country country}.
* Leading and trailing whitespaces will be trimmed.
* @throws ParseException if the given {@code country} is invalid.
*/
public static Country parseCountry(String country) throws ParseException {
requireNonNull(country);
//TODO: Implement country validation & standardisation
String trimmedCountry = country.trim();
return new Country(trimmedCountry);
}
/**
* Parses a {@code String dateOfAdmission} into an {@code DateOfAdmission dateOfAdmission}.
* Leading and trailing whitespaces will be trimmed.
* @throws ParseException if the given {@code dateOfAdmission} is invalid.
*/
public static DateOfAdmission parseDateOfAdmission(String dateOfAdmission) throws ParseException {
requireNonNull(dateOfAdmission);
String trimmedDateOfAdmission = dateOfAdmission.trim();
if (!DateOfAdmission.isValidDateOfAdmission(trimmedDateOfAdmission)) {
throw new ParseException(DateOfBirth.MESSAGE_CONSTRAINTS);
}
return new DateOfAdmission(trimmedDateOfAdmission);
}

/**
* Parses a {@code String status} into an {@code Status status}.
* Leading and trailing whitespaces will be trimmed.
* TODO // Implement parseBloodType
*
* @throws ParseException if the given {@code sex} is invalid.
*/
public static BloodType parseBloodType(String bloodType) throws ParseException {
requireNonNull(bloodType);
String formattedStatus = bloodType.trim();
return new BloodType("A", "POSITIVE");
String trimmedBloodType = bloodType.trim();
String type = trimmedBloodType.substring(0, 1);
String rh = trimmedBloodType.substring(1);
if (!BloodType.isValidBloodType(type, rh)) {
throw new ParseException(BloodType.MESSAGE_CONSTRAINTS);
}
rh = rh.equals("+") ? "POSITIVE" : "NEGATIVE";
return new BloodType(type, rh);
}
/**
* Parses a {@code String allergies} into an {@code Set<Allergies>}.
* Leading and trailing whitespaces will be trimmed.
* @throws ParseException if the given {@code allergies} is invalid.
*/
public static Allergies parseAllergies(String allergies) throws ParseException {
requireNonNull(allergies);
String trimmedAllergies = allergies.trim();
if (!Address.isValidAddress(trimmedAllergies)) {
throw new ParseException(Address.MESSAGE_CONSTRAINTS);
}
return new Allergies(trimmedAllergies);
}
/**
* Parses a {@code String condition} into an {@code Condition}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code condition} is invalid.
*/
public static Condition parseCondition(String condition) throws ParseException {
requireNonNull(condition);
String trimmedCondition = condition.trim();
//TODO: Implement condition validation & standardisation
return new Condition(trimmedCondition);
}
/**
* Parses a {@code String symptom} into an {@code Symptom}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code symptom} is invalid.
*/
public static Symptom parseSymptom(String symptom) throws ParseException {
requireNonNull(symptom);
String trimmedSymptom = symptom.trim();
return new Symptom(trimmedSymptom);
}
/**
* Parses a {@code String diagnosis} into an {@code Diagnosis}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code diagnosis} is invalid.
*/
public static Diagnosis parseDiagnosis(String diagnosis) throws ParseException {
requireNonNull(diagnosis);
String trimmedDiagnosis = diagnosis.trim();
return new Diagnosis(trimmedDiagnosis);
}


/**
* Parses a {@code String email} into an {@code Email}.
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/seedu/address/model/person/BloodType.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
public class BloodType {
public static final String MESSAGE_CONSTRAINTS =
"BloodType should be A, B, AB, O. Rh should be either POSITIVE or NEGATIVE";
"BloodType should be A, B, AB, O. Rh should be either + or -";
private enum Type { A, B, AB, O };
private enum Rh { POSITIVE, NEGATIVE };
private final Type type;
Expand All @@ -28,7 +28,7 @@ public BloodType(String type, String rh) {
requireNonNull(type, rh);
checkArgument(isValidBloodType(type, rh), MESSAGE_CONSTRAINTS);
this.type = Type.valueOf(type);
this.rh = Rh.valueOf(rh);
this.rh = rh == "+" ? Rh.POSITIVE : Rh.NEGATIVE;
}

/**
Expand All @@ -40,8 +40,10 @@ public BloodType(String type, String rh) {
public static boolean isValidBloodType(String testType, String testRh) {
try {
Type type = Type.valueOf(testType);
Rh rh = Rh.valueOf(testRh);
return true;
if (testRh == "+" || testRh == "-") {
return true;
}
return false;
} catch (IllegalArgumentException e) {
return false;
}
Expand Down
Loading

0 comments on commit 983fc0e

Please sign in to comment.