diff --git a/src/main/java/seedu/address/logic/commands/tag/UntagCommand.java b/src/main/java/seedu/address/logic/commands/tag/UntagCommand.java index 2cb139c05c8..47f8a4c9934 100644 --- a/src/main/java/seedu/address/logic/commands/tag/UntagCommand.java +++ b/src/main/java/seedu/address/logic/commands/tag/UntagCommand.java @@ -77,8 +77,7 @@ public CommandResult execute(Model model) throws CommandException { } Person personToEdit = lastShownList.get(index.getZeroBased()); - - Set tagsToKeep = new HashSet<>(); + Set updatedTags = new HashSet<>(personToEdit.getTags()); if (personToEdit.getTags().isEmpty()) { throw new CommandException(Messages.MESSAGE_TAG_NOT_FOUND_IN_CONTACT); @@ -88,30 +87,18 @@ public CommandResult execute(Model model) throws CommandException { throw new CommandException(Messages.MESSAGE_TAG_NOT_FOUND_IN_CONTACT); } - // Checks whether the model has all the specified weddings AND if the person has all the weddings assigned - for (Tag tag : tagsToRemove) { - if (!model.hasTag(tag)) { - throw new CommandException( - Messages.MESSAGE_TAG_NOT_FOUND_IN_CONTACT); - } - if (!personToEdit.hasTag(tag)) { - throw new CommandException(Messages.MESSAGE_TAG_NOT_FOUND_IN_CONTACT); - } + if (!updatedTags.containsAll(tagsToRemove)) { + throw new CommandException(Messages.MESSAGE_TAG_NOT_FOUND_IN_CONTACT); } - // Stores weddings from model that we want to remove - already guaranteed that are all within the model - HashSet modelTagsToRemove = tagsToRemove.stream().map(model::getTag) - .collect(Collectors.toCollection(HashSet::new)); - - // Already checks that person has all weddings, so can go ahead and remove - for (Tag tag : personToEdit.getTags()) { - if (!modelTagsToRemove.contains(tag)) { - tagsToKeep.add(tag); - } else { + for (Tag tag : updatedTags) { + if (tagsToRemove.contains(tag)) { tag.decreaseTaggedCount(); } } + updatedTags.removeAll(tagsToRemove); + Person editedPerson; if (personToEdit instanceof Vendor) { editedPerson = new Vendor( @@ -119,7 +106,7 @@ public CommandResult execute(Model model) throws CommandException { personToEdit.getPhone(), personToEdit.getEmail(), personToEdit.getAddress(), - tagsToKeep, + updatedTags, personToEdit.getWeddings(), personToEdit.getTasks()); } else { @@ -128,7 +115,7 @@ public CommandResult execute(Model model) throws CommandException { personToEdit.getPhone(), personToEdit.getEmail(), personToEdit.getAddress(), - tagsToKeep, + updatedTags, personToEdit.getWeddings(), personToEdit.getTasks()); } diff --git a/src/main/java/seedu/address/logic/commands/wedding/UnassignWeddingCommand.java b/src/main/java/seedu/address/logic/commands/wedding/UnassignWeddingCommand.java index 74448dbba7f..3dce4604c8a 100644 --- a/src/main/java/seedu/address/logic/commands/wedding/UnassignWeddingCommand.java +++ b/src/main/java/seedu/address/logic/commands/wedding/UnassignWeddingCommand.java @@ -73,7 +73,7 @@ public CommandResult execute(Model model) throws CommandException { } Person personToEdit = lastShownList.get(index.getZeroBased()); - Set modelWeddingsToKeep = new HashSet<>(); + Set updatedWeddings = new HashSet<>(personToEdit.getWeddings()); if (personToEdit.getWeddings().isEmpty()) { throw new CommandException(MESSAGE_WEDDING_NOT_FOUND_IN_CONTACT); @@ -83,29 +83,13 @@ public CommandResult execute(Model model) throws CommandException { throw new CommandException(MESSAGE_WEDDING_NOT_FOUND_IN_CONTACT); } - // Checks whether the model has all the specified weddings AND if the person has all the weddings assigned - for (Wedding wedding : weddingsToRemove) { - if (!model.hasWedding(wedding)) { - throw new CommandException( - MESSAGE_WEDDING_NOT_FOUND_IN_CONTACT); - } - if (!personToEdit.hasWedding(wedding)) { - throw new CommandException(MESSAGE_WEDDING_NOT_FOUND_IN_CONTACT); - } + if (!updatedWeddings.containsAll(weddingsToRemove)) { + throw new CommandException(MESSAGE_WEDDING_NOT_FOUND_IN_CONTACT); } - // Stores weddings from model that we want to remove - already guaranteed that are all within the model - HashSet modelWeddingsToRemove = weddingsToRemove.stream().map(model::getWedding) - .collect(Collectors.toCollection(HashSet::new)); - - // Already checks that person has all weddings, so can go ahead and remove - for (Wedding wedding : personToEdit.getWeddings()) { - if (!modelWeddingsToRemove.contains(wedding)) { - modelWeddingsToKeep.add(wedding); - } - } + updatedWeddings.removeAll(weddingsToRemove); - Person editedPerson = PersonWeddingUtil.getNewPerson(personToEdit, modelWeddingsToKeep); + Person editedPerson = PersonWeddingUtil.getNewPerson(personToEdit, updatedWeddings); // Remove Wedding from Person model.setPerson(personToEdit, editedPerson);