forked from nus-cs2103-AY2425S1/tp
-
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
Closed
yhanyi
wants to merge
9
commits into
AY2425S1-CS2103T-F13-1:master
from
yhanyi:feature/update-findcommand
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d9737bb
Revert abstract FindCommand
yhanyi 16624b6
Delete FindNameCommand files
yhanyi e54ffec
Update FindCommand to handle specific prefixes
yhanyi dee0376
Update test json to contain courses
yhanyi 70498be
Update CARL to build with courses
yhanyi c3d5634
Refactor name predicate equals method
yhanyi 763b209
Update parseCommand_find to test new parsing
yhanyi 8c6a435
Update FindCommand tests
yhanyi c3267f1
Update FindCommandParser
yhanyi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
19 changes: 0 additions & 19 deletions
19
src/main/java/seedu/address/logic/commands/FindNameCommand.java
This file was deleted.
Oops, something went wrong.
43 changes: 27 additions & 16 deletions
43
src/main/java/seedu/address/logic/parser/FindCommandParser.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 |
---|---|---|
@@ -1,47 +1,58 @@ | ||
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.ArrayList; | ||
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)); | ||
List<Predicate<Student>> predicates = new ArrayList<>(); | ||
|
||
if (arePrefixesPresent(argMultimap, PREFIX_NAME)) { | ||
NameContainsKeywordsPredicate namePredicate = | ||
new NameContainsKeywordsPredicate(argMultimap.getAllValues(PREFIX_NAME)); | ||
predicates.add(namePredicate); | ||
} | ||
|
||
List<String> nameKeywords = argMultimap.getAllValues(PREFIX_NAME); | ||
if (arePrefixesPresent(argMultimap, PREFIX_COURSE)) { | ||
CourseContainsKeywordsPredicate coursePredicate = | ||
new CourseContainsKeywordsPredicate(argMultimap.getAllValues(PREFIX_COURSE)); | ||
predicates.add(coursePredicate); | ||
} | ||
|
||
return new FindNameCommand(new NameContainsKeywordsPredicate(nameKeywords)); | ||
Predicate<Student> finalPredicate = predicates.stream().reduce(x -> true, Predicate::and); | ||
return new FindCommand(finalPredicate); | ||
} | ||
|
||
/** | ||
* 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()); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/seedu/address/model/student/CourseContainsKeywordsPredicate.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,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. | ||
*/ | ||
public class CourseContainsKeywordsPredicate implements Predicate<Student> { | ||
private final List<String> keywords; | ||
|
||
public CourseContainsKeywordsPredicate(List<String> keywords) { | ||
this.keywords = keywords; | ||
} | ||
|
||
@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))); | ||
} | ||
|
||
@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 | ||
} | ||
} |
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
I've worked on this, see #71.