Skip to content

Commit b21cf84

Browse files
weixue123weixue123
authored andcommitted
Added functionality to convert input date time strings to LocalDateTime objects
1 parent 37754d1 commit b21cf84

7 files changed

Lines changed: 90 additions & 39 deletions

File tree

data/duke.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
T | 1 | CS2107 Revision |
2-
D | 1 | CS2103 Post-Lecture Quiz | 5 Feb 2021 2:00 AM
3-
E | 0 | BT4013 Nano-Quiz 4 | 8 Feb 2021 10:00 AM to 10:30 AM
2+
D | 0 | CS2103 Post-Lecture Quiz | 2020-02-12 02:00
3+
E | 0 | BT4013 Nano-Quiz 4 | 2020-02-15 10:00
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package CustomExceptions;
2+
3+
public class DateTimeFormatNotRecognizedException extends Exception {
4+
5+
public DateTimeFormatNotRecognizedException(String dateString) {
6+
super("I can't recognize '" + dateString + "' as a date. Please follow the 'YYYY-MM-DD HH:mm' format :P");
7+
}
8+
}

src/main/java/Duke.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Tasks.*;
22

3+
import java.time.LocalDateTime;
34
import java.util.Scanner;
45

56
public class Duke {
@@ -22,8 +23,8 @@ public static void main(String[] args) {
2223

2324
String action = handler.getAction();
2425
String description = handler.getDescription();
25-
String by = handler.getBy();
26-
String at = handler.getAt();
26+
LocalDateTime by = handler.getBy();
27+
LocalDateTime at = handler.getAt();
2728

2829
switch (action) {
2930
case "bye":

src/main/java/InputHandler.java

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import CustomExceptions.*;
22

3+
import java.time.LocalDate;
4+
import java.time.LocalDateTime;
5+
import java.time.LocalTime;
6+
import java.time.format.DateTimeFormatter;
7+
import java.time.format.DateTimeParseException;
38
import java.util.ArrayList;
49
import java.util.Arrays;
510

611
public class InputHandler {
712
private final String input;
8-
private final ArrayList<String> validActions
13+
private static final ArrayList<String> validActions
914
= new ArrayList<>(Arrays.asList("todo", "deadline", "event", "done", "delete", "list", "bye"));
1015

11-
1216
public InputHandler(String input) {
1317
this.input = input;
1418
}
@@ -37,20 +41,26 @@ public String getDescription() {
3741
}
3842
}
3943

40-
public String getBy() {
44+
public LocalDateTime getBy() {
45+
return convertToDateTime(this.getByString());
46+
}
47+
48+
public LocalDateTime getAt() {
49+
return convertToDateTime(this.getAtString());
50+
}
51+
52+
private String getByString() {
4153
String action = this.getAction();
4254
String remainingTokens = this.getRemainingTokens();
43-
4455
if (action.equals("deadline") && remainingTokens.contains("/by")) {
4556
return remainingTokens.split("/by", 2)[1].trim();
4657
}
4758
return "";
4859
}
4960

50-
public String getAt() {
61+
private String getAtString() {
5162
String action = this.getAction();
5263
String remainingTokens = this.getRemainingTokens();
53-
5464
if (action.equals("event") && remainingTokens.contains("/at")) {
5565
return remainingTokens.split("/at", 2)[1].trim();
5666
}
@@ -60,9 +70,11 @@ public String getAt() {
6070
public boolean inputsAreValid() {
6171
String action = this.getAction();
6272
String description = this.getDescription();
73+
String byString = this.getByString();
74+
String atString = this.getAtString();
6375

6476
try {
65-
if (!this.validActions.contains(action)) {
77+
if (!validActions.contains(action)) {
6678
throw new InvalidActionException(action);
6779
}
6880

@@ -74,19 +86,28 @@ public boolean inputsAreValid() {
7486
throw new InvalidTaskNumberException();
7587
}
7688

77-
if (action.equals("deadline") && this.getBy().length() == 0) {
89+
if (action.equals("deadline") && byString.length() == 0) {
7890
throw new MissingDeadlineException();
7991
}
8092

81-
if (action.equals("event") && this.getAt().length() == 0) {
93+
if (action.equals("event") && atString.length() == 0) {
8294
throw new MissingEventTimeException();
8395
}
8496

97+
if (action.equals("deadline") && null == convertToDateTime(byString)) {
98+
throw new DateTimeFormatNotRecognizedException(byString);
99+
}
100+
101+
if (action.equals("event") && null == convertToDateTime(atString)) {
102+
throw new DateTimeFormatNotRecognizedException(atString);
103+
}
104+
85105
} catch (MissingDescriptionException
86106
| InvalidActionException
87107
| InvalidTaskNumberException
88108
| MissingDeadlineException
89-
| MissingEventTimeException e) {
109+
| MissingEventTimeException
110+
| DateTimeFormatNotRecognizedException e) {
90111
System.out.println(e.getMessage());
91112
return false;
92113
}
@@ -107,4 +128,25 @@ private static boolean isInteger(String str) {
107128

108129
return true;
109130
}
131+
132+
public static LocalDateTime convertToDateTime(String dateTimeString) {
133+
LocalDateTime dateTime;
134+
135+
try {
136+
dateTime = LocalDateTime.parse(dateTimeString, DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm"));
137+
} catch (DateTimeParseException e) {
138+
dateTime = null;
139+
}
140+
141+
if (null != dateTime) {
142+
return dateTime;
143+
}
144+
145+
try {
146+
LocalDate date = LocalDate.parse(dateTimeString, DateTimeFormatter.ofPattern("uuuu-MM-dd"));
147+
return LocalDateTime.of(date, LocalTime.MIDNIGHT);
148+
} catch (DateTimeParseException e) {
149+
return null;
150+
}
151+
}
110152
}

src/main/java/Loader.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.FileNotFoundException;
55
import java.io.FileWriter;
66
import java.io.IOException;
7+
import java.time.LocalDateTime;
78
import java.util.Scanner;
89

910
public class Loader {
@@ -47,15 +48,16 @@ private Task createTask(String taskDetails) {
4748
String taskType = taskDetailsArray[0].trim();
4849
String done = taskDetailsArray[1].trim();
4950
String description = taskDetailsArray[2].trim();
50-
String time = taskDetailsArray[3].trim();
51+
String dateTimeString = taskDetailsArray[3].trim();
52+
LocalDateTime dateTime = InputHandler.convertToDateTime(dateTimeString);
5153

5254
Task newTask;
5355
if (taskType.equals("T")) {
5456
newTask = new ToDo(description);
5557
} else if (taskType.equals("D")) {
56-
newTask = new Deadline(description, time);
58+
newTask = new Deadline(description, dateTime);
5759
} else {
58-
newTask = new Event(description, time);
60+
newTask = new Event(description, dateTime);
5961
}
6062

6163
if (done.equals("1")) {
@@ -66,22 +68,22 @@ private Task createTask(String taskDetails) {
6668
}
6769

6870
private String convertToSavableString(Task task) {
69-
String time = "";
71+
String dateTimeString = "";
7072
String taskType;
7173

7274
if (task instanceof Deadline) {
7375
taskType = "D";
74-
time = ((Deadline) task).getBy();
76+
dateTimeString = ((Deadline) task).getByString();
7577
} else if (task instanceof Event) {
7678
taskType = "E";
77-
time = ((Event) task).getAt();
79+
dateTimeString = ((Event) task).getAtString();
7880
} else {
7981
taskType = "T";
8082
}
8183

8284
String done = task.isDone() ? "1" : "0";
8385
String description = task.getDescription();
8486

85-
return taskType + " | " + done + " | " + description + " | " + time + "\n";
87+
return taskType + " | " + done + " | " + description + " | " + dateTimeString + "\n";
8688
}
8789
}

src/main/java/Tasks/Deadline.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
package Tasks;
22

3+
import java.time.LocalDateTime;
4+
import java.time.format.DateTimeFormatter;
5+
36
public class Deadline extends Task {
4-
private final String by;
7+
private final LocalDateTime by;
58

6-
public Deadline(String description, String by) {
9+
public Deadline(String description, LocalDateTime by) {
710
super(description);
811
this.by = by;
912
}
1013

11-
private String getByString() {
12-
return "(by: " + this.by + ")";
13-
}
14-
15-
public String getBy() {
16-
return this.by;
14+
public String getByString() {
15+
return this.by.format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm"));
1716
}
1817

1918
public String getStatusString() {
20-
return "[D]" + super.getStatusString() + " " + this.getByString();
19+
return "[D]" + super.getStatusString() + " (by: " + this.getByString() + ")";
2120
}
2221
}

src/main/java/Tasks/Event.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
package Tasks;
22

3+
import java.time.LocalDateTime;
4+
import java.time.format.DateTimeFormatter;
5+
36
public class Event extends Task {
4-
private final String at;
7+
private final LocalDateTime at;
58

6-
public Event(String description, String at) {
9+
public Event(String description, LocalDateTime at) {
710
super(description);
811
this.at = at;
912
}
1013

11-
private String getAtString() {
12-
return "(at: " + this.at + ")";
13-
}
14-
15-
public String getAt() {
16-
return this.at;
14+
public String getAtString() {
15+
return this.at.format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm"));
1716
}
1817

1918
public String getStatusString() {
20-
return "[E]" + super.getStatusString() + " " + this.getAtString();
19+
return "[E]" + super.getStatusString() + " (at: " + this.getAtString() + ")";
2120
}
2221
}

0 commit comments

Comments
 (0)