diff --git a/src/main/java/seedu/address/logic/commands/ListCommand.java b/src/main/java/seedu/address/logic/commands/ListCommand.java index 84be6ad2596..8e040763bb4 100644 --- a/src/main/java/seedu/address/logic/commands/ListCommand.java +++ b/src/main/java/seedu/address/logic/commands/ListCommand.java @@ -14,7 +14,6 @@ public class ListCommand extends Command { public static final String MESSAGE_SUCCESS = "Listed all persons"; - @Override public CommandResult execute(Model model) { requireNonNull(model); diff --git a/src/main/java/seedu/address/logic/commands/SortCommand.java b/src/main/java/seedu/address/logic/commands/SortCommand.java new file mode 100644 index 00000000000..eab0d6eb5ea --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/SortCommand.java @@ -0,0 +1,22 @@ +package seedu.address.logic.commands; + +import static java.util.Objects.requireNonNull; + +import seedu.address.model.Model; + +/** + * Sorts the address book by name in alphabetic order. + */ +public class SortCommand extends Command { + + public static final String COMMAND_WORD = "sort"; + + public static final String MESSAGE_SUCCESS = "Address book has been sorted by name in alphabetical order!"; + + @Override + public CommandResult execute(Model model) { + requireNonNull(model); + model.sortFilteredPersonList(); + return new CommandResult(MESSAGE_SUCCESS); + } +} diff --git a/src/main/java/seedu/address/logic/parser/AddressBookParser.java b/src/main/java/seedu/address/logic/parser/AddressBookParser.java index 50f9c2f3992..7fc70628865 100644 --- a/src/main/java/seedu/address/logic/parser/AddressBookParser.java +++ b/src/main/java/seedu/address/logic/parser/AddressBookParser.java @@ -18,6 +18,7 @@ import seedu.address.logic.commands.HelpCommand; import seedu.address.logic.commands.ListCommand; import seedu.address.logic.commands.MarkCommand; +import seedu.address.logic.commands.SortCommand; import seedu.address.logic.commands.UnmarkCommand; import seedu.address.logic.parser.exceptions.ParseException; @@ -84,6 +85,9 @@ public Command parseCommand(String userInput) throws ParseException { case FilterCommand.COMMAND_WORD: return new FilterCommandParser().parse(arguments); + case SortCommand.COMMAND_WORD: + return new SortCommand(); + default: throw new ParseException(MESSAGE_UNKNOWN_COMMAND); } diff --git a/src/main/java/seedu/address/model/AddressBook.java b/src/main/java/seedu/address/model/AddressBook.java index 1a943a0781a..a3233a7fab1 100644 --- a/src/main/java/seedu/address/model/AddressBook.java +++ b/src/main/java/seedu/address/model/AddressBook.java @@ -27,7 +27,8 @@ public class AddressBook implements ReadOnlyAddressBook { persons = new UniquePersonList(); } - public AddressBook() {} + public AddressBook() { + } /** * Creates an AddressBook using the Persons in the {@code toBeCopied} @@ -81,7 +82,6 @@ public void addPerson(Person p) { */ public void setPerson(Person target, Person editedPerson) { requireNonNull(editedPerson); - persons.setPerson(target, editedPerson); } @@ -106,6 +106,10 @@ public ObservableList getPersonList() { return persons.asUnmodifiableObservableList(); } + public void sort() { + persons.sort(); + } + @Override public boolean equals(Object other) { return other == this // short circuit if same object diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index 23a370de719..3455b8fb8fa 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -85,5 +85,7 @@ public interface Model { */ void updateFilteredPersonList(Predicate predicate); + /** Sorts filteredList by name in alphabetical order. */ + void sortFilteredPersonList(); } diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 86c1df298d7..651434498d5 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -122,6 +122,13 @@ public ObservableList getFilteredPersonList() { return filteredPersons; } + /** + * + */ + public void sortFilteredPersonList() { + this.addressBook.sort(); + } + @Override public void updateFilteredPersonList(Predicate predicate) { requireNonNull(predicate); diff --git a/src/main/java/seedu/address/model/person/Person.java b/src/main/java/seedu/address/model/person/Person.java index f629b2d8891..04eec026b4d 100644 --- a/src/main/java/seedu/address/model/person/Person.java +++ b/src/main/java/seedu/address/model/person/Person.java @@ -48,6 +48,10 @@ public Name getName() { return name; } + public String getNameString() { + return name.toString(); + } + public Phone getPhone() { return phone; } diff --git a/src/main/java/seedu/address/model/person/UniquePersonList.java b/src/main/java/seedu/address/model/person/UniquePersonList.java index 0fee4fe57e6..2ab3a8334dd 100644 --- a/src/main/java/seedu/address/model/person/UniquePersonList.java +++ b/src/main/java/seedu/address/model/person/UniquePersonList.java @@ -3,6 +3,7 @@ import static java.util.Objects.requireNonNull; import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; +import java.util.Comparator; import java.util.Iterator; import java.util.List; @@ -17,7 +18,7 @@ * persons uses Person#isSamePerson(Person) for equality so as to ensure that the person being added or updated is * unique in terms of identity in the UniquePersonList. However, the removal of a person uses Person#equals(Object) so * as to ensure that the person with exactly the same fields will be removed. - * + *

* Supports a minimal set of list operations. * * @see Person#isSamePerson(Person) @@ -48,6 +49,13 @@ public void add(Person toAdd) { internalList.add(toAdd); } + /** + * Sorts the list according to name in alphabetical order. + */ + public void sort() { + internalList.sort(Comparator.comparing(Person::getNameString)); + } + /** * Replaces the person {@code target} in the list with {@code editedPerson}. * {@code target} must exist in the list. @@ -104,6 +112,7 @@ public ObservableList asUnmodifiableObservableList() { return internalUnmodifiableList; } + @Override public Iterator iterator() { return internalList.iterator(); @@ -113,7 +122,7 @@ public Iterator iterator() { public boolean equals(Object other) { return other == this // short circuit if same object || (other instanceof UniquePersonList // instanceof handles nulls - && internalList.equals(((UniquePersonList) other).internalList)); + && internalList.equals(((UniquePersonList) other).internalList)); } @Override diff --git a/src/test/java/seedu/address/logic/commands/AddCommandTest.java b/src/test/java/seedu/address/logic/commands/AddCommandTest.java index 5865713d5dd..20da26f88bf 100644 --- a/src/test/java/seedu/address/logic/commands/AddCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddCommandTest.java @@ -147,6 +147,11 @@ public ObservableList getFilteredPersonList() { public void updateFilteredPersonList(Predicate predicate) { throw new AssertionError("This method should not be called."); } + + @Override + public void sortFilteredPersonList() { + throw new AssertionError("This method should not be called."); + } } /**