diff --git a/src/main/java/seedu/address/logic/commands/GenerateEmailsCommand.java b/src/main/java/seedu/address/logic/commands/GenerateEmailsCommand.java new file mode 100644 index 00000000000..b4a6e360bae --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/GenerateEmailsCommand.java @@ -0,0 +1,52 @@ +package seedu.address.logic.commands; + +import static java.util.Objects.requireNonNull; + +import java.util.List; +import seedu.address.commons.core.Messages; +import seedu.address.commons.core.index.Index; +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.Model; +import seedu.address.model.task.Task; + +public class GenerateEmailsCommand extends Command { + + public static final String COMMAND_WORD = "gen"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + + ": Generates all the emails of the people related to the task " + + "identified by the index number in the task list.\n" + + "Parameters: INDEX (must be a positive integer)\n" + + "Example: " + COMMAND_WORD + " 1"; + + public static final String MESSAGE_NO_CONTACTS_ASSIGNED = + "Failed: There are no contacts assigned to the task %1$s"; + + private static final String MESSAGE_GENERATED_EMAILS = "Here are the emails related to the task %1$s:\n" + + "%2$s"; + + + private final Index targetIndex; + + public GenerateEmailsCommand(Index targetIndex) { + this.targetIndex = targetIndex; + } + + @Override + public CommandResult execute(Model model) throws CommandException { + requireNonNull(model); + List shownTaskList = model.getFilteredTaskList(); + + if (targetIndex.getZeroBased() >= shownTaskList.size()) { + throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX); + } + + Task task = shownTaskList.get(targetIndex.getZeroBased()); + + if (task.getNoOfPeople() == 0) { + return new CommandResult(String.format(MESSAGE_NO_CONTACTS_ASSIGNED, task)); + } + + return new CommandResult(String.format(MESSAGE_GENERATED_EMAILS, task, task.getEmails())); + } +} diff --git a/src/main/java/seedu/address/logic/parser/AddressBookParser.java b/src/main/java/seedu/address/logic/parser/AddressBookParser.java index c4c30880993..aa6aab3e6fb 100644 --- a/src/main/java/seedu/address/logic/parser/AddressBookParser.java +++ b/src/main/java/seedu/address/logic/parser/AddressBookParser.java @@ -18,6 +18,7 @@ import seedu.address.logic.commands.ExitCommand; import seedu.address.logic.commands.FilterCommand; import seedu.address.logic.commands.FindCommand; +import seedu.address.logic.commands.GenerateEmailsCommand; import seedu.address.logic.commands.HelpCommand; import seedu.address.logic.commands.ListCommand; import seedu.address.logic.commands.UnassignCommand; @@ -96,6 +97,9 @@ public Command parseCommand(String userInput) throws ParseException { case EditTaskCommand.COMMAND_WORD: return new EditTaskCommandParser().parse(arguments); + case GenerateEmailsCommand.COMMAND_WORD: + return new GenerateEmailsCommandParser().parse(arguments); + default: throw new ParseException(MESSAGE_UNKNOWN_COMMAND); } diff --git a/src/main/java/seedu/address/logic/parser/GenerateEmailsCommandParser.java b/src/main/java/seedu/address/logic/parser/GenerateEmailsCommandParser.java new file mode 100644 index 00000000000..cb543159e6f --- /dev/null +++ b/src/main/java/seedu/address/logic/parser/GenerateEmailsCommandParser.java @@ -0,0 +1,21 @@ +package seedu.address.logic.parser; + +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; + +import seedu.address.commons.core.index.Index; +import seedu.address.logic.commands.GenerateEmailsCommand; +import seedu.address.logic.parser.exceptions.ParseException; + +public class GenerateEmailsCommandParser implements Parser { + + @Override + public GenerateEmailsCommand parse(String args) throws ParseException { + try { + Index index = ParserUtil.parseIndex(args); + return new GenerateEmailsCommand(index); + } catch (ParseException pe) { + throw new ParseException( + String.format(MESSAGE_INVALID_COMMAND_FORMAT, GenerateEmailsCommand.MESSAGE_USAGE), pe); + } + } +} diff --git a/src/main/java/seedu/address/model/task/Task.java b/src/main/java/seedu/address/model/task/Task.java index 544ecdfcf98..9669f550bbb 100644 --- a/src/main/java/seedu/address/model/task/Task.java +++ b/src/main/java/seedu/address/model/task/Task.java @@ -140,6 +140,16 @@ public int getNoOfPeople() { return this.people.size(); } + /** + * Returns a copy-paste friendly string containing all the emails related to this task. + * + * @return The generated email string + */ + public String getEmails() { + String[] emails = this.people.stream().map(p -> p.getEmail().toString()).toArray(String[]::new); + return String.join(", ", emails); + } + @Override public boolean equals(Object other) { if (other == this) {