Skip to content

Commit

Permalink
Merge branch 'branch-C-BetterSearch'
Browse files Browse the repository at this point in the history
  • Loading branch information
afroneth committed Sep 5, 2020
2 parents 8faa26e + e670b5c commit a006321
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
3 changes: 3 additions & 0 deletions data/duke.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
54 changes: 37 additions & 17 deletions src/main/java/FindCommand.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
/**
* 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
*/
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) {
Expand All @@ -57,12 +56,33 @@ 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
*/
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);
}
}
1 change: 0 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;


/**
* A GUI for Duke using FXML.
*/
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.Arrays;

/**
* Parses user input.
*/
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit a006321

Please sign in to comment.