Skip to content

Commit

Permalink
Implement generate emails command
Browse files Browse the repository at this point in the history
  • Loading branch information
snss231 committed Mar 28, 2022
1 parent 014f51e commit 99841fa
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Task> 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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<GenerateEmailsCommand> {

@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);
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/model/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 99841fa

Please sign in to comment.