Skip to content

Commit

Permalink
Level-8
Browse files Browse the repository at this point in the history
  • Loading branch information
jellywaiyan committed Sep 3, 2023
1 parent a6a9d84 commit 7196ad9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 12 deletions.
13 changes: 4 additions & 9 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,16 @@ public String toString() {
String outputDate = deadlineDate.format(DateTimeFormatter.ofPattern("MMM dd yyyy"));
return "[D]" + super.toString() + "(by: " + outputDate + ")";
} else if (deadLineDateAndTime != null) {
String outputDate = deadLineDateAndTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy HH:mm"));
return "[D]" + super.toString() + "(by: " + outputDate + ")";
String outputDateTime = deadLineDateAndTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy HH:mm"));
return "[D]" + super.toString() + "(by: " + outputDateTime + ")";
} else {
return "[D]" + super.toString() + "(by: " + by + ")";
}
}
@Override
public String writeToFile() {
String printedStuff = "D | " + (getIsDone() ? "1" : "0") + " | " + getDescription() + " | ";
if (deadlineDate != null) {
return printedStuff + this.deadlineDate;
} else if (deadLineDateAndTime != null) {
return printedStuff + this.deadLineDateAndTime;
} else {
String printedStuff = "D | " + (getIsDone() ? "1" : "0") + " | " + getDescription() + "| ";
return printedStuff + this.by;
}
}

protected LocalDate parseDate(String date) {
Expand All @@ -65,6 +59,7 @@ protected LocalDateTime parseDateTime(String date) {
formats.add(DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm"));
formats.add(DateTimeFormatter.ofPattern("dd/MM/yyyy HHmm"));
formats.add(DateTimeFormatter.ofPattern("d/MM/yyyy HHmm"));
formats.add(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm"));

for (int i = 0; i < formats.size(); i++) {
try {
Expand Down
60 changes: 58 additions & 2 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;

public class Event extends Task {

Expand All @@ -14,16 +18,68 @@ public class Event extends Task {

public Event(String description, String from, String to) {
super(description);
this.fromDate = parseDate(from);
this.toDate = parseDate(to);
this.fromDateTime = parseDateTime(from);
this.toDateTime = parseDateTime(to);
this.from = from;
this.to = to;
}

@Override
public String toString() {
return "[E]" + super.toString() + "(from: " + from + " to: " + to + ")";
String fromWhen = from;
String toWhen = to;

if (fromDate != null) {
fromWhen = fromDate.format(DateTimeFormatter.ofPattern("MMM dd yyyy"));
} else if (fromDateTime != null) {
fromWhen = fromDateTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy HH:mm"));
}

if (toDate != null) {
toWhen = toDate.format(DateTimeFormatter.ofPattern("MMM dd yyyy"));
} else if (toDateTime != null) {
toWhen = toDateTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy HH:mm"));
}
return "[E]" + super.toString() + "(from: " + fromWhen + " to: " + toWhen + ")";
}
@Override
public String writeToFile() {
return "E | " + (getIsDone() ? "1" : "0") + " | " + getDescription() + " | " + from + " to " + to;
return "E | " + (getIsDone() ? "1" : "0") + " | " + getDescription() + "| " + from + " to " + to;
}

protected LocalDate parseDate(String date) {
List<DateTimeFormatter> formats = new ArrayList<>();
formats.add(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
formats.add(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
formats.add(DateTimeFormatter.ofPattern("d/M/yyyy"));

for (int i = 0; i < formats.size(); i++) {
try {
return LocalDate.parse(date, formats.get(i));
} catch (DateTimeParseException e) {

}
}
return null;
}

protected LocalDateTime parseDateTime(String date) {
List<DateTimeFormatter> formats = new ArrayList<>();
formats.add(DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm"));
formats.add(DateTimeFormatter.ofPattern("dd/MM/yyyy HHmm"));
formats.add(DateTimeFormatter.ofPattern("d/MM/yyyy HHmm"));
formats.add(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm"));

for (int i = 0; i < formats.size(); i++) {
try {
return LocalDateTime.parse(date, formats.get(i));
} catch (DateTimeParseException e) {

}
}

return null;
}
}
3 changes: 2 additions & 1 deletion taskData/jelly.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
D | 0 | do this | 2020-02-12T18:00
E | 0 | do this | 20/12/2020 1900 to 21/12/2020 1001
D | 1 | finish this | 1/02/1990 0001

0 comments on commit 7196ad9

Please sign in to comment.