Skip to content

Commit

Permalink
Merge pull request AY2324S2-CS2103T-W13-4#36 from chin-herng/branch-a…
Browse files Browse the repository at this point in the history
…ssign-tasks

Assign Tasks to Persons
  • Loading branch information
breezetall authored Mar 19, 2024
2 parents abd9989 + c5ef6cc commit 3ff249a
Show file tree
Hide file tree
Showing 42 changed files with 614 additions and 342 deletions.
5 changes: 3 additions & 2 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Messages {
public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
public static final String MESSAGE_INVALID_TASK_DISPLAYED_INDEX = "The task index provided is invalid";
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): ";
Expand Down Expand Up @@ -44,8 +45,8 @@ public static String format(Person person) {
.append(person.getEmail())
.append("; Address: ")
.append(person.getAddress())
.append("; Tags: ");
person.getTags().forEach(builder::append);
.append("; Tasks: ");
person.getTasks().forEach(builder::append);
return builder.toString();
}

Expand Down
8 changes: 2 additions & 6 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
Expand All @@ -25,15 +24,12 @@ public class AddCommand extends Command {
+ PREFIX_NAME + "NAME "
+ PREFIX_PHONE + "PHONE "
+ PREFIX_EMAIL + "EMAIL "
+ PREFIX_ADDRESS + "ADDRESS "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ PREFIX_ADDRESS + "ADDRESS\n "
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NAME + "John Doe "
+ PREFIX_PHONE + "98765432 "
+ PREFIX_EMAIL + "[email protected] "
+ PREFIX_ADDRESS + "311, Clementi Ave 2, #02-25 "
+ PREFIX_TAG + "friends "
+ PREFIX_TAG + "owesMoney";
+ PREFIX_ADDRESS + "311, Clementi Ave 2, #02-25 ";

public static final String MESSAGE_SUCCESS = "New person added: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book";
Expand Down
88 changes: 88 additions & 0 deletions src/main/java/seedu/address/logic/commands/AssignCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package seedu.address.logic.commands;

import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

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.task.Task;

/**
* Assigns an existing task to an existing person in the address book.
*/
public class AssignCommand extends Command {

public static final String COMMAND_WORD = "assign";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Assigns the task identified "
+ "by the index number used in the last task listing "
+ "to the person identified "
+ "by the index number used in the last person listing. "
+ "Does nothing if the task is already assigned to the person.\n"
+ "Parameters: TASK_INDEX (must be a positive integer) "
+ "to/ [PERSON_INDEX (must be a positive integer)]\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ "to/ 2";

public static final String MESSAGE_SUCCESS = "%1$s has been assigned to %2$s.";

private final Index taskIndex;
private final Index personIndex;

/**
* @param taskIndex of the task in the filtered task list to be assigned to the person
* @param personIndex of the person in the filtered person list to be assigned the task
*/
public AssignCommand(Index taskIndex, Index personIndex) {
requireAllNonNull(taskIndex, personIndex);

this.taskIndex = taskIndex;
this.personIndex = personIndex;
}

@Override
public CommandResult execute(Model model) throws CommandException {
List<Task> lastShownTaskList = model.getTaskList().getSerializeTaskList();
List<Person> lastShownPersonList = model.getFilteredPersonList();

if (taskIndex.getZeroBased() >= lastShownTaskList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}

if (personIndex.getZeroBased() >= lastShownPersonList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

Person personToBeAssigned = lastShownPersonList.get(personIndex.getZeroBased());
Task taskToAssign = lastShownTaskList.get(taskIndex.getZeroBased());
Person assignedPerson = personToBeAssigned.addTask(taskToAssign);

model.setPerson(personToBeAssigned, assignedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);

return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.formatTask(taskToAssign),
Messages.format(assignedPerson)));
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof AssignCommand)) {
return false;
}

AssignCommand e = (AssignCommand) other;
return taskIndex.equals(e.taskIndex)
&& personIndex.equals(e.personIndex);
}
}
38 changes: 6 additions & 32 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -26,7 +23,7 @@
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.tag.Tag;
import seedu.address.model.task.Task;

