Skip to content

Commit

Permalink
Merge pull request #99 from sooyitao/master
Browse files Browse the repository at this point in the history
Update notes for duplicate names
  • Loading branch information
george-yeo authored Nov 7, 2024
2 parents 57ac709 + 2793c8a commit 58098cf
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions src/main/java/seedu/address/logic/commands/NotesCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.FindCommandParser;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.Model;
import seedu.address.model.person.Name;
import seedu.address.model.person.Notes;
Expand Down Expand Up @@ -46,6 +48,9 @@ public class NotesCommand extends Command {
public static final String MESSAGE_ADD_NOTES_SUCCESS = "Added notes for %1$s: \n%2$s";
public static final String MESSAGE_EDIT_NOTES_SUCCESS = "Edited notes for %1$s: \n%2$s";
public static final String MESSAGE_PERSON_NOT_FOUND = "No person found with name: %1$s";
public static final String MESSAGE_MULTIPLE_PERSONS_FOUND = "Multiple contact match the name \"%1$s\".\n"
+ "The displayed list has been filtered based on the name \"%1$s\".\n"
+ "Please specify an index for the correct person.";

private final Mode mode;
private final Notes newNotes;
Expand Down Expand Up @@ -120,7 +125,7 @@ public NotesCommand(Name targetName, Mode mode, Notes notes) {
}

@Override
public CommandResult execute(Model model) throws CommandException {
public CommandResult execute(Model model) throws CommandException, ParseException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

Expand All @@ -132,16 +137,31 @@ public CommandResult execute(Model model) throws CommandException {
}
personToEdit = lastShownList.get(targetIndex.getZeroBased());
} else {
personToEdit = null;
for (Person person : lastShownList) {
if (person.getName().equals(targetName)) {
personToEdit = person;
break;
List<Person> fullPersonList = model.getAddressBook().getPersonList();
List<Person> exactMatch = fullPersonList.stream()
.filter(person -> person.getName().fullName.equalsIgnoreCase(targetName.toString()))
.toList();
List<Person> partialMatches = fullPersonList.stream()
.filter(person -> person.getName().fullName.toLowerCase()
.contains(targetName.toString().toLowerCase()))
.toList();
if (exactMatch.size() == 1) {
personToEdit = exactMatch.get(0);
} else {
if (partialMatches.size() == 1) {
personToEdit = partialMatches.get(0);

Check warning on line 152 in src/main/java/seedu/address/logic/commands/NotesCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/NotesCommand.java#L152

Added line #L152 was not covered by tests
} else if (exactMatch.size() > 1) {
FindCommandParser findExactCommandParser = new FindCommandParser();
findExactCommandParser.parse(targetName.toString()).execute(model);
throw new CommandException(String.format(MESSAGE_MULTIPLE_PERSONS_FOUND, targetName.toString()));

Check warning on line 156 in src/main/java/seedu/address/logic/commands/NotesCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/NotesCommand.java#L154-L156

Added lines #L154 - L156 were not covered by tests
} else if (partialMatches.size() > 1) {
FindCommandParser findCommandParser = new FindCommandParser();
findCommandParser.parse(targetName.toString()).execute(model);
throw new CommandException(String.format(MESSAGE_MULTIPLE_PERSONS_FOUND, targetName.toString()));

Check warning on line 160 in src/main/java/seedu/address/logic/commands/NotesCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/NotesCommand.java#L158-L160

Added lines #L158 - L160 were not covered by tests
} else {
throw new CommandException(String.format(MESSAGE_PERSON_NOT_FOUND, targetName.toString()));
}
}
if (personToEdit == null) {
throw new CommandException(String.format(MESSAGE_PERSON_NOT_FOUND, targetName.toString()));
}
}

switch (mode) {
Expand Down

0 comments on commit 58098cf

Please sign in to comment.