Skip to content

Commit

Permalink
Merge pull request #31 from jskimdev/branch-AddPatient
Browse files Browse the repository at this point in the history
Add sex field in add command
  • Loading branch information
jskimdev authored Mar 19, 2024
2 parents 24a40a0 + e7cbb8a commit d55f447
Show file tree
Hide file tree
Showing 28 changed files with 372 additions and 148 deletions.
7 changes: 5 additions & 2 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SEX;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
Expand All @@ -26,13 +27,15 @@ public class AddCommand extends Command {
+ PREFIX_PHONE + "PHONE "
+ PREFIX_EMAIL + "EMAIL "
+ PREFIX_ADDRESS + "ADDRESS "
+ PREFIX_DATEOFBIRTH + "DATE OF BIRTH \n"
+ PREFIX_DATEOFBIRTH + "DATE OF BIRTH "
+ PREFIX_SEX + "Sex \n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NAME + "John Doe "
+ PREFIX_PHONE + "98765432 "
+ PREFIX_EMAIL + "[email protected] "
+ PREFIX_ADDRESS + "311, Clementi Ave 2, #02-25 "
+ PREFIX_DATEOFBIRTH + "25/2/2024";
+ PREFIX_DATEOFBIRTH + "25/2/2024 "
+ PREFIX_SEX + "Male";

public static final String MESSAGE_SUCCESS = "New patient added: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This patient already exists in the address book";
Expand Down
71 changes: 43 additions & 28 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SEX;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.List;
Expand All @@ -24,6 +25,7 @@
import seedu.address.model.patient.Name;
import seedu.address.model.patient.Patient;
import seedu.address.model.patient.Phone;
import seedu.address.model.patient.Sex;

/**
* Edits the details of an existing patient in the address book.
Expand All @@ -40,7 +42,8 @@ public class EditCommand extends Command {
+ "[" + PREFIX_PHONE + "PHONE] "
+ "[" + PREFIX_EMAIL + "EMAIL] "
+ "[" + PREFIX_ADDRESS + "ADDRESS] "
+ "[" + PREFIX_DATEOFBIRTH + "DATEOFBIRTH] \n"
+ "[" + PREFIX_DATEOFBIRTH + "DATEOFBIRTH] "
+ "[" + PREFIX_SEX + "SEX] \n"
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_PHONE + "91234567 "
+ PREFIX_EMAIL + "[email protected]";
Expand All @@ -50,18 +53,18 @@ public class EditCommand extends Command {
public static final String MESSAGE_DUPLICATE_PERSON = "This patient already exists in the address book.";

private final Index index;
private final EditPersonDescriptor editPersonDescriptor;
private final EditPatientDescriptor editPatientDescriptor;

/**
* @param index of the patient in the filtered patient list to edit
* @param editPersonDescriptor details to edit the patient with
* @param editPatientDescriptor details to edit the patient with
*/
public EditCommand(Index index, EditPersonDescriptor editPersonDescriptor) {
public EditCommand(Index index, EditPatientDescriptor editPatientDescriptor) {
requireNonNull(index);
requireNonNull(editPersonDescriptor);
requireNonNull(editPatientDescriptor);

this.index = index;
this.editPersonDescriptor = new EditPersonDescriptor(editPersonDescriptor);
this.editPatientDescriptor = new EditPatientDescriptor(editPatientDescriptor);
}

@Override
Expand All @@ -74,9 +77,9 @@ public CommandResult execute(Model model) throws CommandException {
}

Patient patientToEdit = lastShownList.get(index.getZeroBased());
Patient editedPatient = createEditedPerson(patientToEdit, editPersonDescriptor);
Patient editedPatient = createEditedPerson(patientToEdit, editPatientDescriptor);

if (!patientToEdit.isSamePerson(editedPatient) && model.hasPerson(editedPatient)) {
if (!patientToEdit.isSamePatient(editedPatient) && model.hasPerson(editedPatient)) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

Expand All @@ -89,16 +92,17 @@ public CommandResult execute(Model model) throws CommandException {
* Creates and returns a {@code Patient} with the details of {@code patientToEdit}
* edited with {@code editPersonDescriptor}.
*/
private static Patient createEditedPerson(Patient patientToEdit, EditPersonDescriptor editPersonDescriptor) {
private static Patient createEditedPerson(Patient patientToEdit, EditPatientDescriptor editPatientDescriptor) {
assert patientToEdit != null;

Name updatedName = editPersonDescriptor.getName().orElse(patientToEdit.getName());
Phone updatedPhone = editPersonDescriptor.getPhone().orElse(patientToEdit.getPhone());
Email updatedEmail = editPersonDescriptor.getEmail().orElse(patientToEdit.getEmail());
Address updatedAddress = editPersonDescriptor.getAddress().orElse(patientToEdit.getAddress());
DateOfBirth updatedDateOfBirth = editPersonDescriptor.getDateOfBirth().orElse(patientToEdit.getDateOfBirth());
Name updatedName = editPatientDescriptor.getName().orElse(patientToEdit.getName());
Phone updatedPhone = editPatientDescriptor.getPhone().orElse(patientToEdit.getPhone());
Email updatedEmail = editPatientDescriptor.getEmail().orElse(patientToEdit.getEmail());
Address updatedAddress = editPatientDescriptor.getAddress().orElse(patientToEdit.getAddress());
DateOfBirth updatedDateOfBirth = editPatientDescriptor.getDateOfBirth().orElse(patientToEdit.getDateOfBirth());
Sex updatedSex = editPatientDescriptor.getSex().orElse(patientToEdit.getSex());

return new Patient(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedDateOfBirth);
return new Patient(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedDateOfBirth, updatedSex);
}

@Override
Expand All @@ -114,41 +118,42 @@ public boolean equals(Object other) {

EditCommand otherEditCommand = (EditCommand) other;
return index.equals(otherEditCommand.index)
&& editPersonDescriptor.equals(otherEditCommand.editPersonDescriptor);
&& editPatientDescriptor.equals(otherEditCommand.editPatientDescriptor);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("index", index)
.add("editPersonDescriptor", editPersonDescriptor)
.add("editPersonDescriptor", editPatientDescriptor)
.toString();
}

/**
* Stores the details to edit the patient with. Each non-empty field value will replace the
* corresponding field value of the patient.
*/
public static class EditPersonDescriptor {
public static class EditPatientDescriptor {
private Name name;
private Phone phone;
private Email email;
private Address address;

private DateOfBirth dateOfBirth;
private Sex sex;

public EditPersonDescriptor() {}
public EditPatientDescriptor() {}

/**
* Copy constructor.
* A defensive copy of {@code tags} is used internally.
*/
public EditPersonDescriptor(EditPersonDescriptor toCopy) {
public EditPatientDescriptor(EditPatientDescriptor toCopy) {
setName(toCopy.name);
setPhone(toCopy.phone);
setEmail(toCopy.email);
setAddress(toCopy.address);
setDateOfBirth(toCopy.dateOfBirth);
setSex(toCopy.sex);
}

/**
Expand Down Expand Up @@ -198,23 +203,32 @@ public Optional<DateOfBirth> getDateOfBirth() {
return Optional.ofNullable(dateOfBirth);
}

public void setSex(Sex sex) {
this.sex = sex;
}

public Optional<Sex> getSex() {
return Optional.ofNullable(sex);
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof EditPersonDescriptor)) {
if (!(other instanceof EditPatientDescriptor)) {
return false;
}

EditPersonDescriptor otherEditPersonDescriptor = (EditPersonDescriptor) other;
return Objects.equals(name, otherEditPersonDescriptor.name)
&& Objects.equals(phone, otherEditPersonDescriptor.phone)
&& Objects.equals(email, otherEditPersonDescriptor.email)
&& Objects.equals(address, otherEditPersonDescriptor.address)
&& Objects.equals(dateOfBirth, otherEditPersonDescriptor.dateOfBirth);
EditPatientDescriptor otherEditPatientDescriptor = (EditPatientDescriptor) other;
return Objects.equals(name, otherEditPatientDescriptor.name)
&& Objects.equals(phone, otherEditPatientDescriptor.phone)
&& Objects.equals(email, otherEditPatientDescriptor.email)
&& Objects.equals(address, otherEditPatientDescriptor.address)
&& Objects.equals(dateOfBirth, otherEditPatientDescriptor.dateOfBirth)
&& Objects.equals(sex, otherEditPatientDescriptor.sex);
}

@Override
Expand All @@ -225,6 +239,7 @@ public String toString() {
.add("email", email)
.add("address", address)
.add("date of birth", dateOfBirth)
.add("sex", sex)
.toString();
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/seedu/address/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SEX;

import java.util.stream.Stream;

Expand All @@ -17,6 +18,7 @@
import seedu.address.model.patient.Name;
import seedu.address.model.patient.Patient;
import seedu.address.model.patient.Phone;
import seedu.address.model.patient.Sex;

/**
* Parses input arguments and creates a new AddCommand object
Expand All @@ -31,23 +33,24 @@ public class AddCommandParser implements Parser<AddCommand> {
public AddCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS,
PREFIX_DATEOFBIRTH);
PREFIX_DATEOFBIRTH, PREFIX_SEX);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_DATEOFBIRTH)
PREFIX_DATEOFBIRTH, PREFIX_SEX)
|| !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,
PREFIX_DATEOFBIRTH);
PREFIX_DATEOFBIRTH, PREFIX_SEX);
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
DateOfBirth dateOfBirth = ParserUtil.parseDateOfBirth(argMultimap.getValue(PREFIX_DATEOFBIRTH).get());
Sex sex = ParserUtil.parseSex(argMultimap.getValue(PREFIX_SEX).get());

Patient patient = new Patient(name, phone, email, address, dateOfBirth);
Patient patient = new Patient(name, phone, email, address, dateOfBirth, sex);

return new AddCommand(patient);
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class CliSyntax {
public static final Prefix PREFIX_PHONE = new Prefix("p/");
public static final Prefix PREFIX_EMAIL = new Prefix("e/");
public static final Prefix PREFIX_ADDRESS = new Prefix("a/");

public static final Prefix PREFIX_DATEOFBIRTH = new Prefix("b/");

public static final Prefix PREFIX_SEX = new Prefix("s/");
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditCommand.EditPersonDescriptor;
import seedu.address.logic.commands.EditCommand.EditPatientDescriptor;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -39,7 +39,7 @@ public EditCommand parse(String args) throws ParseException {

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS);

EditPersonDescriptor editPersonDescriptor = new EditPersonDescriptor();
EditPatientDescriptor editPersonDescriptor = new EditPatientDescriptor();

if (argMultimap.getValue(PREFIX_NAME).isPresent()) {
editPersonDescriptor.setName(ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get()));
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import seedu.address.model.patient.Email;
import seedu.address.model.patient.Name;
import seedu.address.model.patient.Phone;
import seedu.address.model.patient.Sex;

/**
* Contains utility methods used for parsing strings in the various *Parser classes.
Expand Down Expand Up @@ -103,6 +104,21 @@ public static DateOfBirth parseDateOfBirth(String dateOfBirth) throws ParseExcep
if (!DateOfBirth.isValidDateOfBirth(trimmedDateOfBirth)) {
throw new ParseException(DateOfBirth.MESSAGE_CONSTRAINTS);
}
return new DateOfBirth(dateOfBirth);
return new DateOfBirth(trimmedDateOfBirth);
}

/**
* Parses a {@code String sex} into an {@code Sex}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code Sex} is invalid.
*/
public static Sex parseSex(String sex) throws ParseException {
requireNonNull(sex);
String trimmedSex = sex.trim();
if (!Sex.isValidSex(trimmedSex)) {
throw new ParseException(Sex.MESSAGE_CONSTRAINTS);
}
return new Sex(trimmedSex);
}
}
19 changes: 14 additions & 5 deletions src/main/java/seedu/address/model/patient/Patient.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@ public class Patient {
private final Phone phone;
private final Email email;
private final DateOfBirth dateOfBirth;
private final Sex sex;

// Data fields
private final Address address;

/**
* Every field must be present and not null.
*/
public Patient(Name name, Phone phone, Email email, Address address, DateOfBirth dateOfBirth) {
requireAllNonNull(name, phone, email, address);
public Patient(Name name, Phone phone, Email email, Address address, DateOfBirth dateOfBirth, Sex sex) {
requireAllNonNull(name, phone, email, address, dateOfBirth, sex);
this.name = name;
this.phone = phone;
this.email = email;
this.address = address;
this.dateOfBirth = dateOfBirth;
this.sex = sex;
}

public Name getName() {
Expand All @@ -53,11 +55,15 @@ public DateOfBirth getDateOfBirth() {
return dateOfBirth;
}

public Sex getSex() {
return sex;
}

/**
* Returns true if both persons have the same name.
* This defines a weaker notion of equality between two persons.
*/
public boolean isSamePerson(Patient otherPatient) {
public boolean isSamePatient(Patient otherPatient) {
if (otherPatient == this) {
return true;
}
Expand Down Expand Up @@ -85,13 +91,15 @@ public boolean equals(Object other) {
return name.equals(otherPatient.name)
&& phone.equals(otherPatient.phone)
&& email.equals(otherPatient.email)
&& address.equals(otherPatient.address);
&& address.equals(otherPatient.address)
&& dateOfBirth.equals(otherPatient.dateOfBirth)
&& sex.equals(otherPatient.sex);
}

@Override
public int hashCode() {
// use this method for custom fields hashing instead of implementing your own
return Objects.hash(name, phone, email, address, dateOfBirth);
return Objects.hash(name, phone, email, address, dateOfBirth, sex);
}

@Override
Expand All @@ -102,6 +110,7 @@ public String toString() {
.add("email", email)
.add("address", address)
.add("date of birth", dateOfBirth)
.add("sex", sex)
.toString();
}

Expand Down
Loading

0 comments on commit d55f447

Please sign in to comment.