Skip to content
This repository has been archived by the owner on Mar 5, 2023. It is now read-only.

Model: Add deleteTag(Tag) #591

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import seedu.address.model.person.ReadOnlyPerson;
import seedu.address.model.person.exceptions.DuplicatePersonException;
import seedu.address.model.person.exceptions.PersonNotFoundException;
import seedu.address.model.tag.Tag;

/**
* The API of the Model component.
Expand Down Expand Up @@ -33,6 +34,8 @@ public interface Model {
void updatePerson(ReadOnlyPerson target, ReadOnlyPerson editedPerson)
throws DuplicatePersonException, PersonNotFoundException;

void deleteTag(Tag tag) throws PersonNotFoundException, DuplicatePersonException;

/** Returns the filtered person list as an {@code UnmodifiableObservableList<ReadOnlyPerson>} */
UnmodifiableObservableList<ReadOnlyPerson> getFilteredPersonList();

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.util.Set;
import java.util.function.Predicate;
import java.util.logging.Logger;

Expand All @@ -10,9 +11,11 @@
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.core.UnmodifiableObservableList;
import seedu.address.commons.events.model.AddressBookChangedEvent;
import seedu.address.model.person.Person;
import seedu.address.model.person.ReadOnlyPerson;
import seedu.address.model.person.exceptions.DuplicatePersonException;
import seedu.address.model.person.exceptions.PersonNotFoundException;
import seedu.address.model.tag.Tag;

/**
* Represents the in-memory model of the address book data.
Expand Down Expand Up @@ -79,6 +82,20 @@ public void updatePerson(ReadOnlyPerson target, ReadOnlyPerson editedPerson)
indicateAddressBookChanged();
}

@Override
public void deleteTag(Tag tag) throws PersonNotFoundException, DuplicatePersonException {
for (int i = 0; i < addressBook.getPersonList().size(); i++) {
ReadOnlyPerson oldPerson = addressBook.getPersonList().get(i);

Person newPerson = new Person(oldPerson);
Set<Tag> newTags = newPerson.getTags();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getTags() returns an immutable tag set, which throws UnsupportedOperationException if modification is attempted.
making the subsequent newTags.remove(tag) method wrong.

Copy link

@nbriannl nbriannl Oct 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set<Tag> newTags = new HashSet<Tag>(newPerson.getTags()); would work in place of Line 91

newTags.remove(tag);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, .remove(tag) on immutable newTags will throw Exception.

newPerson.setTags(newTags);

addressBook.updatePerson(oldPerson, newPerson);
}
}

//=========== Filtered Person List Accessors =============================================================

/**
Expand Down