From fc8e12f91d103c1993f80eda6e10beac62f8fd7e Mon Sep 17 00:00:00 2001 From: ryanlimdx Date: Tue, 9 Apr 2024 22:06:41 +0800 Subject: [PATCH 01/15] Fix incorrect exception thrown for invalid dob --- src/main/java/seedu/address/logic/parser/ParserUtil.java | 2 +- src/main/java/seedu/address/model/person/Dob.java | 8 ++++++-- .../java/seedu/address/storage/JsonAdaptedPerson.java | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index 2dd9a797ea2..17670d3d1f1 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -106,7 +106,7 @@ public static Dob parseDob(String dob) throws ParseException { throw new ParseException(Dob.MESSAGE_CONSTRAINTS_FORMAT); } if (!Dob.isValidDob(trimmedDob)) { - throw new ParseException(Dob.MESSAGE_CONSTRAINTS_OCCURRENCE); + throw new ParseException(Dob.MESSAGE_CONSTRAINTS_OCCURRENCE_LATER_THAN_CURRENT_DATE); } return new Dob(trimmedDob); } diff --git a/src/main/java/seedu/address/model/person/Dob.java b/src/main/java/seedu/address/model/person/Dob.java index a728d9490d0..eeec1baf1e0 100644 --- a/src/main/java/seedu/address/model/person/Dob.java +++ b/src/main/java/seedu/address/model/person/Dob.java @@ -13,8 +13,12 @@ public class Dob { public static final String MESSAGE_CONSTRAINTS_FORMAT = "Dates of birth takes in a date of format dd/MM/yyyy"; - public static final String MESSAGE_CONSTRAINTS_OCCURRENCE = + public static final String MESSAGE_CONSTRAINTS_OCCURRENCE_LATER_THAN_ADMISSION = "Date of birth should not be later than date of admission"; + + public static final String MESSAGE_CONSTRAINTS_OCCURRENCE_LATER_THAN_CURRENT_DATE = + "Date of birth should not be later than current date"; + private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); public final LocalDate date; public final String value; @@ -27,7 +31,7 @@ public class Dob { public Dob(String value) { requireNonNull(value); checkArgument(isValidDate(value), MESSAGE_CONSTRAINTS_FORMAT); - checkArgument(isValidDob(value), MESSAGE_CONSTRAINTS_OCCURRENCE); + checkArgument(isValidDob(value), MESSAGE_CONSTRAINTS_OCCURRENCE_LATER_THAN_CURRENT_DATE); this.date = LocalDate.parse(value, formatter); this.value = value; } diff --git a/src/main/java/seedu/address/storage/JsonAdaptedPerson.java b/src/main/java/seedu/address/storage/JsonAdaptedPerson.java index e55174bcea1..50851c1d880 100644 --- a/src/main/java/seedu/address/storage/JsonAdaptedPerson.java +++ b/src/main/java/seedu/address/storage/JsonAdaptedPerson.java @@ -96,7 +96,7 @@ public Person toModelType() throws IllegalValueException { throw new IllegalValueException(Dob.MESSAGE_CONSTRAINTS_FORMAT); } if (!Dob.isValidDob(dob)) { - throw new IllegalValueException(Dob.MESSAGE_CONSTRAINTS_OCCURRENCE); + throw new IllegalValueException(Dob.MESSAGE_CONSTRAINTS_OCCURRENCE_LATER_THAN_CURRENT_DATE); } final Dob modelDob = new Dob(dob); From 8e2b2f8262da136d39532811dedae32f10911e39 Mon Sep 17 00:00:00 2001 From: ryanlimdx Date: Tue, 9 Apr 2024 23:26:04 +0800 Subject: [PATCH 02/15] Clean up unused message --- src/main/java/seedu/address/model/person/Dob.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/seedu/address/model/person/Dob.java b/src/main/java/seedu/address/model/person/Dob.java index eeec1baf1e0..bde4b8bb858 100644 --- a/src/main/java/seedu/address/model/person/Dob.java +++ b/src/main/java/seedu/address/model/person/Dob.java @@ -13,8 +13,6 @@ public class Dob { public static final String MESSAGE_CONSTRAINTS_FORMAT = "Dates of birth takes in a date of format dd/MM/yyyy"; - public static final String MESSAGE_CONSTRAINTS_OCCURRENCE_LATER_THAN_ADMISSION = - "Date of birth should not be later than date of admission"; public static final String MESSAGE_CONSTRAINTS_OCCURRENCE_LATER_THAN_CURRENT_DATE = "Date of birth should not be later than current date"; From c646fa27caf8958427fb85cf1ee4f79a34d7b1a6 Mon Sep 17 00:00:00 2001 From: ryanlimdx Date: Tue, 9 Apr 2024 23:40:12 +0800 Subject: [PATCH 03/15] Create DateUtil class --- .../seedu/address/commons/util/DateUtil.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/main/java/seedu/address/commons/util/DateUtil.java diff --git a/src/main/java/seedu/address/commons/util/DateUtil.java b/src/main/java/seedu/address/commons/util/DateUtil.java new file mode 100644 index 00000000000..84fb504628f --- /dev/null +++ b/src/main/java/seedu/address/commons/util/DateUtil.java @@ -0,0 +1,26 @@ +package seedu.address.commons.util; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; + +public class DateUtil { + private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); + + public static boolean isValidDate(String value) { + try { + LocalDate date = LocalDate.parse(value, formatter); + return true; + } catch (DateTimeParseException e) { + return false; + } + } + + public static boolean isFutureDate(String value) { + if (isValidDate(value)) { + LocalDate date = LocalDate.parse(value, formatter); + return date.isAfter(LocalDate.now()); + } + return false; + } +} From ae08382299dd3e5c04950d412538b023b45b0a0c Mon Sep 17 00:00:00 2001 From: ryanlimdx Date: Wed, 10 Apr 2024 00:02:14 +0800 Subject: [PATCH 04/15] Refactor code for Dob --- .../seedu/address/commons/util/DateUtil.java | 11 +++++- .../address/logic/commands/AddCommand.java | 2 +- .../address/logic/commands/EditCommand.java | 2 +- .../address/logic/parser/ParserUtil.java | 2 +- .../java/seedu/address/model/person/Dob.java | 39 +++++-------------- .../address/storage/JsonAdaptedPerson.java | 2 +- .../seedu/address/model/person/DobTest.java | 2 +- 7 files changed, 25 insertions(+), 35 deletions(-) diff --git a/src/main/java/seedu/address/commons/util/DateUtil.java b/src/main/java/seedu/address/commons/util/DateUtil.java index 84fb504628f..015239ff99d 100644 --- a/src/main/java/seedu/address/commons/util/DateUtil.java +++ b/src/main/java/seedu/address/commons/util/DateUtil.java @@ -7,6 +7,11 @@ public class DateUtil { private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); + /** + * Returns true if a given string is a valid date. + * + * @param value The date to be checked. + */ public static boolean isValidDate(String value) { try { LocalDate date = LocalDate.parse(value, formatter); @@ -15,7 +20,11 @@ public static boolean isValidDate(String value) { return false; } } - + /** + * Returns true if a given string is a future date. + * + * @param value The date to be checked. + */ public static boolean isFutureDate(String value) { if (isValidDate(value)) { LocalDate date = LocalDate.parse(value, formatter); diff --git a/src/main/java/seedu/address/logic/commands/AddCommand.java b/src/main/java/seedu/address/logic/commands/AddCommand.java index 1ed71c7f304..0b8103e60e2 100644 --- a/src/main/java/seedu/address/logic/commands/AddCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddCommand.java @@ -62,7 +62,7 @@ public CommandResult execute(Model model) throws CommandException { throw new CommandException(MESSAGE_DUPLICATE_PERSON); } - if (toAdd.getDob().date.isAfter(toAdd.getAdmissionDate().date)) { + if (toAdd.getDob().getDob().isAfter(toAdd.getAdmissionDate().date)) { throw new CommandException(MESSAGE_DOB_AFTER_ADMISSION); } diff --git a/src/main/java/seedu/address/logic/commands/EditCommand.java b/src/main/java/seedu/address/logic/commands/EditCommand.java index 6848c033af3..9c3c220c14a 100644 --- a/src/main/java/seedu/address/logic/commands/EditCommand.java +++ b/src/main/java/seedu/address/logic/commands/EditCommand.java @@ -91,7 +91,7 @@ public CommandResult execute(Model model) throws CommandException { throw new CommandException(MESSAGE_DUPLICATE_PERSON); } - if (editedPerson.getDob().date.isAfter(editedPerson.getAdmissionDate().date)) { + if (editedPerson.getDob().getDob().isAfter(editedPerson.getAdmissionDate().date)) { throw new CommandException(MESSAGE_DOB_AFTER_ADMISSION); } diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index 17670d3d1f1..9964640bc60 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -105,7 +105,7 @@ public static Dob parseDob(String dob) throws ParseException { if (!Dob.isValidDate(trimmedDob)) { throw new ParseException(Dob.MESSAGE_CONSTRAINTS_FORMAT); } - if (!Dob.isValidDob(trimmedDob)) { + if (Dob.isFutureDate(trimmedDob)) { throw new ParseException(Dob.MESSAGE_CONSTRAINTS_OCCURRENCE_LATER_THAN_CURRENT_DATE); } return new Dob(trimmedDob); diff --git a/src/main/java/seedu/address/model/person/Dob.java b/src/main/java/seedu/address/model/person/Dob.java index bde4b8bb858..ccaf3928813 100644 --- a/src/main/java/seedu/address/model/person/Dob.java +++ b/src/main/java/seedu/address/model/person/Dob.java @@ -1,16 +1,17 @@ package seedu.address.model.person; +import seedu.address.commons.util.DateUtil; + import static java.util.Objects.requireNonNull; import static seedu.address.commons.util.AppUtil.checkArgument; import java.time.LocalDate; import java.time.format.DateTimeFormatter; -import java.time.format.DateTimeParseException; /** * Represents a patient's date of birth in the address book. */ -public class Dob { +public class Dob extends DateUtil { public static final String MESSAGE_CONSTRAINTS_FORMAT = "Dates of birth takes in a date of format dd/MM/yyyy"; @@ -18,7 +19,7 @@ public class Dob { "Date of birth should not be later than current date"; private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); - public final LocalDate date; + public final LocalDate dob; public final String value; /** @@ -29,36 +30,16 @@ public class Dob { public Dob(String value) { requireNonNull(value); checkArgument(isValidDate(value), MESSAGE_CONSTRAINTS_FORMAT); - checkArgument(isValidDob(value), MESSAGE_CONSTRAINTS_OCCURRENCE_LATER_THAN_CURRENT_DATE); - this.date = LocalDate.parse(value, formatter); + checkArgument(!isFutureDate(value), MESSAGE_CONSTRAINTS_OCCURRENCE_LATER_THAN_CURRENT_DATE); + this.dob = LocalDate.parse(value, formatter); this.value = value; } /** - * Returns true if a given string is a valid date. - * - * @param dob The date of birth to be checked. + * Returns the date of birth */ - public static boolean isValidDate(String dob) { - try { - LocalDate date = LocalDate.parse(dob, formatter); - return true; - } catch (DateTimeParseException e) { - return false; - } - } - - /** - * Returns true if a given string is a valid date of birth. - * - * @param dob The date of birth to be checked. - */ - public static boolean isValidDob(String dob) { - if (isValidDate(dob)) { - LocalDate date = LocalDate.parse(dob, formatter); - return !date.isAfter(LocalDate.now()); - } - return false; + public LocalDate getDob() { + return dob; } @Override @@ -77,7 +58,7 @@ public boolean equals(Object other) { } Dob otherDob = (Dob) other; - return date.equals(otherDob.date); + return dob.equals(otherDob.dob); } @Override diff --git a/src/main/java/seedu/address/storage/JsonAdaptedPerson.java b/src/main/java/seedu/address/storage/JsonAdaptedPerson.java index 50851c1d880..d9db558e6e8 100644 --- a/src/main/java/seedu/address/storage/JsonAdaptedPerson.java +++ b/src/main/java/seedu/address/storage/JsonAdaptedPerson.java @@ -95,7 +95,7 @@ public Person toModelType() throws IllegalValueException { if (!Dob.isValidDate(dob)) { throw new IllegalValueException(Dob.MESSAGE_CONSTRAINTS_FORMAT); } - if (!Dob.isValidDob(dob)) { + if (Dob.isFutureDate(dob)) { throw new IllegalValueException(Dob.MESSAGE_CONSTRAINTS_OCCURRENCE_LATER_THAN_CURRENT_DATE); } final Dob modelDob = new Dob(dob); diff --git a/src/test/java/seedu/address/model/person/DobTest.java b/src/test/java/seedu/address/model/person/DobTest.java index 8db76d5b6f6..e8d26aa4027 100644 --- a/src/test/java/seedu/address/model/person/DobTest.java +++ b/src/test/java/seedu/address/model/person/DobTest.java @@ -52,7 +52,7 @@ public void isValidDob() { assertTrue(Dob.isValidDate(todayFormatted)); // valid Dob - assertFalse(Dob.isValidDob(tomorrowFormatted)); + assertFalse(!Dob.isFutureDate(tomorrowFormatted)); } @Test From 3b1af78695fbed1a0bcac1f12bb114195f5f1f9a Mon Sep 17 00:00:00 2001 From: ryanlimdx Date: Wed, 10 Apr 2024 00:03:59 +0800 Subject: [PATCH 05/15] Fix testcase --- src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java b/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java index 0c2ed03175b..b025455ad5b 100644 --- a/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java +++ b/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java @@ -93,7 +93,7 @@ public void toModelType_invalidDateDob_throwsIllegalValueException() { public void toModelType_invalidDob_throwsIllegalValueException() { JsonAdaptedPerson person = new JsonAdaptedPerson(VALID_NAME, VALID_TAGS, INVALID_DOB_FUTURE, VALID_IC, VALID_ADMISSION_DATE, VALID_WARD, VALID_REMARK); - String expectedMessage = Dob.MESSAGE_CONSTRAINTS_OCCURRENCE; + String expectedMessage = Dob.MESSAGE_CONSTRAINTS_OCCURRENCE_LATER_THAN_CURRENT_DATE; assertThrows(IllegalValueException.class, expectedMessage, person::toModelType); } From 75a1ade5e11483cfc30e904c9591a79bdb089a9a Mon Sep 17 00:00:00 2001 From: ryanlimdx Date: Wed, 10 Apr 2024 00:05:01 +0800 Subject: [PATCH 06/15] Privatise fields for dob --- src/main/java/seedu/address/model/person/Dob.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/model/person/Dob.java b/src/main/java/seedu/address/model/person/Dob.java index ccaf3928813..fe6ac055766 100644 --- a/src/main/java/seedu/address/model/person/Dob.java +++ b/src/main/java/seedu/address/model/person/Dob.java @@ -19,8 +19,8 @@ public class Dob extends DateUtil { "Date of birth should not be later than current date"; private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); - public final LocalDate dob; - public final String value; + private final LocalDate dob; + private final String value; /** * Constructs a {@code Dob}. From e431012c9186f9dd4bde4832e4960452a9016d4f Mon Sep 17 00:00:00 2001 From: ryanlimdx Date: Wed, 10 Apr 2024 00:26:37 +0800 Subject: [PATCH 07/15] Refactor code for AdmissionDate --- .../seedu/address/commons/util/DateUtil.java | 7 +++ .../address/logic/commands/AddCommand.java | 2 +- .../address/logic/commands/EditCommand.java | 2 +- .../address/logic/parser/ParserUtil.java | 2 +- .../address/model/person/AdmissionDate.java | 43 +++++-------------- .../java/seedu/address/model/person/Dob.java | 2 +- .../address/storage/JsonAdaptedPerson.java | 6 +-- .../java/seedu/address/ui/PersonCard.java | 4 +- .../model/person/AdmissionDateTest.java | 2 +- .../seedu/address/testutil/PersonUtil.java | 8 ++-- 10 files changed, 32 insertions(+), 46 deletions(-) diff --git a/src/main/java/seedu/address/commons/util/DateUtil.java b/src/main/java/seedu/address/commons/util/DateUtil.java index 015239ff99d..99c3f164080 100644 --- a/src/main/java/seedu/address/commons/util/DateUtil.java +++ b/src/main/java/seedu/address/commons/util/DateUtil.java @@ -32,4 +32,11 @@ public static boolean isFutureDate(String value) { } return false; } + + /** + * Returns the formatter for the date. + */ + public static DateTimeFormatter getFormatter() { + return formatter; + } } diff --git a/src/main/java/seedu/address/logic/commands/AddCommand.java b/src/main/java/seedu/address/logic/commands/AddCommand.java index 0b8103e60e2..0391e616f35 100644 --- a/src/main/java/seedu/address/logic/commands/AddCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddCommand.java @@ -62,7 +62,7 @@ public CommandResult execute(Model model) throws CommandException { throw new CommandException(MESSAGE_DUPLICATE_PERSON); } - if (toAdd.getDob().getDob().isAfter(toAdd.getAdmissionDate().date)) { + if (toAdd.getDob().getDate().isAfter(toAdd.getAdmissionDate().getAdmissionDate())) { throw new CommandException(MESSAGE_DOB_AFTER_ADMISSION); } diff --git a/src/main/java/seedu/address/logic/commands/EditCommand.java b/src/main/java/seedu/address/logic/commands/EditCommand.java index 9c3c220c14a..03081f0c7b4 100644 --- a/src/main/java/seedu/address/logic/commands/EditCommand.java +++ b/src/main/java/seedu/address/logic/commands/EditCommand.java @@ -91,7 +91,7 @@ public CommandResult execute(Model model) throws CommandException { throw new CommandException(MESSAGE_DUPLICATE_PERSON); } - if (editedPerson.getDob().getDob().isAfter(editedPerson.getAdmissionDate().date)) { + if (editedPerson.getDob().getDate().isAfter(editedPerson.getAdmissionDate().getAdmissionDate())) { throw new CommandException(MESSAGE_DOB_AFTER_ADMISSION); } diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index 9964640bc60..a356e392fca 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -139,7 +139,7 @@ public static AdmissionDate parseAdmissionDate(String admissionDate) throws Pars throw new ParseException(AdmissionDate.MESSAGE_CONSTRAINTS_FORMAT); } - if (!AdmissionDate.isValidAdmissionDate(admissionDate.trim())) { + if (AdmissionDate.isFutureDate(admissionDate.trim())) { throw new ParseException(AdmissionDate.MESSAGE_CONSTRAINTS_OCCURRENCE); } return new AdmissionDate(trimmedAdmissionDate); diff --git a/src/main/java/seedu/address/model/person/AdmissionDate.java b/src/main/java/seedu/address/model/person/AdmissionDate.java index 8084a7f13af..8766819090b 100644 --- a/src/main/java/seedu/address/model/person/AdmissionDate.java +++ b/src/main/java/seedu/address/model/person/AdmissionDate.java @@ -1,24 +1,23 @@ package seedu.address.model.person; +import seedu.address.commons.util.DateUtil; + import static java.util.Objects.requireNonNull; import static seedu.address.commons.util.AppUtil.checkArgument; import java.time.LocalDate; -import java.time.format.DateTimeFormatter; -import java.time.format.DateTimeParseException; /** * Represents a patient's admission date in the address book. */ -public class AdmissionDate { +public class AdmissionDate extends DateUtil { public static final String MESSAGE_CONSTRAINTS_FORMAT = "Admission dates take in date of format dd/MM/yyyy."; public static final String MESSAGE_CONSTRAINTS_OCCURRENCE = "Admission date should not be earlier than date of birth or later than current date"; - private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); - public final LocalDate date; - public final String value; + private final LocalDate admissionDate; + private final String value; /** * Constructs a {@code AdmissionDate}. @@ -28,36 +27,16 @@ public class AdmissionDate { public AdmissionDate(String value) { requireNonNull(value); checkArgument(isValidDate(value), MESSAGE_CONSTRAINTS_FORMAT); - checkArgument(isValidAdmissionDate(value), MESSAGE_CONSTRAINTS_OCCURRENCE); - this.date = LocalDate.parse(value, formatter); + checkArgument(!isFutureDate(value), MESSAGE_CONSTRAINTS_OCCURRENCE); + this.admissionDate = LocalDate.parse(value, getFormatter()); this.value = value; } /** - * Returns true if a given string is a valid date. - * - * @param admissionDate The admission date to be checked. + * Returns the date of birth */ - public static boolean isValidDate(String admissionDate) { - try { - LocalDate date = LocalDate.parse(admissionDate, formatter); - return true; - } catch (DateTimeParseException e) { - return false; - } - } - - /** - * Returns true if a given string is a valid admission date. - * - * @param admissionDate The admission date to be checked. - */ - public static boolean isValidAdmissionDate(String admissionDate) { - if (isValidDate(admissionDate)) { - LocalDate date = LocalDate.parse(admissionDate, formatter); - return !date.isAfter(LocalDate.now()); - } - return false; + public LocalDate getAdmissionDate() { + return admissionDate; } @Override @@ -76,7 +55,7 @@ public boolean equals(Object other) { } AdmissionDate otherAdmissionDate = (AdmissionDate) other; - return date.equals(otherAdmissionDate.date); + return admissionDate.equals(otherAdmissionDate.admissionDate); } @Override diff --git a/src/main/java/seedu/address/model/person/Dob.java b/src/main/java/seedu/address/model/person/Dob.java index fe6ac055766..3ae079ac364 100644 --- a/src/main/java/seedu/address/model/person/Dob.java +++ b/src/main/java/seedu/address/model/person/Dob.java @@ -38,7 +38,7 @@ public Dob(String value) { /** * Returns the date of birth */ - public LocalDate getDob() { + public LocalDate getDate() { return dob; } diff --git a/src/main/java/seedu/address/storage/JsonAdaptedPerson.java b/src/main/java/seedu/address/storage/JsonAdaptedPerson.java index d9db558e6e8..08c106cbc16 100644 --- a/src/main/java/seedu/address/storage/JsonAdaptedPerson.java +++ b/src/main/java/seedu/address/storage/JsonAdaptedPerson.java @@ -61,9 +61,9 @@ public JsonAdaptedPerson(Person source) { tags.addAll(source.getTags().stream() .map(JsonAdaptedTag::new) .collect(Collectors.toList())); - dob = source.getDob().value; + dob = source.getDob().toString(); ic = source.getIc().value; - admissionDate = source.getAdmissionDate().value; + admissionDate = source.getAdmissionDate().toString(); ward = source.getWard().value; remark = source.getRemark().value; } @@ -115,7 +115,7 @@ public Person toModelType() throws IllegalValueException { if (!AdmissionDate.isValidDate(admissionDate)) { throw new IllegalValueException(AdmissionDate.MESSAGE_CONSTRAINTS_FORMAT); } - if (!AdmissionDate.isValidAdmissionDate(admissionDate)) { + if (AdmissionDate.isFutureDate(admissionDate)) { throw new IllegalValueException(AdmissionDate.MESSAGE_CONSTRAINTS_OCCURRENCE); } final AdmissionDate modelAdmissionDate = new AdmissionDate(admissionDate); diff --git a/src/main/java/seedu/address/ui/PersonCard.java b/src/main/java/seedu/address/ui/PersonCard.java index 4f844584616..4abd75ef2f3 100644 --- a/src/main/java/seedu/address/ui/PersonCard.java +++ b/src/main/java/seedu/address/ui/PersonCard.java @@ -53,9 +53,9 @@ public PersonCard(Person person, int displayedIndex) { this.person = person; id.setText(displayedIndex + ". "); name.setText(person.getName().fullName); - dob.setText("DOB: " + person.getDob().value); + dob.setText("DOB: " + person.getDob().toString()); ic.setText("IC: " + person.getIc().value); - admissionDate.setText("Admission Date: " + person.getAdmissionDate().value); + admissionDate.setText("Admission Date: " + person.getAdmissionDate().toString()); ward.setText("Ward: " + person.getWard().value); person.getTags().stream() .sorted(Comparator.comparing(tag -> tag.tagName)) diff --git a/src/test/java/seedu/address/model/person/AdmissionDateTest.java b/src/test/java/seedu/address/model/person/AdmissionDateTest.java index 1157a476458..ebc802766ff 100644 --- a/src/test/java/seedu/address/model/person/AdmissionDateTest.java +++ b/src/test/java/seedu/address/model/person/AdmissionDateTest.java @@ -52,7 +52,7 @@ public void isValidAdmissionDate() { assertTrue(AdmissionDate.isValidDate(todayFormatted)); // valid AdmissionDate - assertFalse(AdmissionDate.isValidAdmissionDate(tomorrowFormatted)); + assertFalse(!AdmissionDate.isFutureDate(tomorrowFormatted)); } @Test diff --git a/src/test/java/seedu/address/testutil/PersonUtil.java b/src/test/java/seedu/address/testutil/PersonUtil.java index 7caaa80a48e..7ec3b85c476 100644 --- a/src/test/java/seedu/address/testutil/PersonUtil.java +++ b/src/test/java/seedu/address/testutil/PersonUtil.java @@ -34,9 +34,9 @@ public static String getPersonDetails(Person person) { StringBuilder sb = new StringBuilder(); sb.append(PREFIX_NAME + person.getName().fullName + " "); sb.append(PREFIX_IC + person.getIc().value + " "); - sb.append(PREFIX_DOB + person.getDob().value + " "); + sb.append(PREFIX_DOB + person.getDob().toString() + " "); sb.append(PREFIX_WARD + person.getWard().value + " "); - sb.append(PREFIX_ADMISSION_DATE + person.getAdmissionDate().value + " "); + sb.append(PREFIX_ADMISSION_DATE + person.getAdmissionDate().toString() + " "); person.getTags().stream().forEach( s -> sb.append(PREFIX_TAG + s.tagName + " ")); sb.append(PREFIX_REMARK + person.getRemark().value + " "); @@ -50,10 +50,10 @@ public static String getEditPersonDescriptorDetails(EditPersonDescriptor descrip StringBuilder sb = new StringBuilder(); descriptor.getName().ifPresent(name -> sb.append(PREFIX_NAME).append(name.fullName).append(" ")); descriptor.getIc().ifPresent(ic -> sb.append(PREFIX_IC).append(ic.value).append(" ")); - descriptor.getDob().ifPresent(dob -> sb.append(PREFIX_DOB).append(dob.value).append(" ")); + descriptor.getDob().ifPresent(dob -> sb.append(PREFIX_DOB).append(dob).append(" ")); descriptor.getWard().ifPresent(ward -> sb.append(PREFIX_WARD).append(ward.value).append(" ")); descriptor.getAdmissionDate().ifPresent(admissionDate -> sb.append(PREFIX_ADMISSION_DATE) - .append(admissionDate.value).append(" ")); + .append(admissionDate).append(" ")); if (descriptor.getTags().isPresent()) { Set tags = descriptor.getTags().get(); if (tags.isEmpty()) { From 54241cc2a9c44265db89745928e9b49a4b6c86b6 Mon Sep 17 00:00:00 2001 From: ryanlimdx Date: Wed, 10 Apr 2024 00:28:29 +0800 Subject: [PATCH 08/15] Update add and edit command after refactoring --- src/main/java/seedu/address/logic/commands/AddCommand.java | 2 +- src/main/java/seedu/address/logic/commands/EditCommand.java | 2 +- src/main/java/seedu/address/model/person/AdmissionDate.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/AddCommand.java b/src/main/java/seedu/address/logic/commands/AddCommand.java index 0391e616f35..d3b67220642 100644 --- a/src/main/java/seedu/address/logic/commands/AddCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddCommand.java @@ -62,7 +62,7 @@ public CommandResult execute(Model model) throws CommandException { throw new CommandException(MESSAGE_DUPLICATE_PERSON); } - if (toAdd.getDob().getDate().isAfter(toAdd.getAdmissionDate().getAdmissionDate())) { + if (toAdd.getDob().getDate().isAfter(toAdd.getAdmissionDate().getDate())) { throw new CommandException(MESSAGE_DOB_AFTER_ADMISSION); } diff --git a/src/main/java/seedu/address/logic/commands/EditCommand.java b/src/main/java/seedu/address/logic/commands/EditCommand.java index 03081f0c7b4..4a6d40c4362 100644 --- a/src/main/java/seedu/address/logic/commands/EditCommand.java +++ b/src/main/java/seedu/address/logic/commands/EditCommand.java @@ -91,7 +91,7 @@ public CommandResult execute(Model model) throws CommandException { throw new CommandException(MESSAGE_DUPLICATE_PERSON); } - if (editedPerson.getDob().getDate().isAfter(editedPerson.getAdmissionDate().getAdmissionDate())) { + if (editedPerson.getDob().getDate().isAfter(editedPerson.getAdmissionDate().getDate())) { throw new CommandException(MESSAGE_DOB_AFTER_ADMISSION); } diff --git a/src/main/java/seedu/address/model/person/AdmissionDate.java b/src/main/java/seedu/address/model/person/AdmissionDate.java index 8766819090b..2a5d13b0ba2 100644 --- a/src/main/java/seedu/address/model/person/AdmissionDate.java +++ b/src/main/java/seedu/address/model/person/AdmissionDate.java @@ -35,7 +35,7 @@ public AdmissionDate(String value) { /** * Returns the date of birth */ - public LocalDate getAdmissionDate() { + public LocalDate getDate() { return admissionDate; } From 7479c51b43b63118fa9531e04c598e4b39120aa2 Mon Sep 17 00:00:00 2001 From: ryanlimdx Date: Wed, 10 Apr 2024 00:40:40 +0800 Subject: [PATCH 09/15] Refactor date parsing in parserUtil --- .../address/logic/parser/ParserUtil.java | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index a356e392fca..5c3f2ee857f 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -102,13 +102,11 @@ public static List parseTagsKeywords(Collection tags) throws Par public static Dob parseDob(String dob) throws ParseException { requireNonNull(dob); String trimmedDob = dob.trim(); - if (!Dob.isValidDate(trimmedDob)) { - throw new ParseException(Dob.MESSAGE_CONSTRAINTS_FORMAT); + try { + return new Dob(trimmedDob); + } catch (IllegalArgumentException e) { + throw new ParseException(e.getMessage()); } - if (Dob.isFutureDate(trimmedDob)) { - throw new ParseException(Dob.MESSAGE_CONSTRAINTS_OCCURRENCE_LATER_THAN_CURRENT_DATE); - } - return new Dob(trimmedDob); } /** @@ -135,14 +133,11 @@ public static Ic parseIc(String ic) throws ParseException { public static AdmissionDate parseAdmissionDate(String admissionDate) throws ParseException { requireNonNull(admissionDate); String trimmedAdmissionDate = admissionDate.trim(); - if (!AdmissionDate.isValidDate(admissionDate.trim())) { - throw new ParseException(AdmissionDate.MESSAGE_CONSTRAINTS_FORMAT); - } - - if (AdmissionDate.isFutureDate(admissionDate.trim())) { - throw new ParseException(AdmissionDate.MESSAGE_CONSTRAINTS_OCCURRENCE); + try { + return new AdmissionDate(trimmedAdmissionDate); + } catch (IllegalArgumentException e) { + throw new ParseException(e.getMessage()); } - return new AdmissionDate(trimmedAdmissionDate); } /** From 2bfb15ad0efa19c2a2daed3eaf5e0523eb419692 Mon Sep 17 00:00:00 2001 From: ryanlimdx Date: Wed, 10 Apr 2024 00:53:42 +0800 Subject: [PATCH 10/15] Refactor exception messages --- src/main/java/seedu/address/commons/util/DateUtil.java | 3 +++ .../java/seedu/address/model/person/AdmissionDate.java | 3 ++- src/main/java/seedu/address/model/person/Dob.java | 10 +++++----- .../java/seedu/address/storage/JsonAdaptedPerson.java | 2 +- .../seedu/address/storage/JsonAdaptedPersonTest.java | 2 +- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/main/java/seedu/address/commons/util/DateUtil.java b/src/main/java/seedu/address/commons/util/DateUtil.java index 99c3f164080..707375421ec 100644 --- a/src/main/java/seedu/address/commons/util/DateUtil.java +++ b/src/main/java/seedu/address/commons/util/DateUtil.java @@ -5,6 +5,9 @@ import java.time.format.DateTimeParseException; public class DateUtil { + + public static final String MESSAGE_CONSTRAINTS_FORMAT = "%1$s takes in a date of format dd/MM/yyyy"; + public static final String MESSAGE_CONSTRAINTS_FUTURE_OCCURRENCE = "%1$s should not be later than current date"; private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); /** diff --git a/src/main/java/seedu/address/model/person/AdmissionDate.java b/src/main/java/seedu/address/model/person/AdmissionDate.java index 2a5d13b0ba2..9c9d19f83de 100644 --- a/src/main/java/seedu/address/model/person/AdmissionDate.java +++ b/src/main/java/seedu/address/model/person/AdmissionDate.java @@ -11,8 +11,9 @@ * Represents a patient's admission date in the address book. */ public class AdmissionDate extends DateUtil { + public static final String DATE_TYPE = "Admission date"; public static final String MESSAGE_CONSTRAINTS_FORMAT = - "Admission dates take in date of format dd/MM/yyyy."; + String.format(DateUtil.MESSAGE_CONSTRAINTS_FORMAT, DATE_TYPE); public static final String MESSAGE_CONSTRAINTS_OCCURRENCE = "Admission date should not be earlier than date of birth or later than current date"; diff --git a/src/main/java/seedu/address/model/person/Dob.java b/src/main/java/seedu/address/model/person/Dob.java index 3ae079ac364..7f23885f565 100644 --- a/src/main/java/seedu/address/model/person/Dob.java +++ b/src/main/java/seedu/address/model/person/Dob.java @@ -12,11 +12,11 @@ * Represents a patient's date of birth in the address book. */ public class Dob extends DateUtil { + public static final String DATE_TYPE = "Date of birth"; public static final String MESSAGE_CONSTRAINTS_FORMAT = - "Dates of birth takes in a date of format dd/MM/yyyy"; - - public static final String MESSAGE_CONSTRAINTS_OCCURRENCE_LATER_THAN_CURRENT_DATE = - "Date of birth should not be later than current date"; + String.format(DateUtil.MESSAGE_CONSTRAINTS_FORMAT, DATE_TYPE); + public static final String MESSAGE_CONSTRAINTS_FUTURE_OCCURRENCE = + String.format(DateUtil.MESSAGE_CONSTRAINTS_FUTURE_OCCURRENCE, DATE_TYPE); private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); private final LocalDate dob; @@ -30,7 +30,7 @@ public class Dob extends DateUtil { public Dob(String value) { requireNonNull(value); checkArgument(isValidDate(value), MESSAGE_CONSTRAINTS_FORMAT); - checkArgument(!isFutureDate(value), MESSAGE_CONSTRAINTS_OCCURRENCE_LATER_THAN_CURRENT_DATE); + checkArgument(!isFutureDate(value), MESSAGE_CONSTRAINTS_FUTURE_OCCURRENCE); this.dob = LocalDate.parse(value, formatter); this.value = value; } diff --git a/src/main/java/seedu/address/storage/JsonAdaptedPerson.java b/src/main/java/seedu/address/storage/JsonAdaptedPerson.java index 08c106cbc16..5f93be08328 100644 --- a/src/main/java/seedu/address/storage/JsonAdaptedPerson.java +++ b/src/main/java/seedu/address/storage/JsonAdaptedPerson.java @@ -96,7 +96,7 @@ public Person toModelType() throws IllegalValueException { throw new IllegalValueException(Dob.MESSAGE_CONSTRAINTS_FORMAT); } if (Dob.isFutureDate(dob)) { - throw new IllegalValueException(Dob.MESSAGE_CONSTRAINTS_OCCURRENCE_LATER_THAN_CURRENT_DATE); + throw new IllegalValueException(Dob.MESSAGE_CONSTRAINTS_FUTURE_OCCURRENCE); } final Dob modelDob = new Dob(dob); diff --git a/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java b/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java index b025455ad5b..ccceebca302 100644 --- a/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java +++ b/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java @@ -93,7 +93,7 @@ public void toModelType_invalidDateDob_throwsIllegalValueException() { public void toModelType_invalidDob_throwsIllegalValueException() { JsonAdaptedPerson person = new JsonAdaptedPerson(VALID_NAME, VALID_TAGS, INVALID_DOB_FUTURE, VALID_IC, VALID_ADMISSION_DATE, VALID_WARD, VALID_REMARK); - String expectedMessage = Dob.MESSAGE_CONSTRAINTS_OCCURRENCE_LATER_THAN_CURRENT_DATE; + String expectedMessage = Dob.MESSAGE_CONSTRAINTS_FUTURE_OCCURRENCE; assertThrows(IllegalValueException.class, expectedMessage, person::toModelType); } From 7fc45b40f78f0c7919895a01d798e05ec6a3f135 Mon Sep 17 00:00:00 2001 From: ryanlimdx Date: Wed, 10 Apr 2024 01:34:14 +0800 Subject: [PATCH 11/15] Add javadocs for DateUtil --- src/main/java/seedu/address/commons/util/DateUtil.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/seedu/address/commons/util/DateUtil.java b/src/main/java/seedu/address/commons/util/DateUtil.java index 707375421ec..686ba223806 100644 --- a/src/main/java/seedu/address/commons/util/DateUtil.java +++ b/src/main/java/seedu/address/commons/util/DateUtil.java @@ -4,6 +4,9 @@ import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; +/** + * A utility class for handling dates. + */ public class DateUtil { public static final String MESSAGE_CONSTRAINTS_FORMAT = "%1$s takes in a date of format dd/MM/yyyy"; From 57569ed68ebe23d077f5bb58151bec8f1586bbd4 Mon Sep 17 00:00:00 2001 From: ryanlimdx Date: Wed, 10 Apr 2024 01:38:56 +0800 Subject: [PATCH 12/15] Fix checkstyle imports --- src/main/java/seedu/address/model/person/AdmissionDate.java | 4 ++-- src/main/java/seedu/address/model/person/Dob.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/seedu/address/model/person/AdmissionDate.java b/src/main/java/seedu/address/model/person/AdmissionDate.java index 9c9d19f83de..b3f112accec 100644 --- a/src/main/java/seedu/address/model/person/AdmissionDate.java +++ b/src/main/java/seedu/address/model/person/AdmissionDate.java @@ -1,12 +1,12 @@ package seedu.address.model.person; -import seedu.address.commons.util.DateUtil; - import static java.util.Objects.requireNonNull; import static seedu.address.commons.util.AppUtil.checkArgument; import java.time.LocalDate; +import seedu.address.commons.util.DateUtil; + /** * Represents a patient's admission date in the address book. */ diff --git a/src/main/java/seedu/address/model/person/Dob.java b/src/main/java/seedu/address/model/person/Dob.java index 7f23885f565..2fd0b9e3d1e 100644 --- a/src/main/java/seedu/address/model/person/Dob.java +++ b/src/main/java/seedu/address/model/person/Dob.java @@ -1,13 +1,13 @@ package seedu.address.model.person; -import seedu.address.commons.util.DateUtil; - import static java.util.Objects.requireNonNull; import static seedu.address.commons.util.AppUtil.checkArgument; import java.time.LocalDate; import java.time.format.DateTimeFormatter; +import seedu.address.commons.util.DateUtil; + /** * Represents a patient's date of birth in the address book. */ From ca0e315ac940dcf51d30b6c1c1f298e37bae1608 Mon Sep 17 00:00:00 2001 From: ryanlimdx Date: Wed, 10 Apr 2024 01:50:44 +0800 Subject: [PATCH 13/15] Fix JsonAdaptedPersonTest --- src/main/java/seedu/address/storage/JsonAdaptedPerson.java | 5 +++-- .../java/seedu/address/storage/JsonAdaptedPersonTest.java | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/seedu/address/storage/JsonAdaptedPerson.java b/src/main/java/seedu/address/storage/JsonAdaptedPerson.java index 5f7579be575..bc4d241741f 100644 --- a/src/main/java/seedu/address/storage/JsonAdaptedPerson.java +++ b/src/main/java/seedu/address/storage/JsonAdaptedPerson.java @@ -194,8 +194,9 @@ private Remark validateAndConvertRemark() throws IllegalValueException { } private void isDobBeforeAdmissionDate(Dob dob, AdmissionDate admissionDate) throws IllegalValueException { - if (dob.date.isAfter(admissionDate.date)) { - throw new IllegalValueException(Dob.MESSAGE_CONSTRAINTS_OCCURRENCE); + if (dob.getDate().isAfter(admissionDate.getDate())) { + // TODO: Update after refactoring + throw new IllegalValueException("Date of birth should not be later than admission date"); } } } diff --git a/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java b/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java index 9f87a0942fc..88b0fb2c312 100644 --- a/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java +++ b/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java @@ -141,7 +141,7 @@ public void toModelType_dobLaterThanAdmission_throwsIllegalValueException() { public void toModelType_dobAndAdmissionDateInFuture_throwsIllegalValueException() { JsonAdaptedPerson person = new JsonAdaptedPerson(VALID_NAME, VALID_TAGS, INVALID_DOB_FUTURE, VALID_IC, INVALID_ADMISSION_DATE_FUTURE, VALID_WARD, VALID_REMARK); - String expectedMessage = Dob.MESSAGE_CONSTRAINTS_OCCURRENCE; + String expectedMessage = Dob.MESSAGE_CONSTRAINTS_FUTURE_OCCURRENCE; assertThrows(IllegalValueException.class, expectedMessage, person::toModelType); } From 236e25cf14186c9e81c657b6bf539d49ccd8a71b Mon Sep 17 00:00:00 2001 From: ryanlimdx Date: Wed, 10 Apr 2024 02:03:05 +0800 Subject: [PATCH 14/15] Checks if dob is after admission date with the Dob class --- src/main/java/seedu/address/model/person/Dob.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/seedu/address/model/person/Dob.java b/src/main/java/seedu/address/model/person/Dob.java index 2fd0b9e3d1e..3da5832f213 100644 --- a/src/main/java/seedu/address/model/person/Dob.java +++ b/src/main/java/seedu/address/model/person/Dob.java @@ -35,6 +35,13 @@ public Dob(String value) { this.value = value; } + /** + * Checks if the date of birth is after the admission date + */ + public boolean isAfterAdmissionDate(AdmissionDate admissionDate) { + return dob.isAfter(admissionDate.getDate()); + } + /** * Returns the date of birth */ From 30132a093efcd36b1c46d905e47f5a0329716042 Mon Sep 17 00:00:00 2001 From: ryanlimdx Date: Wed, 10 Apr 2024 10:43:29 +0800 Subject: [PATCH 15/15] Abstract dob and admission date relativity to dob --- .../java/seedu/address/logic/commands/AddCommand.java | 8 +++----- .../java/seedu/address/logic/commands/EditCommand.java | 6 ++---- src/main/java/seedu/address/model/person/Dob.java | 3 +++ .../java/seedu/address/logic/commands/AddCommandTest.java | 3 ++- .../seedu/address/logic/commands/EditCommandTest.java | 4 ++-- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/AddCommand.java b/src/main/java/seedu/address/logic/commands/AddCommand.java index d3b67220642..15ea1cd2713 100644 --- a/src/main/java/seedu/address/logic/commands/AddCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddCommand.java @@ -13,6 +13,7 @@ import seedu.address.logic.Messages; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.Model; +import seedu.address.model.person.Dob; import seedu.address.model.person.Person; /** @@ -41,9 +42,6 @@ public class AddCommand extends Command { public static final String MESSAGE_DUPLICATE_PERSON = "A patient with this IC already exists in the address book"; - public static final String MESSAGE_DOB_AFTER_ADMISSION = - "Date of birth should not be later than date of admission."; - private final Person toAdd; /** @@ -62,8 +60,8 @@ public CommandResult execute(Model model) throws CommandException { throw new CommandException(MESSAGE_DUPLICATE_PERSON); } - if (toAdd.getDob().getDate().isAfter(toAdd.getAdmissionDate().getDate())) { - throw new CommandException(MESSAGE_DOB_AFTER_ADMISSION); + if (toAdd.getDob().isAfterAdmissionDate(toAdd.getAdmissionDate())) { + throw new CommandException(Dob.MESSAGE_DOB_AFTER_ADMISSION); } model.addPerson(toAdd); diff --git a/src/main/java/seedu/address/logic/commands/EditCommand.java b/src/main/java/seedu/address/logic/commands/EditCommand.java index 4a6d40c4362..50e8efce215 100644 --- a/src/main/java/seedu/address/logic/commands/EditCommand.java +++ b/src/main/java/seedu/address/logic/commands/EditCommand.java @@ -57,8 +57,6 @@ public class EditCommand extends Command { public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Edited Person: %1$s"; public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided."; public static final String MESSAGE_DUPLICATE_PERSON = "A patient with this IC already exists in the address book."; - public static final String MESSAGE_DOB_AFTER_ADMISSION = - "Date of birth should not be later than date of admission."; private final Index index; private final EditPersonDescriptor editPersonDescriptor; @@ -91,8 +89,8 @@ public CommandResult execute(Model model) throws CommandException { throw new CommandException(MESSAGE_DUPLICATE_PERSON); } - if (editedPerson.getDob().getDate().isAfter(editedPerson.getAdmissionDate().getDate())) { - throw new CommandException(MESSAGE_DOB_AFTER_ADMISSION); + if (editedPerson.getDob().isAfterAdmissionDate(editedPerson.getAdmissionDate())) { + throw new CommandException(Dob.MESSAGE_DOB_AFTER_ADMISSION); } model.setPerson(personToEdit, editedPerson); diff --git a/src/main/java/seedu/address/model/person/Dob.java b/src/main/java/seedu/address/model/person/Dob.java index 3da5832f213..00ef26e98ad 100644 --- a/src/main/java/seedu/address/model/person/Dob.java +++ b/src/main/java/seedu/address/model/person/Dob.java @@ -18,6 +18,9 @@ public class Dob extends DateUtil { public static final String MESSAGE_CONSTRAINTS_FUTURE_OCCURRENCE = String.format(DateUtil.MESSAGE_CONSTRAINTS_FUTURE_OCCURRENCE, DATE_TYPE); + public static final String MESSAGE_DOB_AFTER_ADMISSION = + "Date of birth should not be later than date of admission."; + private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); private final LocalDate dob; private final String value; diff --git a/src/test/java/seedu/address/logic/commands/AddCommandTest.java b/src/test/java/seedu/address/logic/commands/AddCommandTest.java index 036273bda0a..83b06ecb5d9 100644 --- a/src/test/java/seedu/address/logic/commands/AddCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddCommandTest.java @@ -25,6 +25,7 @@ import seedu.address.model.ReadOnlyAddressBook; import seedu.address.model.ReadOnlyUserPrefs; import seedu.address.model.UserPrefs; +import seedu.address.model.person.Dob; import seedu.address.model.person.Person; import seedu.address.testutil.PersonBuilder; @@ -34,7 +35,7 @@ public void execute_dobLaterThanAdmission_failure() { Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); Person person = new PersonBuilder().withDob("01/01/2000").withAdmissionDate("01/01/1999").build(); AddCommand addCommand = new AddCommand(person); - assertThrows(CommandException.class, AddCommand.MESSAGE_DOB_AFTER_ADMISSION, () -> addCommand.execute(model)); + assertThrows(CommandException.class, Dob.MESSAGE_DOB_AFTER_ADMISSION, () -> addCommand.execute(model)); } @Test diff --git a/src/test/java/seedu/address/logic/commands/EditCommandTest.java b/src/test/java/seedu/address/logic/commands/EditCommandTest.java index 0b7d53c36b0..d7ec3818890 100644 --- a/src/test/java/seedu/address/logic/commands/EditCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/EditCommandTest.java @@ -10,7 +10,6 @@ import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; import static seedu.address.logic.commands.CommandTestUtil.showPersonAtIndex; -import static seedu.address.logic.commands.EditCommand.MESSAGE_DOB_AFTER_ADMISSION; import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; @@ -24,6 +23,7 @@ import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs; +import seedu.address.model.person.Dob; import seedu.address.model.person.Person; import seedu.address.testutil.EditPersonDescriptorBuilder; import seedu.address.testutil.PersonBuilder; @@ -168,7 +168,7 @@ public void execute_dobLaterThanAdmission_failure() { .withAdmissionDate("12/08/2019").build(); EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, descriptor); - assertCommandFailure(editCommand, model, MESSAGE_DOB_AFTER_ADMISSION); + assertCommandFailure(editCommand, model, Dob.MESSAGE_DOB_AFTER_ADMISSION); } @Test