Skip to content

Commit

Permalink
Merge pull request AY2324S2-CS2103T-T08-3#204 from ashleyy2444/branch…
Browse files Browse the repository at this point in the history
…-fixDeleteTagBug

Fix delete tag bug
  • Loading branch information
ashleyy2444 authored Apr 15, 2024
2 parents 3dd47c9 + 902a695 commit 6e5c35d
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions src/main/java/seedu/address/logic/commands/DeleteTagCommand.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import java.util.List;
import java.util.stream.Collectors;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
import seedu.address.model.tag.Tag;
import seedu.address.model.tag.TagContainsKeywordsPredicate;

Expand All @@ -22,6 +21,7 @@ public class DeleteTagCommand extends DeleteAbstractCommand {
+ "Example: " + COMMAND_WORD + " friend";

public static final String MESSAGE_DELETE_TAG_SUCCESS = "Deleted all contacts with tag: %1$s";
public static final String MESSAGE_TAG_DOES_NOT_EXIST = "The following tags do not exist: %1$s";

private final TagContainsKeywordsPredicate tagToDelete;

Expand All @@ -43,16 +43,25 @@ public DeleteTagCommand(TagContainsKeywordsPredicate tagToDelete) {
*/
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Tag> tags = tagToDelete.getTags();
System.out.println("Deleting all contacts with tag: " + tags);
for (Tag tag : tags) {
System.out.println(tag);
model.getFilteredPersonList().stream()
.filter(person -> person.getTags().contains(tag))
.peek(person -> System.out.println(person))
.forEach(person -> model.deletePerson(person));
List<Tag> allTags = model.getAddressBook().getPersonList().stream()
.flatMap(person -> person.getTags().stream())
.distinct()
.collect(Collectors.toList());

if (!allTags.containsAll(tags)) {
List<Tag> nonExistentTags = tags.stream()
.filter(tag -> !allTags.contains(tag))
.collect(Collectors.toList());
throw new CommandException(String.format(MESSAGE_TAG_DOES_NOT_EXIST, nonExistentTags));
}

List<Person> personsToDelete = model.getFilteredPersonList().stream()
.filter(person -> tagToDelete.test(person))
.collect(Collectors.toList());

personsToDelete.forEach(model::deletePerson);

return new CommandResult(String.format(MESSAGE_DELETE_TAG_SUCCESS, tagToDelete));
}

Expand All @@ -62,19 +71,12 @@ public boolean equals(Object other) {
return true;
}

// instanceof handles nulls
if (!(other instanceof DeleteCommand)) {
if (!(other instanceof DeleteTagCommand)) {
return false;
}

DeleteTagCommand otherDeleteTagCommand = (DeleteTagCommand) other;
return tagToDelete.equals(otherDeleteTagCommand.tagToDelete);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("tagToDelete", tagToDelete)
.toString();
}
}

0 comments on commit 6e5c35d

Please sign in to comment.