Skip to content

Commit

Permalink
Add logic to check find type
Browse files Browse the repository at this point in the history
Use Regex to check which tag is input to find
  • Loading branch information
KrashKart committed Oct 10, 2024
1 parent dea071e commit 6b6b9a0
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/main/java/seedu/address/logic/parser/FindCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,44 @@
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import seedu.address.logic.commands.AbstractFindCommand;
import seedu.address.logic.commands.FindByNameCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.NameContainsKeywordsPredicate;

/**
* Parses input arguments and creates a new FindCommand object
*/
public class FindCommandParser implements Parser<FindCommand> {
public class FindCommandParser implements Parser<AbstractFindCommand> {

public static final Pattern KEYWORD_EXTRACTOR =
Pattern.compile("^^(?<tag>/[enc]) (?<argument>.+)$");

/**
* 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 {
public AbstractFindCommand parse(String args) throws ParseException {
String trimmedArgs = args.trim();
if (trimmedArgs.isEmpty()) {
Matcher m = KEYWORD_EXTRACTOR.matcher(args);
if (trimmedArgs.isEmpty() || !m.matches()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}

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

return new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList(nameKeywords)));
String tag = m.group("tag");
String searchTerm = m.group("argument");
switch (tag) {
case "/n":
return new FindByNameCommand(
new NameContainsKeywordsPredicate(Arrays.asList(searchTerm)));
default:
return null; // temporary value, this should not occur due to regex
}
}

}

0 comments on commit 6b6b9a0

Please sign in to comment.