Skip to content

Commit f275db5

Browse files
committed
Added B-DoWithinPeriodTasks with unit testing
1 parent debd119 commit f275db5

File tree

5 files changed

+84
-10
lines changed

5 files changed

+84
-10
lines changed

src/main/java/command/Parser.java

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import common.DukeException;
44
import common.TaskList;
5-
import task.Deadline;
6-
import task.Event;
7-
import task.Task;
8-
import task.Todo;
5+
import task.*;
96
import ui.Ui;
107

118
import java.text.ParseException;
@@ -62,19 +59,20 @@ public static boolean parse(String input, TaskList tasklist, Ui ui, Storage stor
6259
} else if (isFind(input)) {
6360
processFind(input, tasklist, ui);
6461

62+
} else if (isWithinPeriodTask(input)) {
63+
processWithin(input, tasklist, ui);
64+
storage.save(tasklist.returnArrayList());
6565
} else {
6666
throw new DukeException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
67-
6867
}
69-
7068
} catch (DukeException e) {
7169
ui.exceptionMessage(e.getMessage());
72-
return true;
7370
}
74-
7571
return false;
7672
}
7773

74+
75+
7876
/**
7977
* Processes the find command and outputs a list of tasks containing the word.
8078
* @param input Input from the user.
@@ -198,6 +196,33 @@ private static void processEvent(String input, TaskList tasklist, Ui ui) {
198196
}
199197
}
200198

199+
/**
200+
* Processes the within command and adds a withinPeriodTask to the user's Tasklist.
201+
* @param input Input from the user.
202+
* @param tasklist Tasklist of the user.
203+
* @param ui Ui that interacts with the user.
204+
*/
205+
private static void processWithin(String input, TaskList tasklist, Ui ui) {
206+
try {
207+
String[] splitspace = input.split(" ", 2);
208+
String[] splitslash = splitspace[1].split("/", 2);
209+
String taskDescription = splitslash[0];
210+
String[] splittime = splitslash[1].split(" ", 2);
211+
String[] splitand = splittime[1].split("and ", 2);
212+
String taskstart = splitand[0];
213+
String taskend = splitand[1];
214+
Date formattedtimestart = dataformat.parse(taskstart);
215+
Date formattedtimeend = dataformat.parse(taskend);
216+
WithinPeriodTask withinPeriodTask = new WithinPeriodTask(taskDescription, dataformat.format(formattedtimestart), dataformat.format(formattedtimeend));
217+
tasklist.addTask(withinPeriodTask);
218+
ui.printAddedMessage(withinPeriodTask, tasklist);
219+
} catch (ArrayIndexOutOfBoundsException e) {
220+
ui.exceptionMessage(" ☹ OOPS!!! The description of a withinPeriodTask cannot be empty.");
221+
} catch (ParseException e) {
222+
ui.exceptionMessage(" ☹ OOPS!!! Format of time is wrong.");
223+
}
224+
}
225+
201226

202227
private static boolean isBye(String input) {
203228
return input.equals("bye");
@@ -230,4 +255,8 @@ private static boolean isDelete(String input) {
230255
private static boolean isFind(String input) {
231256
return input.startsWith("find");
232257
}
258+
259+
private static boolean isWithinPeriodTask(String input) {
260+
return input.startsWith("within");
261+
}
233262
}

src/main/java/task/Deadline.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class Deadline extends Task implements Serializable {
1212
/**
1313
* Creates a Deadline instance and initialises the required attributes.
1414
* @param description Description of the deadline.
15-
* @param by Deadline of the task.
15+
* @param by Deadline of the task in format "dd/MM/yyyy HHmm".
1616
*/
1717
public Deadline(String description, String by) {
1818
super(description);

src/main/java/task/Event.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class Event extends Task implements Serializable {
1111
/**
1212
* Creates an Event instance and initialises the required attributes.
1313
* @param description Description of the event.
14-
* @param at Time of the event.
14+
* @param at Time of the event in format "dd/MM/yyyy HHmm".
1515
*/
1616
public Event(String description, String at) {
1717
super(description);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package task;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* Task containing information of a Task to be done within a period.
7+
*/
8+
public class WithinPeriodTask extends Task implements Serializable {
9+
protected String start;
10+
protected String end;
11+
12+
/**
13+
* Creates a WithinPeriodTask instance and initialises the required attributes.
14+
* @param description Description of the task.
15+
* @param start Start time of the task in format "dd/MM/yyyy HHmm".
16+
* @param end End time of the task in format "dd/MM/yyyy HHmm".
17+
*/
18+
public WithinPeriodTask(String description, String start, String end) {
19+
super(description);
20+
this.start = start;
21+
this.end = end;
22+
}
23+
24+
/**
25+
* Returns a string status of the WithinPeriodTask task.
26+
* @return The task's status icon, description, start and end times.
27+
*/
28+
@Override
29+
public String giveTask() {
30+
return "[W]" + super.giveTask() + "(between: " + start + " and " + end + ")";
31+
}
32+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import org.junit.jupiter.api.Test;
2+
import task.WithinPeriodTask;
3+
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
public class WithinPeriodTaskTest {
7+
8+
@Test
9+
public void WithinPeriodTaskTest() {
10+
WithinPeriodTask withinPeriodTask = new WithinPeriodTask("collect certificate ", "02/12/2019 1800", "05/12/2019 1500" );
11+
assertEquals(withinPeriodTask.giveTask(), "[W][✘] collect certificate (between: 02/12/2019 1800 and 05/12/2019 1500)");
12+
}
13+
}

0 commit comments

Comments
 (0)