Skip to content

Commit

Permalink
Merge pull request #288 from jereeemyyyy/branch-U-NameContainsKeyword
Browse files Browse the repository at this point in the history
Update Name predicates
  • Loading branch information
Harithhh06 authored Nov 6, 2024
2 parents 04a9bbc + b138951 commit 6876d70
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 46 deletions.
12 changes: 9 additions & 3 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ public class Messages {
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
public static final String MESSAGE_DUPLICATE_FIELDS =
"Multiple values specified for the following single-valued field(s): ";
public static final String MESSAGE_PERSON_NOT_FOUND = "The client cannot be found in the clientHub \n"
+ "Please use the list command to see all clients!";
public static final String MESSAGE_PERSON_NOT_FOUND = "Client not found. \n"
+ "Please double check the name of the client!";
public static final String MESSAGE_ADDRESS_NOT_FOUND = "Client not found. \n"
+ "Please double check the address of the client!";
public static final String MESSAGE_PHONE_NOT_FOUND = "Client not found. \n"
+ "Please double check the phone number of the client!";
public static final String MESSAGE_CLIENT_TYPE_NOT_FOUND = "Client not found. \n"
+ "Please double check the client type(s) of the client!";
public static final String MESSAGE_VAGUE_DELETE = "Please be more specific in the name \n"
+ "or use $ to indicate the end of an EXACT name";
public static final String MESSAGE_PERSON_LISTED_OVERVIEW_FOR_VIEW = "%1$d client found for viewing!";
Expand Down Expand Up @@ -55,7 +61,7 @@ public static String getErrorMessageForDuplicatePrefixes(Prefix... duplicatePref
*/
public static String getMessagePersonsListedOverview(int count) {
if (count == 0) {
return "0 client listed!";
return "0 client listed!, please use the list command to see all clients!";
}
if (count == 1) {
return "1 client listed!";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.AddressContainsKeywordsPredicate;

Expand Down Expand Up @@ -35,9 +37,17 @@ public FindAddressCommand(AddressContainsKeywordsPredicate predicate) {
}

@Override
public CommandResult execute(Model model) {
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
model.updateFilteredPersonList(predicate);

// Check if there is anyone in the filtered list
if (model.getDisplayPersons().isEmpty()) {

// If noone found, show all persons (no change)
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
throw new CommandException(Messages.MESSAGE_ADDRESS_NOT_FOUND);
}
return new CommandResult(
Messages.getMessagePersonsListedOverview(model.getDisplayPersons().size()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CLIENT_TYPE;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
Expand Down Expand Up @@ -47,9 +48,9 @@ public CommandResult execute(Model model) throws CommandException {

// Check if there is anyone in the filtered list
if (model.getDisplayPersons().isEmpty()) {
throw new CommandException(
String.format(Messages.MESSAGE_NO_PERSON_FOUND_FOR_VIEW)
);
// If noone found, show all persons (no change)
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
throw new CommandException(Messages.MESSAGE_CLIENT_TYPE_NOT_FOUND);
}
return new CommandResult(
Messages.getMessagePersonsListedOverview(model.getDisplayPersons().size()));
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/seedu/address/logic/commands/FindNameCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.NameContainsKeywordsPredicate;

Expand Down Expand Up @@ -36,9 +38,18 @@ public FindNameCommand(NameContainsKeywordsPredicate predicate) {
}

@Override
public CommandResult execute(Model model) {
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
model.updateFilteredPersonList(predicate);

// Check if there is anyone in the filtered list
if (model.getDisplayPersons().isEmpty()) {

// If noone found, show all persons (no change)
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
throw new CommandException(Messages.MESSAGE_PERSON_NOT_FOUND);
}

return new CommandResult(
Messages.getMessagePersonsListedOverview(model.getDisplayPersons().size()));
}
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/seedu/address/logic/commands/FindPhoneCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.PhoneBeginsWithKeywordPredicate;

Expand Down Expand Up @@ -35,9 +37,17 @@ public FindPhoneCommand(PhoneBeginsWithKeywordPredicate predicate) {
}

@Override
public CommandResult execute(Model model) {
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
model.updateFilteredPersonList(predicate);

// Check if there is anyone in the filtered list
if (model.getDisplayPersons().isEmpty()) {

// If noone found, show all persons (no change)
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
throw new CommandException(Messages.MESSAGE_PHONE_NOT_FOUND);
}
return new CommandResult(
Messages.getMessagePersonsListedOverview(model.getDisplayPersons().size()));
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/seedu/address/logic/commands/ViewCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
Expand Down Expand Up @@ -28,7 +29,7 @@ public class ViewCommand extends Command {
"\nMultiple clients found. Please specify the name of your client further.";

public static final String NO_PERSON_FOUND_VIEW_MESSAGE =
"\nClient not found. Use the list command to see all clients";
"Client not found for viewing. Please double check the name of your client!";

public static final String SHOWING_VIEW_MESSAGE = "Opened view window.";

Expand All @@ -47,11 +48,10 @@ public CommandResult execute(Model model) throws CommandException {

// Check if there is anyone in the filtered list
if (model.getDisplayPersons().isEmpty()) {
throw new CommandException(
String.format(Messages.MESSAGE_PERSON_LISTED_OVERVIEW_FOR_VIEW,
model.getDisplayPersonsListSize())
+ NO_PERSON_FOUND_VIEW_MESSAGE
);

// If noone found, show all persons (no change)
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
throw new CommandException(NO_PERSON_FOUND_VIEW_MESSAGE);
}

// Check if there are duplicates
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.address.model.person;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
Expand Down Expand Up @@ -36,10 +37,26 @@ public boolean test(Person person) {
} else if (keywords.get(keywords.size() - 1).contains("$")) {
return isExact(person);
} else {
return keywords.stream()
.allMatch(keyword ->
Arrays.stream(person.getName().fullName.split("\\s+"))
.anyMatch(part -> part.toLowerCase().startsWith(keyword.toLowerCase())));
String[] nameParts = person.getName().fullName.split("\\s+");
// Create a list to track which name parts have been matched
List<String> remainingParts = new ArrayList<>(Arrays.asList(nameParts));

for (String keyword : keywords) {
boolean foundMatch = false;
// Try to match this keyword with any remaining (unmatched) name part
for (int i = remainingParts.size() - 1; i >= 0; i--) {
if (remainingParts.get(i).toLowerCase().startsWith(keyword.toLowerCase())) {
remainingParts.remove(i);
foundMatch = true;
break;
}
}
// If any keyword doesn't find a match, return false
if (!foundMatch) {
return false;
}
}
return true;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.address.model.person;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
Expand All @@ -23,10 +24,26 @@ public boolean test(Person person) {
} else if (keywords.get(keywords.size() - 1).contains("$")) {
return isExact(person);
} else {
return keywords.stream()
.allMatch(keyword ->
Arrays.stream(person.getName().fullName.split("\\s+"))
.anyMatch(part -> part.toLowerCase().startsWith(keyword.toLowerCase())));
String[] nameParts = person.getName().fullName.split("\\s+");
// Create a list to track which name parts have been matched
List<String> remainingParts = new ArrayList<>(Arrays.asList(nameParts));

for (String keyword : keywords) {
boolean foundMatch = false;
// Try to match this keyword with any remaining (unmatched) name part
for (int i = remainingParts.size() - 1; i >= 0; i--) {
if (remainingParts.get(i).toLowerCase().startsWith(keyword.toLowerCase())) {
remainingParts.remove(i);
foundMatch = true;
break;
}
}
// If any keyword doesn't find a match, return false
if (!foundMatch) {
return false;
}
}
return true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalPersons.DANIEL;
import static seedu.address.testutil.TypicalPersons.GEORGE;
import static seedu.address.testutil.TypicalPersons.getTypicalClientHub;

import java.util.Arrays;
import java.util.Collections;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -55,12 +55,10 @@ public void equals() {

@Test
public void execute_zeroKeywords_noPersonFound() {
String expectedMessage = Messages.getMessagePersonsListedOverview(0);
String expectedMessage = Messages.MESSAGE_ADDRESS_NOT_FOUND;
AddressContainsKeywordsPredicate predicate = prepareAddressPredicate(" ");
FindAddressCommand command = new FindAddressCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Collections.emptyList(), model.getDisplayPersons());
assertCommandFailure(command, model, expectedMessage);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.Messages.MESSAGE_NO_PERSON_FOUND_FOR_VIEW;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailureWithNewList;
import static seedu.address.logic.Messages.MESSAGE_CLIENT_TYPE_NOT_FOUND;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalPersons.ALICE;
import static seedu.address.testutil.TypicalPersons.BENSON;
Expand Down Expand Up @@ -58,11 +58,11 @@ public void equals() {

@Test
public void execute_zeroKeywords_noClientFound() {
String expectedMessage = String.format(MESSAGE_NO_PERSON_FOUND_FOR_VIEW);
String expectedMessage = String.format(MESSAGE_CLIENT_TYPE_NOT_FOUND);
String userInput = " ";
ClientTypeContainsKeywordsPredicate predicate = preparePredicate(userInput);
FindClientTypeCommand command = new FindClientTypeCommand(predicate);
assertCommandFailureWithNewList(command, userInput, model, expectedMessage);
assertCommandFailure(command, model, expectedMessage);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.testutil.TypicalPersons.getTypicalClientHub;

import java.util.Arrays;
Expand Down Expand Up @@ -53,12 +53,10 @@ public void equals() {

@Test
public void execute_zeroKeywords_noPersonFound() {
String expectedMessage = Messages.getMessagePersonsListedOverview(0);
String expectedMessage = Messages.MESSAGE_PERSON_NOT_FOUND;
NameContainsKeywordsPredicate predicate = preparePredicate(" ");
FindNameCommand command = new FindNameCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Collections.emptyList(), model.getDisplayPersons());
assertCommandFailure(command, model, expectedMessage);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalPersons.ELLE;
import static seedu.address.testutil.TypicalPersons.FIONA;
import static seedu.address.testutil.TypicalPersons.GEORGE;
import static seedu.address.testutil.TypicalPersons.getTypicalClientHub;

import java.util.Arrays;
import java.util.Collections;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -56,12 +56,10 @@ public void equals() {

@Test
public void execute_zeroKeywords_noPhoneFound() {
String expectedMessage = Messages.getMessagePersonsListedOverview(0);
String expectedMessage = Messages.MESSAGE_PHONE_NOT_FOUND;
PhoneBeginsWithKeywordPredicate predicate = preparePredicate(" ");
FindPhoneCommand command = new FindPhoneCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Collections.emptyList(), model.getDisplayPersons());
assertCommandFailure(command, model, expectedMessage);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.Messages.MESSAGE_PERSON_LISTED_OVERVIEW_FOR_VIEW;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailureWithNewList;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalPersons.BENSON;
Expand Down Expand Up @@ -79,16 +80,14 @@ public void execute_partialKeyword_onePersonFound() {
public void execute_multipleSpaces_throwsCommandException() {
NameContainsKeywordsPredicate predicate = preparePredicate(" ");
ViewCommand command = new ViewCommand(predicate);
assertCommandFailureWithNewList(command, " ", model, String.format(
MESSAGE_PERSON_LISTED_OVERVIEW_FOR_VIEW + ViewCommand.NO_PERSON_FOUND_VIEW_MESSAGE, 0));
assertCommandFailure(command, model, ViewCommand.NO_PERSON_FOUND_VIEW_MESSAGE);
}

@Test
public void execute_singleKeywordNoPersonFound_throwsCommandException() {
NameContainsKeywordsPredicate predicate = preparePredicate("NonExistentName");
ViewCommand command = new ViewCommand(predicate);
assertCommandFailureWithNewList(command, "NonExistentName", model, String.format(
MESSAGE_PERSON_LISTED_OVERVIEW_FOR_VIEW + ViewCommand.NO_PERSON_FOUND_VIEW_MESSAGE, 0));
assertCommandFailure(command, model, ViewCommand.NO_PERSON_FOUND_VIEW_MESSAGE);
}

@Test
Expand Down

0 comments on commit 6876d70

Please sign in to comment.