|
@@ -19,7 +19,7 @@ public class ListCreation {
|
19
|
19
|
}
|
20
|
20
|
|
21
|
21
|
public String formattedGroceryListString(ArrayList<Item> groceryList) {
|
22
|
|
- LinkedHashMap<Double,Integer> priceOccur = new LinkedHashMap<>();
|
|
22
|
+ TreeMap<Double,Integer> priceOccur = new TreeMap<>();
|
23
|
23
|
String list = "";
|
24
|
24
|
for (String key : keySet(groceryList)) {
|
25
|
25
|
Pattern p = Pattern.compile(key);
|
|
@@ -60,8 +60,8 @@ public class ListCreation {
|
60
|
60
|
return keys;
|
61
|
61
|
}
|
62
|
62
|
|
63
|
|
- public LinkedHashMap<Double, Integer> valueCount(ArrayList<Item> groceryList,String key) {
|
64
|
|
- LinkedHashMap<Double, Integer> priceOccurrences = new LinkedHashMap<>();
|
|
63
|
+ public TreeMap<Double, Integer> valueCount(ArrayList<Item> groceryList,String key) {
|
|
64
|
+ TreeMap<Double, Integer> priceOccurrences = new TreeMap<>();
|
65
|
65
|
for (Item item : groceryList) {
|
66
|
66
|
Double price = item.getPrice();
|
67
|
67
|
if (key.equals(item.getName())) {
|
|
@@ -75,16 +75,20 @@ public class ListCreation {
|
75
|
75
|
return priceOccurrences;
|
76
|
76
|
}
|
77
|
77
|
|
78
|
|
- private String printValues(LinkedHashMap<Double, Integer> priceOccurrences) {
|
|
78
|
+ private String printValues(TreeMap<Double, Integer> priceOccurrences) {
|
79
|
79
|
String valuesAndOccurrences = "";
|
80
|
|
- for (Map.Entry<Double, Integer> entry : priceOccurrences.entrySet()) {
|
|
80
|
+
|
|
81
|
+ for (Map.Entry<Double, Integer> entry : priceOccurrences.descendingMap().entrySet()) {
|
81
|
82
|
if(entry.getValue().equals(1)) {
|
82
|
83
|
valuesAndOccurrences += String.format("Price: %1$.2f seen: %2$d time \n", entry.getKey(), entry.getValue());
|
83
|
84
|
} else {
|
84
|
85
|
valuesAndOccurrences += String.format("Price: %1$.2f seen: %2$d times\n", entry.getKey(), entry.getValue());
|
85
|
86
|
}
|
86
|
|
- valuesAndOccurrences += "------------- -------------\n";
|
87
|
|
-
|
|
87
|
+ if(priceOccurrences.size()>1 && entry.getKey() != priceOccurrences.firstKey()) {
|
|
88
|
+ valuesAndOccurrences += "------------- -------------\n";
|
|
89
|
+ } else if (priceOccurrences.size() == 1) {
|
|
90
|
+ valuesAndOccurrences += "------------- -------------\n";
|
|
91
|
+ }
|
88
|
92
|
}
|
89
|
93
|
valuesAndOccurrences += "\n";
|
90
|
94
|
return valuesAndOccurrences;
|