From bac462c698bef021cb37d868425bd811c07ec348 Mon Sep 17 00:00:00 2001 From: saradj Date: Mon, 14 Oct 2019 23:15:09 +0800 Subject: [PATCH 1/2] adding stats v1 --- src/main/java/duke/duke.plantuml | 26 ++++++++++ src/main/java/duke/stats/Statistics.java | 62 ++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/main/java/duke/duke.plantuml create mode 100644 src/main/java/duke/stats/Statistics.java 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..7fa46208 --- /dev/null +++ b/src/main/java/duke/stats/Statistics.java @@ -0,0 +1,62 @@ +package duke.stats; + +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.dishes = new HashSet(); + this.quantityDishes = new Map(); + this.ratedDishes = new Map(); + } + + public void update(Order order) { + for (Dish dish : order.getDishes()) { + 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; + ratedDishes.put(dish, new Pair<>(newRating, newTotal)); + } + } + + public List 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 List 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(); + + } + +} From 7e26e675acc536ba286bb37cc342cd80ba7a8c32 Mon Sep 17 00:00:00 2001 From: saradj Date: Wed, 16 Oct 2019 22:11:29 +0800 Subject: [PATCH 2/2] Statistics --- src/main/java/duke/stats/Statistics.java | 27 ++++++++++++------------ 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/main/java/duke/stats/Statistics.java b/src/main/java/duke/stats/Statistics.java index 7fa46208..6245300d 100644 --- a/src/main/java/duke/stats/Statistics.java +++ b/src/main/java/duke/stats/Statistics.java @@ -1,5 +1,6 @@ package duke.stats; +import duke.task.Dish; import javafx.util.Pair; import java.nio.file.Path; @@ -9,31 +10,30 @@ public class Statistics { private Path path; - private Map> ratedDishes; + private Map> ratedDishes; public Statistics(Path path, List dishes) { this.path = path; - this.dishes = new HashSet(); - this.quantityDishes = new Map(); - this.ratedDishes = new Map(); + this.ratedDishes = new HashMap>(); } - 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 getTopRated() { + public Set getTopRated() { return ratedDishes .entrySet() .stream() - .sorted(new Comparator>>() { + .sorted(new Comparator>>() { @Override - public int compare(Map.Entry> o1, Map.Entry> o2) { + public int compare(Map.Entry> o1, Map.Entry> o2) { return o2.getValue().getKey().compareTo(o1.getValue().getKey()); } }) @@ -43,20 +43,19 @@ public int compare(Map.Entry> o1, Map.Entry getMostOrdered() { + public Set getMostOrdered() { return ratedDishes .entrySet() .stream() - .sorted(new Comparator>>() { + .sorted(new Comparator>>() { @Override - public int compare(Map.Entry> o1, Map.Entry> o2) { + 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(); - } }