From 2912d071c8abbedcfa473b4fdc2431e94b5f7de3 Mon Sep 17 00:00:00 2001 From: Ruizhi2001 <70838461+Ruizhi2001@users.noreply.github.com> Date: Wed, 25 Oct 2023 20:39:07 +0800 Subject: [PATCH 1/3] Fix UI when view-item --- src/main/java/seedu/address/logic/Logic.java | 4 ++ .../seedu/address/logic/LogicManager.java | 5 ++ .../logic/commands/ViewItemCommand.java | 1 + src/main/java/seedu/address/model/Model.java | 17 +++++-- .../seedu/address/model/ModelManager.java | 13 +++++ .../java/seedu/address/model/stall/Stall.java | 5 ++ src/main/java/seedu/address/ui/ItemCard.java | 45 +++++++++++++++++ .../java/seedu/address/ui/ItemListPanel.java | 49 +++++++++++++++++++ .../seedu/address/ui/ItemReviewPanel.java | 5 +- .../java/seedu/address/ui/MainWindow.java | 34 +++++++------ .../java/seedu/address/ui/StallListPanel.java | 8 +-- src/main/resources/view/ItemReviewPanel.fxml | 34 ++++++------- ...StallListPanel.fxml => LeftListPanel.fxml} | 2 +- src/main/resources/view/MainWindow.fxml | 33 +++++++------ .../logic/commands/AddStallCommandTest.java | 12 ++++- 15 files changed, 203 insertions(+), 64 deletions(-) create mode 100644 src/main/java/seedu/address/ui/ItemCard.java create mode 100644 src/main/java/seedu/address/ui/ItemListPanel.java rename src/main/resources/view/{StallListPanel.fxml => LeftListPanel.fxml} (78%) diff --git a/src/main/java/seedu/address/logic/Logic.java b/src/main/java/seedu/address/logic/Logic.java index 424aecae0a3..d2a21b302c7 100644 --- a/src/main/java/seedu/address/logic/Logic.java +++ b/src/main/java/seedu/address/logic/Logic.java @@ -38,6 +38,10 @@ public interface Logic { /** Return the desired filtered item */ Item getFilteredItem(); + + /** Returns an unmodifiable view of the filtered list of items */ + ObservableList getFilteredItemList(); + /** * Returns the user prefs' address book file path. */ diff --git a/src/main/java/seedu/address/logic/LogicManager.java b/src/main/java/seedu/address/logic/LogicManager.java index f3b50dfe005..23c0b97766b 100644 --- a/src/main/java/seedu/address/logic/LogicManager.java +++ b/src/main/java/seedu/address/logic/LogicManager.java @@ -72,6 +72,11 @@ public Item getFilteredItem() { return model.getFilteredItem(); } + @Override + public ObservableList getFilteredItemList() { + return model.getFilteredItemList(); + } + @Override public ObservableList getFilteredStallList() { return model.getFilteredStallList(); diff --git a/src/main/java/seedu/address/logic/commands/ViewItemCommand.java b/src/main/java/seedu/address/logic/commands/ViewItemCommand.java index 3882af3bcd8..5a6d7356991 100644 --- a/src/main/java/seedu/address/logic/commands/ViewItemCommand.java +++ b/src/main/java/seedu/address/logic/commands/ViewItemCommand.java @@ -61,6 +61,7 @@ public CommandResult execute(Model model) throws CommandException { Item itemToView = model.getFilteredItem(stallIndex, itemIndex); model.setFilteredItem(itemToView); + model.setFilteredItemList(stallIndex); return new CommandResult(String.format(MESSAGE_VIEW_ITEM_SUCCESS, Messages.format(itemToView), Messages .format(stallToViewFrom)), false, false, false, true); } diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index 15d9967c3ec..1fb715c52ac 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -131,18 +131,25 @@ public interface Model { */ void deleteItem(Index stallIndex, Index itemIndex); + /** + * Adds the given item review. + * + * {@code itemReview} must not already exist in the item. + */ + void addItemReview(Item itemIndex, ItemReview itemReview); + /** * Deletes the given item review. - * The item review must exist in the item. + * + * {@code itemReview} must exist in the item. */ void deleteItemReview(Item itemIndex); /** - * Adds the given item review. - * - * {@code itemReview} must not already exist in the item. + * Returns an unmodifiable view of the filtered item list */ - void addItemReview(Item itemIndex, ItemReview itemReview); + void setFilteredItemList(Index stallIndex); + ObservableList getFilteredItemList(); } diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 1e86ee2f495..636872ede6a 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -26,6 +26,8 @@ public class ModelManager implements Model { private final UserPrefs userPrefs; private final FilteredList filteredStalls; private final FilteredList tempFilteredStalls; + + private ObservableList filteredItemList; private Item filteredItem; /** @@ -209,6 +211,17 @@ public void setFilteredItem(Item item) { this.filteredItem = item; } + @Override + public void setFilteredItemList(Index stallIndex) { + requireNonNull(stallIndex); + filteredItemList = filteredStalls.get(stallIndex.getZeroBased()).getMenuList(); + } + + @Override + public ObservableList getFilteredItemList() { + return filteredItemList; + } + @Override public boolean equals(Object other) { if (other == this) { diff --git a/src/main/java/seedu/address/model/stall/Stall.java b/src/main/java/seedu/address/model/stall/Stall.java index 0cf5c88a863..de93225ce8a 100644 --- a/src/main/java/seedu/address/model/stall/Stall.java +++ b/src/main/java/seedu/address/model/stall/Stall.java @@ -5,6 +5,7 @@ import java.util.Objects; import seedu.address.commons.core.index.Index; +import javafx.collections.ObservableList; import seedu.address.commons.util.ToStringBuilder; import seedu.address.model.item.Item; import seedu.address.model.stall.review.StallReview; @@ -53,6 +54,10 @@ public Menu getMenu() { return menu; } + public ObservableList getMenuList() { + return menu.getItemList(); + } + public StallReview getStallReview() { return stallReview; } diff --git a/src/main/java/seedu/address/ui/ItemCard.java b/src/main/java/seedu/address/ui/ItemCard.java new file mode 100644 index 00000000000..1b5a551a2b4 --- /dev/null +++ b/src/main/java/seedu/address/ui/ItemCard.java @@ -0,0 +1,45 @@ +package seedu.address.ui; + +import javafx.fxml.FXML; +import javafx.scene.control.Label; +import javafx.scene.layout.HBox; +import javafx.scene.layout.Region; +import seedu.address.model.item.Item; + +/** + * An UI component that displays information of a {@code Stall}. + */ +public class ItemCard extends UiPart { + + private static final String FXML = "StallListCard.fxml"; + + /** + * Note: Certain keywords such as "location" and "resources" are reserved keywords in JavaFX. + * As a consequence, UI elements' variable names cannot be set to such keywords + * or an exception will be thrown by JavaFX during runtime. + * + * @see The issue on AddressBook level 4 + */ + + public final Item item; + + @FXML + private HBox cardPane; + @FXML + private Label name; + @FXML + private Label id; + @FXML + private Label locationName; + + /** + * Creates a {@code PersonCode} with the given {@code Stall} and index to display. + */ + public ItemCard(Item item, int displayedIndex) { + super(FXML); + this.item = item; + id.setText(displayedIndex + ". "); + name.setText(item.getName().fullName); + locationName.setText(item.getItemRatingString()); + } +} diff --git a/src/main/java/seedu/address/ui/ItemListPanel.java b/src/main/java/seedu/address/ui/ItemListPanel.java new file mode 100644 index 00000000000..bf459b7e79a --- /dev/null +++ b/src/main/java/seedu/address/ui/ItemListPanel.java @@ -0,0 +1,49 @@ +package seedu.address.ui; + +import java.util.logging.Logger; + +import javafx.collections.ObservableList; +import javafx.fxml.FXML; +import javafx.scene.control.ListCell; +import javafx.scene.control.ListView; +import javafx.scene.layout.Region; +import seedu.address.commons.core.LogsCenter; +import seedu.address.model.item.Item; + +/** + * Panel containing the list of persons. + */ +public class ItemListPanel extends UiPart { + private static final String FXML = "LeftListPanel.fxml"; + private final Logger logger = LogsCenter.getLogger(ItemListPanel.class); + + @FXML + private ListView leftListView; + + /** + * Creates a {@code itemListPanel} with the given {@code ObservableList}. + */ + public ItemListPanel(ObservableList itemList) { + super(FXML); + leftListView.setItems(itemList); + leftListView.setCellFactory(listView -> new ItemListViewCell()); + } + + /** + * Custom {@code ListCell} that displays the graphics of a {@code Stall} using a {@code StallCard}. + */ + class ItemListViewCell extends ListCell { + @Override + protected void updateItem(Item item, boolean empty) { + super.updateItem(item, empty); + + if (empty || item == null) { + setGraphic(null); + setText(null); + } else { + setGraphic(new ItemCard(item, getIndex() + 1).getRoot()); + } + } + } + +} diff --git a/src/main/java/seedu/address/ui/ItemReviewPanel.java b/src/main/java/seedu/address/ui/ItemReviewPanel.java index d9785b3983a..71baaf26cee 100644 --- a/src/main/java/seedu/address/ui/ItemReviewPanel.java +++ b/src/main/java/seedu/address/ui/ItemReviewPanel.java @@ -16,16 +16,17 @@ public class ItemReviewPanel extends UiPart { private HBox cardPane; @FXML - private Label id; + private Label itemReview; @FXML - private Label itemReview; + private Label itemName; /** * Creates an {@code ItemNamePanel} with the given item. */ public ItemReviewPanel(Item item) { super(FXML); + itemName.setText(item.getName().fullName); itemReview.setText(item.getItemDescriptionString()); } } diff --git a/src/main/java/seedu/address/ui/MainWindow.java b/src/main/java/seedu/address/ui/MainWindow.java index ac035b22e59..b9114d21082 100644 --- a/src/main/java/seedu/address/ui/MainWindow.java +++ b/src/main/java/seedu/address/ui/MainWindow.java @@ -9,6 +9,7 @@ import javafx.scene.input.KeyCombination; import javafx.scene.input.KeyEvent; import javafx.scene.layout.StackPane; +import javafx.scene.layout.VBox; import javafx.stage.Stage; import seedu.address.commons.core.GuiSettings; import seedu.address.commons.core.LogsCenter; @@ -44,17 +45,17 @@ public class MainWindow extends UiPart { @FXML private MenuItem helpMenuItem; - @FXML - private StackPane leftPanelPlaceholder; + private StackPane resultDisplayPlaceholder; @FXML - private StackPane rightPanelPlaceholder; + private StackPane statusbarPlaceholder; + @FXML - private StackPane resultDisplayPlaceholder; + private VBox leftMainPanel; @FXML - private StackPane statusbarPlaceholder; + private VBox rightMainPanel; /** * Creates a {@code MainWindow} with the given {@code Stage} and {@code Logic}. @@ -118,7 +119,7 @@ private void setAccelerator(MenuItem menuItem, KeyCombination keyCombination) { */ void fillInnerParts() { stallListPanel = new StallListPanel(logic.getFilteredStallList()); - leftPanelPlaceholder.getChildren().add(stallListPanel.getRoot()); + leftMainPanel.getChildren().add(stallListPanel.getRoot()); resultDisplay = new ResultDisplay(); resultDisplayPlaceholder.getChildren().add(resultDisplay.getRoot()); @@ -132,9 +133,10 @@ void fillInnerParts() { void backToHomePage() { stallListPanel = new StallListPanel(logic.getFilteredStallList()); - leftPanelPlaceholder.getChildren().add(stallListPanel.getRoot()); + leftMainPanel.getChildren().clear(); + leftMainPanel.getChildren().add(stallListPanel.getRoot()); - rightPanelPlaceholder.getChildren().clear(); + rightMainPanel.getChildren().clear(); StatusBarFooter statusBarFooter = new StatusBarFooter(logic.getAddressBookFilePath()); statusbarPlaceholder.getChildren().add(statusBarFooter.getRoot()); @@ -189,22 +191,22 @@ private void handleExit() { @FXML public void handleIsStallDetail() { oneStallPanel = new OneStallPanel(logic.getTempFilteredStallList()); - rightPanelPlaceholder.getChildren().add(oneStallPanel.getRoot()); + rightMainPanel.getChildren().add(oneStallPanel.getRoot()); } - public StallListPanel getStallListPanel() { - return stallListPanel; + public StallListPanel getstallListPanel() { + return this.stallListPanel; } private void handleItemSelectionChanged(Item item) { - ItemNamePanel itemNamePanel = new ItemNamePanel(item); - leftPanelPlaceholder.getChildren().clear(); - leftPanelPlaceholder.getChildren().add(itemNamePanel.getRoot()); + ItemListPanel itemListPanel = new ItemListPanel(logic.getFilteredItemList()); + leftMainPanel.getChildren().clear(); + leftMainPanel.getChildren().add(itemListPanel.getRoot()); ItemReviewPanel itemReviewPanel = new ItemReviewPanel(item); - rightPanelPlaceholder.getChildren().clear(); - rightPanelPlaceholder.getChildren().add(itemReviewPanel.getRoot()); + rightMainPanel.getChildren().clear(); + rightMainPanel.getChildren().add(itemReviewPanel.getRoot()); } /** diff --git a/src/main/java/seedu/address/ui/StallListPanel.java b/src/main/java/seedu/address/ui/StallListPanel.java index 28e1b1bfa22..2d67e3aa77a 100644 --- a/src/main/java/seedu/address/ui/StallListPanel.java +++ b/src/main/java/seedu/address/ui/StallListPanel.java @@ -14,19 +14,19 @@ * Panel containing the list of persons. */ public class StallListPanel extends UiPart { - private static final String FXML = "StallListPanel.fxml"; + private static final String FXML = "LeftListPanel.fxml"; private final Logger logger = LogsCenter.getLogger(StallListPanel.class); @FXML - private ListView stallListView; + private ListView leftListView; /** * Creates a {@code StallListPanel} with the given {@code ObservableList}. */ public StallListPanel(ObservableList stallList) { super(FXML); - stallListView.setItems(stallList); - stallListView.setCellFactory(listView -> new StallListViewCell()); + leftListView.setItems(stallList); + leftListView.setCellFactory(listView -> new StallListViewCell()); } /** diff --git a/src/main/resources/view/ItemReviewPanel.fxml b/src/main/resources/view/ItemReviewPanel.fxml index 6ac5da7f331..15b581d3c6c 100644 --- a/src/main/resources/view/ItemReviewPanel.fxml +++ b/src/main/resources/view/ItemReviewPanel.fxml @@ -6,7 +6,6 @@ - @@ -14,22 +13,21 @@ - - - - - - - - + diff --git a/src/main/resources/view/StallListPanel.fxml b/src/main/resources/view/LeftListPanel.fxml similarity index 78% rename from src/main/resources/view/StallListPanel.fxml rename to src/main/resources/view/LeftListPanel.fxml index 6ef84d71299..b91889178ae 100644 --- a/src/main/resources/view/StallListPanel.fxml +++ b/src/main/resources/view/LeftListPanel.fxml @@ -4,5 +4,5 @@ - + diff --git a/src/main/resources/view/MainWindow.fxml b/src/main/resources/view/MainWindow.fxml index 352a9e5f0e2..f50c748513a 100644 --- a/src/main/resources/view/MainWindow.fxml +++ b/src/main/resources/view/MainWindow.fxml @@ -13,6 +13,7 @@ + @@ -48,29 +49,29 @@ - + + + - + + + + + - - - - - - + + + + + - - - - - - + - + - + diff --git a/src/test/java/seedu/address/logic/commands/AddStallCommandTest.java b/src/test/java/seedu/address/logic/commands/AddStallCommandTest.java index 6e5ded49f9d..a14322f853d 100644 --- a/src/test/java/seedu/address/logic/commands/AddStallCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddStallCommandTest.java @@ -12,9 +12,8 @@ import java.util.Arrays; import java.util.function.Predicate; -import org.junit.jupiter.api.Test; - import javafx.collections.ObservableList; +import org.junit.jupiter.api.Test; import seedu.address.commons.core.GuiSettings; import seedu.address.commons.core.index.Index; import seedu.address.logic.Messages; @@ -212,6 +211,11 @@ public void setFilteredItem(Item item) { throw new AssertionError("This method should not be called."); } + @Override + public void setFilteredItemList(Index index) { + throw new AssertionError("This method should not be called."); + } + @Override public Item getFilteredItem() { throw new AssertionError("This method should not be called."); @@ -221,6 +225,10 @@ public Item getFilteredItem() { public Item getFilteredItem(Index stallIndex, Index itemIndex) { throw new AssertionError("This method should not be called."); } + + public ObservableList getFilteredItemList() { + throw new AssertionError("This method should not be called."); + } } /** From daa95a4ac66b0b857599f81ab2c1b20060679d1f Mon Sep 17 00:00:00 2001 From: Ruizhi2001 <70838461+Ruizhi2001@users.noreply.github.com> Date: Thu, 26 Oct 2023 00:44:40 +0800 Subject: [PATCH 2/3] Add ability to sort stall by location or rating --- .../logic/commands/AddStallReviewCommand.java | 1 + .../address/logic/commands/ListCommand.java | 1 - .../commands/SortStallLocationCommand.java | 25 ++++++++++++ .../commands/SortStallRatingCommand.java | 25 ++++++++++++ .../logic/parser/AddressBookParser.java | 9 +++++ .../java/seedu/address/model/AddressBook.java | 10 +++++ src/main/java/seedu/address/model/Model.java | 14 +++++++ .../seedu/address/model/ModelManager.java | 11 ++++++ .../java/seedu/address/model/stall/Stall.java | 11 ++++++ .../model/stall/StallLocationComparator.java | 13 +++++++ .../model/stall/StallRatingComparator.java | 13 +++++++ .../address/model/stall/UniqueStallList.java | 16 ++++++++ .../model/stall/review/StallReview.java | 4 ++ .../logic/commands/AddStallCommandTest.java | 12 ++++++ .../SortStallLocationCommandTest.java | 39 +++++++++++++++++++ .../commands/SortStallRatingCommandTest.java | 39 +++++++++++++++++++ 16 files changed, 242 insertions(+), 1 deletion(-) create mode 100644 src/main/java/seedu/address/logic/commands/SortStallLocationCommand.java create mode 100644 src/main/java/seedu/address/logic/commands/SortStallRatingCommand.java create mode 100644 src/main/java/seedu/address/model/stall/StallLocationComparator.java create mode 100644 src/main/java/seedu/address/model/stall/StallRatingComparator.java create mode 100644 src/test/java/seedu/address/logic/commands/SortStallLocationCommandTest.java create mode 100644 src/test/java/seedu/address/logic/commands/SortStallRatingCommandTest.java diff --git a/src/main/java/seedu/address/logic/commands/AddStallReviewCommand.java b/src/main/java/seedu/address/logic/commands/AddStallReviewCommand.java index 1e566cd87fa..b447be4da8f 100644 --- a/src/main/java/seedu/address/logic/commands/AddStallReviewCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddStallReviewCommand.java @@ -25,6 +25,7 @@ public class AddStallReviewCommand extends Command { + PREFIX_RATING + "RATING " + PREFIX_DESCRIPTION + "DESCRIPTION \n" + "Example: " + COMMAND_WORD + " " + + PREFIX_STALL + "1 " + PREFIX_RATING + "2 " + PREFIX_DESCRIPTION + "The auntie is pretty and ambience good."; diff --git a/src/main/java/seedu/address/logic/commands/ListCommand.java b/src/main/java/seedu/address/logic/commands/ListCommand.java index 64c00ea6656..4ecd3101e03 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 stalls"; - @Override public CommandResult execute(Model model) { requireNonNull(model); diff --git a/src/main/java/seedu/address/logic/commands/SortStallLocationCommand.java b/src/main/java/seedu/address/logic/commands/SortStallLocationCommand.java new file mode 100644 index 00000000000..0866ccdae96 --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/SortStallLocationCommand.java @@ -0,0 +1,25 @@ +package seedu.address.logic.commands; + +import static java.util.Objects.requireNonNull; +import static seedu.address.model.Model.PREDICATE_SHOW_ALL_STALLS; + +import seedu.address.model.Model; + +public class SortStallLocationCommand extends Command { + + public static final String COMMAND_WORD = "sort-stalls-locations"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Sorts all stalls by location in alphabetical order." + + "and displays them as a list with index number.\n" + + "Example: " + COMMAND_WORD; + + public static final String MESSAGE_SUCCESS = "Sorted all stalls by location in alphabetical order."; + + @Override + public CommandResult execute(Model model) { + requireNonNull(model); + model.updateFilteredStallList(PREDICATE_SHOW_ALL_STALLS); + model.sortStallLocation(); + return new CommandResult(MESSAGE_SUCCESS, false, false, false, false); + } +} diff --git a/src/main/java/seedu/address/logic/commands/SortStallRatingCommand.java b/src/main/java/seedu/address/logic/commands/SortStallRatingCommand.java new file mode 100644 index 00000000000..6c218cdbe69 --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/SortStallRatingCommand.java @@ -0,0 +1,25 @@ +package seedu.address.logic.commands; + +import static java.util.Objects.requireNonNull; +import static seedu.address.model.Model.PREDICATE_SHOW_ALL_STALLS; + +import seedu.address.model.Model; + +public class SortStallRatingCommand extends Command { + + public static final String COMMAND_WORD = "sort-stalls-ratings"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Sorts all stalls by rating in descending order." + + "and displays them as a list with index number.\n" + + "Example: " + COMMAND_WORD; + + public static final String MESSAGE_SUCCESS = "Sorted all stalls by rating in descending order."; + + @Override + public CommandResult execute(Model model) { + requireNonNull(model); + model.updateFilteredStallList(PREDICATE_SHOW_ALL_STALLS); + model.sortStallRating(); + return new CommandResult(MESSAGE_SUCCESS, false, false, false, false); + } +} diff --git a/src/main/java/seedu/address/logic/parser/AddressBookParser.java b/src/main/java/seedu/address/logic/parser/AddressBookParser.java index 7c931c88e7e..1d7cb219f49 100644 --- a/src/main/java/seedu/address/logic/parser/AddressBookParser.java +++ b/src/main/java/seedu/address/logic/parser/AddressBookParser.java @@ -23,6 +23,8 @@ import seedu.address.logic.commands.FindCommand; import seedu.address.logic.commands.HelpCommand; import seedu.address.logic.commands.ListCommand; +import seedu.address.logic.commands.SortStallLocationCommand; +import seedu.address.logic.commands.SortStallRatingCommand; import seedu.address.logic.commands.ViewItemCommand; import seedu.address.logic.commands.ViewStallCommand; import seedu.address.logic.parser.exceptions.ParseException; @@ -98,6 +100,12 @@ public Command parseCommand(String userInput) throws ParseException { case ListCommand.COMMAND_WORD: return new ListCommand(); + case SortStallRatingCommand.COMMAND_WORD: + return new SortStallRatingCommand(); + + case SortStallLocationCommand.COMMAND_WORD: + return new SortStallLocationCommand(); + case ExitCommand.COMMAND_WORD: return new ExitCommand(); @@ -110,6 +118,7 @@ public Command parseCommand(String userInput) throws ParseException { case ViewStallCommand.COMMAND_WORD: return new ViewStallCommandParser().parse(arguments); + default: logger.finer("This user input caused a ParseException: " + userInput); 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 f4aa15ebc61..4005c8dd31d 100644 --- a/src/main/java/seedu/address/model/AddressBook.java +++ b/src/main/java/seedu/address/model/AddressBook.java @@ -67,6 +67,7 @@ public boolean hasStall(Stall stall) { return stalls.contains(stall); } + /** * Adds a stall to the address book. * The stall must not already exist in the address book. @@ -94,6 +95,15 @@ public void removeStall(Stall key) { stalls.remove(key); } + public void sortStallRating() { + stalls.sortByRating(); + } + + public void sortStallLocation() { + stalls.sortByLocation(); + } + + //// util methods @Override diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index c45e303700b..a64a3cb4f4e 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -150,6 +150,20 @@ public interface Model { */ void setFilteredItemList(Index stallIndex); + /** + * Sorts the stall list by rating in descending order. + */ + void sortStallRating(); + + /** + * Sorts the stall list by location in alphabetical order. + */ + void sortStallLocation(); + + /** + * Get the item list that is filtered by stall. + * @throws NullPointerException if {@code predicate} is null. + */ ObservableList getFilteredItemList(); } diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 9672640a281..2db30b6de5e 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -153,6 +153,16 @@ public Stall getFilteredStall(Index stallIndex) { return filteredStalls.get(stallIndex.getZeroBased()); } + @Override + public void sortStallRating() { + addressBook.sortStallRating(); + } + + @Override + public void sortStallLocation() { + addressBook.sortStallLocation(); + } + //=========== Filtered Item List Accessors ============================================================= @Override @@ -222,6 +232,7 @@ public ObservableList getFilteredItemList() { return filteredItemList; } + @Override public boolean equals(Object other) { if (other == this) { diff --git a/src/main/java/seedu/address/model/stall/Stall.java b/src/main/java/seedu/address/model/stall/Stall.java index 8f0d6e8dd08..c84b201181b 100644 --- a/src/main/java/seedu/address/model/stall/Stall.java +++ b/src/main/java/seedu/address/model/stall/Stall.java @@ -63,6 +63,10 @@ public Location getLocation() { return location; } + public String getLocationString() { + return location.toString(); + } + public Menu getMenu() { return menu; } @@ -79,6 +83,13 @@ public StallReview getStallReview() { return stallReview; } + public int getStallRatingValue() { + if (stallReview == null) { + return 0; + } + return stallReview.getRatingValue(); + } + public void setStallReview(StallReview stallReview) { requireAllNonNull(stallReview); this.stallReview = stallReview; diff --git a/src/main/java/seedu/address/model/stall/StallLocationComparator.java b/src/main/java/seedu/address/model/stall/StallLocationComparator.java new file mode 100644 index 00000000000..d367c6f9439 --- /dev/null +++ b/src/main/java/seedu/address/model/stall/StallLocationComparator.java @@ -0,0 +1,13 @@ +package seedu.address.model.stall; + +import java.util.Comparator; + +/** + * Compares two stalls based on their locations. + */ +public class StallLocationComparator implements Comparator { + @Override + public int compare(Stall stall1, Stall stall2) { + return stall1.getLocationString().compareTo(stall2.getLocationString()); + } +} diff --git a/src/main/java/seedu/address/model/stall/StallRatingComparator.java b/src/main/java/seedu/address/model/stall/StallRatingComparator.java new file mode 100644 index 00000000000..eef8d336096 --- /dev/null +++ b/src/main/java/seedu/address/model/stall/StallRatingComparator.java @@ -0,0 +1,13 @@ +package seedu.address.model.stall; + +import java.util.Comparator; + +/** + * Compares two stalls based on their ratings. + */ +public class StallRatingComparator implements Comparator { + @Override + public int compare(Stall stall1, Stall stall2) { + return stall2.getStallRatingValue() - stall1.getStallRatingValue(); + } +} diff --git a/src/main/java/seedu/address/model/stall/UniqueStallList.java b/src/main/java/seedu/address/model/stall/UniqueStallList.java index f84cb9023a2..d4eada41640 100644 --- a/src/main/java/seedu/address/model/stall/UniqueStallList.java +++ b/src/main/java/seedu/address/model/stall/UniqueStallList.java @@ -3,6 +3,7 @@ import static java.util.Objects.requireNonNull; import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; +import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -68,6 +69,17 @@ public void setStall(Stall target, Stall editedStall) { internalList.set(index, editedStall); } + /** + * Sorts the stalls in the list by rating. + */ + public void sortByRating() { + Collections.sort(internalList, new StallRatingComparator()); + } + + public void sortByLocation() { + Collections.sort(internalList, new StallLocationComparator()); + } + /** * Removes the equivalent stall from the list. * The stall must exist in the list. @@ -97,6 +109,10 @@ public void setStalls(List stalls) { internalList.setAll(stalls); } + public void sortStallRating() { + Collections.sort(internalList, new StallRatingComparator()); + } + /** * Returns the backing list as an unmodifiable {@code ObservableList}. */ diff --git a/src/main/java/seedu/address/model/stall/review/StallReview.java b/src/main/java/seedu/address/model/stall/review/StallReview.java index 060552e0e75..2d6ecda3308 100644 --- a/src/main/java/seedu/address/model/stall/review/StallReview.java +++ b/src/main/java/seedu/address/model/stall/review/StallReview.java @@ -34,6 +34,10 @@ public Rating getRating() { return rating; } + public int getRatingValue() { + return Integer.parseInt(rating.rating); + } + public Description getDescription() { return description; } diff --git a/src/test/java/seedu/address/logic/commands/AddStallCommandTest.java b/src/test/java/seedu/address/logic/commands/AddStallCommandTest.java index 153db7412d9..c4cb7a50fea 100644 --- a/src/test/java/seedu/address/logic/commands/AddStallCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddStallCommandTest.java @@ -226,9 +226,21 @@ public Item getFilteredItem(Index stallIndex, Index itemIndex) { throw new AssertionError("This method should not be called."); } + @Override public ObservableList getFilteredItemList() { throw new AssertionError("This method should not be called."); } + + @Override + public void sortStallLocation() { + throw new AssertionError("This method should not be called."); + } + + @Override + public void sortStallRating() { + throw new AssertionError("This method should not be called."); + } + } /** diff --git a/src/test/java/seedu/address/logic/commands/SortStallLocationCommandTest.java b/src/test/java/seedu/address/logic/commands/SortStallLocationCommandTest.java new file mode 100644 index 00000000000..c776ca01323 --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/SortStallLocationCommandTest.java @@ -0,0 +1,39 @@ +package seedu.address.logic.commands; + +import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +import static seedu.address.testutil.TypicalStalls.getTypicalAddressBook; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import seedu.address.model.Model; +import seedu.address.model.ModelManager; +import seedu.address.model.UserPrefs; + +/** + * Contains integration tests (interaction with the Model) and unit tests for SortStallLocationCommand. + */ +public class SortStallLocationCommandTest { + + private Model model; + private Model expectedSortedModel; + + @BeforeEach + public void setUp() { + model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + expectedSortedModel = new ModelManager(model.getAddressBook(), new UserPrefs()); + expectedSortedModel.sortStallLocation(); + } + + @Test + public void execute_listIsNotSortedByLocation_showsSameList() { + assertCommandSuccess(new SortStallLocationCommand(), model, SortStallLocationCommand.MESSAGE_SUCCESS, + expectedSortedModel); + } + + @Test + public void execute_listIsSortedLocation_showsEverything() { + assertCommandSuccess(new SortStallLocationCommand(), expectedSortedModel, + SortStallLocationCommand.MESSAGE_SUCCESS, + expectedSortedModel); + } +} diff --git a/src/test/java/seedu/address/logic/commands/SortStallRatingCommandTest.java b/src/test/java/seedu/address/logic/commands/SortStallRatingCommandTest.java new file mode 100644 index 00000000000..126982d7dcf --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/SortStallRatingCommandTest.java @@ -0,0 +1,39 @@ +package seedu.address.logic.commands; + +import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +import static seedu.address.testutil.TypicalStalls.getTypicalAddressBook; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import seedu.address.model.Model; +import seedu.address.model.ModelManager; +import seedu.address.model.UserPrefs; + +/** + * Contains integration tests (interaction with the Model) and unit tests for SortStallLocationCommand. + */ +public class SortStallRatingCommandTest { + + private Model model; + private Model expectedSortedModel; + + @BeforeEach + public void setUp() { + model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + expectedSortedModel = new ModelManager(model.getAddressBook(), new UserPrefs()); + expectedSortedModel.sortStallRating(); + } + + @Test + public void execute_listIsNotSortedByRating_showsSameList() { + assertCommandSuccess(new SortStallRatingCommand(), model, SortStallRatingCommand.MESSAGE_SUCCESS, + expectedSortedModel); + } + + @Test + public void execute_listIsSortedByRating_showsEverything() { + assertCommandSuccess(new SortStallRatingCommand(), expectedSortedModel, + SortStallRatingCommand.MESSAGE_SUCCESS, + expectedSortedModel); + } +} From 862e1b5da07789ef85a0a256556a1169f3380289 Mon Sep 17 00:00:00 2001 From: Ruizhi2001 <70838461+Ruizhi2001@users.noreply.github.com> Date: Thu, 26 Oct 2023 01:09:29 +0800 Subject: [PATCH 3/3] Fix UI bug when view-stall after view-item --- .../address/logic/commands/SortStallLocationCommand.java | 3 +++ .../seedu/address/logic/commands/SortStallRatingCommand.java | 3 +++ src/main/java/seedu/address/model/stall/Stall.java | 2 +- src/main/java/seedu/address/ui/MainWindow.java | 4 ++++ .../seedu/address/logic/commands/AddStallCommandTest.java | 4 ++-- .../address/logic/commands/SortStallLocationCommandTest.java | 1 + .../address/logic/commands/SortStallRatingCommandTest.java | 1 + 7 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/SortStallLocationCommand.java b/src/main/java/seedu/address/logic/commands/SortStallLocationCommand.java index 0866ccdae96..5b75a185b04 100644 --- a/src/main/java/seedu/address/logic/commands/SortStallLocationCommand.java +++ b/src/main/java/seedu/address/logic/commands/SortStallLocationCommand.java @@ -5,6 +5,9 @@ import seedu.address.model.Model; +/** + * Sorts all stalls by location in alphabetical order. + */ public class SortStallLocationCommand extends Command { public static final String COMMAND_WORD = "sort-stalls-locations"; diff --git a/src/main/java/seedu/address/logic/commands/SortStallRatingCommand.java b/src/main/java/seedu/address/logic/commands/SortStallRatingCommand.java index 6c218cdbe69..9015fffe966 100644 --- a/src/main/java/seedu/address/logic/commands/SortStallRatingCommand.java +++ b/src/main/java/seedu/address/logic/commands/SortStallRatingCommand.java @@ -5,6 +5,9 @@ import seedu.address.model.Model; +/** + * Sorts all stalls by location in alphabetical order. + */ public class SortStallRatingCommand extends Command { public static final String COMMAND_WORD = "sort-stalls-ratings"; diff --git a/src/main/java/seedu/address/model/stall/Stall.java b/src/main/java/seedu/address/model/stall/Stall.java index c84b201181b..a861d0bdd07 100644 --- a/src/main/java/seedu/address/model/stall/Stall.java +++ b/src/main/java/seedu/address/model/stall/Stall.java @@ -4,8 +4,8 @@ import java.util.Objects; -import seedu.address.commons.core.index.Index; import javafx.collections.ObservableList; +import seedu.address.commons.core.index.Index; import seedu.address.commons.util.ToStringBuilder; import seedu.address.model.item.Item; import seedu.address.model.stall.review.StallReview; diff --git a/src/main/java/seedu/address/ui/MainWindow.java b/src/main/java/seedu/address/ui/MainWindow.java index b9114d21082..65768bed866 100644 --- a/src/main/java/seedu/address/ui/MainWindow.java +++ b/src/main/java/seedu/address/ui/MainWindow.java @@ -191,6 +191,10 @@ private void handleExit() { @FXML public void handleIsStallDetail() { oneStallPanel = new OneStallPanel(logic.getTempFilteredStallList()); + stallListPanel = new StallListPanel(logic.getFilteredStallList()); + leftMainPanel.getChildren().clear(); + leftMainPanel.getChildren().add(stallListPanel.getRoot()); + rightMainPanel.getChildren().clear(); rightMainPanel.getChildren().add(oneStallPanel.getRoot()); } diff --git a/src/test/java/seedu/address/logic/commands/AddStallCommandTest.java b/src/test/java/seedu/address/logic/commands/AddStallCommandTest.java index c4cb7a50fea..c6e2b2e3b0c 100644 --- a/src/test/java/seedu/address/logic/commands/AddStallCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddStallCommandTest.java @@ -12,8 +12,9 @@ import java.util.Arrays; import java.util.function.Predicate; -import javafx.collections.ObservableList; import org.junit.jupiter.api.Test; + +import javafx.collections.ObservableList; import seedu.address.commons.core.GuiSettings; import seedu.address.commons.core.index.Index; import seedu.address.logic.Messages; @@ -26,7 +27,6 @@ import seedu.address.model.item.review.ItemReview; import seedu.address.model.stall.Stall; import seedu.address.testutil.StallBuilder; - public class AddStallCommandTest { @Test diff --git a/src/test/java/seedu/address/logic/commands/SortStallLocationCommandTest.java b/src/test/java/seedu/address/logic/commands/SortStallLocationCommandTest.java index c776ca01323..4cb0d6c6659 100644 --- a/src/test/java/seedu/address/logic/commands/SortStallLocationCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/SortStallLocationCommandTest.java @@ -5,6 +5,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; + import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs; diff --git a/src/test/java/seedu/address/logic/commands/SortStallRatingCommandTest.java b/src/test/java/seedu/address/logic/commands/SortStallRatingCommandTest.java index 126982d7dcf..eb7b02bde85 100644 --- a/src/test/java/seedu/address/logic/commands/SortStallRatingCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/SortStallRatingCommandTest.java @@ -5,6 +5,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; + import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs;