Skip to content

Commit

Permalink
Fix-dates-and-display-messages
Browse files Browse the repository at this point in the history
  • Loading branch information
solomonng2001 committed Apr 4, 2024
1 parent 9b1b7a3 commit c6f26cc
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 14 deletions.
16 changes: 14 additions & 2 deletions src/main/java/seedu/address/commons/util/DateTimeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,24 @@ public static boolean isParsableDateTimeString(String test) {
return false;
}

LocalDateTime dateTime;

try {
LocalDateTime.parse(test, DateTimeFormatter.ofPattern(DATE_FORMAT));
return true;
dateTime = LocalDateTime.parse(test, DateTimeFormatter.ofPattern(DATE_FORMAT));
} catch (DateTimeParseException e) {
return false;
}

// Date is not parsable if truncation needed, eg. 29 Feb on non leap years will be truncated by parser
if (!DateTimeUtil.parseDateToString(dateTime).equals(test)) {
return false;
}

if (dateTime == null) {
return false;
}

return true;
}

/**
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/seedu/address/commons/util/DateUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
* Helper functions for handling dates and times.
*/
public class DateUtil {
public static final String MESSAGE_CONSTRAINTS = "should be in the format YYYY-MM-DD and must be an actual date";
public static final String MESSAGE_CONSTRAINTS =
"date should be in the format YYYY-MM-DD and must be an actual date.";
public static final String DATE_FORMAT = "yyyy-MM-dd";
public static final String VALIDATION_REGEX = "\\d{4}-\\d{2}-\\d{2}";

Expand Down Expand Up @@ -37,12 +38,24 @@ public static boolean isParsableDateString(String test) {
return false;
}

LocalDate date;

try {
LocalDate.parse(test, DateTimeFormatter.ofPattern(DATE_FORMAT));
return true;
date = LocalDate.parse(test, DateTimeFormatter.ofPattern(DATE_FORMAT));
} catch (DateTimeParseException e) {
return false;
}

// Date is not parsable if truncation needed, eg. 29 Feb on non leap years will be truncated by parser
if (!DateUtil.parseDateToString(date).equals(test)) {
return false;
}

if (date == null) {
return false;
}

return true;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Messages {
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_LASTMET_FUTURE = "You cannot meet someone from the future.";
public static final String MESSAGE_SCHEDULE_PAST = "You cannot schedule an appointment in the past.";
public static final String MESSAGE_SCHEDULE_DONE = "This schedule has already been completed.";
public static final String MESSAGE_SCHEDULE_DONE = "This client has no unmarked scheduled appointments.";
public static final String MESSAGE_DUPLICATE_FIELDS =
"Multiple values specified for the following single-valued field(s): ";
public static final String MESSAGE_DUPLICATE_POLICY =
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/logic/commands/MarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
public class MarkCommand extends Command {
public static final String COMMAND_WORD = "mark";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": marks a schedule as done. "
public static final String MESSAGE_USAGE = COMMAND_WORD + ": marks a scheduled appointment with a client as done. "
+ "Parameters: "
+ "INDEX "
+ "INDEX (client's index: must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " "
+ "1 ";

Expand Down
6 changes: 2 additions & 4 deletions src/main/java/seedu/address/logic/commands/SetCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ public class SetCommand extends Command {
public static final String COMMAND_WORD = "set";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": sets the LastMet overdue time period. "
+ "Parameters: "
+ "NUMBER_OF_DAYS "
+ "Example: " + COMMAND_WORD + " "
+ "90.\n "
+ "Parameters: NUMBER_OF_DAYS\n"
+ "Example: " + COMMAND_WORD + " 90.\n"
+ "NUMBER_OF_DAYS must be an integer and be at least 0.";

public static final String MESSAGE_SUCCESS = "LastMet Overdue time period has been set to %1$s days.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Parses input arguments and creates a new LastMetCommand object
*/
public class LastMetCommandParser {
public static final String DATE_MESSAGE_CONSTRAINTS = DateUtil.getMessageConstraintsForDateType("LastMet");

/**
* Parses the given {@code String} of arguments in the context of the LastMetCommand
Expand All @@ -46,6 +47,9 @@ public LastMetCommand parse(String args) throws ParseException {

String lastMetString = argMultimap.getValue(PREFIX_LASTMET).orElse("");

if (!DateUtil.isValidDateString(lastMetString)) {
throw new ParseException(DATE_MESSAGE_CONSTRAINTS);
}
try {
LastMet lastMet = convertStringToLastMet(lastMetString);
return new LastMetCommand(index, lastMet);
Expand All @@ -62,7 +66,10 @@ private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Pre
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}

private LastMet convertStringToLastMet(String input) {
private LastMet convertStringToLastMet(String input) throws DateTimeParseException {
if (!DateUtil.isValidDateString(input)) {
throw new DateTimeParseException(DATE_MESSAGE_CONSTRAINTS, input, 0);
}
LocalDate formattedInput = DateUtil.parseStringToDate(input);
return new LastMet(formattedInput);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import seedu.address.commons.core.index.Index;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.commons.util.DateTimeUtil;
import seedu.address.commons.util.DateUtil;
import seedu.address.logic.commands.ScheduleCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Schedule;
Expand All @@ -19,6 +20,7 @@
* Parses input arguments and creates a new ScheduleCommand object
*/
public class ScheduleCommandParser {
public static final String DATE_MESSAGE_CONSTRAINTS = DateUtil.getMessageConstraintsForDateType("Schedule");

/**
* Parses the given {@code String} of arguments in the context of the ScheduleCommand
Expand Down Expand Up @@ -46,6 +48,9 @@ public ScheduleCommand parse(String args) throws ParseException {

String scheduleString = argMultimap.getValue(PREFIX_SCHEDULE).orElse("");

if (!DateTimeUtil.isValidDateTimeString(scheduleString)) {
throw new ParseException(DATE_MESSAGE_CONSTRAINTS);
}
try {
Schedule schedule = convertStringToSchedule(scheduleString);
return new ScheduleCommand(index, schedule);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/model/person/Birthday.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
* Guarantees: immutable; is valid as declared in {@link #isValidBirthday(String)}
*/
public class Birthday implements Comparable<Birthday> {
public static final String MESSAGE_CONSTRAINTS = DateUtil.getMessageConstraintsForDateType("Birthday");
public static final String MESSAGE_CONSTRAINTS = DateUtil.getMessageConstraintsForDateType("Birthday")
+ " Birthday should not be in the future.";
public final LocalDate date;

/**
Expand Down

0 comments on commit c6f26cc

Please sign in to comment.