Skip to content
This repository has been archived by the owner on Nov 13, 2020. It is now read-only.

Commit

Permalink
Merge branch 'branch-Level-9' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
seowalex committed Aug 26, 2020
2 parents 8c99d77 + 8c77a31 commit 86686f4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package duke;

import duke.command.AddCommand;
import duke.command.Command;
import duke.command.DeleteCommand;
import duke.command.DoneCommand;
import duke.command.DueCommand;
import duke.command.ExitCommand;
import duke.command.ListCommand;
import duke.command.*;
import duke.task.TaskType;

import java.time.LocalDate;
Expand Down Expand Up @@ -134,6 +128,8 @@ public static Command parse(String fullCommand) throws DukeException {
}

return new DeleteCommand(Integer.parseInt(args));
case "find":
return new FindCommand(args);
case "todo":
return new AddCommand(TaskType.TODO, args);
case "deadline": {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/duke/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public List<Task> getDueTasks(LocalDate date) {
return tasks.stream().filter(task -> task.isDue(date)).collect(Collectors.toList());
}

public List<Task> findTasks(String keyword) {
return tasks.stream().filter(task -> task.hasKeyword(keyword)).collect(Collectors.toList());
}

public List<Task> getTasks() {
return tasks;
}
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/duke/command/FindCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package duke.command;

import duke.DukeException;
import duke.Storage;
import duke.TaskList;
import duke.Ui;
import duke.task.Task;

import java.time.format.DateTimeFormatter;
import java.util.List;

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 DukeException {
List<Task> foundTasks = tasks.findTasks(keyword);

if (foundTasks.size() == 0) {
throw new DukeException("There are no tasks in your list with the keyword \""
+ keyword + "\".");
}

StringBuilder output = new StringBuilder("Here are the matching tasks in your list:\n");

for (Task task : foundTasks) {
output.append(tasks.getTasks().indexOf(task) + 1).append(".").append(task).append('\n');
}

ui.showPrompt(output.toString());
}
}
4 changes: 4 additions & 0 deletions src/main/java/duke/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public Task(String description, boolean isDone) {
*/
public abstract boolean isDue(LocalDate date);

public boolean hasKeyword(String keyword) {
return description.contains(keyword);
}

/**
* Returns a status icon corresponding to whether the task is done.
*
Expand Down

0 comments on commit 86686f4

Please sign in to comment.