Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/main/java/speed/command/FindCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package speed.command;

import speed.exception.SpeedException;
import speed.storage.Storage;
import speed.task.Task;
import speed.task.TaskList;
import speed.ui.Ui;

import java.util.ArrayList;

public class FindCommand extends Command {
private final String keyword;
public FindCommand(String keyword) {
this.keyword = keyword;
}

@Override
public void execute(TaskList tasks, Ui ui, Storage storage) throws SpeedException {
ArrayList<Task> matchedTasks = tasks.find(keyword);
ui.showFindList(matchedTasks);
}
}
14 changes: 14 additions & 0 deletions src/main/java/speed/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import speed.command.DeleteCommand;
import speed.command.EventCommand;
import speed.command.ExitCommand;
import speed.command.FindCommand;
import speed.command.HelpCommand;
import speed.command.ListCommand;
import speed.command.MarkCommand;
Expand Down Expand Up @@ -63,6 +64,9 @@ public static Command parseCommand(String input, int totalTaskCount) throws Spee
Event task = parseEvent(input);
return new EventCommand(task);

} else if (input.equals("find") || input.startsWith("find ") ) {
String keyword = parseFind(input);
return new FindCommand(keyword);
} else {
throw new SpeedException(Ui.ERROR_UNKNOWN_COMMAND);
}
Expand Down Expand Up @@ -164,4 +168,14 @@ private static Todo parseTodo(String input) throws SpeedException {
}
return new Todo(description);
}

private static String parseFind(String input) throws SpeedException {
String[] parts = input.split(" ", 2);
String keyword = parts.length < 2 ? "" : parts[1].trim();

if (keyword.isEmpty()) {
throw new SpeedException(Ui.ERROR_MISSING_KEYWORD);
}
return keyword;
}
}
13 changes: 13 additions & 0 deletions src/main/java/speed/task/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.ArrayList;

import static java.util.stream.Collectors.toList;

/**
* Represents a list of tasks with operations to manage them.
*/
Expand Down Expand Up @@ -98,4 +100,15 @@ public boolean isEmpty() {
public ArrayList<Task> getTasks() {
return tasks;
}

public ArrayList<Task> find (String keyword) {
String key = keyword.toLowerCase();

ArrayList<Task> matchedTasks = (ArrayList<Task>) tasks.stream()
.filter(t -> t.getDescription()
.toLowerCase()
.contains(key))
.collect(toList());
return matchedTasks;
}
}
24 changes: 23 additions & 1 deletion src/main/java/speed/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import speed.task.Task;
import speed.task.TaskList;

import java.util.ArrayList;
import java.util.Scanner;

/**
Expand All @@ -16,6 +17,9 @@ public class Ui {
public static final String ERROR_EMPTY_TODO =
"Hold up! You forgot to give a description of the todo!!";

public static final String ERROR_MISSING_KEYWORD =
"Give me a keyword of what you want me to find bro!";

public static final String ERROR_NO_TASK_NUMBER =
"Please give me the task number as well bro.";

Expand Down Expand Up @@ -117,7 +121,8 @@ public void printCommandList() {
System.out.println("5.mark <Task number>");
System.out.println("6.unmark <Task number>");
System.out.println("7.delete <Task number>");
System.out.println("8.bye");
System.out.println("8.find <keyword>");
System.out.println("9.bye");
printLine();
}

Expand Down Expand Up @@ -202,4 +207,21 @@ public void showTaskDeleted(Task task, int remainingTasksCount) {
System.out.println("Now you have " + remainingTasksCount + " tasks in the list.");
printLine();
}

public void showFindList(ArrayList<Task> matchedTasks) {
if (matchedTasks.isEmpty()) {
printLine();
System.out.println("No matches found!");
printLine();
} else {
printLine();
System.out.println("Here are the matching tasks in your list:");
int taskCount = 1;
for (Task task : matchedTasks) {
System.out.println((taskCount++) + "." + task.displayString());
}
printLine();
}

}
}