|
@@ -1,31 +1,126 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
3
|
|
-import java.util.ArrayList;
|
4
|
|
-import java.util.Arrays;
|
|
3
|
+
|
|
4
|
+import com.sun.tools.javac.jvm.Items;
|
|
5
|
+
|
|
6
|
+import java.io.*;
|
|
7
|
+import java.util.*;
|
|
8
|
+import java.util.regex.Matcher;
|
|
9
|
+import java.util.regex.Pattern;
|
|
10
|
+
|
|
11
|
+import static io.zipcoder.LeetUtils.capitalizeMe;
|
|
12
|
+import static io.zipcoder.LeetUtils.fullManipulate;
|
5
|
13
|
|
6
|
14
|
public class ItemParser {
|
7
|
15
|
|
|
16
|
+ public boolean checkForValue(String itemString) {
|
|
17
|
+ Pattern pattern = Pattern.compile("(:;)|(:%)|(:\\*)|(:@)|(:!)");
|
|
18
|
+ Matcher m = pattern.matcher(itemString);
|
|
19
|
+
|
|
20
|
+ return m.find();
|
|
21
|
+ }
|
8
|
22
|
|
9
|
23
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
10
|
24
|
String stringPattern = "##";
|
11
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
12
|
|
- return response;
|
|
25
|
+ return splitStringWithRegexPattern(stringPattern , rawData);
|
13
|
26
|
}
|
14
|
27
|
|
15
|
28
|
public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
29
|
+
|
|
30
|
+ if (checkForValue(rawItem)) {
|
|
31
|
+ throw new ItemParseException();
|
|
32
|
+ } else {
|
|
33
|
+ String[] values = allValues(rawItem.toLowerCase()).split(",");
|
|
34
|
+ return new Item(fullManipulate(values[0]), Double.parseDouble(values[1]), fullManipulate(values[2]), values[3]);
|
|
35
|
+ }
|
|
36
|
+ }
|
|
37
|
+
|
|
38
|
+ public String allValues(String rawItem) {
|
|
39
|
+ StringBuilder sb = new StringBuilder();
|
|
40
|
+
|
|
41
|
+ ArrayList<String> pairs = findKeyValuePairsInRawItemData(rawItem);
|
|
42
|
+ for (String s: pairs ) {
|
|
43
|
+ sb.append((sb.toString().equals("")) ? returnValue(s) : "," + returnValue(s));
|
|
44
|
+ }
|
|
45
|
+ return sb.toString();
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ public String returnValue (String pair) {
|
|
49
|
+ return pair.substring(pair.lastIndexOf(":") + 1);
|
17
|
50
|
}
|
18
|
51
|
|
19
|
52
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
21
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
22
|
|
- return response;
|
|
53
|
+ String stringPattern = "[;^%*@!]";
|
|
54
|
+ return splitStringWithRegexPattern(stringPattern , rawItem);
|
23
|
55
|
}
|
24
|
56
|
|
25
|
57
|
private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
26
|
|
- return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
|
58
|
+ return new ArrayList<>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
59
|
}
|
28
|
60
|
|
|
61
|
+ public static void main(String[] args) throws FileNotFoundException, ItemParseException {
|
|
62
|
+ ItemParser ip = new ItemParser();
|
|
63
|
+ String rawMultipleItems = "naMe:C00kies;price:3.23;type:Food;expiration:1/25/2016##"
|
|
64
|
+ +"naME:;price:1.23;type:Food;expiration:1/02/2016##"
|
|
65
|
+ +"NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##"
|
|
66
|
+ +"NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##";
|
|
67
|
+
|
|
68
|
+ ip.printToFile("list.txt" ,rawMultipleItems);
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+
|
29
|
72
|
|
|
73
|
+ public void printToFile(String fileName, String rawItemData) throws FileNotFoundException, ItemParseException {
|
|
74
|
+ // Output to file
|
|
75
|
+ ArrayList<String> allItemsRaw = parseRawDataIntoStringArray(rawItemData);
|
|
76
|
+ ArrayList<Item> allItemObjects = new ArrayList<>();
|
|
77
|
+ int errors = 0;
|
|
78
|
+
|
|
79
|
+ for (String s:allItemsRaw) {
|
|
80
|
+ if (checkForValue(s)) {
|
|
81
|
+ errors++;
|
|
82
|
+ } else {
|
|
83
|
+ allItemObjects.add(parseStringIntoItem(s));
|
|
84
|
+ }
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ HashMap<String, SameItems> itemCounters = sameItemCounter(allItemObjects);
|
|
88
|
+
|
|
89
|
+ PrintStream file = new PrintStream(new File(fileName));
|
|
90
|
+ PrintStream console = System.out;
|
|
91
|
+
|
|
92
|
+ System.setOut(file);
|
|
93
|
+
|
|
94
|
+ StringBuilder resultsOutput = new StringBuilder();
|
|
95
|
+
|
|
96
|
+ resultsOutput.append("Tariq's Messed up Grocery List...\n\n");
|
|
97
|
+
|
|
98
|
+ for (SameItems s : itemCounters.values()) {
|
|
99
|
+ resultsOutput.append(String.format("name: %8s seen: %d", capitalizeMe(s.getName()), s.getCount()));
|
|
100
|
+// resultsOutput.append(String.format("%s %s", item.getName(), item.getPrice()));
|
|
101
|
+ }
|
|
102
|
+ System.out.println(resultsOutput.toString());
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ public HashMap<String, SameItems> sameItemCounter(ArrayList<Item> allItems) {
|
|
106
|
+
|
|
107
|
+ HashMap<String, SameItems> itemCounters = new HashMap<>();
|
|
108
|
+ for (Item i: allItems) {
|
|
109
|
+ SameItems temp;
|
|
110
|
+ if (!itemCounters.keySet().contains(i.getName())) {
|
|
111
|
+ temp = new SameItems(i.getName());
|
|
112
|
+ temp.getPrices().addNewPrice(i.getPrice());
|
|
113
|
+ itemCounters.put(i.getName(), temp);
|
|
114
|
+ } else {
|
|
115
|
+
|
|
116
|
+ temp = itemCounters.get(i.getName());
|
|
117
|
+ temp.getPrices().addNewPrice(i.getPrice());
|
|
118
|
+ temp.setCount(temp.getCount()+1);
|
|
119
|
+ itemCounters.put(i.getName(),temp);
|
|
120
|
+ }
|
|
121
|
+ }
|
|
122
|
+
|
|
123
|
+ return itemCounters;
|
|
124
|
+ }
|
30
|
125
|
|
31
|
126
|
}
|