Skip to content

Commit

Permalink
Merge pull request #67 from iamtr/branch-Dob-earlier-than-AdmissionDate
Browse files Browse the repository at this point in the history
Fix bug where DOB can be later than admission date
  • Loading branch information
iamtr authored Apr 1, 2024
2 parents db3ffb7 + 6862100 commit 2774556
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class Messages {
public static final String MESSAGE_DUPLICATE_FIELDS =
"Multiple values specified for the following single-valued field(s): ";

public static final String MESSAGE_DOB_LATER_THAN_ADMISSION =
"Date of birth should not be later than date of admission";
/**
* Returns an error message indicating the duplicate prefixes.
*/
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_WARD;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -48,7 +50,7 @@ public class EditCommand extends Command {
+ "[" + PREFIX_ADMISSION_DATE + "ADMISSION DATE] "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_IC + "T123456Q "
+ PREFIX_IC + "T1234567Q "
+ PREFIX_DOB + "12/08/1999";

public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Edited Person: %1$s";
Expand Down Expand Up @@ -86,6 +88,14 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");

LocalDate dobToCheck = LocalDate.parse(editedPerson.getDob().value, formatter);
LocalDate adToCheck = LocalDate.parse(editedPerson.getAdmissionDate().value, formatter);
if (dobToCheck.isAfter(adToCheck)) {
throw new CommandException(Messages.MESSAGE_DOB_LATER_THAN_ADMISSION);
}

model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_DOB_LATER_THAN_ADMISSION;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADMISSION_DATE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DOB;
Expand All @@ -8,6 +9,8 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.CliSyntax.PREFIX_WARD;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Set;
import java.util.stream.Stream;

Expand Down Expand Up @@ -51,6 +54,12 @@ public AddCommand parse(String args) throws ParseException {
ParserUtil.parseAdmissionDate(argMultimap.getValue(PREFIX_ADMISSION_DATE).orElse(null));
Ward ward = ParserUtil.parseWard(argMultimap.getValue(PREFIX_WARD).orElse(null));

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate dobDate = LocalDate.parse(dob.value, formatter);
LocalDate admissionDateDate = LocalDate.parse(admissionDate.value, formatter);
if (dobDate.isAfter(admissionDateDate)) {
throw new ParseException(MESSAGE_DOB_LATER_THAN_ADMISSION);
}

Person person = new Person(name, tagList, dob, ic, admissionDate, ward);

Expand Down
5 changes: 1 addition & 4 deletions src/main/java/seedu/address/model/person/Dob.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
public class Dob {
public static final String MESSAGE_CONSTRAINTS = "Dates of birth takes in a date. "
+ "Date of birth should not be later than date of recording";
+ "Date of birth should not be later than date of admission";
public static final String VALIDATION_REGEX = "^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/[0-9]{4}$";

public final String value;
Expand Down Expand Up @@ -65,7 +65,4 @@ public boolean equals(Object other) {
public int hashCode() {
return value.hashCode();
}

// Todo: isValidDob

}
2 changes: 0 additions & 2 deletions src/main/java/seedu/address/model/person/Ic.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,4 @@ public boolean equals(Object other) {
public int hashCode() {
return value.hashCode();
}

// Todo: isValidIc
}
7 changes: 7 additions & 0 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
Expand Down Expand Up @@ -33,6 +35,11 @@ public class Person {
public Person(Name name, Set<Tag> tags,
Dob dob, Ic ic, AdmissionDate admissionDate, Ward ward) {
requireAllNonNull(name, dob, ic, admissionDate, ward);

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate dobToCompare = LocalDate.parse(dob.toString(), formatter);
LocalDate admissionDateToCompare = LocalDate.parse(admissionDate.toString(), formatter);

this.name = name;
this.tags.addAll(tags);
this.dob = dob;
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/seedu/address/logic/commands/EditCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import seedu.address.commons.core.index.Index;
Expand Down Expand Up @@ -143,6 +144,34 @@ public void execute_invalidPersonIndexFilteredList_failure() {

assertCommandFailure(editCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
@Test
@Disabled
public void execute_dobEarlierThanAdmission_success() {
Person person = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(person).withDob("12/08/2002")
.withAdmissionDate("12/08/2019").build();

PersonBuilder personBuilder = new PersonBuilder(person);


EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, descriptor);
String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS,
Messages.format(person));

Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs());

assertCommandSuccess(editCommand, model, expectedMessage, expectedModel);
}

@Test
public void execute_dobLaterThanAdmission_failure() {
Person person = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(person).withDob("12/08/2020")
.withAdmissionDate("12/08/2019").build();
EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, descriptor);

assertCommandFailure(editCommand, model, Messages.MESSAGE_DOB_LATER_THAN_ADMISSION);
}

@Test
public void equals() {
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/seedu/address/logic/parser/AddCommandParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,21 @@ public void parse_invalidValue_failure() {
+ ADMISSION_DATE_DESC_BOB + WARD_DESC_BOB,
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}

@Test
public void parse_invalidDob_failure() {
// invalid dob
assertParseFailure(parser, NAME_DESC_BOB
+ TAG_DESC_FALL_RISK + TAG_DESC_DIABETES + INVALID_DOB_DESC + IC_DESC_BOB
+ ADMISSION_DATE_DESC_BOB + WARD_DESC_BOB, Dob.MESSAGE_CONSTRAINTS);
}

@Test
public void parse_dobLaterThanAdmissionDate_failure() {
// dob later than admission date
String userInput = NAME_DESC_BOB
+ " dob\\02/02/2022 " + IC_DESC_BOB
+ " ad\\02/02/2002 " + WARD_DESC_BOB;
assertParseFailure(parser, userInput, Messages.MESSAGE_DOB_LATER_THAN_ADMISSION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.stream.Stream;

import seedu.address.logic.commands.EditCommand.EditPersonDescriptor;
import seedu.address.model.person.AdmissionDate;
import seedu.address.model.person.Dob;
import seedu.address.model.person.Ic;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
Expand Down Expand Up @@ -72,6 +74,20 @@ public EditPersonDescriptorBuilder withTags(String... tags) {
descriptor.setTags(tagSet);
return this;
}
/**
* Sets the {@code Dob} of the {@code EditPersonDescriptor} that we are building.
*/
public EditPersonDescriptorBuilder withDob(String dob) {
descriptor.setDob(new Dob(dob));
return this;
}
/**
* Sets the {@code AdmissionDate} of the {@code EditPersonDescriptor} that we are building.
*/
public EditPersonDescriptorBuilder withAdmissionDate(String admissionDate) {
descriptor.setAdmissionDate(new AdmissionDate(admissionDate));
return this;
}

public EditPersonDescriptor build() {
return descriptor;
Expand Down

0 comments on commit 2774556

Please sign in to comment.