From 5f9c6efe6e64011ed3f4487d5df3f981dd176749 Mon Sep 17 00:00:00 2001 From: feliciagan Date: Thu, 13 Oct 2022 21:05:40 +0800 Subject: [PATCH] Update mark unmark task commands Mark and unmark task commands do not change the gui immediately after being executed. Updating the commands gets rid of this bug. Let's update mark and unmark task commands. --- .../logic/commands/EditTaskDescriptor.java | 12 ++ .../logic/commands/task/EditTaskCommand.java | 113 ++---------------- .../logic/commands/task/MarkTaskCommand.java | 72 +++++++++-- .../commands/task/UnmarkTaskCommand.java | 70 +++++++++-- .../parser/task/EditTaskCommandParser.java | 4 +- .../parser/task/MarkTaskCommandParser.java | 14 ++- .../parser/task/UnmarkTaskCommandParser.java | 14 ++- .../java/seedu/address/model/AddressBook.java | 22 +++- .../seedu/address/model/ModelManager.java | 10 +- .../address/model/ReadOnlyAddressBook.java | 6 - .../java/seedu/address/model/task/Task.java | 17 +-- .../seedu/address/model/task/TaskList.java | 56 ++++++++- .../seedu/address/model/AddressBookTest.java | 5 - 13 files changed, 247 insertions(+), 168 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/EditTaskDescriptor.java b/src/main/java/seedu/address/logic/commands/EditTaskDescriptor.java index 3cb71f3bbc5..7a3bae4ab18 100644 --- a/src/main/java/seedu/address/logic/commands/EditTaskDescriptor.java +++ b/src/main/java/seedu/address/logic/commands/EditTaskDescriptor.java @@ -7,6 +7,7 @@ import seedu.address.commons.util.CollectionUtil; import seedu.address.model.tag.Tag; +import seedu.address.model.task.Deadline; import seedu.address.model.task.Description; /** @@ -15,6 +16,7 @@ */ public class EditTaskDescriptor { private Description description; + private Deadline deadline; private boolean isDone; private Set tags; @@ -26,6 +28,7 @@ public EditTaskDescriptor() {} */ public EditTaskDescriptor(EditTaskDescriptor toCopy) { setDescription(toCopy.description); + setDeadline(toCopy.deadline); setIsDone(toCopy.isDone); setTags(toCopy.tags); } @@ -45,6 +48,14 @@ public Optional getDescription() { return Optional.ofNullable(description); } + public void setDeadline(Deadline deadline) { + this.deadline = deadline; + } + + public Optional getDeadline() { + return Optional.ofNullable(deadline); + } + public void setIsDone(boolean isDone) { this.isDone = isDone; } @@ -86,6 +97,7 @@ public boolean equals(Object other) { EditTaskDescriptor e = (EditTaskDescriptor) other; return getDescription().equals(e.getDescription()) + && getDeadline().equals(e.getDeadline()) && getIsDone().equals(e.getIsDone()) && getTags().equals(e.getTags()); } diff --git a/src/main/java/seedu/address/logic/commands/task/EditTaskCommand.java b/src/main/java/seedu/address/logic/commands/task/EditTaskCommand.java index e09a6d87f99..0d9f41b384b 100644 --- a/src/main/java/seedu/address/logic/commands/task/EditTaskCommand.java +++ b/src/main/java/seedu/address/logic/commands/task/EditTaskCommand.java @@ -4,17 +4,14 @@ import static seedu.address.logic.parser.CliSyntax.PREFIX_TASK_DESCRIPTION; import static seedu.address.model.Model.PREDICATE_SHOW_ALL_TASKS; -import java.util.Collections; -import java.util.HashSet; import java.util.List; -import java.util.Optional; import java.util.Set; import seedu.address.commons.core.Messages; import seedu.address.commons.core.index.Index; -import seedu.address.commons.util.CollectionUtil; import seedu.address.logic.commands.Command; import seedu.address.logic.commands.CommandResult; +import seedu.address.logic.commands.EditTaskDescriptor; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.Model; import seedu.address.model.tag.Tag; @@ -42,18 +39,19 @@ public class EditTaskCommand extends Command { public static final String MESSAGE_DUPLICATE_TASK = "This task already exists in the task list."; private final Index index; - private final EditTaskCommand.EditTaskDescriptor editTaskDescriptor; + private final EditTaskDescriptor editTaskDescriptor; /** - * @param index of the person in the filtered person list to edit - * @param editTaskDescriptor details to edit the person with + * Creates an EditTaskCommand object. + * @param index of the task in the filtered task list to edit. + * @param editTaskDescriptor details to edit the task with. */ - public EditTaskCommand(Index index, EditTaskCommand.EditTaskDescriptor editTaskDescriptor) { + public EditTaskCommand(Index index, EditTaskDescriptor editTaskDescriptor) { requireNonNull(index); requireNonNull(editTaskDescriptor); this.index = index; - this.editTaskDescriptor = new EditTaskCommand.EditTaskDescriptor(editTaskDescriptor); + this.editTaskDescriptor = new EditTaskDescriptor(editTaskDescriptor); } @Override @@ -62,7 +60,7 @@ public CommandResult execute(Model model) throws CommandException { List lastShownList = model.getFilteredTaskList(); if (index.getZeroBased() >= lastShownList.size()) { - throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX); } Task taskToEdit = lastShownList.get(index.getZeroBased()); @@ -81,12 +79,12 @@ public CommandResult execute(Model model) throws CommandException { * Creates and returns a {@code Task} with the details of {@code taskToEdit} * edited with {@code editTaskDescriptor}. */ - private static Task createEditedTask(Task taskToEdit, EditTaskCommand.EditTaskDescriptor editTaskDescriptor) { + private static Task createEditedTask(Task taskToEdit, EditTaskDescriptor editTaskDescriptor) { assert taskToEdit != null; Description updatedDescription = editTaskDescriptor.getDescription().orElse(taskToEdit.getDescription()); Deadline updatedDeadline = editTaskDescriptor.getDeadline().orElse(taskToEdit.getDeadline()); - Boolean updatedIsDone = editTaskDescriptor.getStatus().orElse(taskToEdit.getStatus()); + Boolean updatedIsDone = editTaskDescriptor.getIsDone().orElse(taskToEdit.getStatus()); Set updatedTags = editTaskDescriptor.getTags().orElse(taskToEdit.getTags()); return new Task(updatedDescription, updatedDeadline, updatedIsDone, updatedTags); @@ -109,95 +107,4 @@ public boolean equals(Object 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 person. - */ - public static class EditTaskDescriptor { - private Description description; - private Deadline deadline; - private Boolean isDone; - private Set tags; - - public EditTaskDescriptor() {} - - /** - * Copy constructor. - * A defensive copy of {@code tags} is used internally. - */ - public EditTaskDescriptor(EditTaskCommand.EditTaskDescriptor toCopy) { - setDescription(toCopy.description); - setDeadline(toCopy.deadline); - setStatus(toCopy.isDone); - setTags(toCopy.tags); - } - - /** - * Returns true if at least one field is edited. - */ - public boolean isAnyFieldEdited() { - return CollectionUtil.isAnyNonNull(description, tags); - } - - public void setDescription(Description description) { - this.description = description; - } - - public Optional getDescription() { - return Optional.ofNullable(description); - } - - public void setDeadline(Deadline deadline) { - this.deadline = deadline; - } - - public Optional getDeadline() { - return Optional.ofNullable(deadline); - } - - public void setStatus(Boolean isDone) { - this.isDone = isDone; - } - - public Optional getStatus() { - return Optional.ofNullable(isDone); - } - - /** - * Sets {@code tags} to this object's {@code tags}. - * A defensive copy of {@code tags} is used internally. - */ - public void setTags(Set 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> getTags() { - return (tags != null) ? Optional.of(Collections.unmodifiableSet(tags)) : Optional.empty(); - } - - @Override - public boolean equals(Object other) { - // short circuit if same object - if (other == this) { - return true; - } - - // instanceof handles nulls - if (!(other instanceof EditTaskCommand.EditTaskDescriptor)) { - return false; - } - - // state check - EditTaskCommand.EditTaskDescriptor e = (EditTaskCommand.EditTaskDescriptor) other; - - return getDescription().equals(e.getDescription()) - && getTags().equals(e.getTags()); - } - } } diff --git a/src/main/java/seedu/address/logic/commands/task/MarkTaskCommand.java b/src/main/java/seedu/address/logic/commands/task/MarkTaskCommand.java index afce0df08a5..c55ea6bc3d1 100644 --- a/src/main/java/seedu/address/logic/commands/task/MarkTaskCommand.java +++ b/src/main/java/seedu/address/logic/commands/task/MarkTaskCommand.java @@ -1,15 +1,21 @@ package seedu.address.logic.commands.task; import static java.util.Objects.requireNonNull; +import static seedu.address.model.Model.PREDICATE_SHOW_ALL_TASKS; import java.util.List; +import java.util.Set; import seedu.address.commons.core.Messages; import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.Command; import seedu.address.logic.commands.CommandResult; +import seedu.address.logic.commands.EditTaskDescriptor; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.Model; +import seedu.address.model.tag.Tag; +import seedu.address.model.task.Deadline; +import seedu.address.model.task.Description; import seedu.address.model.task.Task; /** @@ -24,12 +30,23 @@ public class MarkTaskCommand extends Command { + "Parameters: INDEX (must be a positive integer)\n" + "Example: " + COMMAND_WORD + " 1"; - public static final String MESSAGE_MARK_TEST_SUCCESS = "Marked Task: %1$s"; + public static final String MESSAGE_MARK_TASK_SUCCESS = "Marked Task: %1$s"; + public static final String MESSAGE_DUPLICATE_TASK = "This task already exists in the task list."; - private final Index targetIndex; + private final Index index; + private final EditTaskDescriptor editTaskDescriptor; - public MarkTaskCommand(Index targetIndex) { - this.targetIndex = targetIndex; + /** + * Creates an MarkTaskCommand object. + * @param index of the task in the filtered task list to edit. + * @param editTaskDescriptor details to edit the task with. + */ + public MarkTaskCommand(Index index, EditTaskDescriptor editTaskDescriptor) { + requireNonNull(index); + requireNonNull(editTaskDescriptor); + + this.index = index; + this.editTaskDescriptor = editTaskDescriptor; } @Override @@ -37,19 +54,52 @@ public CommandResult execute(Model model) throws CommandException { requireNonNull(model); List lastShownList = model.getFilteredTaskList(); - if (targetIndex.getZeroBased() >= lastShownList.size()) { + if (index.getZeroBased() >= lastShownList.size()) { throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX); } - Task taskToMark = lastShownList.get(targetIndex.getZeroBased()); - taskToMark.markTask(); - return new CommandResult(String.format(MESSAGE_MARK_TEST_SUCCESS, taskToMark)); + Task taskToMark = lastShownList.get(index.getZeroBased()); + Task markedTask = createEditedTask(taskToMark, editTaskDescriptor); + + if (!taskToMark.isSameTask(markedTask) && model.hasTask(markedTask)) { + throw new CommandException(MESSAGE_DUPLICATE_TASK); + } + + model.setTask(taskToMark, markedTask); + model.updateFilteredTaskList(PREDICATE_SHOW_ALL_TASKS); + return new CommandResult(String.format(MESSAGE_MARK_TASK_SUCCESS, markedTask)); + } + + /** + * 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; + + Description updatedDescription = editTaskDescriptor.getDescription().orElse(taskToEdit.getDescription()); + Deadline updatedDeadline = editTaskDescriptor.getDeadline().orElse(taskToEdit.getDeadline()); + Boolean updatedIsDone = editTaskDescriptor.getIsDone().orElse(taskToEdit.getStatus()); + Set updatedTags = editTaskDescriptor.getTags().orElse(taskToEdit.getTags()); + + return new Task(updatedDescription, updatedDeadline, updatedIsDone, updatedTags); } @Override public boolean equals(Object other) { - return other == this - || (other instanceof MarkTaskCommand - && targetIndex.equals(((MarkTaskCommand) other).targetIndex)); + // short circuit if same object + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof MarkTaskCommand)) { + return false; + } + + // state check + MarkTaskCommand m = (MarkTaskCommand) other; + return index.equals(m.index) + && editTaskDescriptor.equals(m.editTaskDescriptor); } } diff --git a/src/main/java/seedu/address/logic/commands/task/UnmarkTaskCommand.java b/src/main/java/seedu/address/logic/commands/task/UnmarkTaskCommand.java index 414b7238401..3bacbf164f6 100644 --- a/src/main/java/seedu/address/logic/commands/task/UnmarkTaskCommand.java +++ b/src/main/java/seedu/address/logic/commands/task/UnmarkTaskCommand.java @@ -1,15 +1,21 @@ package seedu.address.logic.commands.task; import static java.util.Objects.requireNonNull; +import static seedu.address.model.Model.PREDICATE_SHOW_ALL_TASKS; import java.util.List; +import java.util.Set; import seedu.address.commons.core.Messages; import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.Command; import seedu.address.logic.commands.CommandResult; +import seedu.address.logic.commands.EditTaskDescriptor; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.Model; +import seedu.address.model.tag.Tag; +import seedu.address.model.task.Deadline; +import seedu.address.model.task.Description; import seedu.address.model.task.Task; /** @@ -25,11 +31,22 @@ public class UnmarkTaskCommand extends Command { + "Example: " + COMMAND_WORD + " 1"; public static final String MESSAGE_UNMARK_TASK_SUCCESS = "Unmarked Task: %1$s"; + public static final String MESSAGE_DUPLICATE_TASK = "This task already exists in the task list."; - private final Index targetIndex; + private final Index index; + private final EditTaskDescriptor editTaskDescriptor; - public UnmarkTaskCommand(Index targetIndex) { - this.targetIndex = targetIndex; + /** + * Creates an UnmarkTaskCommand object. + * @param index of the task in the filtered task list to edit. + * @param editTaskDescriptor details to edit the task with. + */ + public UnmarkTaskCommand(Index index, EditTaskDescriptor editTaskDescriptor) { + requireNonNull(index); + requireNonNull(editTaskDescriptor); + + this.index = index; + this.editTaskDescriptor = editTaskDescriptor; } @Override @@ -37,19 +54,52 @@ public CommandResult execute(Model model) throws CommandException { requireNonNull(model); List lastShownList = model.getFilteredTaskList(); - if (targetIndex.getZeroBased() >= lastShownList.size()) { + if (index.getZeroBased() >= lastShownList.size()) { throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX); } - Task taskToUnmark = lastShownList.get(targetIndex.getZeroBased()); - taskToUnmark.unmarkTask(); - return new CommandResult(String.format(MESSAGE_UNMARK_TASK_SUCCESS, taskToUnmark)); + Task taskToUnmark = lastShownList.get(index.getZeroBased()); + Task unmarkedTask = createEditedTask(taskToUnmark, editTaskDescriptor); + + if (!taskToUnmark.isSameTask(unmarkedTask) && model.hasTask(unmarkedTask)) { + throw new CommandException(MESSAGE_DUPLICATE_TASK); + } + + model.setTask(taskToUnmark, unmarkedTask); + model.updateFilteredTaskList(PREDICATE_SHOW_ALL_TASKS); + return new CommandResult(String.format(MESSAGE_UNMARK_TASK_SUCCESS, unmarkedTask)); + } + + /** + * 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; + + Description updatedDescription = editTaskDescriptor.getDescription().orElse(taskToEdit.getDescription()); + Deadline updatedDeadline = editTaskDescriptor.getDeadline().orElse(taskToEdit.getDeadline()); + Boolean updatedIsDone = editTaskDescriptor.getIsDone().orElse(taskToEdit.getStatus()); + Set updatedTags = editTaskDescriptor.getTags().orElse(taskToEdit.getTags()); + + return new Task(updatedDescription, updatedDeadline, updatedIsDone, updatedTags); } @Override public boolean equals(Object other) { - return other == this - || (other instanceof UnmarkTaskCommand - && targetIndex.equals(((UnmarkTaskCommand) other).targetIndex)); + // short circuit if same object + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof UnmarkTaskCommand)) { + return false; + } + + // state check + UnmarkTaskCommand u = (UnmarkTaskCommand) other; + return index.equals(u.index) + && editTaskDescriptor.equals(u.editTaskDescriptor); } } diff --git a/src/main/java/seedu/address/logic/parser/task/EditTaskCommandParser.java b/src/main/java/seedu/address/logic/parser/task/EditTaskCommandParser.java index 68f780894a7..d74a8cf096c 100644 --- a/src/main/java/seedu/address/logic/parser/task/EditTaskCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/task/EditTaskCommandParser.java @@ -11,8 +11,8 @@ import java.util.Set; import seedu.address.commons.core.index.Index; +import seedu.address.logic.commands.EditTaskDescriptor; import seedu.address.logic.commands.task.EditTaskCommand; -import seedu.address.logic.commands.task.EditTaskCommand.EditTaskDescriptor; import seedu.address.logic.parser.ArgumentMultimap; import seedu.address.logic.parser.ArgumentTokenizer; import seedu.address.logic.parser.Parser; @@ -44,7 +44,7 @@ public EditTaskCommand parse(String args) throws ParseException { String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditTaskCommand.MESSAGE_USAGE), pe); } - EditTaskCommand.EditTaskDescriptor editTaskDescriptor = new EditTaskDescriptor(); + EditTaskDescriptor editTaskDescriptor = new EditTaskDescriptor(); if (argMultimap.getValue(PREFIX_TASK_DESCRIPTION).isPresent()) { editTaskDescriptor.setDescription( ParserUtil.parseDescription(argMultimap.getValue(PREFIX_TASK_DESCRIPTION).get())); diff --git a/src/main/java/seedu/address/logic/parser/task/MarkTaskCommandParser.java b/src/main/java/seedu/address/logic/parser/task/MarkTaskCommandParser.java index 3961cf4d3c4..ac25404edb0 100644 --- a/src/main/java/seedu/address/logic/parser/task/MarkTaskCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/task/MarkTaskCommandParser.java @@ -1,8 +1,10 @@ package seedu.address.logic.parser.task; +import static java.util.Objects.requireNonNull; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; import seedu.address.commons.core.index.Index; +import seedu.address.logic.commands.EditTaskDescriptor; import seedu.address.logic.commands.task.MarkTaskCommand; import seedu.address.logic.parser.Parser; import seedu.address.logic.parser.ParserUtil; @@ -18,12 +20,20 @@ public class MarkTaskCommandParser implements Parser { * @throws ParseException if the user input does not conform the expected format */ public MarkTaskCommand parse(String args) throws ParseException { + requireNonNull(args); + + Index index; + try { - Index index = ParserUtil.parseIndex(args); - return new MarkTaskCommand(index); + index = ParserUtil.parseIndex(args); } catch (ParseException pe) { throw new ParseException( String.format(MESSAGE_INVALID_COMMAND_FORMAT, MarkTaskCommand.MESSAGE_USAGE), pe); } + + EditTaskDescriptor editTaskDescriptor = new EditTaskDescriptor(); + editTaskDescriptor.setIsDone(true); + + return new MarkTaskCommand(index, editTaskDescriptor); } } diff --git a/src/main/java/seedu/address/logic/parser/task/UnmarkTaskCommandParser.java b/src/main/java/seedu/address/logic/parser/task/UnmarkTaskCommandParser.java index 043885e913d..d94c3dd3597 100644 --- a/src/main/java/seedu/address/logic/parser/task/UnmarkTaskCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/task/UnmarkTaskCommandParser.java @@ -1,8 +1,10 @@ package seedu.address.logic.parser.task; +import static java.util.Objects.requireNonNull; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; import seedu.address.commons.core.index.Index; +import seedu.address.logic.commands.EditTaskDescriptor; import seedu.address.logic.commands.task.UnmarkTaskCommand; import seedu.address.logic.parser.Parser; import seedu.address.logic.parser.ParserUtil; @@ -18,12 +20,20 @@ public class UnmarkTaskCommandParser implements Parser { * @throws ParseException if the user input does not conform the expected format */ public UnmarkTaskCommand parse(String args) throws ParseException { + requireNonNull(args); + + Index index; + try { - Index index = ParserUtil.parseIndex(args); - return new UnmarkTaskCommand(index); + index = ParserUtil.parseIndex(args); } catch (ParseException pe) { throw new ParseException( String.format(MESSAGE_INVALID_COMMAND_FORMAT, UnmarkTaskCommand.MESSAGE_USAGE), pe); } + + EditTaskDescriptor editTaskDescriptor = new EditTaskDescriptor(); + editTaskDescriptor.setIsDone(false); + + return new UnmarkTaskCommand(index, editTaskDescriptor); } } diff --git a/src/main/java/seedu/address/model/AddressBook.java b/src/main/java/seedu/address/model/AddressBook.java index f16a63904d6..b69f0cfaf22 100644 --- a/src/main/java/seedu/address/model/AddressBook.java +++ b/src/main/java/seedu/address/model/AddressBook.java @@ -3,6 +3,7 @@ import static java.util.Objects.requireNonNull; import java.util.List; +import java.util.Objects; import javafx.collections.ObservableList; import seedu.address.model.person.Person; @@ -116,14 +117,26 @@ public ObservableList getPersonList() { @Override public boolean equals(Object other) { - return other == this // short circuit if same object - || (other instanceof AddressBook // instanceof handles nulls - && persons.equals(((AddressBook) other).persons)); + // short circuit if same object + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof AddressBook)) { + return false; + } + + // state check + AddressBook a = (AddressBook) other; + return persons.equals(a.persons) + && tasks.equals(a.tasks) + && tags.equals(a.tags); } @Override public int hashCode() { - return persons.hashCode(); + return Objects.hash(persons, tasks, tags); } //task methods @@ -133,7 +146,6 @@ public ObservableList getTaskList() { return tasks.asUnmodifiableObservableList(); } - @Override public void addTask(Task task) { tasks.addTask(task); } diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index b35281110ad..b7d0c0da7ab 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -26,6 +26,7 @@ public class ModelManager implements Model { private final UserPrefs userPrefs; private final FilteredList filteredPersons; private final FilteredList filteredTasks; + private final FilteredList filteredTags; /** * Initializes a ModelManager with the given addressBook and userPrefs. @@ -39,6 +40,7 @@ public ModelManager(ReadOnlyAddressBook addressBook, ReadOnlyUserPrefs userPrefs this.userPrefs = new UserPrefs(userPrefs); filteredPersons = new FilteredList<>(this.addressBook.getPersonList()); filteredTasks = new FilteredList<>(this.addressBook.getTaskList()); + filteredTags = new FilteredList<>(this.addressBook.getTagList()); } public ModelManager() { @@ -149,7 +151,9 @@ public boolean equals(Object obj) { ModelManager other = (ModelManager) obj; return addressBook.equals(other.addressBook) && userPrefs.equals(other.userPrefs) - && filteredPersons.equals(other.filteredPersons); + && filteredPersons.equals(other.filteredPersons) + && filteredTasks.equals(other.filteredTasks) + && filteredTags.equals(other.filteredTags); } //=========== TaskList ====================================================================== @@ -190,6 +194,10 @@ public void updateFilteredTaskList(Predicate predicate) { } //=========== Tag List Accessors ============================================================= + /** + * Returns an unmodifiable view of the list of {@code Tag} backed by the internal list of + * {@code versionedAddressBook} + */ @Override public ObservableList getTagList() { return addressBook.getTagList(); diff --git a/src/main/java/seedu/address/model/ReadOnlyAddressBook.java b/src/main/java/seedu/address/model/ReadOnlyAddressBook.java index 52536e812fa..8c6caa471a7 100644 --- a/src/main/java/seedu/address/model/ReadOnlyAddressBook.java +++ b/src/main/java/seedu/address/model/ReadOnlyAddressBook.java @@ -25,10 +25,4 @@ public interface ReadOnlyAddressBook { * Returns an unmodifiable view of the tags list. */ ObservableList getTagList(); - - /** - * Adds a task to the TaskList - * @param task - */ - void addTask(Task task); } diff --git a/src/main/java/seedu/address/model/task/Task.java b/src/main/java/seedu/address/model/task/Task.java index 8ecf273c88e..20ca5bfe10f 100644 --- a/src/main/java/seedu/address/model/task/Task.java +++ b/src/main/java/seedu/address/model/task/Task.java @@ -72,20 +72,6 @@ public Set getTags() { return Collections.unmodifiableSet(tags); } - /** - * Marks task as done. - */ - public void markTask() { - isDone = true; - } - - /** - * Marks task as not done. - */ - public void unmarkTask() { - isDone = false; - } - /** * Returns true if task contains any of the description or deadline keywords. * By default, empty lists will return true. @@ -133,7 +119,8 @@ public boolean equals(Object other) { Task otherTask = (Task) other; return otherTask.getDescription().equals(getDescription()) && otherTask.getDeadline().equals(getDeadline()) - && otherTask.getStatus().equals(getStatus()); + && otherTask.getStatus().equals(getStatus()) + && otherTask.getTags().equals(getTags()); } @Override diff --git a/src/main/java/seedu/address/model/task/TaskList.java b/src/main/java/seedu/address/model/task/TaskList.java index fe369eda6e5..07dfc2f0816 100644 --- a/src/main/java/seedu/address/model/task/TaskList.java +++ b/src/main/java/seedu/address/model/task/TaskList.java @@ -4,6 +4,7 @@ import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; import java.util.Iterator; +import java.util.List; import javafx.collections.FXCollections; import javafx.collections.ObservableList; @@ -27,15 +28,14 @@ public ObservableList asUnmodifiableObservableList() { /** * Adds a Task to the TaskList. - * @param task The Task tp be added. - * @return A String describing the number of tasks in the TaskList. + * @param task The Task to be added. */ - public String addTask(Task task) { + public void addTask(Task task) { requireNonNull(task); - + if (contains(task)) { + throw new DuplicateTaskException(); + } internalList.add(task); - - return TaskUi.addText(internalList.get(internalList.size() - 1).toString(), internalList.size()); } /** @@ -58,6 +58,24 @@ public void setTask(Task target, Task editedTask) { internalList.set(index, editedTask); } + public void setTasks(TaskList replacement) { + requireNonNull(replacement); + internalList.setAll(replacement.internalList); + } + + /** + * Replaces the contents of this list with {@code tasks}. + * {@code tasks} must not contain duplicate tasks. + */ + public void setTasks(List tasks) { + requireAllNonNull(tasks); + if (!tasksAreUnique(tasks)) { + throw new DuplicateTaskException(); + } + + internalList.setAll(tasks); + } + @Override public Iterator iterator() { return internalList.iterator(); @@ -70,4 +88,30 @@ public boolean contains(Task toCheck) { requireNonNull(toCheck); return internalList.stream().anyMatch(toCheck::isSameTask); } + + @Override + public boolean equals(Object other) { + return other == this // short circuit if same object + || (other instanceof TaskList // instanceof handles nulls + && internalList.equals(((TaskList) other).internalList)); + } + + @Override + public int hashCode() { + return internalList.hashCode(); + } + + /** + * Returns true if {@code tasks} contains only unique tasks. + */ + private boolean tasksAreUnique(List tasks) { + for (int i = 0; i < tasks.size() - 1; i++) { + for (int j = i + 1; j < tasks.size(); j++) { + if (tasks.get(i).isSameTask(tasks.get(j))) { + return false; + } + } + } + return true; + } } diff --git a/src/test/java/seedu/address/model/AddressBookTest.java b/src/test/java/seedu/address/model/AddressBookTest.java index 2a520d8f04b..5aae489fc2f 100644 --- a/src/test/java/seedu/address/model/AddressBookTest.java +++ b/src/test/java/seedu/address/model/AddressBookTest.java @@ -109,11 +109,6 @@ public ObservableList getTaskList() { public ObservableList getTagList() { throw new AssertionError("This method should not be called."); } - - @Override - public void addTask(Task task) { - throw new AssertionError("This method should not be called."); - } } }