Skip to content

Commit 881efae

Browse files
committed
Merge branch 'branch-Level-8'
2 parents b675cbc + a1c3f25 commit 881efae

File tree

6 files changed

+108
-33
lines changed

6 files changed

+108
-33
lines changed

src/main/java/Deadline.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1+
import java.time.LocalDateTime;
2+
import java.time.format.DateTimeFormatter;
3+
14
/**
25
* Encapsulate information of task with deadline.
36
*/
47
public class Deadline extends Task{
5-
protected String by;
68

79
/**
810
* Normal constructor.
911
*/
10-
public Deadline(String description, String by) {
11-
super(description);
12-
this.by = by;
12+
public Deadline(String description, LocalDateTime by) {
13+
super(description, by);
1314
}
1415

1516
/**
1617
* Returns the representative string for saving in data file.
1718
*/
1819
@Override
1920
public String toFileFormat() {
21+
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm");
2022
int status = super.isDone ? 1 : 0;
21-
return "E" + " | " + status + " | " + super.description + " | " + by;
23+
return "D" + " | " + status + " | " + super.description + " | " + dtf.format(super.dateTime);
2224
}
2325

2426
@Override
2527
public String toString() {
26-
return "[D]" + super.toString() + " (by: " + by + ")";
28+
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
29+
return "[D]" + super.toString() + " (by: " + dtf.format(super.dateTime) + ")";
2730
}
2831
}

src/main/java/Event.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1+
import java.time.LocalDateTime;
2+
import java.time.format.DateTimeFormatter;
3+
14
/**
25
* Encapsulate information of event task.
36
*/
47
public class Event extends Task {
5-
protected String at;
68

79
/**
810
* Normal constructor.
911
*/
10-
public Event(String description, String at) {
11-
super(description);
12-
this.at = at;
12+
public Event(String description, LocalDateTime at) {
13+
super(description, at);
1314
}
1415

1516
/**
1617
* Returns the representative string for saving in data file.
1718
*/
1819
@Override
1920
public String toFileFormat() {
21+
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm");
2022
int status = super.isDone ? 1 : 0;
21-
return "E" + " | " + status + " | " + super.description + " | " + at;
23+
return "E" + " | " + status + " | " + super.description + " | " + dtf.format(super.dateTime);
2224
}
2325

2426
@Override
2527
public String toString() {
26-
return "[E]" + super.toString() + " (at: " + at + ")";
28+
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
29+
return "[E]" + super.toString() + " (at: " + dtf.format(super.dateTime) + ")";
2730
}
2831
}

src/main/java/Spike.java

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import java.io.FileNotFoundException;
33
import java.io.FileWriter;
44
import 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;
88
import java.util.ArrayList;
99
import 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
}

src/main/java/Task.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
1+
import java.time.LocalDateTime;
2+
13
/**
24
* Encapsulate information of a user task.
35
*/
46
public class Task {
57
protected String description;
68
protected boolean isDone;
9+
protected LocalDateTime dateTime;
710

811
/**
912
* Constructor for Task objects.
1013
*/
1114
public Task(String description) {
1215
this.description = description;
1316
this.isDone = false;
17+
this.dateTime = LocalDateTime.parse("1700-01-01T00:00:00");
18+
}
19+
20+
public Task(String description, LocalDateTime dateTime) {
21+
this.description = description;
22+
this.isDone = false;
23+
this.dateTime = dateTime;
1424
}
1525

1626
/**
@@ -21,14 +31,14 @@ public String getStatusIcon() {
2131
}
2232

2333
/**
24-
* Change status of task to be 'Done'.
34+
* Changes status of task to be 'Done'.
2535
*/
2636
public void markAsDone() {
2737
this.isDone = true;
2838
}
2939

3040
/**
31-
* Change status of task to be 'Not done'.
41+
* Changes status of task to be 'Not done'.
3242
*/
3343
public void markAsNotDone() {
3444
this.isDone = false;
@@ -43,6 +53,14 @@ public String toFileFormat() {
4353

4454
/**
4555
* Format the string representation of Task objects.
56+
* Gets the date and time for tasks with such information.
57+
*/
58+
public LocalDateTime getDateTime() {
59+
return this.dateTime;
60+
}
61+
62+
/**
63+
* Formats the string representation of Task objects.
4664
*/
4765
@Override
4866
public String toString() {

text-ui-test/runtest.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ IF ERRORLEVEL 1 (
1515
REM no error here, errorlevel == 0
1616

1717
REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT
18-
java -classpath ..\bin Duke < input.txt > ACTUAL.TXT
18+
java -classpath ..\bin Spike < input.txt > ACTUAL.TXT
1919

2020
REM compare the output to the expected output
2121
FC ACTUAL.TXT EXPECTED.TXT

text-ui-test/runtest.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ then
2020
fi
2121

2222
# run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT
23-
java -classpath ../bin Duke < input.txt > ACTUAL.TXT
23+
java -classpath ../bin Spike < input.txt > ACTUAL.TXT
2424

2525
# convert to UNIX format
2626
cp EXPECTED.TXT EXPECTED-UNIX.TXT

0 commit comments

Comments
 (0)