forked from nus-cs2103-AY1819S1/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement tasks unassign and contacts unassign (#102)
* Implement tasks unassign and contacts unassign * Fix lint
- Loading branch information
1 parent
6cb8f6f
commit c31f4c3
Showing
6 changed files
with
306 additions
and
0 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
src/main/java/seedu/address/logic/commands/contacts/UnassignCommand.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,98 @@ | ||
package seedu.address.logic.commands.contacts; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.contacts.CliSyntax.PREFIX_CONTACT_ID; | ||
import static seedu.address.logic.parser.contacts.CliSyntax.PREFIX_TASK_ID; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import seedu.address.commons.core.EventsCenter; | ||
import seedu.address.commons.core.Messages; | ||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.commons.events.ui.JumpToPersonListRequestEvent; | ||
import seedu.address.logic.CommandHistory; | ||
import seedu.address.logic.commands.Command; | ||
import seedu.address.logic.commands.CommandResult; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.Person; | ||
import seedu.address.model.person.PersonId; | ||
import seedu.address.model.task.Task; | ||
import seedu.address.model.task.TaskId; | ||
|
||
/** | ||
* Unassigns a contact from a task. Both contact and task are identified by the index number used in the displayed | ||
* person and task list respectively. | ||
*/ | ||
public class UnassignCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "unassign"; | ||
|
||
public static final String MESSAGE_USAGE = getCommandFormat(COMMAND_WORD) | ||
+ ": Unassigns a contact to a task. Both contact and task are identified by the index number used in the " | ||
+ "displayed person and task list respectively.\n" | ||
+ "Parameters: " | ||
+ PREFIX_CONTACT_ID + "CONTACT_INDEX " | ||
+ PREFIX_TASK_ID + "TASK_INDEX\n" | ||
+ "Example: " + getCommandFormat(COMMAND_WORD) + " " | ||
+ PREFIX_CONTACT_ID + "2 " | ||
+ PREFIX_TASK_ID + "4"; | ||
|
||
public static final String MESSAGE_ASSIGN_PERSON_SUCCESS = "Unassigned Person %1$s from Task %2$s"; | ||
|
||
private final Index targetContactIndex; | ||
private final Index targetTaskIndex; | ||
|
||
public UnassignCommand(Index targetContactIndex, Index targetTaskIndex) { | ||
this.targetContactIndex = targetContactIndex; | ||
this.targetTaskIndex = targetTaskIndex; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model, CommandHistory history) throws CommandException { | ||
requireNonNull(model); | ||
|
||
List<Person> filteredPersonList = model.getFilteredPersonList(); | ||
if (targetContactIndex.getZeroBased() >= filteredPersonList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); | ||
} | ||
|
||
List<Task> filteredTaskList = model.getFilteredTaskList(); | ||
if (targetTaskIndex.getZeroBased() >= filteredTaskList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX); | ||
} | ||
|
||
Person personToEdit = filteredPersonList.get(targetContactIndex.getZeroBased()); | ||
Task taskToUnassign = filteredTaskList.get(targetTaskIndex.getZeroBased()); | ||
|
||
Set<TaskId> updatedTaskIds = new HashSet<>(personToEdit.getTaskIds()); | ||
updatedTaskIds.remove(taskToUnassign.getId()); | ||
Person editedPerson = new Person(personToEdit.getId(), personToEdit.getName(), personToEdit.getPhone(), | ||
personToEdit.getEmail(), personToEdit.getAddress(), personToEdit.getTags(), updatedTaskIds); | ||
|
||
Set<PersonId> updatedPersonIds = new HashSet<>(taskToUnassign.getPersonIds()); | ||
updatedPersonIds.remove(personToEdit.getId()); | ||
Task editedTask = new Task(taskToUnassign.getId(), taskToUnassign.getName(), taskToUnassign.getStartDateTime(), | ||
taskToUnassign.getEndDateTime(), taskToUnassign.getTags(), updatedPersonIds); | ||
|
||
model.updatePerson(personToEdit, editedPerson); | ||
model.updateTask(taskToUnassign, editedTask); | ||
model.commitAddressBook(); | ||
|
||
EventsCenter.getInstance().post(new JumpToPersonListRequestEvent(targetContactIndex)); | ||
return new CommandResult(String.format(MESSAGE_ASSIGN_PERSON_SUCCESS, | ||
targetContactIndex.getOneBased(), targetTaskIndex.getOneBased())); | ||
|
||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof UnassignCommand // instanceof handles nulls | ||
&& targetContactIndex.equals(((UnassignCommand) other).targetContactIndex) // state checks | ||
&& targetTaskIndex.equals(((UnassignCommand) other).targetTaskIndex)); // state checks | ||
} | ||
} | ||
|
98 changes: 98 additions & 0 deletions
98
src/main/java/seedu/address/logic/commands/tasks/UnassignCommand.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,98 @@ | ||
package seedu.address.logic.commands.tasks; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.contacts.CliSyntax.PREFIX_CONTACT_ID; | ||
import static seedu.address.logic.parser.contacts.CliSyntax.PREFIX_TASK_ID; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import seedu.address.commons.core.EventsCenter; | ||
import seedu.address.commons.core.Messages; | ||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.commons.events.ui.JumpToPersonListRequestEvent; | ||
import seedu.address.logic.CommandHistory; | ||
import seedu.address.logic.commands.Command; | ||
import seedu.address.logic.commands.CommandResult; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.Person; | ||
import seedu.address.model.person.PersonId; | ||
import seedu.address.model.task.Task; | ||
import seedu.address.model.task.TaskId; | ||
|
||
/** | ||
* Unassigns a task from a contact. Both contact and task are identified by the index number used in the displayed | ||
* person and task list respectively. | ||
*/ | ||
public class UnassignCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "unassign"; | ||
|
||
public static final String MESSAGE_USAGE = getCommandFormat(COMMAND_WORD) | ||
+ ": Unassigns a task from a contact. Both contact and task are identified by the index number used in the " | ||
+ "displayed person and task list respectively.\n" | ||
+ "Parameters: " | ||
+ PREFIX_CONTACT_ID + "CONTACT_INDEX " | ||
+ PREFIX_TASK_ID + "TASK_INDEX\n" | ||
+ "Example: " + getCommandFormat(COMMAND_WORD) + " " | ||
+ PREFIX_CONTACT_ID + "2 " | ||
+ PREFIX_TASK_ID + "4"; | ||
|
||
public static final String MESSAGE_ASSIGN_TASK_SUCCESS = "Unassigned Task %1$s from Person %2$s"; | ||
|
||
private final Index targetContactIndex; | ||
private final Index targetTaskIndex; | ||
|
||
public UnassignCommand(Index targetContactIndex, Index targetTaskIndex) { | ||
this.targetContactIndex = targetContactIndex; | ||
this.targetTaskIndex = targetTaskIndex; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model, CommandHistory history) throws CommandException { | ||
requireNonNull(model); | ||
|
||
List<Person> filteredPersonList = model.getFilteredPersonList(); | ||
if (targetContactIndex.getZeroBased() >= filteredPersonList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); | ||
} | ||
|
||
List<Task> filteredTaskList = model.getFilteredTaskList(); | ||
if (targetTaskIndex.getZeroBased() >= filteredTaskList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX); | ||
} | ||
|
||
Person personToEdit = filteredPersonList.get(targetContactIndex.getZeroBased()); | ||
Task taskToUnassign = filteredTaskList.get(targetTaskIndex.getZeroBased()); | ||
|
||
Set<TaskId> updatedTaskIds = new HashSet<>(personToEdit.getTaskIds()); | ||
updatedTaskIds.remove(taskToUnassign.getId()); | ||
Person editedPerson = new Person(personToEdit.getId(), personToEdit.getName(), personToEdit.getPhone(), | ||
personToEdit.getEmail(), personToEdit.getAddress(), personToEdit.getTags(), updatedTaskIds); | ||
|
||
Set<PersonId> updatedPersonIds = new HashSet<>(taskToUnassign.getPersonIds()); | ||
updatedPersonIds.remove(personToEdit.getId()); | ||
Task editedTask = new Task(taskToUnassign.getId(), taskToUnassign.getName(), taskToUnassign.getStartDateTime(), | ||
taskToUnassign.getEndDateTime(), taskToUnassign.getTags(), updatedPersonIds); | ||
|
||
model.updatePerson(personToEdit, editedPerson); | ||
model.updateTask(taskToUnassign, editedTask); | ||
model.commitAddressBook(); | ||
|
||
EventsCenter.getInstance().post(new JumpToPersonListRequestEvent(targetContactIndex)); | ||
return new CommandResult(String.format(MESSAGE_ASSIGN_TASK_SUCCESS, | ||
targetTaskIndex.getOneBased(), targetContactIndex.getOneBased())); | ||
|
||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof UnassignCommand // instanceof handles nulls | ||
&& targetContactIndex.equals(((UnassignCommand) other).targetContactIndex) // state checks | ||
&& targetTaskIndex.equals(((UnassignCommand) other).targetTaskIndex)); // state checks | ||
} | ||
} | ||
|
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
50 changes: 50 additions & 0 deletions
50
src/main/java/seedu/address/logic/parser/contacts/UnassignCommandParser.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,50 @@ | ||
package seedu.address.logic.parser.contacts; | ||
|
||
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.contacts.CliSyntax.PREFIX_CONTACT_ID; | ||
import static seedu.address.logic.parser.contacts.CliSyntax.PREFIX_TASK_ID; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.contacts.UnassignCommand; | ||
import seedu.address.logic.parser.ArgumentMultimap; | ||
import seedu.address.logic.parser.ArgumentTokenizer; | ||
import seedu.address.logic.parser.Parser; | ||
import seedu.address.logic.parser.Prefix; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses input arguments and creates a new UnassignCommand object | ||
*/ | ||
public class UnassignCommandParser implements Parser<UnassignCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the UnassignCommand | ||
* and returns an UnassignCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public UnassignCommand parse(String args) throws ParseException { | ||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, PREFIX_CONTACT_ID, PREFIX_TASK_ID); | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_CONTACT_ID, PREFIX_TASK_ID) | ||
|| !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, UnassignCommand.MESSAGE_USAGE), | ||
true); | ||
} | ||
|
||
Index contactIndex = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_CONTACT_ID).get()); | ||
Index taskIndex = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_TASK_ID).get()); | ||
return new UnassignCommand(contactIndex, taskIndex); | ||
} | ||
|
||
/** | ||
* Returns true if none of the prefixes contains empty {@code Optional} values in the given | ||
* {@code ArgumentMultimap}. | ||
*/ | ||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
} | ||
|
50 changes: 50 additions & 0 deletions
50
src/main/java/seedu/address/logic/parser/tasks/UnassignCommandParser.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,50 @@ | ||
package seedu.address.logic.parser.tasks; | ||
|
||
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.contacts.CliSyntax.PREFIX_CONTACT_ID; | ||
import static seedu.address.logic.parser.contacts.CliSyntax.PREFIX_TASK_ID; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.tasks.UnassignCommand; | ||
import seedu.address.logic.parser.ArgumentMultimap; | ||
import seedu.address.logic.parser.ArgumentTokenizer; | ||
import seedu.address.logic.parser.Parser; | ||
import seedu.address.logic.parser.Prefix; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses input arguments and creates a new UnassignCommand object | ||
*/ | ||
public class UnassignCommandParser implements Parser<UnassignCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the UnassignCommand | ||
* and returns an UnassignCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public UnassignCommand parse(String args) throws ParseException { | ||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, PREFIX_CONTACT_ID, PREFIX_TASK_ID); | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_CONTACT_ID, PREFIX_TASK_ID) | ||
|| !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, UnassignCommand.MESSAGE_USAGE), | ||
true); | ||
} | ||
|
||
Index contactIndex = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_CONTACT_ID).get()); | ||
Index taskIndex = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_TASK_ID).get()); | ||
return new UnassignCommand(contactIndex, taskIndex); | ||
} | ||
|
||
/** | ||
* Returns true if none of the prefixes contains empty {@code Optional} values in the given | ||
* {@code ArgumentMultimap}. | ||
*/ | ||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
} | ||
|