-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update FindCommand to handle prefixes #68
Changes from 3 commits
d9737bb
16624b6
e54ffec
dee0376
70498be
c3d5634
763b209
8c6a435
c3267f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,51 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_COURSE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.FindCommand; | ||
import seedu.address.logic.commands.FindNameCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.student.CourseContainsKeywordsPredicate; | ||
import seedu.address.model.student.NameContainsKeywordsPredicate; | ||
import seedu.address.model.student.Student; | ||
|
||
/** | ||
* Parses input arguments and creates a new FindNameCommand object | ||
* Parses input arguments and creates a new FindCommand object | ||
*/ | ||
public class FindCommandParser implements Parser<FindCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the FindNameCommand | ||
* and returns a FindNameCommand object for execution. | ||
* Parses the given {@code String} of arguments in the context of the FindCommand | ||
* and returns a FindCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public FindCommand parse(String args) throws ParseException { | ||
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME); | ||
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_COURSE); | ||
|
||
if (!argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindNameCommand.MESSAGE_USAGE)); | ||
if (argMultimap.getPreamble().isEmpty() | ||
&& (!arePrefixesPresent(argMultimap, PREFIX_NAME) | ||
&& !arePrefixesPresent(argMultimap, PREFIX_COURSE))) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_NAME)) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindNameCommand.MESSAGE_USAGE)); | ||
Predicate<Student> predicate = person -> true; | ||
|
||
if (arePrefixesPresent(argMultimap, PREFIX_NAME)) { | ||
predicate = predicate.and(new NameContainsKeywordsPredicate(argMultimap.getAllValues(PREFIX_NAME))); | ||
} | ||
|
||
List<String> nameKeywords = argMultimap.getAllValues(PREFIX_NAME); | ||
if (arePrefixesPresent(argMultimap, PREFIX_COURSE)) { | ||
predicate = predicate.and(new CourseContainsKeywordsPredicate(argMultimap.getAllValues(PREFIX_COURSE))); | ||
} | ||
|
||
return new FindNameCommand(new NameContainsKeywordsPredicate(nameKeywords)); | ||
return new FindCommand(predicate); | ||
} | ||
|
||
/** | ||
* Returns true if none of the prefixes contains empty {@code Optional} values in the given | ||
* {@code ArgumentMultimap}. | ||
*/ | ||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
return Stream.of(prefixes).anyMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package seedu.address.model.student; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.util.StringUtil; | ||
|
||
/** | ||
* Tests that a {@code Course}'s {@code courseCode} matches any of the keywords given. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we want course matching to be exact and case-insensitive, because a tutor for CS2040 may want to exclude students from CS2040S for example. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've worked on this, see #71. |
||
*/ | ||
public class CourseContainsKeywordsPredicate implements Predicate<Student> { | ||
private final List<String> keywords; | ||
|
||
public CourseContainsKeywordsPredicate(List<String> keywords) { | ||
this.keywords = keywords; | ||
} | ||
Check warning on line 16 in src/main/java/seedu/address/model/student/CourseContainsKeywordsPredicate.java Codecov / codecov/patchsrc/main/java/seedu/address/model/student/CourseContainsKeywordsPredicate.java#L14-L16
|
||
|
||
@Override | ||
public boolean test(Student student) { | ||
return keywords.stream() | ||
.anyMatch(keyword -> | ||
student.getCourses().stream() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand this works, but maybe for the sake of readability / slap / less nesting etc, maybe a separate function to get a student's courses as a |
||
.anyMatch(course -> | ||
StringUtil.containsWordIgnoreCase(course.courseCode, keyword))); | ||
Check warning on line 24 in src/main/java/seedu/address/model/student/CourseContainsKeywordsPredicate.java Codecov / codecov/patchsrc/main/java/seedu/address/model/student/CourseContainsKeywordsPredicate.java#L20-L24
|
||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof CourseContainsKeywordsPredicate // instanceof handles nulls | ||
&& keywords.equals(((CourseContainsKeywordsPredicate) other).keywords)); // state check | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logic for this looks good.