From e3af4066065523758ad5c6add4fad62fe4a42905 Mon Sep 17 00:00:00 2001 From: CYX22222003 Date: Sun, 20 Oct 2024 22:06:08 +0800 Subject: [PATCH] Implement command to delete tag --- .../java/seedu/address/logic/Messages.java | 3 +- .../logic/commands/DeleteTagCommand.java | 68 +++++++++++++++++++ .../address/logic/commands/EditCommand.java | 4 +- .../seedu/address/model/person/Person.java | 14 ++++ 4 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 src/main/java/seedu/address/logic/commands/DeleteTagCommand.java diff --git a/src/main/java/seedu/address/logic/Messages.java b/src/main/java/seedu/address/logic/Messages.java index 7f7619ad56e..db72288f686 100644 --- a/src/main/java/seedu/address/logic/Messages.java +++ b/src/main/java/seedu/address/logic/Messages.java @@ -18,7 +18,8 @@ public class Messages { public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!"; public static final String MESSAGE_DUPLICATE_FIELDS = "Multiple values specified for the following single-valued field(s): "; - + public static final String MESSAGE_NO_TAG = "The person does not contain the tag"; + public static final String MESSAGE_DELETE_TAG_SUCCESS = "Tag %1$s is deleted from student %2$s"; /** * Returns an error message indicating the duplicate prefixes. */ diff --git a/src/main/java/seedu/address/logic/commands/DeleteTagCommand.java b/src/main/java/seedu/address/logic/commands/DeleteTagCommand.java new file mode 100644 index 00000000000..effb70aedf2 --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/DeleteTagCommand.java @@ -0,0 +1,68 @@ +package seedu.address.logic.commands; + +import static java.util.Objects.requireNonNull; + +import java.util.List; +import java.util.Set; + +import seedu.address.commons.core.index.Index; +import seedu.address.logic.Messages; +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.Model; +import seedu.address.model.person.Person; +import seedu.address.model.tag.Tag; + +/** + * Implements command to delete a tag from a person + * format: deletetag [INDEX] [TAG NAME] + */ +public class DeleteTagCommand extends Command { + public static final String COMMAND_WORD = "deletetag"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + + ": Deletes tag from the person identified by the index number used in the displayed person list.\n" + + "Parameters: INDEX (must be a positive integer) [TAG NAME]\n" + + "Example: " + COMMAND_WORD + " 1 " + " hello_world"; + + private final Index targetIndex; + private final Tag targetTag; + + public DeleteTagCommand(Index targetIndex, Tag target) { + this.targetIndex = targetIndex; + this.targetTag = target; + } + + @Override + public CommandResult execute(Model model) throws CommandException { + requireNonNull(model); + List lastShownList = model.getFilteredPersonList(); + if (targetIndex.getZeroBased() >= lastShownList.size()) { + throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + } + + Person personToEdit = lastShownList.get(targetIndex.getZeroBased()); + if (!personToEdit.hasTag(this.targetTag)) { + throw new CommandException(Messages.MESSAGE_NO_TAG); + } + + personToEdit.removeTag(this.targetTag); + return new CommandResult(String.format(Messages.MESSAGE_DELETE_TAG_SUCCESS, + this.targetTag, personToEdit.getName())); + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof DeleteTagCommand)) { + return false; + } + + DeleteTagCommand otherDeleteTagCommand = (DeleteTagCommand) other; + return targetIndex.equals(otherDeleteTagCommand.targetIndex) + && targetTag.equals(otherDeleteTagCommand.targetTag); + } +} diff --git a/src/main/java/seedu/address/logic/commands/EditCommand.java b/src/main/java/seedu/address/logic/commands/EditCommand.java index 9c4318d3303..b1cfd9841cc 100644 --- a/src/main/java/seedu/address/logic/commands/EditCommand.java +++ b/src/main/java/seedu/address/logic/commands/EditCommand.java @@ -76,9 +76,7 @@ public CommandResult execute(Model model) throws CommandException { Person personToEdit = lastShownList.get(index.getZeroBased()); Person editedPerson = createEditedPerson(personToEdit, editPersonDescriptor); - // if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) { - // throw new CommandException(MESSAGE_DUPLICATE_PERSON); - // } + model.deletePerson(personToEdit); if (model.hasPerson(editedPerson)) { model.insertPerson(personToEdit, index.getZeroBased()); diff --git a/src/main/java/seedu/address/model/person/Person.java b/src/main/java/seedu/address/model/person/Person.java index 2e98f371d6a..8e014b8d49b 100644 --- a/src/main/java/seedu/address/model/person/Person.java +++ b/src/main/java/seedu/address/model/person/Person.java @@ -99,6 +99,20 @@ public boolean hasDuplicateInfo(Person otherPerson) { || this.hasSamePhoneNumber(otherPerson); } + /** + * Checks whether the person has the tags + */ + public boolean hasTag(Tag t) { + return this.getTags().contains(t); + } + + /** + * Remove a tag from a person + */ + public void removeTag(Tag t) { + this.getTags().remove(t); + } + /** * Returns true if both persons have the same identity and data fields. * This defines a stronger notion of equality between two persons.