forked from nus-cs2103-AY2425S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from CYX22222003/delete-tag
Implement command to delete tag
- Loading branch information
Showing
18 changed files
with
322 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/java/seedu/address/logic/commands/DeleteTagCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.List; | ||
|
||
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] t/[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; | ||
|
||
/** | ||
* Constructs new DeleteTag command. | ||
* @param targetIndex the index of the person in the list. | ||
* @param target the tag the user wants to delete. | ||
*/ | ||
public DeleteTagCommand(Index targetIndex, Tag target) { | ||
assert targetIndex != null; | ||
assert target != null; | ||
this.targetIndex = targetIndex; | ||
this.targetTag = target; | ||
} | ||
|
||
/** | ||
* Executes the command and delete a tag from a specific person. | ||
*/ | ||
@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()); | ||
assert personToEdit != null; | ||
if (!personToEdit.hasTag(this.targetTag)) { | ||
throw new CommandException(Messages.MESSAGE_NO_TAG); | ||
} | ||
|
||
model.deletePersonTag(personToEdit, 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/seedu/address/logic/parser/DeleteTagCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.Messages.MESSAGE_TAG_NOT_FOUND; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.DeleteTagCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.tag.Tag; | ||
|
||
/** | ||
* Parse the delete tag command. | ||
*/ | ||
public class DeleteTagCommandParser implements Parser<DeleteTagCommand> { | ||
/** | ||
* Parses the given string of arguments in the context of Delete tag command. | ||
* and returns DeleteTagCommand object for execution. | ||
* @throws ParseException if user input does not conform the expected input | ||
*/ | ||
public DeleteTagCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
assert !args.isEmpty(); | ||
ArgumentMultimap argMultiMap = ArgumentTokenizer.tokenize(args, PREFIX_TAG); | ||
Index index; | ||
|
||
try { | ||
index = ParserUtil.parseIndex(argMultiMap.getPreamble()); | ||
} catch (ParseException pe) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteTagCommand.MESSAGE_USAGE), pe); | ||
} | ||
|
||
argMultiMap.verifyNoDuplicatePrefixesFor(PREFIX_TAG); | ||
if (argMultiMap.getValue(PREFIX_TAG).isEmpty()) { | ||
throw new ParseException(MESSAGE_TAG_NOT_FOUND); | ||
} | ||
|
||
Tag target = ParserUtil.parseTag(argMultiMap.getValue(PREFIX_TAG).get()); | ||
return new DeleteTagCommand(index, target); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/test/java/seedu/address/logic/commands/DeleteTagCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; | ||
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; | ||
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; | ||
import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON; | ||
import static seedu.address.testutil.TypicalPersons.FRIEND_TAG; | ||
import static seedu.address.testutil.TypicalPersons.OWES_MONEY_TAG; | ||
import static seedu.address.testutil.TypicalPersons.getTypicalCampusConnect; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.ModelManager; | ||
import seedu.address.model.UserPrefs; | ||
import seedu.address.model.person.Person; | ||
import seedu.address.model.tag.Tag; | ||
import seedu.address.testutil.PersonBuilder; | ||
|
||
public class DeleteTagCommandTest { | ||
private static final Model model = new ModelManager(getTypicalCampusConnect(), new UserPrefs()); | ||
private static final String DEFAULT_TAG = "test"; | ||
private static final String DEFAULT_TAG_SECOND = "test2"; | ||
private static final String TEST_EMAIL = "test@test"; | ||
private static final String TEST_PHONE = "84209817"; | ||
private static final String TEST_USER = "test user"; | ||
@Test | ||
public void execute_validIndexPersonList_success() { | ||
Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); | ||
Person p = new PersonBuilder(firstPerson).withName(TEST_USER).withEmail(TEST_EMAIL) | ||
.withPhone(TEST_PHONE).withTags(FRIEND_TAG, OWES_MONEY_TAG).build(); | ||
model.setPerson(firstPerson, p); | ||
|
||
Person expectedPerson = new PersonBuilder(firstPerson).withName(TEST_USER).withEmail(TEST_EMAIL) | ||
.withPhone(TEST_PHONE).withTags(OWES_MONEY_TAG).build(); | ||
|
||
Model expectedModel = new ModelManager(getTypicalCampusConnect(), new UserPrefs()); | ||
expectedModel.setPerson(firstPerson, expectedPerson); | ||
|
||
DeleteTagCommand deleteTagCommand = new DeleteTagCommand(INDEX_FIRST_PERSON, new Tag(FRIEND_TAG)); | ||
String expectedMessage = String.format(Messages.MESSAGE_DELETE_TAG_SUCCESS, | ||
new Tag(FRIEND_TAG), p.getName()); | ||
assertCommandSuccess(deleteTagCommand, model, expectedMessage, expectedModel); | ||
} | ||
|
||
@Test | ||
public void execute_invalidIndexPersonList_throwsCommandException() { | ||
Index outOfBoundIndex = Index.fromOneBased(model.getFilteredPersonList().size() + 1); | ||
DeleteTagCommand deleteTagCommand = new DeleteTagCommand(outOfBoundIndex, new Tag(DEFAULT_TAG)); | ||
assertCommandFailure(deleteTagCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); | ||
} | ||
|
||
@Test | ||
public void execute_personHasNoTag_throwsCommandException() { | ||
Index lastIndex = Index.fromOneBased(model.getFilteredPersonList().size()); | ||
DeleteTagCommand deleteTagCommand = new DeleteTagCommand(lastIndex, new Tag(DEFAULT_TAG)); | ||
assertCommandFailure(deleteTagCommand, model, Messages.MESSAGE_NO_TAG); | ||
} | ||
|
||
@Test | ||
public void equals() { | ||
DeleteTagCommand deleteTagCommandA = new DeleteTagCommand(INDEX_FIRST_PERSON, new Tag(DEFAULT_TAG)); | ||
DeleteTagCommand deleteTagCommandB = new DeleteTagCommand(INDEX_SECOND_PERSON, new Tag(DEFAULT_TAG)); | ||
DeleteTagCommand deleteTagCommandC = new DeleteTagCommand(INDEX_FIRST_PERSON, new Tag(DEFAULT_TAG_SECOND)); | ||
DeleteTagCommand deleteTagCommandD = new DeleteTagCommand(INDEX_FIRST_PERSON, new Tag(DEFAULT_TAG)); | ||
|
||
assertTrue(deleteTagCommandA.equals(deleteTagCommandD)); | ||
assertTrue(deleteTagCommandA.equals(deleteTagCommandA)); | ||
assertFalse(deleteTagCommandA.equals(deleteTagCommandB)); | ||
assertFalse(deleteTagCommandA.equals(deleteTagCommandC)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/test/java/seedu/address/logic/parser/DeleteTagCommandParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; | ||
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; | ||
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.logic.commands.DeleteTagCommand; | ||
import seedu.address.model.tag.Tag; | ||
|
||
public class DeleteTagCommandParserTest { | ||
private DeleteTagCommandParser parser = new DeleteTagCommandParser(); | ||
|
||
@Test | ||
public void parse_validArgs_returnsDeleteTagCommand() { | ||
assertParseSuccess(parser, "1 t/hello", | ||
new DeleteTagCommand(INDEX_FIRST_PERSON, new Tag("hello"))); | ||
} | ||
|
||
@Test | ||
public void parse_invalidArgs_throwsParseException() { | ||
assertParseFailure(parser, "a", | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteTagCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
} |
Oops, something went wrong.