Skip to content

Commit

Permalink
Merge pull request #1 from Kobot7/branch-Level-9
Browse files Browse the repository at this point in the history
Add find task feature
Kobot7 authored Feb 24, 2024
2 parents fdebce6 + 1f63348 commit 96b54a6
Showing 5 changed files with 63 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/java/kobot/command/Command.java
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ public abstract class Command {
protected TaskList taskList;

Command(TaskList taskList) {
canExecute = false;
this.canExecute = false;
this.taskList = taskList;
}
public abstract void execute();
39 changes: 39 additions & 0 deletions src/main/java/kobot/command/FindCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package kobot.command;

import kobot.task.TaskList;
import kobot.task.Task;
import kobot.ui.Ui;

public class FindCommand extends Command {
private String keyword;

public FindCommand(TaskList tasklist, String keyword) {
super(tasklist);
parseArguments(keyword);
}

public void parseArguments(String keyword) {
try {
this.keyword = keyword.trim();
} catch (NullPointerException exception) {
Ui.printMissingArgumentErrorMessage();
Ui.printFindCommandUsage();
return;
}

if (this.keyword.isEmpty()) {
Ui.printEmptyArgumentErrorMessage();
Ui.printFindCommandUsage();
return;
}

this.canExecute = true;
}
public void execute() {
if (!canExecute) {
return;
}

taskList.findTasks(keyword);
}
}
3 changes: 3 additions & 0 deletions src/main/java/kobot/command/Parser.java
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ public class Parser {
private static final String EXIT_COMMAND = "exit";
private static final String BYE_COMMAND = "bye";
private static final String LIST_COMMAND = "list";
private static final String FIND_COMMAND = "find";
private static final String TODO_COMMAND = "todo";
private static final String DEADLINE_COMMAND = "deadline";
private static final String EVENT_COMMAND = "event";
@@ -65,6 +66,8 @@ public Command prepareCommand(TaskList taskList) {
case BYE_COMMAND:
exit();
return null;
case FIND_COMMAND:
return new FindCommand(taskList, this.arguments);
case LIST_COMMAND:
return new ListCommand(taskList);
case TODO_COMMAND:
15 changes: 14 additions & 1 deletion src/main/java/kobot/task/TaskList.java
Original file line number Diff line number Diff line change
@@ -70,7 +70,7 @@ public void printTaskList() {
index++;
}
}

/**
* Marks a specified task as done.
*
@@ -148,4 +148,17 @@ public String toStorageFormat() {

return String.valueOf(storage);
}

public void findTasks(String keyword) {
System.out.println("Tasks containing \"" + keyword + "\":");

int index = 1;
for (Task task:taskList) {
if (task.getDescription().contains(keyword)) {
System.out.print(index + ". ");
System.out.println(task);
index++;
}
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/kobot/ui/Ui.java
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ public class Ui {
private static final String MARK_TASK_COMMAND_FORMAT = "mark <task index>";
private static final String UNMARK_TASK_COMMAND_FORMAT = "unmark <task index>";
private static final String DELETE_TASK_COMMAND_FORMAT = "delete <task index>";
private static final String FIND_TASK_COMMAND_FORMAT = "find <keyword>";

private static final String INVALID_COMMAND_ERROR = "Invalid command. Please try again.";
private static final String EMPTY_ARGUMENT_ERROR = "Empty or whitespace-only fields are not allowed.";
@@ -124,4 +125,9 @@ public static void printDeleteCommandUsage() {
System.out.println("Command to delete task:");
System.out.println(DELETE_TASK_COMMAND_FORMAT);
}

public static void printFindCommandUsage() {
System.out.println("Command to find tasks:");
System.out.println(FIND_TASK_COMMAND_FORMAT);
}
}

0 comments on commit 96b54a6

Please sign in to comment.