Skip to content

Commit fa02c68

Browse files
Merge pull request nus-cs2103-AY2021S1#67 from Licheng-Wu/branch-Find-exercises
Add find exercises feature
2 parents 1a33d1b + 5077f39 commit fa02c68

File tree

5 files changed

+111
-1
lines changed

5 files changed

+111
-1
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package seedu.address.logic.commands;
2+
3+
import static java.util.Objects.requireNonNull;
4+
5+
import seedu.address.commons.core.Messages;
6+
import seedu.address.model.Model;
7+
import seedu.address.model.person.ExerciseNameContainsKeywordsPredicate;
8+
9+
/**
10+
* Finds and lists all exercises in fitNUS whose name contains any of the argument keywords.
11+
* Keyword matching is case insensitive.
12+
*/
13+
public class FindExercisesCommand extends Command {
14+
15+
public static final String COMMAND_WORD = "find_exercises";
16+
17+
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all exercises whose names contain any of "
18+
+ "the specified keywords (case-insensitive) and displays them as a list with index numbers.\n"
19+
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n"
20+
+ "Example: " + COMMAND_WORD + " bench press";
21+
22+
private final ExerciseNameContainsKeywordsPredicate predicate;
23+
24+
public FindExercisesCommand(ExerciseNameContainsKeywordsPredicate predicate) {
25+
this.predicate = predicate;
26+
}
27+
28+
@Override
29+
public CommandResult execute(Model model) {
30+
requireNonNull(model);
31+
model.updateFilteredExerciseList(predicate);
32+
return new CommandResult(
33+
String.format(Messages.MESSAGE_EXERCISES_LISTED_OVERVIEW, model.getFilteredExerciseList().size()));
34+
}
35+
36+
@Override
37+
public boolean equals(Object other) {
38+
return other == this // short circuit if same object
39+
|| (other instanceof FindExercisesCommand // instanceof handles nulls
40+
&& predicate.equals(((FindExercisesCommand) other).predicate)); // state check
41+
}
42+
}

src/main/java/seedu/address/logic/parser/AddressBookParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import seedu.address.logic.commands.ExerciseDeleteCommand;
1818
import seedu.address.logic.commands.ExitCommand;
1919
import seedu.address.logic.commands.FindCommand;
20+
import seedu.address.logic.commands.FindExercisesCommand;
2021
import seedu.address.logic.commands.HelpCommand;
2122
import seedu.address.logic.commands.LessonAddCommand;
2223
import seedu.address.logic.commands.LessonDeleteCommand;
@@ -78,6 +79,9 @@ public Command parseCommand(String userInput) throws ParseException {
7879
case FindCommand.COMMAND_WORD:
7980
return new FindCommandParser().parse(arguments);
8081

82+
case FindExercisesCommand.COMMAND_WORD:
83+
return new FindExercisesCommandParser().parse(arguments);
84+
8185
case ListCommand.COMMAND_WORD:
8286
return new ListCommand();
8387

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package seedu.address.logic.parser;
2+
3+
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
4+
5+
import java.util.Arrays;
6+
7+
import seedu.address.logic.commands.FindExercisesCommand;
8+
import seedu.address.logic.parser.exceptions.ParseException;
9+
import seedu.address.model.person.ExerciseNameContainsKeywordsPredicate;
10+
11+
/**
12+
* Parses input arguments and creates a new FindExercisesCommand object
13+
*/
14+
public class FindExercisesCommandParser implements Parser<FindExercisesCommand> {
15+
16+
/**
17+
* Parses the given {@code String} of arguments in the context of the FindExercisesCommand
18+
* and returns a FindExercisesCommand object for execution.
19+
* @throws ParseException if the user input does not conform the expected format
20+
*/
21+
public FindExercisesCommand parse(String args) throws ParseException {
22+
String trimmedArgs = args.trim();
23+
if (trimmedArgs.isEmpty()) {
24+
throw new ParseException(
25+
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindExercisesCommand.MESSAGE_USAGE));
26+
}
27+
28+
String[] nameKeywords = trimmedArgs.split("\\s+");
29+
30+
return new FindExercisesCommand(new ExerciseNameContainsKeywordsPredicate(Arrays.asList(nameKeywords)));
31+
}
32+
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package seedu.address.model.person;
2+
3+
import java.util.List;
4+
import java.util.function.Predicate;
5+
6+
import seedu.address.commons.util.StringUtil;
7+
8+
/**
9+
* Tests that a {@code Exercise}'s {@code Name} matches any of the keywords given.
10+
*/
11+
public class ExerciseNameContainsKeywordsPredicate implements Predicate<Exercise> {
12+
private final List<String> keywords;
13+
14+
public ExerciseNameContainsKeywordsPredicate(List<String> keywords) {
15+
this.keywords = keywords;
16+
}
17+
18+
@Override
19+
public boolean test(Exercise exercise) {
20+
return keywords.stream()
21+
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(exercise.getName().fullName, keyword));
22+
}
23+
24+
@Override
25+
public boolean equals(Object other) {
26+
return other == this // short circuit if same object
27+
|| (other instanceof ExerciseNameContainsKeywordsPredicate // instanceof handles nulls
28+
&& keywords.equals(((ExerciseNameContainsKeywordsPredicate) other).keywords)); // state check
29+
}
30+
31+
}

src/main/resources/view/MainWindow.fxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
<StackPane fx:id="personListPanelPlaceholder" VBox.vgrow="ALWAYS" />
5858
</VBox>
5959

60-
<VBox fx:id="routineList" styleClass="pane-with-border" minWidth="340" prefWidth="340" VBox.vgrow="ALWAYS">
60+
<VBox fx:id="routineList" styleClass="pane-with-border" minWidth="200" prefWidth="340" VBox.vgrow="ALWAYS">
6161
<padding>
6262
<Insets top="10" right="10" bottom="10" left="10" />
6363
</padding>

0 commit comments

Comments
 (0)