From 2d79ec43b17bc0434479603987f55d6c426bd971 Mon Sep 17 00:00:00 2001 From: AbdulrahmanAlRammah <142239643+AbdulrahmanAlRammah@users.noreply.github.com> Date: Thu, 7 Nov 2024 15:52:32 +0800 Subject: [PATCH 1/9] Create SortSettings --- .../address/commons/core/SortSettings.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/main/java/seedu/address/commons/core/SortSettings.java diff --git a/src/main/java/seedu/address/commons/core/SortSettings.java b/src/main/java/seedu/address/commons/core/SortSettings.java new file mode 100644 index 00000000000..514e827a903 --- /dev/null +++ b/src/main/java/seedu/address/commons/core/SortSettings.java @@ -0,0 +1,75 @@ +package seedu.address.commons.core; + +import java.io.Serializable; +import java.util.Objects; + +import seedu.address.commons.util.ToStringBuilder; +import seedu.address.model.person.PersonComparator; + +/** + * A Serializable class that contains the Sort settings. + * Guarantees: immutable. + */ +public class SortSettings implements Serializable { + + private static final String DEFAULT_SORT_PARAMETER = PersonComparator.NAME; + private static final boolean DEFAULT_IS_ASCENDING_ORDER = true; + + private final String sortParameter; + + private final boolean isAscendingOrder; + + /** + * Constructs a {@code SortSettings} with the default sortParameter and Order. + */ + public SortSettings() { + sortParameter = DEFAULT_SORT_PARAMETER; + isAscendingOrder = DEFAULT_IS_ASCENDING_ORDER; + } + + /** + * Constructs a {@code SortSettings} with the specified sortParameter and Order. + */ + public SortSettings(String sortParameter, boolean isAscendingOrder) { + this.sortParameter = sortParameter; + this.isAscendingOrder = isAscendingOrder; + } + + public String getSortParameter() { + return this.sortParameter; + } + + public boolean isAscendingOrder() { + return this.isAscendingOrder; + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof SortSettings)) { + return false; + } + + SortSettings otherSortSettings = (SortSettings) other; + return this.sortParameter == otherSortSettings.sortParameter + && (this.isAscendingOrder == otherSortSettings.isAscendingOrder); + + } + + @Override + public int hashCode() { + return Objects.hash(sortParameter, isAscendingOrder); + } + + @Override + public String toString() { + return new ToStringBuilder(this) + .add("sortParameter", sortParameter) + .add("isAscendingOrder", isAscendingOrder) + .toString(); + } +} From c58887f0c352d1b9b5899f46e3231e1c22945776 Mon Sep 17 00:00:00 2001 From: AbdulrahmanAlRammah <142239643+AbdulrahmanAlRammah@users.noreply.github.com> Date: Thu, 7 Nov 2024 15:58:30 +0800 Subject: [PATCH 2/9] Update User Prefs to include sortSettings --- src/main/java/seedu/address/model/UserPrefs.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/model/UserPrefs.java b/src/main/java/seedu/address/model/UserPrefs.java index e8e63b67689..54244cf0142 100644 --- a/src/main/java/seedu/address/model/UserPrefs.java +++ b/src/main/java/seedu/address/model/UserPrefs.java @@ -7,6 +7,7 @@ import java.util.Objects; import seedu.address.commons.core.GuiSettings; +import seedu.address.commons.core.SortSettings; /** * Represents User's preferences. @@ -14,6 +15,7 @@ public class UserPrefs implements ReadOnlyUserPrefs { private GuiSettings guiSettings = new GuiSettings(); + private SortSettings sortSettings = new SortSettings(); private Path addressBookFilePath = Paths.get("data", "socialbook.json"); /** @@ -35,6 +37,7 @@ public UserPrefs(ReadOnlyUserPrefs userPrefs) { public void resetData(ReadOnlyUserPrefs newUserPrefs) { requireNonNull(newUserPrefs); setGuiSettings(newUserPrefs.getGuiSettings()); + setSortSettings(newUserPrefs.getSortSettings()); setAddressBookFilePath(newUserPrefs.getAddressBookFilePath()); } @@ -42,11 +45,20 @@ public GuiSettings getGuiSettings() { return guiSettings; } + public SortSettings getSortSettings() { + return sortSettings; + } + public void setGuiSettings(GuiSettings guiSettings) { requireNonNull(guiSettings); this.guiSettings = guiSettings; } + public void setSortSettings(SortSettings sortSettings) { + requireNonNull(sortSettings); + this.sortSettings = sortSettings; + } + public Path getAddressBookFilePath() { return addressBookFilePath; } @@ -74,7 +86,7 @@ public boolean equals(Object other) { @Override public int hashCode() { - return Objects.hash(guiSettings, addressBookFilePath); + return Objects.hash(guiSettings, addressBookFilePath, sortSettings); } @Override @@ -82,6 +94,7 @@ public String toString() { StringBuilder sb = new StringBuilder(); sb.append("Gui Settings : " + guiSettings); sb.append("\nLocal data file location : " + addressBookFilePath); + sb.append("\nSort settings : "); return sb.toString(); } From a0f3f9be2d25a85abfa84b499baf56ec95366ce3 Mon Sep 17 00:00:00 2001 From: AbdulrahmanAlRammah <142239643+AbdulrahmanAlRammah@users.noreply.github.com> Date: Thu, 7 Nov 2024 17:01:06 +0800 Subject: [PATCH 3/9] Implement sorting of list when adding or editing a person --- src/main/java/seedu/address/model/Model.java | 15 +++++++++-- .../seedu/address/model/ModelManager.java | 27 ++++++++++++++++--- .../address/model/ReadOnlyUserPrefs.java | 3 +++ .../model/person/PersonComparator.java | 2 +- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index be4f1f3110a..48656c12050 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -5,6 +5,7 @@ import javafx.collections.ObservableList; import seedu.address.commons.core.GuiSettings; +import seedu.address.commons.core.SortSettings; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.person.Person; @@ -33,6 +34,16 @@ public interface Model { */ GuiSettings getGuiSettings(); + /** + * Sets the user prefs' Sort settings. + */ + void setSortSettings(SortSettings sortSettings); + + /** + * Returns the user prefs' Sort settings. + */ + SortSettings getSortSettings(); + /** * Sets the user prefs' GUI settings. */ @@ -71,14 +82,14 @@ public interface Model { * Adds the given person. * {@code person} must not already exist in the address book. */ - void addPerson(Person person); + void addPerson(Person person) throws CommandException; /** * Replaces the given person {@code target} with {@code editedPerson}. * {@code target} must exist in the address book. * The person identity of {@code editedPerson} must not be the same as another existing person in the address book. */ - void setPerson(Person target, Person editedPerson); + void setPerson(Person target, Person editedPerson) throws CommandException; /** Returns an unmodifiable view of the filtered person list */ ObservableList getFilteredPersonList(); diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 9f6f527c1f4..235a98decb6 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -10,7 +10,9 @@ import javafx.collections.ObservableList; import javafx.collections.transformation.FilteredList; import seedu.address.commons.core.GuiSettings; +import seedu.address.commons.core.SortSettings; import seedu.address.commons.core.LogsCenter; +import seedu.address.logic.commands.Command; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.person.Person; import seedu.address.model.person.PersonComparator; @@ -66,6 +68,17 @@ public void setGuiSettings(GuiSettings guiSettings) { userPrefs.setGuiSettings(guiSettings); } + @Override + public SortSettings getSortSettings() { + return userPrefs.getSortSettings(); + } + + @Override + public void setSortSettings(SortSettings sortSettings) { + requireNonNull(sortSettings); + userPrefs.setSortSettings(sortSettings); + } + @Override public Path getAddressBookFilePath() { return userPrefs.getAddressBookFilePath(); @@ -101,15 +114,16 @@ public void deletePerson(Person target) { } @Override - public void addPerson(Person person) { + public void addPerson(Person person) throws CommandException { addressBook.addPerson(person); + resortPersonList(getSortSettings()); updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); } @Override - public void setPerson(Person target, Person editedPerson) { + public void setPerson(Person target, Person editedPerson) throws CommandException { requireAllNonNull(target, editedPerson); - + resortPersonList(getSortSettings()); addressBook.setPerson(target, editedPerson); } @@ -150,6 +164,13 @@ public boolean equals(Object other) { @Override public void sortPersonList(String parameter, boolean isAscending) throws CommandException { addressBook.sort(new PersonComparator().getComparator(parameter, isAscending)); + setSortSettings(new SortSettings(parameter, isAscending)); + } + + private void resortPersonList(SortSettings sortSettings) throws CommandException { + String parameter = sortSettings.getSortParameter(); + boolean isAscending = sortSettings.isAscendingOrder(); + addressBook.sort(new PersonComparator().getComparator(parameter, isAscending)); } } diff --git a/src/main/java/seedu/address/model/ReadOnlyUserPrefs.java b/src/main/java/seedu/address/model/ReadOnlyUserPrefs.java index befd58a4c73..0aa49b45777 100644 --- a/src/main/java/seedu/address/model/ReadOnlyUserPrefs.java +++ b/src/main/java/seedu/address/model/ReadOnlyUserPrefs.java @@ -3,6 +3,7 @@ import java.nio.file.Path; import seedu.address.commons.core.GuiSettings; +import seedu.address.commons.core.SortSettings; /** * Unmodifiable view of user prefs. @@ -11,6 +12,8 @@ public interface ReadOnlyUserPrefs { GuiSettings getGuiSettings(); + SortSettings getSortSettings(); + Path getAddressBookFilePath(); } diff --git a/src/main/java/seedu/address/model/person/PersonComparator.java b/src/main/java/seedu/address/model/person/PersonComparator.java index 93163b52934..514ed70f976 100644 --- a/src/main/java/seedu/address/model/person/PersonComparator.java +++ b/src/main/java/seedu/address/model/person/PersonComparator.java @@ -11,7 +11,7 @@ public class PersonComparator { public static final String NAME = "name"; public static final String DATE_OF_LAST_VISIT = "date of last visit"; - private static final String SORT_EXCEPTION = "The specified parameter is invalid."; + private static final String SORT_EXCEPTION = "The specified sort parameter being used is invalid."; public PersonComparator() { From 9b7950a4f5ea73d61ef3661236c92dc31047936d Mon Sep 17 00:00:00 2001 From: AbdulrahmanAlRammah <142239643+AbdulrahmanAlRammah@users.noreply.github.com> Date: Thu, 7 Nov 2024 18:07:25 +0800 Subject: [PATCH 4/9] Handle exception for sorting --- src/main/java/seedu/address/logic/commands/SeedCommand.java | 3 ++- src/main/java/seedu/address/model/ModelManager.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/SeedCommand.java b/src/main/java/seedu/address/logic/commands/SeedCommand.java index 26fdc0f963f..3e550ab9db7 100644 --- a/src/main/java/seedu/address/logic/commands/SeedCommand.java +++ b/src/main/java/seedu/address/logic/commands/SeedCommand.java @@ -2,6 +2,7 @@ import static java.util.Objects.requireNonNull; +import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.Model; import seedu.address.model.person.Person; import seedu.address.model.util.SampleDataUtil; @@ -17,7 +18,7 @@ public class SeedCommand extends Command { public static final String MESSAGE_USAGE = COMMAND_WORD + ": Seeds dummy data into the SocialBook."; @Override - public CommandResult execute(Model model) { + public CommandResult execute(Model model) throws CommandException { requireNonNull(model); for (Person person : SampleDataUtil.getSamplePersons()) { if (model.hasPerson(person)) { diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 235a98decb6..c1a200708d0 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -123,8 +123,8 @@ public void addPerson(Person person) throws CommandException { @Override public void setPerson(Person target, Person editedPerson) throws CommandException { requireAllNonNull(target, editedPerson); - resortPersonList(getSortSettings()); addressBook.setPerson(target, editedPerson); + resortPersonList(getSortSettings()); } //=========== Filtered Person List Accessors ============================================================= From 150fa338adbd81ae7b33626f8f450fc66882fa0b Mon Sep 17 00:00:00 2001 From: AbdulrahmanAlRammah <142239643+AbdulrahmanAlRammah@users.noreply.github.com> Date: Thu, 7 Nov 2024 18:53:17 +0800 Subject: [PATCH 5/9] Improve handling of sortparameter exception --- src/main/java/seedu/address/model/Model.java | 4 ++-- .../java/seedu/address/model/ModelManager.java | 16 ++++++++++++---- .../address/model/person/PersonComparator.java | 2 +- .../address/logic/commands/AddCommandTest.java | 11 +++++++++++ 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index 48656c12050..1f040bfefd8 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -82,14 +82,14 @@ public interface Model { * Adds the given person. * {@code person} must not already exist in the address book. */ - void addPerson(Person person) throws CommandException; + void addPerson(Person person); /** * Replaces the given person {@code target} with {@code editedPerson}. * {@code target} must exist in the address book. * The person identity of {@code editedPerson} must not be the same as another existing person in the address book. */ - void setPerson(Person target, Person editedPerson) throws CommandException; + void setPerson(Person target, Person editedPerson); /** Returns an unmodifiable view of the filtered person list */ ObservableList getFilteredPersonList(); diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index c1a200708d0..e2272a2c879 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -5,6 +5,7 @@ import java.nio.file.Path; import java.util.function.Predicate; +import java.util.logging.Level; import java.util.logging.Logger; import javafx.collections.ObservableList; @@ -22,6 +23,8 @@ */ public class ModelManager implements Model { private static final Logger logger = LogsCenter.getLogger(ModelManager.class); + private static final String RESORT_ERROR = "Error: User Prefs had an invalid sort parameter. " + + "Resorting after changes were made to the addressbook was abandoned."; private final AddressBook addressBook; private final UserPrefs userPrefs; @@ -114,14 +117,14 @@ public void deletePerson(Person target) { } @Override - public void addPerson(Person person) throws CommandException { + public void addPerson(Person person) { addressBook.addPerson(person); resortPersonList(getSortSettings()); updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); } @Override - public void setPerson(Person target, Person editedPerson) throws CommandException { + public void setPerson(Person target, Person editedPerson) { requireAllNonNull(target, editedPerson); addressBook.setPerson(target, editedPerson); resortPersonList(getSortSettings()); @@ -167,10 +170,15 @@ public void sortPersonList(String parameter, boolean isAscending) throws Command setSortSettings(new SortSettings(parameter, isAscending)); } - private void resortPersonList(SortSettings sortSettings) throws CommandException { + private void resortPersonList(SortSettings sortSettings) { String parameter = sortSettings.getSortParameter(); boolean isAscending = sortSettings.isAscendingOrder(); - addressBook.sort(new PersonComparator().getComparator(parameter, isAscending)); + try { + addressBook.sort(new PersonComparator().getComparator(parameter, isAscending)); + } catch (CommandException ce) { + //This case should only happen if the user incorrectly edits the UserPrefs manually. + logger.log(Level.WARNING, RESORT_ERROR); + } } } diff --git a/src/main/java/seedu/address/model/person/PersonComparator.java b/src/main/java/seedu/address/model/person/PersonComparator.java index 514ed70f976..56b278e365b 100644 --- a/src/main/java/seedu/address/model/person/PersonComparator.java +++ b/src/main/java/seedu/address/model/person/PersonComparator.java @@ -11,7 +11,7 @@ public class PersonComparator { public static final String NAME = "name"; public static final String DATE_OF_LAST_VISIT = "date of last visit"; - private static final String SORT_EXCEPTION = "The specified sort parameter being used is invalid."; + private static final String SORT_EXCEPTION = "The specified sort parameter is invalid."; public PersonComparator() { diff --git a/src/test/java/seedu/address/logic/commands/AddCommandTest.java b/src/test/java/seedu/address/logic/commands/AddCommandTest.java index 68ba446c1d1..1dc0796d57f 100644 --- a/src/test/java/seedu/address/logic/commands/AddCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddCommandTest.java @@ -19,6 +19,7 @@ import javafx.collections.ObservableList; import seedu.address.commons.core.GuiSettings; +import seedu.address.commons.core.SortSettings; import seedu.address.logic.Messages; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.AddressBook; @@ -168,6 +169,16 @@ public void setGuiSettings(GuiSettings guiSettings) { throw new AssertionError("This method should not be called."); } + @Override + public SortSettings getSortSettings() { + throw new AssertionError("This method should not be called."); + } + + @Override + public void setSortSettings(SortSettings sortSettings) { + throw new AssertionError("This method should not be called."); + } + @Override public Path getAddressBookFilePath() { throw new AssertionError("This method should not be called."); From 2b708d0614c074066e1ce273ccd2205b08c1fa50 Mon Sep 17 00:00:00 2001 From: AbdulrahmanAlRammah <142239643+AbdulrahmanAlRammah@users.noreply.github.com> Date: Thu, 7 Nov 2024 18:57:05 +0800 Subject: [PATCH 6/9] Fix Checkstyle --- src/main/java/seedu/address/model/ModelManager.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index e2272a2c879..864fe30e0b5 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -11,9 +11,8 @@ import javafx.collections.ObservableList; import javafx.collections.transformation.FilteredList; import seedu.address.commons.core.GuiSettings; -import seedu.address.commons.core.SortSettings; import seedu.address.commons.core.LogsCenter; -import seedu.address.logic.commands.Command; +import seedu.address.commons.core.SortSettings; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.person.Person; import seedu.address.model.person.PersonComparator; From 59753e5ad319126199529887c4ff024e13ee915d Mon Sep 17 00:00:00 2001 From: AbdulrahmanAlRammah <142239643+AbdulrahmanAlRammah@users.noreply.github.com> Date: Thu, 7 Nov 2024 19:06:08 +0800 Subject: [PATCH 7/9] Add toString() test for SortSetting --- .../address/commons/core/SortSettingsTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/test/java/seedu/address/commons/core/SortSettingsTest.java diff --git a/src/test/java/seedu/address/commons/core/SortSettingsTest.java b/src/test/java/seedu/address/commons/core/SortSettingsTest.java new file mode 100644 index 00000000000..3240ae29f5f --- /dev/null +++ b/src/test/java/seedu/address/commons/core/SortSettingsTest.java @@ -0,0 +1,17 @@ +package seedu.address.commons.core; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; +import seedu.address.commons.util.ToStringBuilder; + +public class SortSettingsTest { + @Test + public void toStringMethod() { + SortSettings sortSettings = new SortSettings(); + String expected = SortSettings.class.getCanonicalName() + "{sortParameter=" + sortSettings.getSortParameter() + + ", isAscendingOrder=" + sortSettings.isAscendingOrder() + "}"; + assertEquals(expected, sortSettings.toString()); + } +} + From c8acd8e2e836628bda9242f57728c078139011aa Mon Sep 17 00:00:00 2001 From: AbdulrahmanAlRammah <142239643+AbdulrahmanAlRammah@users.noreply.github.com> Date: Thu, 7 Nov 2024 19:08:14 +0800 Subject: [PATCH 8/9] Fix Checkstyle test --- src/test/java/seedu/address/commons/core/SortSettingsTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/seedu/address/commons/core/SortSettingsTest.java b/src/test/java/seedu/address/commons/core/SortSettingsTest.java index 3240ae29f5f..da1f5576724 100644 --- a/src/test/java/seedu/address/commons/core/SortSettingsTest.java +++ b/src/test/java/seedu/address/commons/core/SortSettingsTest.java @@ -3,7 +3,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; -import seedu.address.commons.util.ToStringBuilder; public class SortSettingsTest { @Test From 762e7e3d0c480735a1b57fab6ea25cd310bf6afd Mon Sep 17 00:00:00 2001 From: AbdulrahmanAlRammah <142239643+AbdulrahmanAlRammah@users.noreply.github.com> Date: Thu, 7 Nov 2024 20:16:27 +0800 Subject: [PATCH 9/9] Improve method naming clarity --- src/main/java/seedu/address/model/ModelManager.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 864fe30e0b5..9e781ed01f7 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -23,7 +23,7 @@ public class ModelManager implements Model { private static final Logger logger = LogsCenter.getLogger(ModelManager.class); private static final String RESORT_ERROR = "Error: User Prefs had an invalid sort parameter. " - + "Resorting after changes were made to the addressbook was abandoned."; + + "Re-sorting after changes were made to the addressbook was abandoned."; private final AddressBook addressBook; private final UserPrefs userPrefs; @@ -118,7 +118,7 @@ public void deletePerson(Person target) { @Override public void addPerson(Person person) { addressBook.addPerson(person); - resortPersonList(getSortSettings()); + reSortPersonList(getSortSettings()); updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); } @@ -126,7 +126,7 @@ public void addPerson(Person person) { public void setPerson(Person target, Person editedPerson) { requireAllNonNull(target, editedPerson); addressBook.setPerson(target, editedPerson); - resortPersonList(getSortSettings()); + reSortPersonList(getSortSettings()); } //=========== Filtered Person List Accessors ============================================================= @@ -169,7 +169,7 @@ public void sortPersonList(String parameter, boolean isAscending) throws Command setSortSettings(new SortSettings(parameter, isAscending)); } - private void resortPersonList(SortSettings sortSettings) { + private void reSortPersonList(SortSettings sortSettings) { String parameter = sortSettings.getSortParameter(); boolean isAscending = sortSettings.isAscendingOrder(); try {