Skip to content

Commit b112c3b

Browse files
authored
Merge pull request #4 from tensaida/B-DoWithinPeriodTasks
Add support for Do within a period tasks
2 parents b6f3bbe + 0123089 commit b112c3b

File tree

5 files changed

+115
-5
lines changed

5 files changed

+115
-5
lines changed

data/tasks.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
D;yo;X;2020-02-02
2-
T;andre;
3-
T;commit to github;
4-
T;haha;
5-
D;im done; ;1999-10-10
1+
W;tiffani are you watching me; ;2020-01-01;2020-02-01

src/main/java/drake/Parser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import drake.commands.MarkCommand;
1111
import drake.commands.TodoCommand;
1212
import drake.commands.UnmarkCommand;
13+
import drake.commands.WithinCommand;
1314

1415
/**
1516
* Command parser.
@@ -53,6 +54,8 @@ public static Command parse(String fullInput) throws UnknownCommandException,
5354
return new ByeCommand();
5455
case "find":
5556
return new FindCommand(fullInput);
57+
case "within":
58+
return new WithinCommand(fullInput);
5659
default:
5760
throw new UnknownCommandException();
5861
}

src/main/java/drake/Storage.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import drake.commands.CommandType;
1212
import drake.tasks.Deadline;
13+
import drake.tasks.DoWithinPeriod;
1314
import drake.tasks.Event;
1415
import drake.tasks.Task;
1516
import drake.tasks.Todo;
@@ -71,6 +72,13 @@ public List<Task> fileToList() throws UnknownCommandException {
7172
}
7273
list.add(event);
7374
break;
75+
case "W":
76+
DoWithinPeriod doWithinPeriod = new DoWithinPeriod(taskParts[1], taskParts[3], taskParts[4]);
77+
if (taskParts[2].equals("X")) {
78+
doWithinPeriod.markAsDone();
79+
}
80+
list.add(doWithinPeriod);
81+
break;
7482
default:
7583
throw new UnknownCommandException();
7684
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package drake.commands;
2+
3+
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.regex.Matcher;
7+
import java.util.regex.Pattern;
8+
9+
import drake.DrakeException;
10+
import drake.IncompatibleCommandException;
11+
import drake.Storage;
12+
import drake.TaskList;
13+
import drake.Ui;
14+
import drake.tasks.DoWithinPeriod;
15+
import drake.tasks.Task;
16+
17+
/**
18+
* Represents the within command to create a new DoWithinPeriod task.
19+
*/
20+
public class WithinCommand extends CreateTaskCommand {
21+
22+
private static final Pattern descriptionPattern =
23+
Pattern.compile("(?<taskName>.*) /range (?<from>.*) (?<to>.*)");
24+
/**
25+
* Constructor.
26+
*
27+
* @param fullInput The input given by the user.
28+
*/
29+
public WithinCommand(String fullInput) {
30+
super(fullInput);
31+
assert fullInput.startsWith("within");
32+
}
33+
34+
/**
35+
* Executes the command to create a new deadline task, saving the new task and
36+
* printing the size of the task list after execution.
37+
*
38+
* @param tasks The task list before the command is executed.
39+
* @param ui Gives access to the UI of the program.
40+
* @param storage Gives access to local storage.
41+
* @return The list of replies
42+
* @throws IOException when there is an issue with the IO.
43+
* @throws DrakeException when there is inappropriate input or save file issues.
44+
*/
45+
@Override
46+
public List<String> execute(TaskList tasks, Ui ui, Storage storage) throws DrakeException, IOException {
47+
ArrayList<String> reply = new ArrayList<>();
48+
Matcher match = descriptionPattern.matcher(description);
49+
if (!match.matches()) {
50+
throw new IncompatibleCommandException("I need two dates!");
51+
}
52+
reply.add("I've added this task:");
53+
Task addedTask = tasks.addTask(new DoWithinPeriod(match.group("taskName"),
54+
match.group("from"), match.group("to")));
55+
reply.add(addedTask.toString());
56+
storage.addTask(addedTask);
57+
reply.addAll(super.execute(tasks, ui, storage));
58+
return reply;
59+
}
60+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package drake.tasks;
2+
3+
import java.time.LocalDate;
4+
import java.time.format.DateTimeFormatter;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
* Represents tasks that need to be done with a certain period.
10+
*/
11+
public class DoWithinPeriod extends Task {
12+
13+
private final LocalDate from;
14+
private final LocalDate to;
15+
16+
/**
17+
* Constructor.
18+
* @param description The task description.
19+
* @param from The starting date of the period.
20+
* @param to The ending date of the period.
21+
*/
22+
public DoWithinPeriod(String description, String from, String to) {
23+
super(description);
24+
this.from = LocalDate.parse(from);
25+
this.to = LocalDate.parse(to);
26+
}
27+
28+
@Override
29+
public List<String> toList() {
30+
List<String> result = new ArrayList<>();
31+
result.add("W");
32+
result.addAll(super.toList());
33+
result.add(from.toString());
34+
result.add(to.toString());
35+
return result;
36+
}
37+
38+
@Override
39+
public String toString() {
40+
return "[W]" + super.toString() + " (from: " + from.format(DateTimeFormatter.ofPattern("dd MMM yyyy")) + ", "
41+
+ "to: " + to.format(DateTimeFormatter.ofPattern("dd MMM yyyy")) + ")";
42+
}
43+
}

0 commit comments

Comments
 (0)