Skip to content

Commit

Permalink
Integrate FindByNameCommand into CampusConnect
Browse files Browse the repository at this point in the history
Let's
* add javadocs and comments for new methods
* change FindCommandParser to return approppriate class depending on
tag
  • Loading branch information
KrashKart committed Oct 10, 2024
1 parent 6b6b9a0 commit c789ccb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/seedu/address/logic/parser/FindCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,28 @@
public class FindCommandParser implements Parser<AbstractFindCommand> {

public static final Pattern KEYWORD_EXTRACTOR =
Pattern.compile("^^(?<tag>/[enc]) (?<argument>.+)$");
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 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(
Expand Down

0 comments on commit c789ccb

Please sign in to comment.