diff --git a/src/main/java/duke/duke.plantuml b/src/main/java/duke/duke.plantuml new file mode 100644 index 00000000..d84f064a --- /dev/null +++ b/src/main/java/duke/duke.plantuml @@ -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 philippe.mesmeur@gmail.com +endfooter + +@enduml diff --git a/src/main/java/duke/stats/Statistics.java b/src/main/java/duke/stats/Statistics.java new file mode 100644 index 00000000..6245300d --- /dev/null +++ b/src/main/java/duke/stats/Statistics.java @@ -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> ratedDishes; + + public Statistics(Path path, List dishes) { + this.path = path; + this.ratedDishes = new HashMap>(); + } + + 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 getTopRated() { + return ratedDishes + .entrySet() + .stream() + .sorted(new Comparator>>() { + @Override + public int compare(Map.Entry> o1, Map.Entry> 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 getMostOrdered() { + return ratedDishes + .entrySet() + .stream() + .sorted(new Comparator>>() { + @Override + public int compare(Map.Entry> o1, Map.Entry> o2) { + return o2.getValue().getValue().compareTo(o1.getValue().getValue()); + } + }) + .collect( + toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2, + LinkedHashMap::new)).keySet(); + } + +}