Skip to content

Commit 643120f

Browse files
Merge pull request nus-cs2103-AY2021S1#73 from iqbxl/branch-Lesson-find
Branch lesson find
2 parents 27a53af + a71e364 commit 643120f

9 files changed

+115
-7
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.LessonNameContainsKeywordsPredicate;
8+
9+
/**
10+
* Finds and lists all lessons in fitNUS whose name contains any of the argument keywords.
11+
* Keyword matching is case insensitive.
12+
*/
13+
public class FindLessonsCommand extends Command {
14+
15+
public static final String COMMAND_WORD = "find_lessons";
16+
17+
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all lessons 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 + " CS1231";
21+
22+
private final LessonNameContainsKeywordsPredicate predicate;
23+
24+
public FindLessonsCommand(LessonNameContainsKeywordsPredicate predicate) {
25+
this.predicate = predicate;
26+
}
27+
28+
@Override
29+
public CommandResult execute(Model model) {
30+
requireNonNull(model);
31+
model.updateFilteredLessonList(predicate);
32+
return new CommandResult(
33+
String.format(Messages.MESSAGE_LESSONS_LISTED_OVERVIEW, model.getFilteredLessonList().size()));
34+
}
35+
36+
@Override
37+
public boolean equals(Object other) {
38+
return other == this // short circuit if same object
39+
|| (other instanceof FindLessonsCommand // instanceof handles nulls
40+
&& predicate.equals(((FindLessonsCommand) other).predicate)); // state check
41+
}
42+
}

src/main/java/seedu/address/logic/commands/TimetableAddLessonCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class TimetableAddLessonCommand extends Command {
2020
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a Lesson to the timetable in fitNUS. "
2121
+ "Parameters: "
2222
+ PREFIX_NAME + "LESSON_NAME "
23-
+ PREFIX_DAY + "DAY"
23+
+ PREFIX_DAY + "DAY "
2424
+ PREFIX_TIME + "TIME"
2525
+ "\n"
2626
+ "Example: " + COMMAND_WORD + " "

src/main/java/seedu/address/logic/commands/TimetableAddRoutineCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class TimetableAddRoutineCommand extends Command {
1919
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a Routine to the timetable in fitNUS. "
2020
+ "Parameters: "
2121
+ PREFIX_ROUTINE + "ROUTINE_NAME "
22-
+ PREFIX_DAY + "DAY"
22+
+ PREFIX_DAY + "DAY "
2323
+ PREFIX_TIME + "TIME"
2424
+ "\n"
2525
+ "Example: " + COMMAND_WORD + " "

src/main/java/seedu/address/logic/commands/TimetableDeleteSlotCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class TimetableDeleteSlotCommand extends Command {
1919

2020
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Deletes the slot identified by its day and time.\n"
2121
+ "Parameters: "
22-
+ PREFIX_DAY + "DAY"
22+
+ PREFIX_DAY + "DAY "
2323
+ PREFIX_TIME + "TIME"
2424
+ "\n"
2525
+ "Example: " + COMMAND_WORD + " "

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import seedu.address.logic.commands.ExitCommand;
1919
import seedu.address.logic.commands.FindCommand;
2020
import seedu.address.logic.commands.FindExercisesCommand;
21+
import seedu.address.logic.commands.FindLessonsCommand;
2122
import seedu.address.logic.commands.HelpCommand;
2223
import seedu.address.logic.commands.LessonAddCommand;
2324
import seedu.address.logic.commands.LessonDeleteCommand;
@@ -85,6 +86,9 @@ public Command parseCommand(String userInput) throws ParseException {
8586
case FindExercisesCommand.COMMAND_WORD:
8687
return new FindExercisesCommandParser().parse(arguments);
8788

89+
case FindLessonsCommand.COMMAND_WORD:
90+
return new FindLessonsCommandParser().parse(arguments);
91+
8892
case ListCommand.COMMAND_WORD:
8993
return new ListCommand();
9094

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.FindLessonsCommand;
8+
import seedu.address.logic.parser.exceptions.ParseException;
9+
import seedu.address.model.person.LessonNameContainsKeywordsPredicate;
10+
11+
/**
12+
* Parses input arguments and creates a new FindLessonsCommand object
13+
*/
14+
public class FindLessonsCommandParser implements Parser<FindLessonsCommand> {
15+
16+
/**
17+
* Parses the given {@code String} of arguments in the context of the FindLessonsCommand
18+
* and returns a FindLessonsCommand object for execution.
19+
* @throws ParseException if the user input does not conform the expected format
20+
*/
21+
public FindLessonsCommand 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, FindLessonsCommand.MESSAGE_USAGE));
26+
}
27+
28+
String[] nameKeywords = trimmedArgs.split("\\s+");
29+
30+
return new FindLessonsCommand(new LessonNameContainsKeywordsPredicate(Arrays.asList(nameKeywords)));
31+
}
32+
33+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.util.stream.Stream;
1010

1111
import seedu.address.logic.commands.TimetableAddLessonCommand;
12-
import seedu.address.logic.commands.TimetableAddRoutineCommand;
1312
import seedu.address.logic.parser.exceptions.ParseException;
1413
import seedu.address.model.person.Day;
1514
import seedu.address.model.person.Duration;
@@ -32,7 +31,7 @@ public TimetableAddLessonCommand parse(String args) throws ParseException {
3231
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_DAY, PREFIX_TIME)
3332
|| !argMultimap.getPreamble().isEmpty()) {
3433
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
35-
TimetableAddRoutineCommand.MESSAGE_USAGE));
34+
TimetableAddLessonCommand.MESSAGE_USAGE));
3635
}
3736

3837
Name lessonName = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import java.util.stream.Stream;
88

9-
import seedu.address.logic.commands.TimetableAddRoutineCommand;
109
import seedu.address.logic.commands.TimetableDeleteSlotCommand;
1110
import seedu.address.logic.parser.exceptions.ParseException;
1211
import seedu.address.model.person.Day;
@@ -30,7 +29,7 @@ public TimetableDeleteSlotCommand parse(String args) throws ParseException {
3029
if (!arePrefixesPresent(argMultimap, PREFIX_DAY, PREFIX_TIME)
3130
|| !argMultimap.getPreamble().isEmpty()) {
3231
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
33-
TimetableAddRoutineCommand.MESSAGE_USAGE));
32+
TimetableDeleteSlotCommand.MESSAGE_USAGE));
3433
}
3534

3635
Day day = ParserUtil.parseDay(argMultimap.getValue(PREFIX_DAY).get());
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 Lesson}'s {@code Name} matches any of the keywords given.
10+
*/
11+
public class LessonNameContainsKeywordsPredicate implements Predicate<Lesson> {
12+
private final List<String> keywords;
13+
14+
public LessonNameContainsKeywordsPredicate(List<String> keywords) {
15+
this.keywords = keywords;
16+
}
17+
18+
@Override
19+
public boolean test(Lesson lesson) {
20+
return keywords.stream()
21+
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(lesson.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 LessonNameContainsKeywordsPredicate // instanceof handles nulls
28+
&& keywords.equals(((LessonNameContainsKeywordsPredicate) other).keywords)); // state check
29+
}
30+
31+
}

0 commit comments

Comments
 (0)