Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2021S1#73 from iqbxl/branch-Lesson-find
Browse files Browse the repository at this point in the history
Branch lesson find
  • Loading branch information
khor-jingqian authored Oct 20, 2020
2 parents 27a53af + a71e364 commit 643120f
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 7 deletions.
42 changes: 42 additions & 0 deletions src/main/java/seedu/address/logic/commands/FindLessonsCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import seedu.address.commons.core.Messages;
import seedu.address.model.Model;
import seedu.address.model.person.LessonNameContainsKeywordsPredicate;

/**
* Finds and lists all lessons in fitNUS whose name contains any of the argument keywords.
* Keyword matching is case insensitive.
*/
public class FindLessonsCommand extends Command {

public static final String COMMAND_WORD = "find_lessons";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all lessons whose names 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 + " CS1231";

private final LessonNameContainsKeywordsPredicate predicate;

public FindLessonsCommand(LessonNameContainsKeywordsPredicate predicate) {
this.predicate = predicate;
}

@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredLessonList(predicate);
return new CommandResult(
String.format(Messages.MESSAGE_LESSONS_LISTED_OVERVIEW, model.getFilteredLessonList().size()));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof FindLessonsCommand // instanceof handles nulls
&& predicate.equals(((FindLessonsCommand) other).predicate)); // state check
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class TimetableAddLessonCommand extends Command {
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a Lesson to the timetable in fitNUS. "
+ "Parameters: "
+ PREFIX_NAME + "LESSON_NAME "
+ PREFIX_DAY + "DAY"
+ PREFIX_DAY + "DAY "
+ PREFIX_TIME + "TIME"
+ "\n"
+ "Example: " + COMMAND_WORD + " "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class TimetableAddRoutineCommand extends Command {
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a Routine to the timetable in fitNUS. "
+ "Parameters: "
+ PREFIX_ROUTINE + "ROUTINE_NAME "
+ PREFIX_DAY + "DAY"
+ PREFIX_DAY + "DAY "
+ PREFIX_TIME + "TIME"
+ "\n"
+ "Example: " + COMMAND_WORD + " "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class TimetableDeleteSlotCommand extends Command {

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Deletes the slot identified by its day and time.\n"
+ "Parameters: "
+ PREFIX_DAY + "DAY"
+ PREFIX_DAY + "DAY "
+ PREFIX_TIME + "TIME"
+ "\n"
+ "Example: " + COMMAND_WORD + " "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.FindExercisesCommand;
import seedu.address.logic.commands.FindLessonsCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.LessonAddCommand;
import seedu.address.logic.commands.LessonDeleteCommand;
Expand Down Expand Up @@ -85,6 +86,9 @@ public Command parseCommand(String userInput) throws ParseException {
case FindExercisesCommand.COMMAND_WORD:
return new FindExercisesCommandParser().parse(arguments);

case FindLessonsCommand.COMMAND_WORD:
return new FindLessonsCommandParser().parse(arguments);

case ListCommand.COMMAND_WORD:
return new ListCommand();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import java.util.Arrays;

import seedu.address.logic.commands.FindLessonsCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.LessonNameContainsKeywordsPredicate;

/**
* Parses input arguments and creates a new FindLessonsCommand object
*/
public class FindLessonsCommandParser implements Parser<FindLessonsCommand> {

/**
* Parses the given {@code String} of arguments in the context of the FindLessonsCommand
* and returns a FindLessonsCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public FindLessonsCommand parse(String args) throws ParseException {
String trimmedArgs = args.trim();
if (trimmedArgs.isEmpty()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindLessonsCommand.MESSAGE_USAGE));
}

String[] nameKeywords = trimmedArgs.split("\\s+");

return new FindLessonsCommand(new LessonNameContainsKeywordsPredicate(Arrays.asList(nameKeywords)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.stream.Stream;

import seedu.address.logic.commands.TimetableAddLessonCommand;
import seedu.address.logic.commands.TimetableAddRoutineCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Day;
import seedu.address.model.person.Duration;
Expand All @@ -32,7 +31,7 @@ public TimetableAddLessonCommand parse(String args) throws ParseException {
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_DAY, PREFIX_TIME)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
TimetableAddRoutineCommand.MESSAGE_USAGE));
TimetableAddLessonCommand.MESSAGE_USAGE));
}

Name lessonName = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import java.util.stream.Stream;

import seedu.address.logic.commands.TimetableAddRoutineCommand;
import seedu.address.logic.commands.TimetableDeleteSlotCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Day;
Expand All @@ -30,7 +29,7 @@ public TimetableDeleteSlotCommand parse(String args) throws ParseException {
if (!arePrefixesPresent(argMultimap, PREFIX_DAY, PREFIX_TIME)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
TimetableAddRoutineCommand.MESSAGE_USAGE));
TimetableDeleteSlotCommand.MESSAGE_USAGE));
}

Day day = ParserUtil.parseDay(argMultimap.getValue(PREFIX_DAY).get());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package seedu.address.model.person;

import java.util.List;
import java.util.function.Predicate;

import seedu.address.commons.util.StringUtil;

/**
* Tests that a {@code Lesson}'s {@code Name} matches any of the keywords given.
*/
public class LessonNameContainsKeywordsPredicate implements Predicate<Lesson> {
private final List<String> keywords;

public LessonNameContainsKeywordsPredicate(List<String> keywords) {
this.keywords = keywords;
}

@Override
public boolean test(Lesson lesson) {
return keywords.stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(lesson.getName().fullName, keyword));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof LessonNameContainsKeywordsPredicate // instanceof handles nulls
&& keywords.equals(((LessonNameContainsKeywordsPredicate) other).keywords)); // state check
}

}

0 comments on commit 643120f

Please sign in to comment.