22import java .io .FileNotFoundException ;
33import java .io .FileWriter ;
44import java .io .IOException ;
5- import java .nio . file . Files ;
6- import java .nio . file . Path ;
7- import java .nio . file . Paths ;
5+ import java .time . LocalDateTime ;
6+ import java .time . format . DateTimeFormatter ;
7+ import java .time . format . DateTimeParseException ;
88import java .util .ArrayList ;
99import java .util .Scanner ;
1010
@@ -33,7 +33,15 @@ public String getCommand() {
3333 public static void processCommand (Command c , String command , String [] commandWords ) {
3434 switch (c ) {
3535 case LIST :
36- printList ();
36+ try {
37+ if (commandWords .length >= 2 ) {
38+ printListByDate (command );
39+ } else {
40+ printList ();
41+ }
42+ } catch (SpikeException d ) {
43+ printMsg (d .toString ());
44+ }
3745 break ;
3846 case MARK :
3947 try {
@@ -97,15 +105,16 @@ public static void main(String[] args) {
97105 String currLine = fileRead .nextLine ();
98106 String [] info = currLine .split (" \\ | " );
99107 Task task = null ;
108+ DateTimeFormatter dtf = DateTimeFormatter .ofPattern ("yyyy-MM-dd HHmm" );
100109 switch (info [0 ]) {
101110 case "T" :
102111 task = new ToDo (info [2 ]);
103112 break ;
104113 case "E" :
105- task = new Event (info [2 ], info [3 ]);
114+ task = new Event (info [2 ], LocalDateTime . parse ( info [3 ], dtf ) );
106115 break ;
107116 case "D" :
108- task = new Deadline (info [2 ], info [3 ]);
117+ task = new Deadline (info [2 ], LocalDateTime . parse ( info [3 ], dtf ) );
109118 break ;
110119 default :
111120 break ;
@@ -189,6 +198,7 @@ public static void saveChanges() {
189198 * Adds task into the list and prints.
190199 */
191200 public static void addTask (Command c , String command , String [] commandWords ) throws SpikeException {
201+ DateTimeFormatter dtf = DateTimeFormatter .ofPattern ("yyyy-MM-dd HHmm" );
192202 switch (c ) {
193203 case TODO :
194204 if (command .length () <= 5 ) {
@@ -204,26 +214,45 @@ public static void addTask(Command c, String command, String[] commandWords) thr
204214 || commandWords [1 ].equals ("/by" ) || command .indexOf ("/by" ) + 3 == command .length ()) {
205215 throw new SpikeException ("Deadline or task description missing." );
206216 }
207- Deadline newD = new Deadline (command .substring (command .indexOf ("deadline" ) + 9 ,
208- command .indexOf ("/by" ) - 1 ), command .substring (command .indexOf ("/by" ) + 4 ));
209- taskList .add (newD );
210- printAddedTask (newD );
217+ LocalDateTime deadlineT = parseDateTime (command .substring (command .indexOf ("/by" ) + 4 ), dtf );
218+ if (!(deadlineT == null )) {
219+ Deadline newD = new Deadline (command .substring (command .indexOf ("deadline" ) + 9 ,
220+ command .indexOf ("/by" ) - 1 ), deadlineT );
221+ taskList .add (newD );
222+ printAddedTask (newD );
223+ }
211224 break ;
212225 case EVENT :
213226 if (commandWords .length <= 2 || command .indexOf ("/at" ) == -1
214227 || commandWords [1 ].equals ("/at" ) || command .indexOf ("/at" ) + 3 == command .length ()) {
215228 throw new SpikeException ("Event time or event description missing." );
216229 }
217- Event newE = new Event (command .substring (command .indexOf ("event" ) + 6 ,
218- command .indexOf ("/at" ) - 1 ), command .substring (command .indexOf ("/at" ) + 4 ));
219- taskList .add (newE );
220- printAddedTask (newE );
230+ LocalDateTime eventT = parseDateTime (command .substring (command .indexOf ("/at" ) + 4 ), dtf );
231+ if (!(eventT == null )) {
232+ Event newE = new Event (command .substring (command .indexOf ("event" ) + 6 ,
233+ command .indexOf ("/at" ) - 1 ), eventT );
234+ taskList .add (newE );
235+ printAddedTask (newE );
236+ }
221237 break ;
222238 default :
223239 break ;
224240 }
225241 }
226242
243+ /**
244+ * Parses date and time input by user and returns valid LocalDateTime object
245+ */
246+ public static LocalDateTime parseDateTime (String s , DateTimeFormatter dtf ) {
247+ try {
248+ LocalDateTime dateTime = LocalDateTime .parse (s , dtf );
249+ return dateTime ;
250+ } catch (DateTimeParseException e ) {
251+ printMsg ("Please enter a valid date in the format yyyy-MM-dd HHmm" );
252+ return null ;
253+ }
254+ }
255+
227256 /**
228257 * Marks the task as done.
229258 */
@@ -307,10 +336,32 @@ public static void printList() {
307336 int i = 1 ;
308337 String result = "Here are the task(s) in your list:\n " ;
309338 for (Task task : taskList ) {
310- if (i == taskList .size ()) {
339+ result += i + "." + task ;
340+ if (i != taskList .size ()) {
341+ result += "\n " ;
342+ }
343+ i ++;
344+ }
345+ printMsg (result );
346+ }
347+
348+ /**
349+ * Prints deadlines/events occurring on the given date.
350+ */
351+ public static void printListByDate (String command ) throws SpikeException {
352+ DateTimeFormatter dtf = DateTimeFormatter .ofPattern ("yyyy-MM-dd HHmm" );
353+ LocalDateTime ldt = parseDateTime (command .substring (5 ), dtf );
354+ if (ldt == null ) {
355+ throw new SpikeException ("Kindly enter the date in the format yyyy-MM-dd 0000 to filter by date" );
356+ }
357+ int i = 1 ;
358+ String result = "Here are the task(s) in your list filtered by date:\n " ;
359+ for (Task task : taskList ) {
360+ if (task .getDateTime ().toLocalDate ().equals (ldt .toLocalDate ())) {
311361 result = result + i + "." + task ;
312- } else {
313- result = result + i + "." + task + "\n " ;
362+ if (i != taskList .size ()) {
363+ result += "\n " ;
364+ }
314365 }
315366 i ++;
316367 }
0 commit comments