forked from nus-cs2103-AY2122S2/ip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchCommand.java
95 lines (77 loc) · 2.92 KB
/
SearchCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package duke.commands;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import duke.common.DukeException;
import duke.constants.Constants;
import duke.storage.Storage;
import duke.task.Deadline;
import duke.task.Event;
import duke.task.Task;
import duke.task.TaskList;
/**
* Search task list based on keyword.
*/
public class SearchCommand extends Command {
private final String args;
/**
* Creates SearchCommand object with user input keyword.
*
* @param args args is the keyword that user specified.
*/
public SearchCommand(String args) {
assert args != null : "SearchCommand[SearchCommand] args cannot be null.";
assert args.length() > 0 : "SearchCommand[SearchCommand] args must contain data.";
this.args = args;
}
/**
* Executes the search command.
*/
@Override
public String execute(TaskList taskList, Storage storage) throws DukeException {
assert taskList != null : "SearchCommand[execute] taskList cannot be null.";
assert storage != null : "SearchCommand[execute] storage cannot be null.";
String response = "";
try {
if (this.args.length() == 0) {
throw new IllegalArgumentException();
}
LocalDate date = LocalDate.parse(this.args);
int index = 0;
ArrayList<Task> allTasks = taskList.getTasks();
int length = allTasks.size();
StringBuilder sb = new StringBuilder();
if (length == 0) {
response = "No tasks found based on given date! Also, quit lazing around!";
return response;
}
sb.append("Here are the tasks with date, "
+ date.format(DateTimeFormatter.ofPattern("MMM dd yyyy"))
+ ", in your list:\n");
for (int i = 0; i < length; ++i) {
boolean shouldAppend = false;
Task task = allTasks.get(i);
if (task.getType() == 'D') {
Deadline deadline = (Deadline) task;
if (deadline.getDate().isEqual(date)) {
sb.append(++index + ". " + deadline.toString());
shouldAppend = true;
}
} else if (task.getType() == 'E') {
Event event = (Event) task;
if (event.getDate().isEqual(date)) {
sb.append(++index + ". " + event.toString());
shouldAppend = true;
}
}
if (shouldAppend) {
sb.append("\n");
}
}
response = index > 0 ? sb.toString() : Constants.NO_TASK_SEARCH_MSG;
return response;
} catch (Exception e) {
throw new DukeException(Constants.INVALID_DATE_MSG);
}
}
}