From 8019e0aae7dd9ff1c3df660625c6a96ff6fcb55e Mon Sep 17 00:00:00 2001 From: kevinz420 Date: Thu, 5 Oct 2023 15:34:32 +0800 Subject: [PATCH] Add find functionality --- src/main/java/duke/parser/Parser.java | 4 ++++ src/main/java/duke/task/Task.java | 4 ++++ src/main/java/duke/tasklist/TaskList.java | 12 ++++++++++++ 3 files changed, 20 insertions(+) diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index d9f761bbc..0e98d3dd4 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -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")) { diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index b05700b09..c00740a51 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -23,4 +23,8 @@ public void setStatus(boolean isDone) { public String getSerializedString() { return isDone + "|" + description; } + + public String getDescription() { + return description; + } } diff --git a/src/main/java/duke/tasklist/TaskList.java b/src/main/java/duke/tasklist/TaskList.java index bc6adda22..7a584e5c6 100644 --- a/src/main/java/duke/tasklist/TaskList.java +++ b/src/main/java/duke/tasklist/TaskList.java @@ -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(); + } }