11import 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 ;
38import java .util .ArrayList ;
49import java .util .Arrays ;
510
611public 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}
0 commit comments