From 7b4a3d91c9eaba65a2f9bccb4605070ed1f04271 Mon Sep 17 00:00:00 2001 From: Zhekai Date: Sun, 17 Mar 2024 13:03:06 +0800 Subject: [PATCH] Refactor Birthday parsing and fix inaccurate javadoc headers --- .../java/seedu/address/logic/parser/ParserUtil.java | 10 +++++++--- src/main/java/seedu/address/model/person/Birthday.java | 5 +++-- .../java/seedu/address/storage/JsonAdaptedPerson.java | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index bed1c35af31..dfce025ce43 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -125,10 +125,14 @@ public static Set parseTags(Collection tags) throws ParseException } /** - * Parses {@code Optional birthday} into a {@code Optional}. + * Parses {@code String birthday} into a {@code Birthday}. */ public static Birthday parseBirthday(String birthday) throws ParseException { - requireNonNull(birthday); - return new Birthday(birthday); + birthday = birthday == null ? "" : birthday; + String trimmedBirthday = birthday.trim(); + if (!Birthday.isValidBirthday(trimmedBirthday)) { + throw new ParseException(Birthday.BIRTHDAY_CONSTRAINTS); + } + return new Birthday(trimmedBirthday); } } diff --git a/src/main/java/seedu/address/model/person/Birthday.java b/src/main/java/seedu/address/model/person/Birthday.java index 5812189a48f..82552c9fdd1 100644 --- a/src/main/java/seedu/address/model/person/Birthday.java +++ b/src/main/java/seedu/address/model/person/Birthday.java @@ -1,5 +1,6 @@ package seedu.address.model.person; +import static java.util.Objects.requireNonNull; import static seedu.address.commons.util.AppUtil.checkArgument; import java.time.LocalDate; @@ -26,7 +27,7 @@ public class Birthday { * @param birthday A valid birthday, or an empty string. */ public Birthday(String birthday) { - birthday = birthday == null ? "" : birthday; + requireNonNull(birthday); if (birthday.isBlank()) { this.birthday = null; return; @@ -37,7 +38,7 @@ public Birthday(String birthday) { /** - * Returns true if a given string is a valid name. + * Returns true if a given string is a valid birthday. */ public static boolean isValidBirthday(String test) { if (test == null || test.isBlank()) { diff --git a/src/main/java/seedu/address/storage/JsonAdaptedPerson.java b/src/main/java/seedu/address/storage/JsonAdaptedPerson.java index 819aea276cc..98eb2e6b8b1 100644 --- a/src/main/java/seedu/address/storage/JsonAdaptedPerson.java +++ b/src/main/java/seedu/address/storage/JsonAdaptedPerson.java @@ -121,7 +121,7 @@ public Person toModelType() throws IllegalValueException { if (!Birthday.isValidBirthday(birthday)) { throw new IllegalValueException(Birthday.BIRTHDAY_CONSTRAINTS); } - final Birthday modelBirthday = new Birthday(birthday); + final Birthday modelBirthday = new Birthday(Optional.ofNullable(birthday).orElse("")); return new Person(modelName, modelPhone, modelEmail, modelAddress, modelRemark, modelTags, modelBirthday); }