Skip to content

Commit

Permalink
Merge pull request nusCS2113-AY1920S1#7 from saradj/B-statistics
Browse files Browse the repository at this point in the history
Adding statis
  • Loading branch information
JiahanYu authored Oct 16, 2019
2 parents a7dbee0 + 7e26e67 commit c9feecd
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/main/java/duke/duke.plantuml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@startuml

title __DUKE's Class Diagram__\n

namespace duke {
class duke.Duke {
+ Duke()
{static} + main()
+ run()
}
}


duke.Duke o-- duke.storage.Storage : storage
duke.Duke o-- duke.task.TaskList : tasks
duke.Duke o-- duke.ui.Ui : ui


right footer


PlantUML diagram generated by SketchIt! (https://bitbucket.org/pmesmeur/sketch.it)
For more information about this tool, please contact [email protected]
endfooter

@enduml
61 changes: 61 additions & 0 deletions src/main/java/duke/stats/Statistics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package duke.stats;

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

import java.nio.file.Path;
import java.util.*;

import static java.util.stream.Collectors.toMap;

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

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

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();
double newRating = (ratedDishes.get(dish).getKey() + dish.getRating() * dish.getAmount()) / newTotal;
ratedDishes.put(dish, new Pair<>(newRating, newTotal));
}
}

public Set<Dish> getTopRated() {
return ratedDishes
.entrySet()
.stream()
.sorted(new Comparator<Map.Entry<Dish, Pair<Double, Integer>>>() {
@Override
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());
}
})
.collect(
toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2,
LinkedHashMap::new)).keySet();

}

public Set<Dish> getMostOrdered() {
return ratedDishes
.entrySet()
.stream()
.sorted(new Comparator<Map.Entry<Dish, Pair<Double, Integer>>>() {
@Override
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 c9feecd

Please sign in to comment.