Skip to content

Commit

Permalink
Change date time class to allow or disallow past detect
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Y-Yan committed Nov 14, 2023
1 parent fb7205f commit e97fd48
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/main/java/seedu/duke/commands/meal/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public AddCommand(List<String> arguments) throws Exception {
calories = Integer.parseInt(arguments.get(1));
category = arguments.get(2);
if (arguments.size() >= 4) {
time = new Date(arguments.get(3));
time = new Date(arguments.get(3), false);
} else {
time = Date.now();
}
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/seedu/duke/data/Date.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
public class Date {
private static DateTimeFormatter[] formatters = new DateTimeFormatter[] {
DateTimeFormatter.ofPattern("yyyy/M/d"),
DateTimeFormatter.ofPattern("yyyy/MM/dd"),
DateTimeFormatter.ofPattern("dd/MM/yyyy"),
DateTimeFormatter.ofPattern("d/M/yyyy"),
DateTimeFormatter.ofPattern("yyyy-M-d"),
DateTimeFormatter.ofPattern("d-M-yyyy"),
Expand All @@ -26,25 +28,31 @@ public class Date {
* @param rawData refers to the date String
* @throws IncorrectFormatException if failed to parse date string input
*/
public Date(String rawData) throws IncorrectFormatException {
setRawData(rawData);
public Date(String rawData, Boolean NotAllowPast) throws IncorrectFormatException {
setRawData(rawData, NotAllowPast);
}

/**
* The method is used to set up the date field of a Date object
* It contains the actual implementation to parse date information from a string
* @param rawData refers to a date string
* @param NotAllowPast if past is not allowed, for example for goal functions
* @throws IncorrectFormatException if failed to parse date string input
*/
public void setRawData(String rawData) throws IncorrectFormatException {
public void setRawData(String rawData, boolean NotAllowPast) throws IncorrectFormatException {
for (DateTimeFormatter formatter : formatters) {
try {
date = LocalDate.parse(rawData, formatter);
if (date.isBefore(LocalDate.now())) {
throw new IncorrectFormatException("Target Deadline has passed! ");
if (NotAllowPast) {
if (date.isBefore(LocalDate.now())) {
throw new IncorrectFormatException("Target Deadline has passed! ");
}
standardString = this.toString();
return;
} else {
standardString = this.toString();
return;
}
standardString = this.toString();
return;
} catch (IncorrectFormatException ide) {
throw new IncorrectFormatException("Target Deadline has passed! ");
} catch (Exception exception) {
Expand All @@ -60,7 +68,7 @@ public String toString() {
}

public static Date now() throws Exception {
Date now = new Date(LocalDate.now().format(toStringFormatter));
Date now = new Date(LocalDate.now().format(toStringFormatter), false);
return now;
}
}
2 changes: 1 addition & 1 deletion src/main/java/seedu/duke/goal/GoalList.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public Goal(int calories, String date) throws IncorrectFormatException {
}

private Date setGoalDateTime(String date) throws IncorrectFormatException {
return new Date(date);
return new Date(date, true);
}

public String toString() {
Expand Down

0 comments on commit e97fd48

Please sign in to comment.