Skip to content

Commit

Permalink
Implement command to delete tag
Browse files Browse the repository at this point in the history
  • Loading branch information
CYX22222003 committed Oct 20, 2024
1 parent 9cdc5e8 commit e3af406
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
68 changes: 68 additions & 0 deletions src/main/java/seedu/address/logic/commands/DeleteTagCommand.java
Original file line number Diff line number Diff line change
@@ -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<Person> 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);
}
}
4 changes: 1 addition & 3 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit e3af406

Please sign in to comment.