forked from nusCS2113-AY1920S1/duke
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request nusCS2113-AY1920S1#7 from saradj/B-statistics
Adding statis
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |