forked from nus-cs2103-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from jiakai-17/jiakai/implement-find-task
feat: Implement Find Task
- Loading branch information
Showing
15 changed files
with
739 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/seedu/address/logic/commands/FindTaskCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.task.TaskContainsKeywordsPredicate; | ||
|
||
/** | ||
* Finds and lists all task in address book whose title or note contains any of the argument keywords. | ||
* Keyword matching is case-insensitive. | ||
*/ | ||
public class FindTaskCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "findTask"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all tasks whose title or note contain any of " | ||
+ "the specified keywords (case-insensitive) and displays them as a list with index numbers.\n" | ||
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n" | ||
+ "Example: " + COMMAND_WORD + " agenda budget"; | ||
|
||
private final TaskContainsKeywordsPredicate predicate; | ||
|
||
public FindTaskCommand(TaskContainsKeywordsPredicate predicate) { | ||
this.predicate = predicate; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) { | ||
requireNonNull(model); | ||
model.updateFilteredTaskList(predicate); | ||
return new CommandResult( | ||
String.format(Messages.MESSAGE_TASKS_LISTED_OVERVIEW, model.getFilteredTaskList().size())); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof FindTaskCommand)) { | ||
return false; | ||
} | ||
|
||
FindTaskCommand otherFindTaskCommand = (FindTaskCommand) other; | ||
return predicate.equals(otherFindTaskCommand.predicate); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("predicate", predicate) | ||
.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/seedu/address/logic/parser/FindTaskCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
|
||
import java.util.Arrays; | ||
|
||
import seedu.address.logic.commands.FindTaskCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.task.TaskContainsKeywordsPredicate; | ||
|
||
/** | ||
* Parses input arguments and creates a new FindTaskCommand object | ||
*/ | ||
public class FindTaskCommandParser implements Parser<FindTaskCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the FindTaskCommand | ||
* and returns a FindTaskCommand object for execution. | ||
* | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public FindTaskCommand parse(String args) throws ParseException { | ||
String trimmedArgs = args.trim(); | ||
if (trimmedArgs.isEmpty()) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindTaskCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
String[] taskKeywords = trimmedArgs.split("\\s+"); | ||
|
||
return new FindTaskCommand(new TaskContainsKeywordsPredicate(Arrays.asList(taskKeywords))); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/seedu/address/model/task/NoteContainsKeywordsPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package seedu.address.model.task; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.util.StringUtil; | ||
import seedu.address.commons.util.ToStringBuilder; | ||
|
||
/** | ||
* Tests that a {@code Task}'s {@code Note} matches any of the keywords given. | ||
*/ | ||
public class NoteContainsKeywordsPredicate implements Predicate<Task> { | ||
private final List<String> keywords; | ||
|
||
public NoteContainsKeywordsPredicate(List<String> keywords) { | ||
this.keywords = keywords; | ||
} | ||
|
||
@Override | ||
public boolean test(Task task) { | ||
return keywords | ||
.stream() | ||
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(task.getNote().toString(), keyword)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof NoteContainsKeywordsPredicate)) { | ||
return false; | ||
} | ||
|
||
NoteContainsKeywordsPredicate otherNoteContainsKeywordsPredicate = (NoteContainsKeywordsPredicate) other; | ||
return keywords.equals(otherNoteContainsKeywordsPredicate.keywords); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this).add("keywords", keywords).toString(); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/seedu/address/model/task/TaskContainsKeywordsPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package seedu.address.model.task; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
|
||
/** | ||
* Tests that a {@code Task}'s {@code Title} or {@code Note} matches any of the keywords given. | ||
*/ | ||
public class TaskContainsKeywordsPredicate implements Predicate<Task> { | ||
private final List<String> keywords; | ||
private final TitleContainsKeywordsPredicate titlePredicate; | ||
private final NoteContainsKeywordsPredicate notePredicate; | ||
|
||
/** | ||
* Constructs a {@code TaskContainsKeywordsPredicate} with the specified keywords. | ||
* | ||
* @param keywords A list of keywords to search for. | ||
*/ | ||
public TaskContainsKeywordsPredicate(List<String> keywords) { | ||
this.keywords = keywords; | ||
this.titlePredicate = new TitleContainsKeywordsPredicate(keywords); | ||
this.notePredicate = new NoteContainsKeywordsPredicate(keywords); | ||
} | ||
|
||
@Override | ||
public boolean test(Task task) { | ||
return titlePredicate.test(task) || notePredicate.test(task); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof TaskContainsKeywordsPredicate)) { | ||
return false; | ||
} | ||
|
||
TaskContainsKeywordsPredicate otherTaskContainsKeywordsPredicate = (TaskContainsKeywordsPredicate) other; | ||
return keywords.equals(otherTaskContainsKeywordsPredicate.keywords); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this).add("keywords", keywords).toString(); | ||
} | ||
} |
Oops, something went wrong.