Skip to content

Commit

Permalink
Merge pull request #25 from jskimdev/branch-Model
Browse files Browse the repository at this point in the history
Add dateofbirth class
  • Loading branch information
Vision-2000 authored Mar 14, 2024
2 parents c8e1655 + 65c02e6 commit bfe7903
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/main/java/seedu/address/model/person/DateOfBirth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package seedu.address.model.person;

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.
* Guarantees: immutable; is valid as declared in {@link #isValidDateOfBirth(String)}
*/
public class DateOfBirth {

public static final String MESSAGE_CONSTRAINTS =
"Date of birth should only contain numeric values in dd/MM/yyyy or yyyy-MM-dd format, and it should not "
+ "be left blank";

public static final DateTimeFormatter inputFormat1 = DateTimeFormatter.ofPattern("d/M/yyyy");
public static final DateTimeFormatter inputFormat2 = DateTimeFormatter.ofPattern("yyyy-M-d");
public static final DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("yyyy-M-d");
public final LocalDate dateOfBirth;

/**
* Constructs a {@code DateOfBirth}.
*
* @param dateOfBirth A valid date of birth.
*/
public DateOfBirth(String dateOfBirth) {
requireNonNull(dateOfBirth);
checkArgument(isValidDateOfBirth(dateOfBirth), MESSAGE_CONSTRAINTS);
this.dateOfBirth = parseDateOfBirth(dateOfBirth);
}

/**
* Returns true if a given string is a valid date of birth.
*/
public static boolean isValidDateOfBirth(String test) {
return parseDateOfBirth(test) == null ? false : true;
}

/**
* Parse String date of birth into LocalDate.
*
* @param dateOfBirthString date of birth input.
* @return date of birth in LocalDate type.
*/
public static LocalDate parseDateOfBirth(String dateOfBirthString) {
String cleanDateString = dateOfBirthString.strip();
DateTimeFormatter inputFormat = determineInputFormat(cleanDateString);
LocalDate dateOfBirth = null;

try {
dateOfBirth = LocalDate.parse(cleanDateString, inputFormat);
return dateOfBirth;
} catch (DateTimeParseException e) {
return null;
}
}

/**
* Determines in which format the date of birth is input.
*
* @return the input format of String date of birth.
*/
private static DateTimeFormatter determineInputFormat(String dateOfBirth) {
boolean isInputFromat1 = dateOfBirth.split("/").length > 1;
return isInputFromat1 ? inputFormat1 : inputFormat2;
}

@Override
public String toString() {
return dateOfBirth.format(outputFormat);
}
}

0 comments on commit bfe7903

Please sign in to comment.