Skip to content

Commit

Permalink
Add findmatch and findall
Browse files Browse the repository at this point in the history
  • Loading branch information
dishenggg committed Sep 15, 2023
1 parent 88b0e6f commit 7bb2cbd
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 31 deletions.
12 changes: 8 additions & 4 deletions src/main/java/joe/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
public class Parser {
private enum CommandType {
list, todo, deadline, event, mark, unmark, delete, bye, find, INVALID
list, todo, deadline, event, mark, unmark, delete, bye, find, findall, findmatch, INVALID
}

private static final Pattern COMMAND_PATTERN = Pattern.compile("^(\\S+)\\s?(.*)$");
Expand Down Expand Up @@ -88,7 +88,11 @@ private static Command parseToCommand(CommandType type, String args) {
case delete:
return handleDelete(args);
case find:
return handleFind(args);
return handleFind(args, FindCommand.DESC);
case findmatch:
return handleFind(args, FindCommand.DESC_MATCH_CASE);
case findall:
return handleFind(args, FindCommand.ALL);
default:
return handleInvalidKeyword();
}
Expand Down Expand Up @@ -201,11 +205,11 @@ private static Command handleDelete(String args) {
}
}

private static Command handleFind(String args) {
private static Command handleFind(String args, int searchType) {
if (args.trim().isEmpty()) {
return new InvalidCommand(INVALID_ARGS_FIND_MESSAGE);
}
return new FindCommand(args.trim());
return new FindCommand(args.trim(), searchType);
}

private static Command handleInvalidKeyword() {
Expand Down
33 changes: 29 additions & 4 deletions src/main/java/joe/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,38 @@ public List<String> getStringList() {
}

/**
* Returns a TaskList of tasks containing the search word.
* Returns a TaskList of tasks with description containing the search word.
*
* @param searchString The search word.
* @param isMatchingCase true if results should match the case of the search string, false otherwise.
* @return A TaskList of tasks.
*/
public TaskList findByDesc(String searchString, boolean isMatchingCase) {
TaskList res = new TaskList();
String processedSearchString = isMatchingCase ? searchString : searchString.toLowerCase();

for (Task task : tasks) {
String description = isMatchingCase ? task.getDescription() : task.getDescription().toLowerCase();

if (description.contains(processedSearchString)) {
res.add(task);
}
}

return res;
}

/**
* Returns a TaskList of tasks with string representation containing the search word.
*
* @param searchString The search word.
* @return A TaskList of tasks.
*/
public TaskList find(String searchString) {
public TaskList findAll(String searchString) {
TaskList res = new TaskList();

for (Task task : tasks) {
if (task.getDescription().toLowerCase().contains(searchString.toLowerCase())) {
if (task.toString().toLowerCase().contains(searchString.toLowerCase())) {
res.add(task);
}
}
Expand All @@ -91,7 +113,10 @@ public TaskList find(String searchString) {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Here are your tasks:\n");

if (tasks.isEmpty()) {
return "No tasks available.";
}
for (int i = 0; i < tasks.size(); i++) {
sb.append(i + 1);
sb.append(".");
Expand Down
28 changes: 24 additions & 4 deletions src/main/java/joe/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@

import joe.Storage;
import joe.TaskList;
import joe.exceptions.JoeException;

/**
* Represents a command to find all tasks containing the search string.
*/
public class FindCommand extends Command {
public static final int DESC = 0;
public static final int DESC_MATCH_CASE = 1;
public static final int ALL = 2;
private final String searchString;
private final int searchType;

/**
* Constructs a FindCommand with the string to be searched for.
*
* @param searchString The string to be searched for.
* @param searchType The type of search
*/
public FindCommand(String searchString) {
public FindCommand(String searchString, int searchType) {
this.searchString = searchString;
this.searchType = searchType;
}

/**
Expand All @@ -25,8 +32,21 @@ public FindCommand(String searchString) {
* @param storage The storage for saving and loading tasks.
*/
@Override
public String execute(TaskList tasks, Storage storage) {
TaskList results = tasks.find(searchString);
return results.toString();
public String execute(TaskList tasks, Storage storage) throws JoeException {
TaskList results;
switch (searchType) {
case DESC:
results = tasks.findByDesc(searchString, false);
break;
case DESC_MATCH_CASE:
results = tasks.findByDesc(searchString, true);
break;
case ALL:
results = tasks.findAll(searchString);
break;
default:
throw new JoeException("Bad search type!");
}
return "Here are the find results:\n" + results.toString();
}
}
2 changes: 1 addition & 1 deletion src/main/java/joe/commands/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public class ListCommand extends Command {
*/
@Override
public String execute(TaskList tasks, Storage storage) {
return tasks.toString();
return "Here are your tasks:\n" + tasks.toString();
}
}
14 changes: 14 additions & 0 deletions src/test/java/joe/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import joe.commands.DeadlineCommand;
import joe.commands.DeleteCommand;
import joe.commands.EventCommand;
import joe.commands.FindCommand;
import joe.commands.InvalidCommand;
import joe.commands.ListCommand;
import joe.commands.MarkCommand;
import joe.commands.TodoCommand;
import joe.commands.UnmarkCommand;


public class ParserTest {

// Tests for empty commands
Expand Down Expand Up @@ -301,4 +303,16 @@ public void parse_eventExpected1_success() {
Command c = Parser.parseUserInput("event 1 egg /from 01/01/2090 0000 /to 01/01/2090 0001");
assertTrue(c instanceof EventCommand);
}

@Test
public void parse_findall_success() {
Command c = Parser.parseUserInput("findall test");
assertTrue(c instanceof FindCommand);
}

@Test
public void parse_findmatch_success() {
Command c = Parser.parseUserInput("findmatch test");
assertTrue(c instanceof FindCommand);
}
}
60 changes: 42 additions & 18 deletions src/test/java/joe/TaskListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

public class TaskListTest {
@Test
public void testEmptyString() {
assertEquals("Here are your tasks:", new TaskList().toString());
public void testEmptyListString() {
assertEquals("No tasks available.", new TaskList().toString());
}

@Test
Expand All @@ -26,8 +26,7 @@ public void testUnmarkedString() {
tasks.add(new DeadlineTask("Deadline", dt));
tasks.add(new EventTask("Event", dt, dt));
assertEquals(
"Here are your tasks:\n"
+ "1.[T][ ] Todo\n"
"1.[T][ ] Todo\n"
+ "2.[D][ ] Deadline (by: 01 Jan 2000 00:00)\n"
+ "3.[E][ ] Event (from: 01 Jan 2000 00:00 to: 01 Jan 2000 00:00)",
tasks.toString());
Expand All @@ -47,9 +46,7 @@ public void testMarkedString() {
newTodo.markAsDone();
newDeadline.markAsDone();
newEvent.markAsDone();
assertEquals(
"Here are your tasks:\n"
+ "1.[T][X] Todo\n"
assertEquals("1.[T][X] Todo\n"
+ "2.[D][X] Deadline (by: 01 Jan 2000 00:00)\n"
+ "3.[E][X] Event (from: 01 Jan 2000 00:00 to: 01 Jan 2000 00:00)",
tasks.toString());
Expand All @@ -66,9 +63,8 @@ public void find_wordExists_success() {
tasks.add(newTodo);
tasks.add(newDeadline);
tasks.add(newEvent);
TaskList res = tasks.find("Deadline");
assertEquals("Here are your tasks:\n"
+ "1.[D][ ] Deadline (by: 01 Jan 2000 00:00)", res.toString());
TaskList res = tasks.findByDesc("Deadline", false);
assertEquals("1.[D][ ] Deadline (by: 01 Jan 2000 00:00)", res.toString());
}

@Test
Expand All @@ -82,8 +78,8 @@ public void find_wordDoesNotExist_noResult() {
tasks.add(newTodo);
tasks.add(newDeadline);
tasks.add(newEvent);
TaskList res = tasks.find("adlfhasdlkfh");
assertEquals("Here are your tasks:", res.toString());
TaskList res = tasks.findByDesc("adlfhasdlkfh", false);
assertEquals("No tasks available.", res.toString());
}

@Test
Expand All @@ -97,9 +93,8 @@ public void find_emptySearchString_allTasks() {
tasks.add(newTodo);
tasks.add(newDeadline);
tasks.add(newEvent);
TaskList res = tasks.find("");
assertEquals("Here are your tasks:\n"
+ "1.[T][ ] Todo\n"
TaskList res = tasks.findByDesc("", false);
assertEquals("1.[T][ ] Todo\n"
+ "2.[D][ ] Deadline (by: 01 Jan 2000 00:00)\n"
+ "3.[E][ ] Event (from: 01 Jan 2000 00:00 to: 01 Jan 2000 00:00)", res.toString());
}
Expand All @@ -115,10 +110,39 @@ public void find_randomCase_success() {
tasks.add(newTodo);
tasks.add(newDeadline);
tasks.add(newEvent);
TaskList res = tasks.find("cAsE");
assertEquals("Here are your tasks:\n"
+ "1.[T][ ] all lower case\n"
TaskList res = tasks.findByDesc("cAsE", false);
assertEquals("1.[T][ ] all lower case\n"
+ "2.[D][ ] ALL UPPER CASE (by: 01 Jan 2000 00:00)\n"
+ "3.[E][ ] rAnDoM CaSe (from: 01 Jan 2000 00:00 to: 01 Jan 2000 00:00)", res.toString());
}

@Test
public void find_matchingCase_success() {
LocalDateTime dt =
LocalDateTime.parse("01/01/2000 0000", DateTimeFormatter.ofPattern("d/M/yyyy HHmm"));
TaskList tasks = new TaskList();
TodoTask newTodo = new TodoTask("all lower case");
DeadlineTask newDeadline = (new DeadlineTask("ALL UPPER CASE", dt));
EventTask newEvent = (new EventTask("rAnDoM CaSe", dt, dt));
tasks.add(newTodo);
tasks.add(newDeadline);
tasks.add(newEvent);
TaskList res = tasks.findByDesc("CaSe", true);
assertEquals("1.[E][ ] rAnDoM CaSe (from: 01 Jan 2000 00:00 to: 01 Jan 2000 00:00)", res.toString());
}

@Test
public void find_matchingCase_noResult() {
LocalDateTime dt =
LocalDateTime.parse("01/01/2000 0000", DateTimeFormatter.ofPattern("d/M/yyyy HHmm"));
TaskList tasks = new TaskList();
TodoTask newTodo = new TodoTask("all lower case");
DeadlineTask newDeadline = (new DeadlineTask("ALL UPPER CASE", dt));
EventTask newEvent = (new EventTask("rAnDoM CaSe", dt, dt));
tasks.add(newTodo);
tasks.add(newDeadline);
tasks.add(newEvent);
TaskList res = tasks.findByDesc("cAse", true);
assertEquals("No tasks available.", res.toString());
}
}

0 comments on commit 7bb2cbd

Please sign in to comment.