|
@@ -1,24 +1,101 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
3
|
|
-import java.util.ArrayList;
|
4
|
|
-import java.util.Arrays;
|
|
3
|
+import java.util.*;
|
5
|
4
|
|
6
|
5
|
public class ItemParser {
|
7
|
6
|
|
|
7
|
+ //"naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
|
|
8
|
+
|
|
9
|
+ public String lowerAllCasesCauseTariqCannotSpell(String rawData){
|
|
10
|
+
|
|
11
|
+ return null;
|
|
12
|
+ }
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+ // 0 "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
|
|
16
|
+
|
|
17
|
+ // 1 +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
|
|
18
|
+
|
|
19
|
+ // 2 +"NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##";
|
8
|
20
|
|
9
|
21
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
10
|
22
|
String stringPattern = "##";
|
11
|
23
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
|
24
|
+
|
12
|
25
|
return response;
|
13
|
26
|
}
|
14
|
27
|
|
15
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
28
|
+ public Item parseStringIntoItem(String rawItems) throws ItemParseException {
|
|
29
|
+ String stringPattern = "[;|^|@|*|%|$|#|!|:]";
|
|
30
|
+ ArrayList<String> itemsPattern = splitStringWithRegexPattern(stringPattern , rawItems);
|
|
31
|
+ Item newItem = null;
|
|
32
|
+ Double d;
|
|
33
|
+ for(String s : itemsPattern)
|
|
34
|
+ if (!s.equals("")) {
|
|
35
|
+ d = Double.valueOf(itemsPattern.get(3));
|
|
36
|
+ newItem = new Item(toLowerCase(itemsPattern.get(1)), d, toLowerCase(itemsPattern.get(5)), toLowerCase(itemsPattern.get(7)));
|
|
37
|
+ }
|
|
38
|
+ else throw new ItemParseException();
|
|
39
|
+ return newItem;
|
17
|
40
|
}
|
18
|
41
|
|
|
42
|
+ public String toLowerCase(String a) {
|
|
43
|
+ String s = "";
|
|
44
|
+ for (int i = 0; i < a.length(); i++) {
|
|
45
|
+ char aChar = a.charAt(i);
|
|
46
|
+ if (65 <= aChar && aChar <= 90) {
|
|
47
|
+ aChar = (char) ((aChar + 32));
|
|
48
|
+ }
|
|
49
|
+ s += aChar;
|
|
50
|
+ }
|
|
51
|
+ return s;
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ public Map<String, Map<Double, Integer>> collectAmounts(Item item) {
|
|
55
|
+ Map<String, Map<Double, Integer>> myMap = new HashMap<>();
|
|
56
|
+ int count;
|
|
57
|
+ String name = item.getName();
|
|
58
|
+ Double price = item.getPrice();
|
|
59
|
+ if (myMap.containsKey(name)) {
|
|
60
|
+ count = myMap.get(name).getOrDefault(price, 0);
|
|
61
|
+ count++;
|
|
62
|
+ myMap.get(name).put(price, count);
|
|
63
|
+ } else{ myMap.put(name, new HashMap<>());
|
|
64
|
+ myMap.get(name).put(price, 1);
|
|
65
|
+ }
|
|
66
|
+ return myMap;
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ public void printOutput(Map<String, Map<Double, Integer>> myMap){
|
|
70
|
+ Double doub = 0.0;
|
|
71
|
+ String key;
|
|
72
|
+ Integer count;
|
|
73
|
+ System.out.println(myMap.size());
|
|
74
|
+ //System.out.println(myMap.keySet());
|
|
75
|
+ // System.out.println(myMap.entrySet());
|
|
76
|
+// for(String m : myMap.keySet()){
|
|
77
|
+// key = m;
|
|
78
|
+// Map map = myMap.get(key);
|
|
79
|
+// Integer y = myMap.get(key).get(doub);
|
|
80
|
+// System.out.println("Name: " + key + " Seen: " + y);
|
|
81
|
+// System.out.println("____________ ________________");
|
|
82
|
+// System.out.println("____________ ________________");
|
|
83
|
+// for(Object d : map.keySet())
|
|
84
|
+// doub = (Double) d;
|
|
85
|
+// count = (Integer) map.get(doub);
|
|
86
|
+// System.out.println("Price: "+ doub + " Seen: " + count);
|
|
87
|
+// System.out.println("____________ ________________");
|
|
88
|
+// }
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
19
|
95
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
|
96
|
+ String stringPattern = "[;|^|@|*|%|$|#|!]";
|
21
|
97
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
|
98
|
+
|
22
|
99
|
return response;
|
23
|
100
|
}
|
24
|
101
|
|