From af2cf5af427855e59071bd1e47d3346f45b068cc Mon Sep 17 00:00:00 2001 From: myteo Date: Fri, 4 Nov 2016 10:15:13 +0800 Subject: [PATCH] Implement unpin command --- .../dailyplanner/logic/parser/Parser.java | 19 +- .../seedu/dailyplanner/model/AddressBook.java | 8 +- .../java/seedu/dailyplanner/model/Model.java | 4 + .../dailyplanner/model/ModelManager.java | 7 +- .../model/task/UniqueTaskList.java | 242 +++++++++--------- 5 files changed, 157 insertions(+), 123 deletions(-) diff --git a/src/main/java/seedu/dailyplanner/logic/parser/Parser.java b/src/main/java/seedu/dailyplanner/logic/parser/Parser.java index a5c7a6fac679..a6c6dd79d9ab 100644 --- a/src/main/java/seedu/dailyplanner/logic/parser/Parser.java +++ b/src/main/java/seedu/dailyplanner/logic/parser/Parser.java @@ -94,9 +94,12 @@ public Command parseCommand(String userInput) { case UndoCommand.COMMAND_WORD: return new UndoCommand(); - + case PinCommand.COMMAND_WORD: - return preparePin(arguments); + return preparePin(arguments); + + case UnpinCommand.COMMAND_WORD: + return prepareUnpin(arguments); case ShowCommand.COMMAND_WORD: if (arguments.equals("")) @@ -109,9 +112,17 @@ public Command parseCommand(String userInput) { } } + private Command prepareUnpin(String arguments) { + Optional index = parseIndex(arguments); + if (!index.isPresent()) { + return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, CompleteCommand.MESSAGE_USAGE)); + } + + return new UnpinCommand(index.get()); + } + private Command preparePin(String arguments) { - // TODO Auto-generated method stub - Optional index = parseIndex(arguments); + Optional index = parseIndex(arguments); if (!index.isPresent()) { return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, CompleteCommand.MESSAGE_USAGE)); } diff --git a/src/main/java/seedu/dailyplanner/model/AddressBook.java b/src/main/java/seedu/dailyplanner/model/AddressBook.java index 15f8bb48fc99..8e4b31215b7f 100644 --- a/src/main/java/seedu/dailyplanner/model/AddressBook.java +++ b/src/main/java/seedu/dailyplanner/model/AddressBook.java @@ -124,6 +124,11 @@ public void markTaskAsComplete(int targetIndex) throws UniqueTaskList.PersonNotF public void pinTask(int targetIndex) throws PersonNotFoundException { persons.pin(targetIndex); } + + public void unpinTask(int targetIndex) { + persons.unpin(targetIndex); + + } //// tag-level operations public void addTag(Tag t) throws UniqueTagList.DuplicateTagException { @@ -172,4 +177,5 @@ public int hashCode() { // use this method for custom fields hashing instead of implementing your own return Objects.hash(persons, tags); } -} + +} \ No newline at end of file diff --git a/src/main/java/seedu/dailyplanner/model/Model.java b/src/main/java/seedu/dailyplanner/model/Model.java index d8b4902a022e..1690f21de197 100644 --- a/src/main/java/seedu/dailyplanner/model/Model.java +++ b/src/main/java/seedu/dailyplanner/model/Model.java @@ -36,6 +36,9 @@ public interface Model { /** Pins the given task. */ void pinTask(int targetIndex) throws PersonNotFoundException; + + /** Unpins the given task. */ + void unpinTask(int i) throws PersonNotFoundException; /** Returns the filtered person list as an {@code UnmodifiableObservableList} */ UnmodifiableObservableList getFilteredPersonList(); @@ -55,4 +58,5 @@ public interface Model { /** Updates the filter of the filtered person list to show only completed tasks*/ void updateFilteredPersonListByCompletion(Set keywords); + } diff --git a/src/main/java/seedu/dailyplanner/model/ModelManager.java b/src/main/java/seedu/dailyplanner/model/ModelManager.java index d94d024ac7fe..c78c7ee8604d 100644 --- a/src/main/java/seedu/dailyplanner/model/ModelManager.java +++ b/src/main/java/seedu/dailyplanner/model/ModelManager.java @@ -96,7 +96,12 @@ public synchronized void markTaskAsComplete(int targetIndex) throws PersonNotFou public void pinTask(int targetIndex) throws PersonNotFoundException { addressBook.pinTask(targetIndex); indicateAddressBookChanged(); - + } + + @Override + public void unpinTask(int targetIndex) throws PersonNotFoundException { + addressBook.unpinTask(targetIndex); + indicateAddressBookChanged(); } // =========== Filtered Person List Accessors diff --git a/src/main/java/seedu/dailyplanner/model/task/UniqueTaskList.java b/src/main/java/seedu/dailyplanner/model/task/UniqueTaskList.java index 5d14abcf9212..96ff29cbf097 100644 --- a/src/main/java/seedu/dailyplanner/model/task/UniqueTaskList.java +++ b/src/main/java/seedu/dailyplanner/model/task/UniqueTaskList.java @@ -18,124 +18,132 @@ */ public class UniqueTaskList implements Iterable { - /** - * Signals that an operation would have violated the 'no duplicates' - * property of the list. - */ - public static class DuplicatePersonException extends DuplicateDataException { - protected DuplicatePersonException() { - super("Operation would result in duplicate persons"); + /** + * Signals that an operation would have violated the 'no duplicates' + * property of the list. + */ + public static class DuplicatePersonException extends DuplicateDataException { + protected DuplicatePersonException() { + super("Operation would result in duplicate persons"); + } } - } - - /** - * Signals that an operation targeting a specified person in the list would - * fail because there is no such matching person in the list. - */ - public static class PersonNotFoundException extends Exception { - } - - private final ObservableList internalList = FXCollections.observableArrayList(); - private final ObservableList pinnedList = FXCollections.observableArrayList(); - - /** - * Constructs empty PersonList. - */ - public UniqueTaskList() { - } - - /** - * Returns true if the list contains an equivalent person as the given - * argument. - */ - public boolean contains(ReadOnlyTask toCheck) { - assert toCheck != null; - return internalList.contains(toCheck); - } - - /** - * Adds a person to the list. - * - * @throws DuplicatePersonException - * if the person to add is a duplicate of an existing person in - * the list. - */ - public void add(Task toAdd) throws DuplicatePersonException { - assert toAdd != null; - if (contains(toAdd)) { - throw new DuplicatePersonException(); + + /** + * Signals that an operation targeting a specified person in the list would + * fail because there is no such matching person in the list. + */ + public static class PersonNotFoundException extends Exception { + } + + private final ObservableList internalList = FXCollections.observableArrayList(); + private final ObservableList pinnedList = FXCollections.observableArrayList(); + + /** + * Constructs empty PersonList. + */ + public UniqueTaskList() { + } + + /** + * Returns true if the list contains an equivalent person as the given + * argument. + */ + public boolean contains(ReadOnlyTask toCheck) { + assert toCheck != null; + return internalList.contains(toCheck); + } + + /** + * Adds a person to the list. + * + * @throws DuplicatePersonException + * if the person to add is a duplicate of an existing person in + * the list. + */ + public void add(Task toAdd) throws DuplicatePersonException { + assert toAdd != null; + if (contains(toAdd)) { + throw new DuplicatePersonException(); + } + internalList.add(toAdd); + FXCollections.sort(internalList); + } + + /** + * Removes the equivalent person from the list. + * + * @throws PersonNotFoundException + * if no such person could be found in the list. + */ + public boolean remove(ReadOnlyTask toRemove) throws PersonNotFoundException { + assert toRemove != null; + final boolean personFoundAndDeleted = internalList.remove(toRemove); + + if (!personFoundAndDeleted) { + throw new PersonNotFoundException(); + } + FXCollections.sort(internalList); + return personFoundAndDeleted; } - internalList.add(toAdd); - FXCollections.sort(internalList); - } - - /** - * Removes the equivalent person from the list. - * - * @throws PersonNotFoundException - * if no such person could be found in the list. - */ - public boolean remove(ReadOnlyTask toRemove) throws PersonNotFoundException { - assert toRemove != null; - final boolean personFoundAndDeleted = internalList.remove(toRemove); - - if (!personFoundAndDeleted) { - throw new PersonNotFoundException(); + + /** + * Marks the task indicated by taskIndex as complete + * + * @throws PersonNotFoundException + * if the task index is invalid. + */ + public void complete(int taskIndex) throws PersonNotFoundException { + + final Task completedTask = internalList.get(taskIndex); + completedTask.markAsComplete(); + internalList.set(taskIndex, completedTask); + FXCollections.sort(internalList); } - FXCollections.sort(internalList); - return personFoundAndDeleted; - } - - - /** - * Marks the task indicated by taskIndex as complete - * - * @throws PersonNotFoundException - * if the task index is invalid. - */ - public void complete(int taskIndex) throws PersonNotFoundException { - - final Task completedTask = internalList.get(taskIndex); - completedTask.markAsComplete(); - internalList.set(taskIndex, completedTask); - FXCollections.sort(internalList); - } - - /** - * Pins the task indicated by taskIndex - * - * @return - */ - public void pin(int taskIndex) throws PersonNotFoundException { - - final Task completedTask = internalList.get(taskIndex); - completedTask.pin(); - internalList.set(taskIndex, completedTask); - pinnedList.add(completedTask); - } - - public ObservableList getInternalList() { - return internalList; - } - - public ObservableList getInternalPinnedList() { - return pinnedList; - } - - @Override - public Iterator iterator() { - return internalList.iterator(); - } - - @Override - public boolean equals(Object other) { - return other == this // short circuit if same object - || (other instanceof UniqueTaskList // instanceof handles nulls - && this.internalList.equals(((UniqueTaskList) other).internalList)); - } - - @Override - public int hashCode() { - return internalList.hashCode(); - } + + /** + * Pins the task indicated by taskIndex + * + * @return + */ + public void pin(int taskIndex) throws PersonNotFoundException { + + final Task taskToPin = internalList.get(taskIndex); + taskToPin.pin(); + internalList.set(taskIndex, taskToPin); + pinnedList.add(taskToPin); + } + + public void unpin(int targetIndex) { + final Task taskToUnpin = internalList.get(targetIndex); + taskToUnpin.unpin(); + internalList.set(targetIndex, taskToUnpin); + pinnedList.remove(taskToUnpin); + + } + + public ObservableList getInternalList() { + return internalList; + } + + public ObservableList getInternalPinnedList() { + return pinnedList; + } + + @Override + public Iterator iterator() { + return internalList.iterator(); + } + + @Override + public boolean equals(Object other) { + return other == this // short circuit if same object + || (other instanceof UniqueTaskList // instanceof handles nulls + && this.internalList.equals(((UniqueTaskList) other).internalList)); + } + + @Override + public int hashCode() { + return internalList.hashCode(); + } + }