Skip to content

Commit

Permalink
Add Level 8. Dates and Times.
Browse files Browse the repository at this point in the history
Added logic in Deadline class to handle date recognition.
Added empty function in Task class for inheritance in Deadline class.
  • Loading branch information
amosting committed Aug 31, 2023
1 parent 9eadc16 commit dc10fae
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,48 @@
import java.time.LocalDate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.time.format.DateTimeFormatter;

public class Deadline extends Task {
protected String by;
protected LocalDate dateBy;

public Deadline(String description, String by) {
super(description);
this.by = by;
}

@Override
public void stringToDate() {
Pattern datePattern = Pattern.compile(
"^((2000|2400|2800|(19|2[0-9])(0[48]|[2468][048]|[13579][26]))-02-29)$"
+ "|^(((19|2[0-9])[0-9]{2})-02-(0[1-9]|1[0-9]|2[0-8]))$"
+ "|^(((19|2[0-9])[0-9]{2})-(0[13578]|10|12)-(0[1-9]|[12][0-9]|3[01]))$"
+ "|^(((19|2[0-9])[0-9]{2})-(0[469]|11)-(0[1-9]|[12][0-9]|30))$");
Matcher dateTimeMatcher = datePattern.matcher(this.by);

if (!dateTimeMatcher.matches()) { // if datetime doesn't match, do nothing
this.dateBy = null;
} else {
Pattern dateTimePattern2 = Pattern.compile(
"(\\d{4})-(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[0-2])"); // YYYY-MM-DD
Matcher dateTimePattern2Matcher = dateTimePattern2.matcher(this.by);
dateTimePattern2Matcher.matches();
int day = Integer.parseInt(dateTimePattern2Matcher.group(3));
int month = Integer.parseInt(dateTimePattern2Matcher.group(2));
int year = Integer.parseInt(dateTimePattern2Matcher.group(1));
this.dateBy = LocalDate.of(year, month, day);
}
}

@Override
public String toString() {
return "[D]" + super.toString() + " (by: " + by + ")";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM d yyyy");
if (dateBy == null) {
return "[D]" + super.toString() + " (by: " + by + ")";
} else {
String formattedDate = this.dateBy.format(formatter);
return "[D]" + super.toString() + " (by: " + formattedDate + ")";
}
}
}
1 change: 1 addition & 0 deletions src/main/java/Jarvis.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ public static void main(String[] args) {
String taskDescription = deadlineMatcher.group(2);
String by = deadlineMatcher.group(3);
newTask = new Deadline(taskDescription, by);
newTask.stringToDate();

} else { // if "event" is entered
String taskDescription = eventMatcher.group(2);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public void setDone(boolean done) {
this.isDone = done;
}

public void stringToDate() {}

public String toString() { // generates the string of marking and task
String marking = "";
if (this.isDone) {
Expand Down

0 comments on commit dc10fae

Please sign in to comment.