From 8958073b30038ca7d34f4887538bb6420920b5e8 Mon Sep 17 00:00:00 2001 From: ChanWeiJie Date: Sun, 23 Jan 2022 18:39:43 +0800 Subject: [PATCH] Add Find feature --- src/main/java/duke/command/AddCommand.java | 2 +- src/main/java/duke/command/Command.java | 4 +++- src/main/java/duke/command/DeleteCommand.java | 2 +- src/main/java/duke/command/ExitCommand.java | 2 +- src/main/java/duke/command/FindCommand.java | 19 +++++++++++++++++++ src/main/java/duke/command/MarkCommand.java | 2 +- src/main/java/duke/command/PrintCommand.java | 2 +- src/main/java/duke/command/UnmarkCommand.java | 2 +- src/main/java/duke/functionality/Parser.java | 11 ++++------- .../java/duke/functionality/TaskList.java | 12 ++++++++++++ 10 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 src/main/java/duke/command/FindCommand.java diff --git a/src/main/java/duke/command/AddCommand.java b/src/main/java/duke/command/AddCommand.java index 3fef2ba34d..270b9b6b85 100644 --- a/src/main/java/duke/command/AddCommand.java +++ b/src/main/java/duke/command/AddCommand.java @@ -6,7 +6,7 @@ public class AddCommand extends Command { public AddCommand(Task t){ - super(t, null); + super(t, null, null); } @Override diff --git a/src/main/java/duke/command/Command.java b/src/main/java/duke/command/Command.java index 327ab9ef8a..a5fb549183 100644 --- a/src/main/java/duke/command/Command.java +++ b/src/main/java/duke/command/Command.java @@ -6,10 +6,12 @@ public abstract class Command{ protected Task task; protected Integer index; + protected String word; - public Command(Task t, Integer number) { + public Command(Task t, Integer number, String word) { this.task = t; this.index = number; + this.word = word; } public abstract void execute(TaskList tasks); diff --git a/src/main/java/duke/command/DeleteCommand.java b/src/main/java/duke/command/DeleteCommand.java index 9248b37d17..dd5bc215a7 100644 --- a/src/main/java/duke/command/DeleteCommand.java +++ b/src/main/java/duke/command/DeleteCommand.java @@ -6,7 +6,7 @@ public class DeleteCommand extends Command { public DeleteCommand(Integer number) { - super(null, number); + super(null, number, null); } @Override diff --git a/src/main/java/duke/command/ExitCommand.java b/src/main/java/duke/command/ExitCommand.java index 91e444b320..28e05304b8 100644 --- a/src/main/java/duke/command/ExitCommand.java +++ b/src/main/java/duke/command/ExitCommand.java @@ -5,7 +5,7 @@ public class ExitCommand extends Command{ public ExitCommand() { - super(null, null); + super(null, null,null); } @Override diff --git a/src/main/java/duke/command/FindCommand.java b/src/main/java/duke/command/FindCommand.java new file mode 100644 index 0000000000..45ff6f4d2f --- /dev/null +++ b/src/main/java/duke/command/FindCommand.java @@ -0,0 +1,19 @@ +package duke.command; + +import duke.functionality.TaskList; + +public class FindCommand extends Command{ + public FindCommand(String word) { + super(null, null, word); + } + + @Override + public void execute(TaskList tasks) { + tasks.findWord(super.word); + } + + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/duke/command/MarkCommand.java b/src/main/java/duke/command/MarkCommand.java index d58c992e75..25504eb2f1 100644 --- a/src/main/java/duke/command/MarkCommand.java +++ b/src/main/java/duke/command/MarkCommand.java @@ -5,7 +5,7 @@ public class MarkCommand extends Command { public MarkCommand(Integer number) { - super(null, number); + super(null, number,null); } @Override diff --git a/src/main/java/duke/command/PrintCommand.java b/src/main/java/duke/command/PrintCommand.java index d6a6f85be8..cad5e41e50 100644 --- a/src/main/java/duke/command/PrintCommand.java +++ b/src/main/java/duke/command/PrintCommand.java @@ -5,7 +5,7 @@ public class PrintCommand extends Command{ public PrintCommand() { - super(null, null); + super(null, null, null); } @Override diff --git a/src/main/java/duke/command/UnmarkCommand.java b/src/main/java/duke/command/UnmarkCommand.java index bfc5d3614c..44e6969468 100644 --- a/src/main/java/duke/command/UnmarkCommand.java +++ b/src/main/java/duke/command/UnmarkCommand.java @@ -5,7 +5,7 @@ public class UnmarkCommand extends Command{ public UnmarkCommand(Integer number) { - super(null, number); + super(null, number, null); } @Override diff --git a/src/main/java/duke/functionality/Parser.java b/src/main/java/duke/functionality/Parser.java index b41aecfee5..e6debea7b7 100644 --- a/src/main/java/duke/functionality/Parser.java +++ b/src/main/java/duke/functionality/Parser.java @@ -6,13 +6,7 @@ import java.time.format.DateTimeParseException; -import duke.command.Command; -import duke.command.AddCommand; -import duke.command.DeleteCommand; -import duke.command.PrintCommand; -import duke.command.UnmarkCommand; -import duke.command.ExitCommand; -import duke.command.MarkCommand; +import duke.command.*; import duke.exception.DukeException; import duke.task.Deadline; import duke.task.Event; @@ -121,6 +115,9 @@ public static Command parse(String input) throws DukeException { } else if (command.equals("delete")) { return new DeleteCommand(Integer.parseInt(inputSplit[1])); + } else if(command.equals("find")) { + return new FindCommand(inputSplit[1]); + } else { throw new InvalidCommandException(); } diff --git a/src/main/java/duke/functionality/TaskList.java b/src/main/java/duke/functionality/TaskList.java index ac4995fadb..df900631ba 100644 --- a/src/main/java/duke/functionality/TaskList.java +++ b/src/main/java/duke/functionality/TaskList.java @@ -55,4 +55,16 @@ public void addToList(Task t) { Storage.updateTextFile(); System.out.println(message + t.toString() + "\nNow you have " + numOfTask + " tasks in the list."); } + + public void findWord(String word) { + String message = "Here are the matching tasks in your list:\n"; + System.out.print(message); + for(int i = 0; i < numOfTask; i++) { + Task t = taskList.get(i); + if(t.getDescription().contains(word)) { + String output = i + 1 + "." + t.toString(); + System.out.println(output); + } + } + } }