Skip to content

Commit

Permalink
Merge pull request #186 from ryanlimdx/fix-dates
Browse files Browse the repository at this point in the history
Aims to refactor the code base for date classes
  • Loading branch information
ryanlimdx authored Apr 11, 2024
2 parents 3fb324f + 30132a0 commit 6730961
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 104 deletions.
48 changes: 48 additions & 0 deletions src/main/java/seedu/address/commons/util/DateUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package seedu.address.commons.util;

import java.time.LocalDate;
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";
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");

/**
* 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);
return true;
} catch (DateTimeParseException e) {
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);
return date.isAfter(LocalDate.now());
}
return false;
}

/**
* Returns the formatter for the date.
*/
public static DateTimeFormatter getFormatter() {
return formatter;
}
}
8 changes: 3 additions & 5 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;

/**
Expand All @@ -62,8 +60,8 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

if (toAdd.getDob().date.isAfter(toAdd.getAdmissionDate().date)) {
throw new CommandException(MESSAGE_DOB_AFTER_ADMISSION);
if (toAdd.getDob().isAfterAdmissionDate(toAdd.getAdmissionDate())) {
throw new CommandException(Dob.MESSAGE_DOB_AFTER_ADMISSION);
}

model.addPerson(toAdd);
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -91,8 +89,8 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

if (editedPerson.getDob().date.isAfter(editedPerson.getAdmissionDate().date)) {
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);
Expand Down
21 changes: 8 additions & 13 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,11 @@ public static List<String> parseTagsKeywords(Collection<String> 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.isValidDob(trimmedDob)) {
throw new ParseException(Dob.MESSAGE_CONSTRAINTS_OCCURRENCE);
}
return new Dob(trimmedDob);
}

/**
Expand All @@ -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.isValidAdmissionDate(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);
}

/**
Expand Down
46 changes: 13 additions & 33 deletions src/main/java/seedu/address/model/person/AdmissionDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
import static seedu.address.commons.util.AppUtil.checkArgument;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

import seedu.address.commons.util.DateUtil;

/**
* Represents a patient's admission date in the address book.
*/
public class AdmissionDate {
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";
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}.
Expand All @@ -28,36 +28,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 getDate() {
return admissionDate;
}

@Override
Expand All @@ -76,7 +56,7 @@ public boolean equals(Object other) {
}

AdmissionDate otherAdmissionDate = (AdmissionDate) other;
return date.equals(otherAdmissionDate.date);
return admissionDate.equals(otherAdmissionDate.admissionDate);
}

@Override
Expand Down
51 changes: 22 additions & 29 deletions src/main/java/seedu/address/model/person/Dob.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

import seedu.address.commons.util.DateUtil;

/**
* Represents a patient's date of birth in the address book.
*/
public class Dob {
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 =
"Date of birth should not be later than date of admission";
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);

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");
public final LocalDate date;
public final String value;
private final LocalDate dob;
private final String value;

/**
* Constructs a {@code Dob}.
Expand All @@ -27,36 +33,23 @@ public class Dob {
public Dob(String value) {
requireNonNull(value);
checkArgument(isValidDate(value), MESSAGE_CONSTRAINTS_FORMAT);
checkArgument(isValidDob(value), MESSAGE_CONSTRAINTS_OCCURRENCE);
this.date = LocalDate.parse(value, formatter);
checkArgument(!isFutureDate(value), MESSAGE_CONSTRAINTS_FUTURE_OCCURRENCE);
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.
* Checks if the date of birth is after the admission date
*/
public static boolean isValidDate(String dob) {
try {
LocalDate date = LocalDate.parse(dob, formatter);
return true;
} catch (DateTimeParseException e) {
return false;
}
public boolean isAfterAdmissionDate(AdmissionDate admissionDate) {
return dob.isAfter(admissionDate.getDate());
}

/**
* Returns true if a given string is a valid date of birth.
*
* @param dob The date of birth to be checked.
* Returns the date of birth
*/
public static boolean isValidDob(String dob) {
if (isValidDate(dob)) {
LocalDate date = LocalDate.parse(dob, formatter);
return !date.isAfter(LocalDate.now());
}
return false;
public LocalDate getDate() {
return dob;
}

@Override
Expand All @@ -75,7 +68,7 @@ public boolean equals(Object other) {
}

Dob otherDob = (Dob) other;
return date.equals(otherDob.date);
return dob.equals(otherDob.dob);
}

@Override
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/seedu/address/storage/JsonAdaptedPerson.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -122,8 +122,8 @@ private Dob validateAndConvertDob() throws IllegalValueException {
if (!Dob.isValidDate(dob)) {
throw new IllegalValueException(Dob.MESSAGE_CONSTRAINTS_FORMAT);
}
if (!Dob.isValidDob(dob)) {
throw new IllegalValueException(Dob.MESSAGE_CONSTRAINTS_OCCURRENCE);
if (Dob.isFutureDate(dob)) {
throw new IllegalValueException(Dob.MESSAGE_CONSTRAINTS_FUTURE_OCCURRENCE);
}
return new Dob(dob);
}
Expand Down Expand Up @@ -158,7 +158,7 @@ private AdmissionDate validateAndConvertAdmissionDate() throws IllegalValueExcep
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);
}
return new AdmissionDate(admissionDate);
Expand Down Expand Up @@ -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");
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/ui/PersonCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 6730961

Please sign in to comment.