Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add find functionality #1

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/duke/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public String executeCommand(String line, TaskList tasks) throws DukeException {
String description = line.substring(divider + 1);

return tasks.addTask(new Todo(description));
} else if (line.startsWith("find")) {
String keyword = line.substring(divider + 1);

return tasks.getIndexedTasksByKeyword(keyword);
}

if (line.contains("mark") || line.startsWith("delete")) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/duke/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ public void setStatus(boolean isDone) {
public String getSerializedString() {
return isDone + "|" + description;
}

public String getDescription() {
return description;
}
}
12 changes: 12 additions & 0 deletions src/main/java/duke/tasklist/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,16 @@ public String getSerializedTasks() {

return formatted.toString();
}

public String getIndexedTasksByKeyword(String keyword) {
int idx = 1;
StringBuilder formatted = new StringBuilder();
for (Task task : tasks) {
if (task.getDescription().contains(keyword)) {
formatted.append(idx++).append(". ").append(task.getFormattedTask()).append("\n");
}
}

return formatted.toString();
}
}