From 54b5b16f06fa413905bf40e90c5f206e25882029 Mon Sep 17 00:00:00 2001 From: nicolengk Date: Sun, 29 Oct 2023 15:18:23 +0800 Subject: [PATCH 1/4] Add findcust feature --- ...FindCommand.java => FindCustomerCommand.java} | 12 ++++++------ .../address/logic/parser/AddressBookParser.java | 6 +++--- ...arser.java => FindCustomerCommandParser.java} | 10 +++++----- ...andTest.java => FindCustomerCommandTest.java} | 16 ++++++++-------- .../logic/parser/AddressBookParserTest.java | 8 ++++---- ...t.java => FindCustomerCommandParserTest.java} | 12 ++++++------ 6 files changed, 32 insertions(+), 32 deletions(-) rename src/main/java/seedu/address/logic/commands/{FindCommand.java => FindCustomerCommand.java} (79%) rename src/main/java/seedu/address/logic/parser/{FindCommandParser.java => FindCustomerCommandParser.java} (70%) rename src/test/java/seedu/address/logic/commands/{FindCommandTest.java => FindCustomerCommandTest.java} (85%) rename src/test/java/seedu/address/logic/parser/{FindCommandParserTest.java => FindCustomerCommandParserTest.java} (68%) diff --git a/src/main/java/seedu/address/logic/commands/FindCommand.java b/src/main/java/seedu/address/logic/commands/FindCustomerCommand.java similarity index 79% rename from src/main/java/seedu/address/logic/commands/FindCommand.java rename to src/main/java/seedu/address/logic/commands/FindCustomerCommand.java index 43f27f730cf..fd8562af632 100644 --- a/src/main/java/seedu/address/logic/commands/FindCommand.java +++ b/src/main/java/seedu/address/logic/commands/FindCustomerCommand.java @@ -11,9 +11,9 @@ * Finds and lists all customers in address book whose name contains any of the argument keywords. * Keyword matching is case insensitive. */ -public class FindCommand extends Command { +public class FindCustomerCommand extends Command { - public static final String COMMAND_WORD = "find"; + public static final String COMMAND_WORD = "findcust"; public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all customers whose names contain any of " + "the specified keywords (case-insensitive) and displays them as a list with index numbers.\n" @@ -22,7 +22,7 @@ public class FindCommand extends Command { private final NameContainsKeywordsPredicate predicate; - public FindCommand(NameContainsKeywordsPredicate predicate) { + public FindCustomerCommand(NameContainsKeywordsPredicate predicate) { this.predicate = predicate; } @@ -41,12 +41,12 @@ public boolean equals(Object other) { } // instanceof handles nulls - if (!(other instanceof FindCommand)) { + if (!(other instanceof FindCustomerCommand)) { return false; } - FindCommand otherFindCommand = (FindCommand) other; - return predicate.equals(otherFindCommand.predicate); + FindCustomerCommand otherFindCustomerCommand = (FindCustomerCommand) other; + return predicate.equals(otherFindCustomerCommand.predicate); } @Override diff --git a/src/main/java/seedu/address/logic/parser/AddressBookParser.java b/src/main/java/seedu/address/logic/parser/AddressBookParser.java index 507ecaf16f1..29a611cbdc6 100644 --- a/src/main/java/seedu/address/logic/parser/AddressBookParser.java +++ b/src/main/java/seedu/address/logic/parser/AddressBookParser.java @@ -17,7 +17,7 @@ import seedu.address.logic.commands.EditCommand; import seedu.address.logic.commands.EditPropertyCommand; import seedu.address.logic.commands.ExitCommand; -import seedu.address.logic.commands.FindCommand; +import seedu.address.logic.commands.FindCustomerCommand; import seedu.address.logic.commands.HelpCommand; import seedu.address.logic.commands.ListCustomerCommand; import seedu.address.logic.commands.ListPropertyCommand; @@ -78,8 +78,8 @@ public Command parseCommand(String userInput) throws ParseException { case ClearCommand.COMMAND_WORD: return new ClearCommand(); - case FindCommand.COMMAND_WORD: - return new FindCommandParser().parse(arguments); + case FindCustomerCommand.COMMAND_WORD: + return new FindCustomerCommandParser().parse(arguments); case ListCustomerCommand.COMMAND_WORD: return new ListCustomerCommand(); diff --git a/src/main/java/seedu/address/logic/parser/FindCommandParser.java b/src/main/java/seedu/address/logic/parser/FindCustomerCommandParser.java similarity index 70% rename from src/main/java/seedu/address/logic/parser/FindCommandParser.java rename to src/main/java/seedu/address/logic/parser/FindCustomerCommandParser.java index 09f51a50592..1431f022a0f 100644 --- a/src/main/java/seedu/address/logic/parser/FindCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/FindCustomerCommandParser.java @@ -4,30 +4,30 @@ import java.util.Arrays; -import seedu.address.logic.commands.FindCommand; +import seedu.address.logic.commands.FindCustomerCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.customer.NameContainsKeywordsPredicate; /** * Parses input arguments and creates a new FindCommand object */ -public class FindCommandParser implements Parser { +public class FindCustomerCommandParser implements Parser { /** * 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 FindCustomerCommand parse(String args) throws ParseException { String trimmedArgs = args.trim(); if (trimmedArgs.isEmpty()) { throw new ParseException( - String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE)); + String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCustomerCommand.MESSAGE_USAGE)); } String[] nameKeywords = trimmedArgs.split("\\s+"); - return new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList(nameKeywords))); + return new FindCustomerCommand(new NameContainsKeywordsPredicate(Arrays.asList(nameKeywords))); } } diff --git a/src/test/java/seedu/address/logic/commands/FindCommandTest.java b/src/test/java/seedu/address/logic/commands/FindCustomerCommandTest.java similarity index 85% rename from src/test/java/seedu/address/logic/commands/FindCommandTest.java rename to src/test/java/seedu/address/logic/commands/FindCustomerCommandTest.java index 25f4b538a96..b46c3f692c7 100644 --- a/src/test/java/seedu/address/logic/commands/FindCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/FindCustomerCommandTest.java @@ -24,7 +24,7 @@ /** * Contains integration tests (interaction with the Model) for {@code FindCommand}. */ -public class FindCommandTest { +public class FindCustomerCommandTest { private Model model = new ModelManager(getTypicalAddressBook(), getTypicalPropertyBook(), new UserPrefs()); private Model expectedModel = new ModelManager(getTypicalAddressBook(), getTypicalPropertyBook(), new UserPrefs()); @@ -35,14 +35,14 @@ public void equals() { NameContainsKeywordsPredicate secondPredicate = new NameContainsKeywordsPredicate(Collections.singletonList("second")); - FindCommand findFirstCommand = new FindCommand(firstPredicate); - FindCommand findSecondCommand = new FindCommand(secondPredicate); + FindCustomerCommand findFirstCommand = new FindCustomerCommand(firstPredicate); + FindCustomerCommand findSecondCommand = new FindCustomerCommand(secondPredicate); // same object -> returns true assertTrue(findFirstCommand.equals(findFirstCommand)); // same values -> returns true - FindCommand findFirstCommandCopy = new FindCommand(firstPredicate); + FindCustomerCommand findFirstCommandCopy = new FindCustomerCommand(firstPredicate); assertTrue(findFirstCommand.equals(findFirstCommandCopy)); // different types -> returns false @@ -59,7 +59,7 @@ public void equals() { public void execute_zeroKeywords_noCustomerFound() { String expectedMessage = String.format(MESSAGE_CUSTOMERS_LISTED_OVERVIEW, 0); NameContainsKeywordsPredicate predicate = preparePredicate(" "); - FindCommand command = new FindCommand(predicate); + FindCustomerCommand command = new FindCustomerCommand(predicate); expectedModel.updateFilteredCustomerList(predicate); assertCommandSuccess(command, model, expectedMessage, expectedModel); assertEquals(Collections.emptyList(), model.getFilteredCustomerList()); @@ -69,7 +69,7 @@ public void execute_zeroKeywords_noCustomerFound() { public void execute_multipleKeywords_multipleCustomersFound() { String expectedMessage = String.format(MESSAGE_CUSTOMERS_LISTED_OVERVIEW, 3); NameContainsKeywordsPredicate predicate = preparePredicate("Kurz Elle Kunz"); - FindCommand command = new FindCommand(predicate); + FindCustomerCommand command = new FindCustomerCommand(predicate); expectedModel.updateFilteredCustomerList(predicate); assertCommandSuccess(command, model, expectedMessage, expectedModel); assertEquals(Arrays.asList(CARL, ELLE, FIONA), model.getFilteredCustomerList()); @@ -78,8 +78,8 @@ public void execute_multipleKeywords_multipleCustomersFound() { @Test public void toStringMethod() { NameContainsKeywordsPredicate predicate = new NameContainsKeywordsPredicate(Arrays.asList("keyword")); - FindCommand findCommand = new FindCommand(predicate); - String expected = FindCommand.class.getCanonicalName() + "{predicate=" + predicate + "}"; + FindCustomerCommand findCommand = new FindCustomerCommand(predicate); + String expected = FindCustomerCommand.class.getCanonicalName() + "{predicate=" + predicate + "}"; assertEquals(expected, findCommand.toString()); } diff --git a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java index ae6817bc2cc..4a499c1570d 100644 --- a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java +++ b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java @@ -22,7 +22,7 @@ import seedu.address.logic.commands.EditCommand; import seedu.address.logic.commands.EditPropertyCommand; import seedu.address.logic.commands.ExitCommand; -import seedu.address.logic.commands.FindCommand; +import seedu.address.logic.commands.FindCustomerCommand; import seedu.address.logic.commands.HelpCommand; import seedu.address.logic.commands.ListCustomerCommand; import seedu.address.logic.parser.exceptions.ParseException; @@ -101,9 +101,9 @@ public void parseCommand_exit() throws Exception { @Test public void parseCommand_find() throws Exception { List keywords = Arrays.asList("foo", "bar", "baz"); - FindCommand command = (FindCommand) parser.parseCommand( - FindCommand.COMMAND_WORD + " " + keywords.stream().collect(Collectors.joining(" "))); - assertEquals(new FindCommand(new NameContainsKeywordsPredicate(keywords)), command); + FindCustomerCommand command = (FindCustomerCommand) parser.parseCommand( + FindCustomerCommand.COMMAND_WORD + " " + keywords.stream().collect(Collectors.joining(" "))); + assertEquals(new FindCustomerCommand(new NameContainsKeywordsPredicate(keywords)), command); } @Test diff --git a/src/test/java/seedu/address/logic/parser/FindCommandParserTest.java b/src/test/java/seedu/address/logic/parser/FindCustomerCommandParserTest.java similarity index 68% rename from src/test/java/seedu/address/logic/parser/FindCommandParserTest.java rename to src/test/java/seedu/address/logic/parser/FindCustomerCommandParserTest.java index 41749c34f0e..e07ee6982a8 100644 --- a/src/test/java/seedu/address/logic/parser/FindCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/FindCustomerCommandParserTest.java @@ -8,23 +8,23 @@ import org.junit.jupiter.api.Test; -import seedu.address.logic.commands.FindCommand; +import seedu.address.logic.commands.FindCustomerCommand; import seedu.address.model.customer.NameContainsKeywordsPredicate; -public class FindCommandParserTest { +public class FindCustomerCommandParserTest { - private FindCommandParser parser = new FindCommandParser(); + private FindCustomerCommandParser parser = new FindCustomerCommandParser(); @Test public void parse_emptyArg_throwsParseException() { - assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE)); + assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCustomerCommand.MESSAGE_USAGE)); } @Test public void parse_validArgs_returnsFindCommand() { // no leading and trailing whitespaces - FindCommand expectedFindCommand = - new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob"))); + FindCustomerCommand expectedFindCommand = + new FindCustomerCommand(new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob"))); assertParseSuccess(parser, "Alice Bob", expectedFindCommand); // multiple whitespaces between keywords From 5e88f031ea408c6a8b45ae185ac2e2e3f7ab08f7 Mon Sep 17 00:00:00 2001 From: nicolengk Date: Sun, 29 Oct 2023 16:13:00 +0800 Subject: [PATCH 2/4] Add findcust feature --- .../address/commons/util/StringUtil.java | 26 +++++++++++++++++++ .../NameContainsKeywordsPredicate.java | 3 ++- .../parser/FindCustomerCommandParserTest.java | 3 ++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/commons/util/StringUtil.java b/src/main/java/seedu/address/commons/util/StringUtil.java index 5e2993045f7..cd65c1ed0e0 100644 --- a/src/main/java/seedu/address/commons/util/StringUtil.java +++ b/src/main/java/seedu/address/commons/util/StringUtil.java @@ -38,6 +38,32 @@ public static boolean containsWordIgnoreCase(String sentence, String word) { .anyMatch(preppedWord::equalsIgnoreCase); } + /** + * Returns true if the {@code sentence} contains the {@code word}. + * Ignores case and a full word match is NOT required. + *
examples:
+     *       containsWordIgnoreCase("ABc def", "abc") == true
+     *       containsWordIgnoreCase("ABc def", "DEF") == true
+     *       containsWordIgnoreCase("ABc def", "AB") == true // full word match not needed
+     *       
+ * @param sentence cannot be null + * @param word cannot be null, cannot be empty, must be a single word + */ + public static boolean containsWordIgnoreCaseWithoutFullMatch(String sentence, String word) { + requireNonNull(sentence); + requireNonNull(word); + + String preppedWord = word.trim(); + checkArgument(!preppedWord.isEmpty(), "Word parameter cannot be empty"); + checkArgument(preppedWord.split("\\s+").length == 1, "Word parameter should be a single word"); + + String preppedSentence = sentence; + String[] wordsInPreppedSentence = preppedSentence.split("\\s+"); + + return Arrays.stream(wordsInPreppedSentence) + .anyMatch(w -> w.toLowerCase().contains(preppedWord.toLowerCase())); + } + /** * Returns a detailed message of the t, including the stack trace. */ diff --git a/src/main/java/seedu/address/model/customer/NameContainsKeywordsPredicate.java b/src/main/java/seedu/address/model/customer/NameContainsKeywordsPredicate.java index 4d98ef21b0b..c64cbdaa46e 100644 --- a/src/main/java/seedu/address/model/customer/NameContainsKeywordsPredicate.java +++ b/src/main/java/seedu/address/model/customer/NameContainsKeywordsPredicate.java @@ -19,7 +19,8 @@ public NameContainsKeywordsPredicate(List keywords) { @Override public boolean test(Customer customer) { return keywords.stream() - .anyMatch(keyword -> StringUtil.containsWordIgnoreCase(customer.getName().fullName, keyword)); + .anyMatch(keyword -> StringUtil.containsWordIgnoreCaseWithoutFullMatch(customer.getName().fullName, + keyword)); } @Override diff --git a/src/test/java/seedu/address/logic/parser/FindCustomerCommandParserTest.java b/src/test/java/seedu/address/logic/parser/FindCustomerCommandParserTest.java index e07ee6982a8..6e0d07b722a 100644 --- a/src/test/java/seedu/address/logic/parser/FindCustomerCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/FindCustomerCommandParserTest.java @@ -17,7 +17,8 @@ public class FindCustomerCommandParserTest { @Test public void parse_emptyArg_throwsParseException() { - assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCustomerCommand.MESSAGE_USAGE)); + assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, + FindCustomerCommand.MESSAGE_USAGE)); } @Test From b18fc2e644046e39e0a8c83e609711c39c27a096 Mon Sep 17 00:00:00 2001 From: nicolengk Date: Sun, 29 Oct 2023 16:23:59 +0800 Subject: [PATCH 3/4] Update findcust feature --- src/main/java/seedu/address/logic/parser/AddressBookParser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/logic/parser/AddressBookParser.java b/src/main/java/seedu/address/logic/parser/AddressBookParser.java index d2c064b438f..a88909a0194 100644 --- a/src/main/java/seedu/address/logic/parser/AddressBookParser.java +++ b/src/main/java/seedu/address/logic/parser/AddressBookParser.java @@ -17,8 +17,8 @@ import seedu.address.logic.commands.EditCommand; import seedu.address.logic.commands.EditPropertyCommand; import seedu.address.logic.commands.ExitCommand; -import seedu.address.logic.commands.FindCustomerCommand; import seedu.address.logic.commands.FilterCustomerCommand; +import seedu.address.logic.commands.FindCustomerCommand; import seedu.address.logic.commands.HelpCommand; import seedu.address.logic.commands.ListCustomerCommand; import seedu.address.logic.commands.ListPropertyCommand; From c1cb1cf2db3ace50f13920be7f6d6c9f1921a445 Mon Sep 17 00:00:00 2001 From: nicolengk Date: Mon, 30 Oct 2023 20:22:32 +0800 Subject: [PATCH 4/4] Update findcust feature --- .../address/commons/util/StringUtil.java | 21 +++++++++++++++++++ .../NameContainsKeywordsPredicate.java | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/commons/util/StringUtil.java b/src/main/java/seedu/address/commons/util/StringUtil.java index cd65c1ed0e0..36945e7bdcf 100644 --- a/src/main/java/seedu/address/commons/util/StringUtil.java +++ b/src/main/java/seedu/address/commons/util/StringUtil.java @@ -64,6 +64,27 @@ public static boolean containsWordIgnoreCaseWithoutFullMatch(String sentence, St .anyMatch(w -> w.toLowerCase().contains(preppedWord.toLowerCase())); } + /** + * Returns true if the {@code sentence} start with the {@code word}. + * Ignores case and a full word match is NOT required. + * @param sentence cannot be null + * @param word cannot be null, cannot be empty, must be a single word + */ + public static boolean startsWithWordIgnoreCaseWithoutFullMatch(String sentence, String word) { + requireNonNull(sentence); + requireNonNull(word); + + String preppedWord = word.trim(); + checkArgument(!preppedWord.isEmpty(), "Word parameter cannot be empty"); + checkArgument(preppedWord.split("\\s+").length == 1, "Word parameter should be a single word"); + + String preppedSentence = sentence; + String[] wordsInPreppedSentence = preppedSentence.split("\\s+"); + + return Arrays.stream(wordsInPreppedSentence) + .anyMatch(w -> w.toLowerCase().startsWith(preppedWord.toLowerCase())); + } + /** * Returns a detailed message of the t, including the stack trace. */ diff --git a/src/main/java/seedu/address/model/customer/NameContainsKeywordsPredicate.java b/src/main/java/seedu/address/model/customer/NameContainsKeywordsPredicate.java index c64cbdaa46e..5b361bffaf4 100644 --- a/src/main/java/seedu/address/model/customer/NameContainsKeywordsPredicate.java +++ b/src/main/java/seedu/address/model/customer/NameContainsKeywordsPredicate.java @@ -19,7 +19,7 @@ public NameContainsKeywordsPredicate(List keywords) { @Override public boolean test(Customer customer) { return keywords.stream() - .anyMatch(keyword -> StringUtil.containsWordIgnoreCaseWithoutFullMatch(customer.getName().fullName, + .anyMatch(keyword -> StringUtil.startsWithWordIgnoreCaseWithoutFullMatch(customer.getName().fullName, keyword)); }