-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrocery.java
89 lines (76 loc) · 2.82 KB
/
Grocery.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.*;
public class Grocery{
private String name;
private ArrayList<Product> products;
private Point location;
public Grocery(String name, Point location){
this.name = name;
this.location = location;
products = new ArrayList<Product>();
}
public String getName(){
return name;
}
public ArrayList<Product> getProducts(){
return products;
}
public Point getLoc(){
return location;
}
public void addProduct(Product a){
products.add(a);
}
public List<Product> search(String search){
List<Product> matchingProducts = new ArrayList<>();
for (Product p : products) {
if (containsProduct(p, search)) matchingProducts.add(p);
}
return matchingProducts;
}
/** helper method that returns true if a product matches a search */
private boolean containsProduct(Product p, String search) {
String[] split = search.split(" ");
List<String> keywords = new ArrayList<>();
for (String s : split) {
keywords.add(s.toLowerCase());
}
String[] productSplit = p.getName().split(" ");
HashMap<String, Integer> productName = new HashMap<>();
for (String s : productSplit) {
productName.put(s.toLowerCase(), 0);
}
for (String keyword : keywords) {
if (!productName.containsKey(keyword)) return false;
}
return true;
}
public Product bestProduct(String productName, User u, double averageCost) {
List<Product> products = search(productName);
Map<Double, Product> scores = new TreeMap<>();
for (Product p: products) {
scores.put(p.calculateScore(u, averageCost), p);
}
double bestScore = Collections.max(scores.keySet());
return scores.get(bestScore);
}
public double storeScore(String[] groceryList, User u, List<Double> costs) {
double totalAverage = 0;
for (int i = 0; i < groceryList.length; i++) {
String product = groceryList[i];
double weightedAverage = 0;
List<Product> products = search(product);
List<Double> sortedProducts = new ArrayList<>();
for (Product p : products) {
sortedProducts.add(p.calculateScore(u, costs.get(i)));
}
Collections.sort(sortedProducts);
double total = (sortedProducts.size() + 1) * (sortedProducts.size()) / 2.0;
for (int j = 0; j < sortedProducts.size(); j++) {
weightedAverage += sortedProducts.get(j) * (j + 1) / (total);
}
weightedAverage = Math.round(weightedAverage * 100.0)/100.0;
totalAverage += weightedAverage;
}
return totalAverage / groceryList.length;
}
}