Skip to content

Commit

Permalink
Merge pull request #101 from Ruizhi2001/branch-SortStallByLocationOrR…
Browse files Browse the repository at this point in the history
…ating

Branch sort stall by location or rating
  • Loading branch information
samuelmui8 authored Oct 26, 2023
2 parents c99c250 + 862e1b5 commit 2e00fa2
Show file tree
Hide file tree
Showing 27 changed files with 456 additions and 64 deletions.
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item> getFilteredItemList();

/**
* Returns the user prefs' address book file path.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public Item getFilteredItem() {
return model.getFilteredItem();
}

@Override
public ObservableList<Item> getFilteredItemList() {
return model.getFilteredItemList();
}

@Override
public ObservableList<Stall> getFilteredStallList() {
return model.getFilteredStallList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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;

/**
* Sorts all stalls by location in alphabetical order.
*/
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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;

/**
* Sorts all stalls by location in alphabetical order.
*/
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import seedu.address.logic.commands.FindStallCommand;
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;
Expand Down Expand Up @@ -102,6 +104,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();

Expand All @@ -114,6 +122,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);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
31 changes: 26 additions & 5 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,39 @@ public interface Model {
*/
void deleteItem(Index stallIndex, Index itemIndex);

/**
* Adds the given item review.
*
* {@code itemReview} must not already exist in the item.
*/
void setItemReview(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 setItemReview(Item itemIndex, ItemReview itemReview);
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<Item> getFilteredItemList();

}
24 changes: 24 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class ModelManager implements Model {
private final UserPrefs userPrefs;
private final FilteredList<Stall> filteredStalls;
private final FilteredList<Stall> tempFilteredStalls;

private ObservableList<Item> filteredItemList;
private Item filteredItem;

/**
Expand Down Expand Up @@ -151,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
Expand Down Expand Up @@ -209,6 +221,18 @@ 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<Item> getFilteredItemList() {
return filteredItemList;
}


@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/seedu/address/model/stall/Stall.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.Objects;

import javafx.collections.ObservableList;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.item.Item;
Expand Down Expand Up @@ -62,10 +63,18 @@ public Location getLocation() {
return location;
}

public String getLocationString() {
return location.toString();
}

public Menu getMenu() {
return menu;
}


public ObservableList<Item> getMenuList() {
return menu.getItemList();
}
public boolean hasStallReview() {
return stallReview != null;
}
Expand All @@ -74,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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Stall> {
@Override
public int compare(Stall stall1, Stall stall2) {
return stall1.getLocationString().compareTo(stall2.getLocationString());
}
}
13 changes: 13 additions & 0 deletions src/main/java/seedu/address/model/stall/StallRatingComparator.java
Original file line number Diff line number Diff line change
@@ -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<Stall> {
@Override
public int compare(Stall stall1, Stall stall2) {
return stall2.getStallRatingValue() - stall1.getStallRatingValue();
}
}
16 changes: 16 additions & 0 deletions src/main/java/seedu/address/model/stall/UniqueStallList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -97,6 +109,10 @@ public void setStalls(List<Stall> stalls) {
internalList.setAll(stalls);
}

public void sortStallRating() {
Collections.sort(internalList, new StallRatingComparator());
}

/**
* Returns the backing list as an unmodifiable {@code ObservableList}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public Rating getRating() {
return rating;
}

public int getRatingValue() {
return Integer.parseInt(rating.rating);
}

public Description getDescription() {
return description;
}
Expand Down
Loading

0 comments on commit 2e00fa2

Please sign in to comment.