forked from nus-cs2103-AY2223S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 3
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 #66 from Santosh3007/65-edit-task
Add `edit_task` Command
- Loading branch information
Showing
9 changed files
with
612 additions
and
9 deletions.
There are no files selected for viewing
168 changes: 168 additions & 0 deletions
168
src/main/java/swift/logic/commands/EditTaskCommand.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,168 @@ | ||
package swift.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static swift.logic.parser.CliSyntax.PREFIX_CONTACT; | ||
import static swift.logic.parser.CliSyntax.PREFIX_NAME; | ||
import static swift.model.Model.PREDICATE_SHOW_ALL_TASKS; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import swift.commons.core.Messages; | ||
import swift.commons.core.index.Index; | ||
import swift.commons.util.CollectionUtil; | ||
import swift.logic.commands.exceptions.CommandException; | ||
import swift.model.Model; | ||
import swift.model.task.Task; | ||
import swift.model.task.TaskName; | ||
|
||
/** | ||
* Edits the details of an existing task in the address book. | ||
*/ | ||
public class EditTaskCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "edit_task"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the task identified " | ||
+ "by the index number used in the displayed task list. " | ||
+ "Existing values will be overwritten by the input values.\n" | ||
+ "Parameters: INDEX (must be a positive integer) " | ||
+ "[" + PREFIX_NAME + "NAME] " | ||
+ "[" + PREFIX_CONTACT + "CONTACT] " | ||
+ "Example: " + COMMAND_WORD + " 1 " | ||
+ PREFIX_NAME + "Finish Assignment " | ||
+ PREFIX_CONTACT + "2"; | ||
|
||
public static final String MESSAGE_EDIT_TASK_SUCCESS = "Edited Task: %1$s"; | ||
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided."; | ||
public static final String MESSAGE_DUPLICATE_TASK = "This task already exists in the address book."; | ||
|
||
private final Index index; | ||
private final EditTaskDescriptor editTaskDescriptor; | ||
|
||
/** | ||
* @param index of the task in the filtered task list to edit | ||
* @param editTaskDescriptor details to edit the task with | ||
*/ | ||
public EditTaskCommand(Index index, EditTaskDescriptor editTaskDescriptor) { | ||
requireNonNull(index); | ||
requireNonNull(editTaskDescriptor); | ||
|
||
this.index = index; | ||
this.editTaskDescriptor = new EditTaskDescriptor(editTaskDescriptor); | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
List<Task> lastShownList = model.getFilteredTaskList(); | ||
|
||
if (index.getZeroBased() >= lastShownList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX); | ||
} | ||
|
||
Task taskToEdit = lastShownList.get(index.getZeroBased()); | ||
Task editedTask = createEditedTask(taskToEdit, editTaskDescriptor); | ||
|
||
if (!taskToEdit.equals(editedTask) && model.hasTask(editedTask)) { | ||
throw new CommandException(MESSAGE_DUPLICATE_TASK); | ||
} | ||
|
||
model.setTask(taskToEdit, editedTask); | ||
model.updateFilteredTaskList(PREDICATE_SHOW_ALL_TASKS); | ||
return new CommandResult(String.format(MESSAGE_EDIT_TASK_SUCCESS, editedTask)); | ||
} | ||
|
||
/** | ||
* Creates and returns a {@code Task} with the details of {@code taskToEdit} | ||
* edited with {@code editTaskDescriptor}. | ||
*/ | ||
private static Task createEditedTask(Task taskToEdit, EditTaskDescriptor editTaskDescriptor) { | ||
assert taskToEdit != null; | ||
|
||
TaskName updatedTaskName = editTaskDescriptor.getTaskName().orElse(taskToEdit.getTaskName()); | ||
Index updatedContactIndex = editTaskDescriptor.getContactIndex().orElse(taskToEdit.getContactIndex()); | ||
|
||
return new Task(updatedTaskName, updatedContactIndex); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
// short circuit if same object | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof EditTaskCommand)) { | ||
return false; | ||
} | ||
|
||
// state check | ||
EditTaskCommand e = (EditTaskCommand) other; | ||
return index.equals(e.index) | ||
&& editTaskDescriptor.equals(e.editTaskDescriptor); | ||
} | ||
|
||
/** | ||
* Stores the details to edit the task with. Each non-empty field value will replace the | ||
* corresponding field value of the task. | ||
*/ | ||
public static class EditTaskDescriptor { | ||
private TaskName taskName; | ||
private Index contactIndex; | ||
|
||
public EditTaskDescriptor() {} | ||
|
||
/** | ||
* Copy constructor. | ||
* A defensive copy of {@code tags} is used internally. | ||
*/ | ||
public EditTaskDescriptor(EditTaskDescriptor toCopy) { | ||
setTaskName(toCopy.taskName); | ||
setContactIndex(toCopy.contactIndex); | ||
} | ||
|
||
/** | ||
* Returns true if at least one field is edited. | ||
*/ | ||
public boolean isAnyFieldEdited() { | ||
return CollectionUtil.isAnyNonNull(taskName, contactIndex); | ||
} | ||
|
||
public void setTaskName(TaskName taskName) { | ||
this.taskName = taskName; | ||
} | ||
|
||
public Optional<TaskName> getTaskName() { | ||
return Optional.ofNullable(taskName); | ||
} | ||
|
||
public void setContactIndex(Index contactIndex) { | ||
this.contactIndex = contactIndex; | ||
} | ||
|
||
public Optional<Index> getContactIndex() { | ||
return Optional.ofNullable(contactIndex); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
// short circuit if same object | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof EditTaskDescriptor)) { | ||
return false; | ||
} | ||
|
||
// state check | ||
EditTaskDescriptor e = (EditTaskDescriptor) other; | ||
|
||
return getTaskName().equals(e.getTaskName()) | ||
&& getContactIndex().equals(e.getContactIndex()); | ||
} | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
src/main/java/swift/logic/parser/EditTaskCommandParser.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,51 @@ | ||
package swift.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static swift.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static swift.logic.parser.CliSyntax.PREFIX_CONTACT; | ||
import static swift.logic.parser.CliSyntax.PREFIX_NAME; | ||
|
||
import swift.commons.core.index.Index; | ||
import swift.logic.commands.EditTaskCommand; | ||
import swift.logic.commands.EditTaskCommand.EditTaskDescriptor; | ||
import swift.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses input arguments and creates a new EditTaskCommand object | ||
*/ | ||
public class EditTaskCommandParser implements Parser<EditTaskCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the EditTaskCommand | ||
* and returns an EditTaskCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public EditTaskCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_CONTACT); | ||
|
||
Index index; | ||
|
||
try { | ||
index = ParserUtil.parseIndex(argMultimap.getPreamble()); | ||
} catch (ParseException pe) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditTaskCommand.MESSAGE_USAGE), pe); | ||
} | ||
|
||
EditTaskDescriptor editTaskDescriptor = new EditTaskDescriptor(); | ||
if (argMultimap.getValue(PREFIX_NAME).isPresent()) { | ||
editTaskDescriptor.setTaskName(ParserUtil.parseTaskName(argMultimap.getValue(PREFIX_NAME).get())); | ||
} | ||
if (argMultimap.getValue(PREFIX_CONTACT).isPresent()) { | ||
editTaskDescriptor.setContactIndex(ParserUtil.parseIndex(argMultimap.getValue(PREFIX_CONTACT).get())); | ||
} | ||
|
||
if (!editTaskDescriptor.isAnyFieldEdited()) { | ||
throw new ParseException(EditTaskCommand.MESSAGE_NOT_EDITED); | ||
} | ||
|
||
return new EditTaskCommand(index, editTaskDescriptor); | ||
} | ||
|
||
} |
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
Oops, something went wrong.