From c789ccb4b7e6975ab57ad4f628706d6023566af4 Mon Sep 17 00:00:00 2001 From: KrashKart Date: Thu, 10 Oct 2024 11:33:28 +0800 Subject: [PATCH] Integrate FindByNameCommand into CampusConnect Let's * add javadocs and comments for new methods * change FindCommandParser to return approppriate class depending on tag --- .../address/logic/commands/FindByNameCommand.java | 4 ++++ .../seedu/address/logic/parser/AddressBookParser.java | 3 ++- .../seedu/address/logic/parser/FindCommandParser.java | 11 ++++++++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/FindByNameCommand.java b/src/main/java/seedu/address/logic/commands/FindByNameCommand.java index 486496b8bf1..e46a6985577 100644 --- a/src/main/java/seedu/address/logic/commands/FindByNameCommand.java +++ b/src/main/java/seedu/address/logic/commands/FindByNameCommand.java @@ -2,6 +2,10 @@ import seedu.address.model.person.NameContainsKeywordsPredicate; +/** + * Finds and lists all persons in address book whose name contains any of the argument keywords. + * Keyword matching is case-insensitive. + */ public class FindByNameCommand extends AbstractFindCommand { public FindByNameCommand(NameContainsKeywordsPredicate predicate) { super(predicate); diff --git a/src/main/java/seedu/address/logic/parser/AddressBookParser.java b/src/main/java/seedu/address/logic/parser/AddressBookParser.java index 3149ee07e0b..7b5b2e6365e 100644 --- a/src/main/java/seedu/address/logic/parser/AddressBookParser.java +++ b/src/main/java/seedu/address/logic/parser/AddressBookParser.java @@ -8,6 +8,7 @@ import java.util.regex.Pattern; import seedu.address.commons.core.LogsCenter; +import seedu.address.logic.commands.AbstractFindCommand; import seedu.address.logic.commands.AddCommand; import seedu.address.logic.commands.ClearCommand; import seedu.address.logic.commands.Command; @@ -65,7 +66,7 @@ public Command parseCommand(String userInput) throws ParseException { case ClearCommand.COMMAND_WORD: return new ClearCommand(); - case FindCommand.COMMAND_WORD: + case AbstractFindCommand.COMMAND_WORD: return new FindCommandParser().parse(arguments); case ListCommand.COMMAND_WORD: diff --git a/src/main/java/seedu/address/logic/parser/FindCommandParser.java b/src/main/java/seedu/address/logic/parser/FindCommandParser.java index d173d2cdee6..1ff18b57a81 100644 --- a/src/main/java/seedu/address/logic/parser/FindCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/FindCommandParser.java @@ -18,7 +18,7 @@ public class FindCommandParser implements Parser { public static final Pattern KEYWORD_EXTRACTOR = - Pattern.compile("^^(?/[enc]) (?.+)$"); + Pattern.compile("^(?/[enc]) (?.+)$"); /** * Parses the given {@code String} of arguments in the context of the FindCommand @@ -26,15 +26,20 @@ public class FindCommandParser implements Parser { * @throws ParseException if the user input does not conform the expected format */ public AbstractFindCommand parse(String args) throws ParseException { - String trimmedArgs = args.trim(); - Matcher m = KEYWORD_EXTRACTOR.matcher(args); + String trimmedArgs = args.trim(); // trim space + Matcher m = KEYWORD_EXTRACTOR.matcher(trimmedArgs); // find tag and search words + + // will throw exception if no args/command format not correct if (trimmedArgs.isEmpty() || !m.matches()) { throw new ParseException( String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE)); } + // extract tag and search argument String tag = m.group("tag"); String searchTerm = m.group("argument"); + + // return approppriate FindCommand class depending on tag switch (tag) { case "/n": return new FindByNameCommand(