From e670b5c49c5dc9c7c7f0eaad37d50bf2246f0d85 Mon Sep 17 00:00:00 2001 From: afroneth Date: Sat, 5 Sep 2020 23:16:17 +0800 Subject: [PATCH] Add C - Better Search --- data/duke.txt | 3 ++ src/main/java/FindCommand.java | 54 +++++++++++++++++++++++----------- src/main/java/Main.java | 1 - src/main/java/Parser.java | 8 +++-- 4 files changed, 45 insertions(+), 21 deletions(-) diff --git a/data/duke.txt b/data/duke.txt index f8ea583f0f..2c1e6b841d 100644 --- a/data/duke.txt +++ b/data/duke.txt @@ -2,3 +2,6 @@ T|1|sleep E|0|project meeting|30/3/2019 1412 D|0|concert preparation|14/12/2020 1312 D|1|project|12/12/2012 1212 +T|0|eat mala project +E|0|malay project|12/12/2012 1212 +T|0|meet someone diff --git a/src/main/java/FindCommand.java b/src/main/java/FindCommand.java index 8cd19d9e0d..929974285f 100644 --- a/src/main/java/FindCommand.java +++ b/src/main/java/FindCommand.java @@ -1,22 +1,19 @@ -/** - * Implements methods for FindCommand. - */ public class FindCommand extends Command { protected boolean isExit; - private String keyword; + private String[] searchTerms; /** * Instantiates FindCommand object. - * @param keyword Keyword used to find matching tasks. + * @param searchTerms Keyword to find. */ - public FindCommand(String keyword) { - this.keyword = keyword; + public FindCommand(String[] searchTerms) { + this.searchTerms = searchTerms; } /** * Runs command to handle find command. * - * @param arrayOfTasks Array of tasks that we have parsed. + * @param arrayOfTasks Array of Tasks that we have parsed. * @param ui Ui object to aid in program execution. * @param storage Storage object to aid in program execution. * @return Response object @@ -24,21 +21,23 @@ public FindCommand(String keyword) { public Response runCommand(TaskList arrayOfTasks, Ui ui, Storage storage) { assert arrayOfTasks != null || ui != null || storage != null : "arrayOfTasks, Ui and Storage objects cannot be null"; + TaskList matchedTasksList = new TaskList(); int arraySize = arrayOfTasks.taskArraySize(); int index = 0; while (index < arraySize) { Task matchedTask = arrayOfTasks.get(index); String[] splitElements = matchedTask.description.split(" "); - int numOfElements = splitElements.length; boolean isMatching = false; - int secondIndex = 0; - while (secondIndex < numOfElements) { - if (splitElements[secondIndex].strip().equalsIgnoreCase(keyword)) { - isMatching = true; - break; - } - secondIndex++; + + String delimiter = ""; + String keyword = String.join(delimiter, searchTerms); + String taskInList = String.join(delimiter, splitElements); + int keywordLength = keyword.length(); + int taskLength = taskInList.length(); + + if (isSubSequence(keyword, taskInList, keywordLength, taskLength)) { + isMatching = true; } if (!isMatching) { @@ -57,7 +56,7 @@ public Response runCommand(TaskList arrayOfTasks, Ui ui, Storage storage) { } /** - * Checks if the program has to exit Duke. + * Since this is not a exit command, it does not signal the program to exit. * * @return exitCheck as False */ @@ -65,4 +64,25 @@ public boolean exitChecker() { isExit = false; return isExit; } + + static boolean isSubSequence(String keyword, String task, int keywordLength, int taskLength) { + if (taskLength == 0) { + return false; + } + + if (keywordLength == 0) { + return true; + } + + char keywordCharacter = keyword.charAt(keywordLength - 1); + char taskCharacter = task.charAt(taskLength - 1); + + // Checks if last characters of two strings matches + if (keywordCharacter == taskCharacter) { + return isSubSequence(keyword, task, keywordLength - 1, taskLength - 1); + } + + // Recurse if last characters do not match + return isSubSequence(keyword, task, keywordLength, taskLength - 1); + } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 99bedd9e12..30e66bcd97 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -6,7 +6,6 @@ import javafx.scene.layout.AnchorPane; import javafx.stage.Stage; - /** * A GUI for Duke using FXML. */ diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index e3cdd793cb..94affbbf27 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -1,3 +1,5 @@ +import java.util.Arrays; + /** * Parses user input. */ @@ -231,9 +233,9 @@ private static FindCommand processFindCommand(String inputLine) throws DukeExcep if (numOfTokens < 2) { throw new DukeException("Please specific keyword to locate task. Eg. find book"); } - String tempString = inputLine.split("find")[1]; - String strippedKeyword = tempString.strip(); - return new FindCommand(strippedKeyword); + + String[] copiedArray = Arrays.copyOfRange(arrayOfElements, 1, arrayOfElements.length); + return new FindCommand(copiedArray); } private static HelpCommand processHelpCommand() {