From c2a3d8a416c5ac843694978b3733fb39a8b0971c Mon Sep 17 00:00:00 2001 From: gx-huang Date: Tue, 28 Jan 2020 22:42:54 +0800 Subject: [PATCH] Level-9: Find --- src/main/java/Duke.java | 16 +++++++++++++--- src/main/java/Task.java | 6 +++++- src/main/java/UserText.java | 11 ++++++----- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index f8fcf12056..76bdb98fb2 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -33,7 +33,7 @@ public static void main(String[] args) { isListening = false; } else if (action.equalsIgnoreCase("list")) { - tasks.printInputs(); + tasks.printTasks(); } else if (action.equalsIgnoreCase("done")) { String context = command_broken[1]; @@ -59,10 +59,20 @@ public static void main(String[] args) { tasks.addInput(new ToDos(command_broken[1], false)); } else if (action.equalsIgnoreCase(("event"))) { - System.out.println("Got it, I've added this task"); + System.out.println("UNDERSTOOD. CHANCE OF FAILURE: 0%"); String context = command_broken[1]; String[] context_broken = context.split(" /at ", 2); - tasks.addInput(new Events(context_broken[0], false,context_broken[1])); + tasks.addInput(new Events(context_broken[0], false, context_broken[1])); + + } else if (action.equalsIgnoreCase("find")) { + System.out.println("OPERATION SUCCESSFULL"); + int count = 1; + for (Task t: tasks.getAllTasks()) { + if (t.containsSubstring(command_broken[1])) { + System.out.println(count + "." + t); + count++; + } + } } else { throw new DukeException("Ooops! I'm sorry, i don't know what it means"); diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 6ae709b710..41704d8c7d 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -7,7 +7,7 @@ public Task(String description, Boolean isDone) { this.isDone = isDone; } - public static Task taskList(String[] taskParams) { + public static Task createStartingTask(String[] taskParams) { String type = taskParams[0]; boolean isDone = taskParams[1].equals("true"); String description = taskParams[2]; @@ -27,6 +27,10 @@ public static Task taskList(String[] taskParams) { } } + public boolean containsSubstring(String search) { + return description.contains(search); + } + public boolean isDone() { return isDone; } diff --git a/src/main/java/UserText.java b/src/main/java/UserText.java index 7e4f0ee039..10c912146a 100644 --- a/src/main/java/UserText.java +++ b/src/main/java/UserText.java @@ -46,10 +46,7 @@ public void readText() { String s; while ((s = bufferedreader.readLine()) != null) { String[] taskParams = s.split("/divide"); - /*for (String ss : taskParams) { - System.out.println(ss); - }*/ - Task task = Task.taskList(taskParams); + Task task = Task.createStartingTask(taskParams); allTasks.add(task); } } catch (IOException ioException) { @@ -57,7 +54,7 @@ public void readText() { } } - public void printInputs() { + public void printTasks() { int count = 1; System.out.println("Here is your list"); for (Task s : allTasks) { @@ -70,6 +67,10 @@ public Task getTask(int n) { return this.allTasks.get(n-1); } + public List getAllTasks() { + return this.allTasks; + } + public void markDone(int taskNo) { allTasks.get(taskNo - 1).markAsDone(); System.out.println("Nice! I marked this task as done");