/**
* Edits the details of an existing person in the address book.
Expand All @@ -42,8 +39,7 @@ public class EditCommand extends Command {
+ "[" + PREFIX_NAME + "NAME] "
+ "[" + PREFIX_PHONE + "PHONE] "
+ "[" + PREFIX_EMAIL + "EMAIL] "
+ "[" + PREFIX_ADDRESS + "ADDRESS] "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "[" + PREFIX_ADDRESS + "ADDRESS]\n "
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_PHONE + "91234567 "
+ PREFIX_EMAIL + "[email protected]";
Expand Down Expand Up @@ -99,9 +95,9 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript
Phone updatedPhone = editPersonDescriptor.getPhone().orElse(personToEdit.getPhone());
Email updatedEmail = editPersonDescriptor.getEmail().orElse(personToEdit.getEmail());
Address updatedAddress = editPersonDescriptor.getAddress().orElse(personToEdit.getAddress());
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());
Set<Task> updatedTasks = personToEdit.getTasks(); // edit command does not allow editing tasks

return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags);
return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTasks);
}

@Override
Expand Down Expand Up @@ -137,27 +133,24 @@ public static class EditPersonDescriptor {
private Phone phone;
private Email email;
private Address address;
private Set<Tag> tags;

public EditPersonDescriptor() {}

/**
* Copy constructor.
* A defensive copy of {@code tags} is used internally.
*/
public EditPersonDescriptor(EditPersonDescriptor toCopy) {
setName(toCopy.name);
setPhone(toCopy.phone);
setEmail(toCopy.email);
setAddress(toCopy.address);
setTags(toCopy.tags);
}

/**
* Returns true if at least one field is edited.
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(name, phone, email, address, tags);
return CollectionUtil.isAnyNonNull(name, phone, email, address);
}

public void setName(Name name) {
Expand Down Expand Up @@ -192,23 +185,6 @@ public Optional<Address> getAddress() {
return Optional.ofNullable(address);
}

/**
* Sets {@code tags} to this object's {@code tags}.
* A defensive copy of {@code tags} is used internally.
*/
public void setTags(Set<Tag> tags) {
this.tags = (tags != null) ? new HashSet<>(tags) : null;
}

/**
* Returns an unmodifiable tag set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
* Returns {@code Optional#empty()} if {@code tags} is null.
*/
public Optional<Set<Tag>> getTags() {
return (tags != null) ? Optional.of(Collections.unmodifiableSet(tags)) : Optional.empty();
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand All @@ -224,8 +200,7 @@ public boolean equals(Object other) {
return Objects.equals(name, otherEditPersonDescriptor.name)
&& Objects.equals(phone, otherEditPersonDescriptor.phone)
&& Objects.equals(email, otherEditPersonDescriptor.email)
&& Objects.equals(address, otherEditPersonDescriptor.address)
&& Objects.equals(tags, otherEditPersonDescriptor.tags);
&& Objects.equals(address, otherEditPersonDescriptor.address);
}

@Override
Expand All @@ -235,7 +210,6 @@ public String toString() {
.add("phone", phone)
.add("email", email)
.add("address", address)
.add("tags", tags)
.toString();
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/seedu/address/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;

Expand All @@ -17,7 +17,7 @@
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.tag.Tag;
import seedu.address.model.task.Task;

/**
* Parses input arguments and creates a new AddCommand object
Expand All @@ -31,7 +31,7 @@ public class AddCommandParser implements Parser<AddCommand> {
*/
public AddCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_TAG);
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL)
|| !argMultimap.getPreamble().isEmpty()) {
Expand All @@ -43,9 +43,9 @@ public AddCommand parse(String args) throws ParseException {
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
Set<Task> taskList = new HashSet<>(); // add command does not allow assigning tasks straight away

Person person = new Person(name, phone, email, address, tagList);
Person person = new Person(name, phone, email, address, taskList);

return new AddCommand(person);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.AddTaskCommand;
import seedu.address.logic.commands.AssignCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteCommand;
Expand Down Expand Up @@ -75,6 +76,9 @@ public Command parseCommand(String userInput) throws ParseException {
case ListCommand.COMMAND_WORD:
return new ListCommand();

case AssignCommand.COMMAND_WORD:
return new AssignCommandParser().parse(arguments);

case ExitCommand.COMMAND_WORD:
return new ExitCommand();

Expand Down
39 changes: 39 additions & 0 deletions src/main/java/seedu/address/logic/parser/AssignCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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_TO;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.logic.commands.AssignCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new {@code AssignCommand} object
*/
public class AssignCommandParser implements Parser<AssignCommand> {
/**
* Parses the given {@code String} of arguments in the context of the {@code AssignCommand}
* and returns a {@code AssignCommand} object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public AssignCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args,
PREFIX_TO);

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_TO);
Index taskIndex;
Index personIndex;
try {
taskIndex = ParserUtil.parseIndex(argMultimap.getPreamble());
personIndex = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_TO).orElse(""));
} catch (IllegalValueException ive) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
AssignCommand.MESSAGE_USAGE), ive);
}

return new AssignCommand(taskIndex, personIndex);
}
}
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public class CliSyntax {
public static final Prefix PREFIX_EMAIL = new Prefix("e/");
public static final Prefix PREFIX_ADDRESS = new Prefix("a/");
public static final Prefix PREFIX_TAG = new Prefix("t/");
public static final Prefix PREFIX_TO = new Prefix("to/");

}
Loading

0 comments on commit 3ff249a

Please sign in to comment.