diff --git a/src/main/java/duke/common/Message.java b/src/main/java/duke/common/Message.java index b6fae7974e..3b424eb54e 100644 --- a/src/main/java/duke/common/Message.java +++ b/src/main/java/duke/common/Message.java @@ -10,7 +10,7 @@ public class Message { public static final String MESSAGE_ADDED = "Got it. I've added this task:"; public static final String MESSAGE_DELETED = "Noted. I've removed this task:"; public static final String MESSAGE_MARK_DONE = "Nice! I've marked this task as done:"; - public static final String MESSAGE_SHOW_TASK_LIST = "Here are the tasks in your list:"; + public static final String MESSAGE_SHOW_TASK_LIST = "Here are the%1$s tasks in your list:"; public static final String MESSAGE_SHOW_TASK_SIZE = "Now you have %1$s tasks in the list."; public static final String MESSAGE_INVALID_COMMAND_FORMAT = "I'm sorry, but I don't know what that means :-("; public static final String MESSAGE_INVALID_TODO_FORMAT = "The description of a todo cannot be empty."; @@ -19,6 +19,7 @@ public class Message { public static final String MESSAGE_INVALID_EVENT_FORMAT = "Events must have /at and cannot be empty. E.g. event project meeting /at 2/12/2019 1800"; public static final String MESSAGE_INVALID_TASK_INDEX = "Invalid task number. Task numbers follow 1-based indexing."; + public static final String MESSAGE_INVALID_KEYWORD_FORMAT = "Invalid keyword. Specify keyword to search for tasks."; public static final String MESSAGE_ERROR_MISSING_STORAGE_FILE = "%1$sMissing storage file: %2$s"; public static final String MESSAGE_ERROR_CREATING_STORAGE_FILE = "Unable to create new file %1$s. %2$s"; public static final String MESSAGE_STORAGE_FILE_CREATED = "%1$sCreated new empty storage file: %2$s"; diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index f9417bbe30..29c3d6cf7b 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -39,6 +39,8 @@ public static Command parse(String inputLine) throws DukeException { return new AddCommand(createEventFrom(inputLine)); case "delete": return new DeleteCommand(getIndexFrom(inputLine)); + case "find": + return new FindCommand(getKeywordFrom(inputLine)); default: throw new DukeException(Message.MESSAGE_INVALID_COMMAND_FORMAT); } @@ -97,4 +99,12 @@ private static Event createEventFrom(String inputLine) throws DukeException { throw new DukeException((Message.MESSAGE_INVALID_DATE_FORMAT)); } } + + private static String getKeywordFrom(String inputLine) throws DukeException { + String keyword = inputLine.substring("find".length()).strip(); + if (keyword.isEmpty()) { + throw new DukeException(Message.MESSAGE_INVALID_KEYWORD_FORMAT); + } + return keyword; + } } diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index 2b47a7002e..1319cbf4a5 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -19,6 +19,13 @@ public Task(String taskName) { this.isDone = false; } + /** + * @return the name of this task. + */ + public String getTaskName() { + return this.taskName; + } + /** * Sets this task to the given done status. * diff --git a/src/main/java/duke/task/TaskList.java b/src/main/java/duke/task/TaskList.java index 8eef805ea3..73b479743b 100644 --- a/src/main/java/duke/task/TaskList.java +++ b/src/main/java/duke/task/TaskList.java @@ -23,7 +23,6 @@ public TaskList() { * * @param tasks list of tasks. */ - public TaskList(List tasks) { this.tasks = tasks; } @@ -76,6 +75,20 @@ public List getTaskNames() { return taskNames; } + /** + * @param keyword keyword that is used for finding tasks. + * @return a list of task names and descriptions of tasks that contain this keyword. + */ + public List getTaskNamesIfMatch(String keyword) { + List taskNamesThatMatch = new ArrayList<>(); + for (int i = 0; i < tasks.size(); i++) { + if (tasks.get(i).getTaskName().contains(keyword)) { + taskNamesThatMatch.add((i + 1) + ". " + tasks.get(i)); + } + } + return taskNamesThatMatch; + } + /** * @return a list of simplified summaries of all tasks in the list. */ diff --git a/src/main/java/duke/ui/UserInterface.java b/src/main/java/duke/ui/UserInterface.java index 4f698e25d5..ac105c2eb4 100644 --- a/src/main/java/duke/ui/UserInterface.java +++ b/src/main/java/duke/ui/UserInterface.java @@ -96,7 +96,7 @@ public void showDeletion(Task task) { * @param taskNames names of the tasks. */ public void showTaskList(List taskNames) { - showToUser(Message.MESSAGE_SHOW_TASK_LIST); + showToUser(String.format(Message.MESSAGE_SHOW_TASK_LIST, " matching")); for (String taskName : taskNames) { showToUser(taskName); }