forked from nus-cs2103-AY2324S1/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 #118 from pra-navi/branch-addTag
feat: Implement addTagTask and addTagPerson commands
- Loading branch information
Showing
12 changed files
with
990 additions
and
0 deletions.
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
src/main/java/seedu/address/logic/commands/AddTagPersonCommand.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,142 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.Address; | ||
import seedu.address.model.person.Email; | ||
import seedu.address.model.person.Name; | ||
import seedu.address.model.person.Person; | ||
import seedu.address.model.person.Phone; | ||
import seedu.address.model.tag.Tag; | ||
|
||
/** | ||
* Adds tags to an existing person in the address book. | ||
*/ | ||
public class AddTagPersonCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "addTagPerson"; | ||
public static final String SHORTENED_COMMAND_WORD = "atagp"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + " (alias: " + SHORTENED_COMMAND_WORD + ")" | ||
+ ": Adds new tags to the person identified " | ||
+ "by the index number used in the displayed contact list. \n" | ||
+ "Parameters: INDEX (must be a positive integer) " | ||
+ "[" + PREFIX_TAG + "TAG]...\n" | ||
+ "Example: " + COMMAND_WORD + " 1 " | ||
+ PREFIX_TAG + "catering " + PREFIX_TAG + "smallBusiness"; | ||
|
||
public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Edited Person: %1$s \n" + "Successfully added these " | ||
+ "tags: %2$s \n" + "These tags were not added as they already exists: %3$s"; | ||
|
||
// newTags stores those from tagsToAdd that are not already in personToEdit's tags | ||
private static Set<Tag> newTags; | ||
// oldTags stores those from tagsToAdd that are already in personToEdit's tags | ||
private static Set<Tag> oldTags; | ||
private final Set<Tag> tagsToAdd; | ||
private final Index index; | ||
|
||
/** | ||
* @param index of the person in the filtered contact list to edit | ||
* @param tagsToAdd tags to add to the person | ||
*/ | ||
public AddTagPersonCommand(Index index, Set<Tag> tagsToAdd) { | ||
requireNonNull(index); | ||
requireNonNull(tagsToAdd); | ||
|
||
this.index = index; | ||
this.tagsToAdd = tagsToAdd; | ||
newTags = new HashSet<>(); | ||
oldTags = new HashSet<>(); | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
List<Person> lastShownPersonList = model.getFilteredPersonList(); | ||
|
||
if (index.getZeroBased() >= lastShownPersonList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); | ||
} | ||
|
||
Person personToEdit = lastShownPersonList.get(index.getZeroBased()); | ||
Person editedPerson = createEditedPerson(personToEdit, tagsToAdd); | ||
|
||
model.setPerson(personToEdit, editedPerson); | ||
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); | ||
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson), | ||
setToString(newTags), setToString(oldTags))); | ||
} | ||
|
||
/** | ||
* Creates and returns a {@code Person} with the details of {@code personToEdit}. | ||
*/ | ||
private static Person createEditedPerson(Person personToEdit, Set<Tag> tagsToAdd) { | ||
assert personToEdit != null; | ||
|
||
Name name = personToEdit.getName(); | ||
Phone phone = personToEdit.getPhone(); | ||
Email email = personToEdit.getEmail(); | ||
Address address = personToEdit.getAddress(); | ||
Set<Tag> existingTags = personToEdit.getTags(); | ||
|
||
Set<Tag> commonTags = new HashSet<>(existingTags); | ||
commonTags.retainAll(tagsToAdd); | ||
|
||
oldTags.addAll(commonTags); | ||
|
||
newTags.addAll(tagsToAdd); | ||
newTags.removeAll(existingTags); | ||
|
||
Set<Tag> updatedTags = new HashSet<>(existingTags); | ||
updatedTags.addAll(tagsToAdd); | ||
|
||
return new Person(name, phone, email, address, updatedTags); | ||
} | ||
|
||
public static String setToString(Set<Tag> tags) { | ||
|
||
if (tags.isEmpty()) { | ||
return "-"; | ||
} else { | ||
return tags.stream() | ||
.map(Tag::toString) | ||
.collect(Collectors.joining(", ")); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof AddTagPersonCommand)) { | ||
return false; | ||
} | ||
|
||
AddTagPersonCommand otherAddTagPersonCommand = (AddTagPersonCommand) other; | ||
return index.equals(otherAddTagPersonCommand.index) | ||
&& tagsToAdd.equals(otherAddTagPersonCommand.tagsToAdd); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("index", index) | ||
.add("tagsToAdd", tagsToAdd) | ||
.toString(); | ||
} | ||
} |
140 changes: 140 additions & 0 deletions
140
src/main/java/seedu/address/logic/commands/AddTagTaskCommand.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,140 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_TASKS; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.tag.Tag; | ||
import seedu.address.model.task.Note; | ||
import seedu.address.model.task.Status; | ||
import seedu.address.model.task.Task; | ||
import seedu.address.model.task.Title; | ||
|
||
/** | ||
* Adds tags to an existing task in the address book. | ||
*/ | ||
public class AddTagTaskCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "addTagTask"; | ||
public static final String SHORTENED_COMMAND_WORD = "atagt"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + " (alias: " + SHORTENED_COMMAND_WORD + ")" | ||
+ ": Adds new tags to the task identified " | ||
+ "by the index number used in the displayed task list. \n" | ||
+ "Parameters: INDEX (must be a positive integer) " | ||
+ "[" + PREFIX_TAG + "TAG]...\n" | ||
+ "Example: " + COMMAND_WORD + " 1 " | ||
+ PREFIX_TAG + "class " + PREFIX_TAG + "finance"; | ||
|
||
public static final String MESSAGE_EDIT_TASK_SUCCESS = "Edited Task: %1$s \n" + "Successfully added these " | ||
+ "tags: %2$s \n" + "These tags were not added as they already exists: %3$s"; | ||
|
||
// newTags stores those from tagsToAdd that are not already in taskToEdit's tags | ||
private static Set<Tag> newTags; | ||
// oldTags stores those from tagsToAdd that are already in taskToEdit's tags | ||
private static Set<Tag> oldTags; | ||
private final Set<Tag> tagsToAdd; | ||
private final Index index; | ||
|
||
/** | ||
* @param index of the task in the filtered task list to edit | ||
* @param tagsToAdd tags to add to the task | ||
*/ | ||
public AddTagTaskCommand(Index index, Set<Tag> tagsToAdd) { | ||
requireNonNull(index); | ||
requireNonNull(tagsToAdd); | ||
|
||
this.index = index; | ||
this.tagsToAdd = tagsToAdd; | ||
newTags = new HashSet<>(); | ||
oldTags = new HashSet<>(); | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
List<Task> lastShownTaskList = model.getFilteredTaskList(); | ||
|
||
if (index.getZeroBased() >= lastShownTaskList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX); | ||
} | ||
|
||
Task taskToEdit = lastShownTaskList.get(index.getZeroBased()); | ||
Task editedTask = createEditedTask(taskToEdit, tagsToAdd); | ||
|
||
model.setTask(taskToEdit, editedTask); | ||
model.updateFilteredTaskList(PREDICATE_SHOW_ALL_TASKS); | ||
return new CommandResult(String.format(MESSAGE_EDIT_TASK_SUCCESS, Messages.format(editedTask), | ||
setToString(newTags), setToString(oldTags))); | ||
} | ||
|
||
/** | ||
* Creates and returns a {@code Task} with the details of {@code taskToEdit}. | ||
*/ | ||
private static Task createEditedTask(Task taskToEdit, Set<Tag> tagsToAdd) { | ||
assert taskToEdit != null; | ||
|
||
Title title = taskToEdit.getTitle(); | ||
Note note = taskToEdit.getNote(); | ||
Status taskStatus = taskToEdit.getStatus(); | ||
Set<Tag> existingTags = taskToEdit.getTags(); | ||
|
||
Set<Tag> commonTags = new HashSet<>(existingTags); | ||
commonTags.retainAll(tagsToAdd); | ||
|
||
oldTags.addAll(commonTags); | ||
|
||
newTags.addAll(tagsToAdd); | ||
newTags.removeAll(existingTags); | ||
|
||
Set<Tag> updatedTags = new HashSet<>(existingTags); | ||
updatedTags.addAll(tagsToAdd); | ||
|
||
return new Task(title, note, taskStatus, updatedTags); | ||
} | ||
|
||
public static String setToString(Set<Tag> tags) { | ||
|
||
if (tags.isEmpty()) { | ||
return "-"; | ||
} else { | ||
return tags.stream() | ||
.map(Tag::toString) | ||
.collect(Collectors.joining(", ")); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof AddTagTaskCommand)) { | ||
return false; | ||
} | ||
|
||
AddTagTaskCommand otherAddTagTaskCommand = (AddTagTaskCommand) other; | ||
return index.equals(otherAddTagTaskCommand.index) | ||
&& tagsToAdd.equals(otherAddTagTaskCommand.tagsToAdd); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("index", index) | ||
.add("tagsToAdd", tagsToAdd) | ||
.toString(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/seedu/address/logic/parser/AddTagPersonCommandParser.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,47 @@ | ||
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.parser.CliSyntax.PREFIX_TAG; | ||
|
||
import java.util.Set; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.AddTagPersonCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.tag.Tag; | ||
|
||
/** | ||
* Parses input arguments and creates a new AddTagPersonCommand object. | ||
*/ | ||
public class AddTagPersonCommandParser implements Parser<AddTagPersonCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the AddTagPersonCommand | ||
* and returns an AddTagTaskCommand object for execution. | ||
* | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public AddTagPersonCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
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, AddTagPersonCommand.MESSAGE_USAGE), | ||
pe); | ||
} | ||
|
||
Set<Tag> tags = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG)); | ||
|
||
if (tags.isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddTagPersonCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
return new AddTagPersonCommand(index, tags); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/seedu/address/logic/parser/AddTagTaskCommandParser.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,47 @@ | ||
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.parser.CliSyntax.PREFIX_TAG; | ||
|
||
import java.util.Set; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.AddTagTaskCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.tag.Tag; | ||
|
||
/** | ||
* Parses input arguments and creates a new AddTagTaskCommand object. | ||
*/ | ||
public class AddTagTaskCommandParser implements Parser<AddTagTaskCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the AddTagTaskCommand | ||
* and returns an AddTagTaskCommand object for execution. | ||
* | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public AddTagTaskCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
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, AddTagTaskCommand.MESSAGE_USAGE), | ||
pe); | ||
} | ||
|
||
Set<Tag> tags = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG)); | ||
|
||
if (tags.isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddTagTaskCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
return new AddTagTaskCommand(index, tags); | ||
} | ||
|
||
} |
Oops, something went wrong.