Skip to content

Commit

Permalink
Statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
saradj committed Oct 16, 2019
1 parent bac462c commit 7e26e67
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/main/java/duke/stats/Statistics.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package duke.stats;

import duke.task.Dish;
import javafx.util.Pair;

import java.nio.file.Path;
Expand All @@ -9,31 +10,30 @@

public class Statistics {
private Path path;
private Map<Dish, Pair<Integer, Integer>> ratedDishes;
private Map<Dish, Pair<Double, Integer>> ratedDishes;

public Statistics(Path path, List<Dish> dishes) {
this.path = path;
this.dishes = new HashSet<Dish>();
this.quantityDishes = new Map<Integer, Dish>();
this.ratedDishes = new Map<Integer, Dish>();
this.ratedDishes = new HashMap<Dish, Pair<Double, Integer>>();
}

public void update(Order order) {
for (Dish dish : order.getDishes()) {
public void update(OrderList orderList) {
for (Order order : orderList.getAllOrders()) {
Dish dish = order.getDish();
ratedDishes.putIfAbsent(dish, new Pair(0, 0));
int newTotal = ratedDishes.get(dish).getValue() + dish.getAmount();
int newRating = (ratedDishes.get(dish).getKey() + dish.getRating()*dish.getAmount())/newTotal;
double newRating = (ratedDishes.get(dish).getKey() + dish.getRating() * dish.getAmount()) / newTotal;
ratedDishes.put(dish, new Pair<>(newRating, newTotal));
}
}

public List<Dish> getTopRated() {
public Set<Dish> getTopRated() {
return ratedDishes
.entrySet()
.stream()
.sorted(new Comparator<Map.Entry<Dish, Pair<Integer, Integer>>>() {
.sorted(new Comparator<Map.Entry<Dish, Pair<Double, Integer>>>() {
@Override
public int compare(Map.Entry<Dish, Pair<Integer, Integer>> o1, Map.Entry<Dish, Pair<Integer, Integer>> o2) {
public int compare(Map.Entry<Dish, Pair<Double, Integer>> o1, Map.Entry<Dish, Pair<Double, Integer>> o2) {
return o2.getValue().getKey().compareTo(o1.getValue().getKey());
}
})
Expand All @@ -43,20 +43,19 @@ public int compare(Map.Entry<Dish, Pair<Integer, Integer>> o1, Map.Entry<Dish, P

}

public List<Dish> getMostOrdered() {
public Set<Dish> getMostOrdered() {
return ratedDishes
.entrySet()
.stream()
.sorted(new Comparator<Map.Entry<Dish, Pair<Integer, Integer>>>() {
.sorted(new Comparator<Map.Entry<Dish, Pair<Double, Integer>>>() {
@Override
public int compare(Map.Entry<Dish, Pair<Integer, Integer>> o1, Map.Entry<Dish, Pair<Integer, Integer>> o2) {
public int compare(Map.Entry<Dish, Pair<Double, Integer>> o1, Map.Entry<Dish, Pair<Double, Integer>> o2) {
return o2.getValue().getValue().compareTo(o1.getValue().getValue());
}
})
.collect(
toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2,
LinkedHashMap::new)).keySet();

}

}

0 comments on commit 7e26e67

Please sign in to comment.