Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/AY2324S2-CS2103T-F10-1/tp
Browse files Browse the repository at this point in the history
…into branch-update-help-command
  • Loading branch information
iamtr committed Apr 9, 2024
2 parents db0e90f + cb78853 commit bb1d945
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/main/java/seedu/address/logic/parser/FindCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,27 @@ public FindCommand parse(String args) throws ParseException {

ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_IC);

if (!argMultimap.getPreamble().isEmpty()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_IC);

if (argMultimap.getValue(PREFIX_NAME).isPresent() && !argMultimap.getValue(PREFIX_IC).isPresent()) {
if (!hasOneParamOnly(argMultimap)) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}

if (argMultimap.getValue(PREFIX_NAME).isPresent()) {
String keywords = argMultimap.getValue(PREFIX_NAME).get();
if (keywords.equals("")) {
return new FindCommand(new NameContainsKeywordsPredicate(Collections.emptyList()));
} else {
String[] nameKeywords = keywords.split("\\s+");
return new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList(nameKeywords)));
}
} else if (argMultimap.getValue(PREFIX_IC).isPresent() && !argMultimap.getValue(PREFIX_NAME).isPresent()) {
} else if (argMultimap.getValue(PREFIX_IC).isPresent()) {
String keyword = argMultimap.getValue(PREFIX_IC).get();
if (keyword.split("\\s+").length != 1) {
throw new ParseException(
Expand All @@ -55,4 +65,16 @@ public FindCommand parse(String args) throws ParseException {
}
}

/**
* Check if only 1 parameter is used.
* @param argMultimap Input being mapped.
* @return true if only name or ic input.
*/
private boolean hasOneParamOnly(ArgumentMultimap argMultimap) {
boolean hasNameOnly =
argMultimap.getValue(PREFIX_NAME).isPresent() && !argMultimap.getValue(PREFIX_IC).isPresent();
boolean hasIcOnly =
argMultimap.getValue(PREFIX_IC).isPresent() && !argMultimap.getValue(PREFIX_NAME).isPresent();
return hasIcOnly || hasNameOnly;
}
}

0 comments on commit bb1d945

Please sign in to comment.