|
@@ -1,10 +1,30 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
|
3
|
+import com.sun.xml.internal.fastinfoset.util.CharArray;
|
|
4
|
+
|
3
|
5
|
import java.util.ArrayList;
|
4
|
6
|
import java.util.Arrays;
|
|
7
|
+import java.util.HashSet;
|
|
8
|
+import java.util.Set;
|
|
9
|
+import java.util.logging.Level;
|
|
10
|
+import java.util.logging.Logger;
|
|
11
|
+import java.util.regex.Matcher;
|
|
12
|
+import java.util.regex.Pattern;
|
5
|
13
|
|
6
|
14
|
public class ItemParser {
|
7
|
15
|
|
|
16
|
+ private static final Logger log = Logger.getLogger( ItemParser.class.getName() );
|
|
17
|
+ private int exceptionCount = 0;
|
|
18
|
+ private Pattern patternAlpha = Pattern.compile("(?<=:)[^##\\s].*");
|
|
19
|
+ private Pattern patternDate = Pattern.compile("\\d/\\d\\d/\\d\\d\\d\\d");
|
|
20
|
+
|
|
21
|
+ public int getExceptionCount() {
|
|
22
|
+ return exceptionCount-1;
|
|
23
|
+ }
|
|
24
|
+
|
|
25
|
+ public void setExceptionCount(int exceptionCount) {
|
|
26
|
+ this.exceptionCount = exceptionCount;
|
|
27
|
+ }
|
8
|
28
|
|
9
|
29
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
10
|
30
|
String stringPattern = "##";
|
|
@@ -12,12 +32,110 @@ public class ItemParser {
|
12
|
32
|
return response;
|
13
|
33
|
}
|
14
|
34
|
|
|
35
|
+ public String toLowerCase(String str){
|
|
36
|
+ StringBuilder inputLineT = new StringBuilder(str);
|
|
37
|
+ for(int i = 0 ; i < inputLineT.length() ; i++) {
|
|
38
|
+ if(inputLineT.charAt(i) >= 65 && inputLineT.charAt(i) <=91)
|
|
39
|
+ { inputLineT.setCharAt(i, (char)(inputLineT.charAt(i)+32)); }
|
|
40
|
+ }
|
|
41
|
+ return inputLineT.toString();
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ public String fixCookies(String str){
|
|
45
|
+ String result ="";
|
|
46
|
+ for (char c: str.toCharArray()) {
|
|
47
|
+ if(c == '0'){result += "o";}
|
|
48
|
+ else if (c != 0) result += c;
|
|
49
|
+ }
|
|
50
|
+ return result;
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+
|
15
|
54
|
public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
55
|
+ ArrayList<String> properties = findKeyValuePairsInRawItemData(toLowerCase(toLowerCase(rawItem)));
|
|
56
|
+ Matcher mName = patternAlpha.matcher(properties.get(0));
|
|
57
|
+ Matcher mPrice = patternAlpha.matcher(properties.get(1));
|
|
58
|
+ Matcher mDescription = patternAlpha.matcher(properties.get(2));
|
|
59
|
+ Matcher mExpiration = patternDate.matcher(properties.get(3));
|
|
60
|
+
|
|
61
|
+ if (mName.find() && mDescription.find() && mPrice.find() && mExpiration.find()) {
|
|
62
|
+ String name = fixCookies(mName.group());
|
|
63
|
+ Double price = Double.parseDouble(mPrice.group());
|
|
64
|
+ String type = mDescription.group();
|
|
65
|
+ String expiration = mExpiration.group();
|
|
66
|
+ return new Item(name, price, type, expiration);
|
|
67
|
+ } else {
|
|
68
|
+ throw new ItemParseException(); }
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ public ArrayList<Item> makeItemList(String rawData){
|
|
72
|
+ ArrayList<String> data = parseRawDataIntoStringArray(rawData);
|
|
73
|
+ ArrayList<Item> items = new ArrayList<Item>();
|
|
74
|
+ int exceptionCount = 1;
|
|
75
|
+ for (String parcel : data) {
|
|
76
|
+ try {
|
|
77
|
+ Item itemObj = parseStringIntoItem(parcel);
|
|
78
|
+ items.add(itemObj);
|
|
79
|
+ } catch (ItemParseException e){
|
|
80
|
+ exceptionCount++;
|
|
81
|
+ continue;
|
|
82
|
+ }
|
|
83
|
+ }
|
|
84
|
+ setExceptionCount(exceptionCount);
|
|
85
|
+ return items;
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ public Set<String> getUniqueNames(ArrayList<Item> list){
|
|
89
|
+ Set<String> set = new HashSet<String>();
|
|
90
|
+ for (Item item : list) {
|
|
91
|
+ set.add(item.getName());
|
|
92
|
+ }
|
|
93
|
+ return set;
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ public int getCountName(String name, ArrayList<Item> itemList){
|
|
97
|
+ int count = 0;
|
|
98
|
+ for (Item item : itemList) {
|
|
99
|
+ if (item.getName().equals(name)) count++;
|
|
100
|
+ }
|
|
101
|
+ return count;}
|
|
102
|
+
|
|
103
|
+ public Set<Double> getUniquePrices(String name, ArrayList<Item> itemList){
|
|
104
|
+ Set<Double> prices = new HashSet<Double>();
|
|
105
|
+ for (Item item: itemList) {
|
|
106
|
+ if(item.getName().equals(name)){
|
|
107
|
+ prices.add(item.getPrice());
|
|
108
|
+ }
|
|
109
|
+ }
|
|
110
|
+ return prices;
|
|
111
|
+ }
|
|
112
|
+
|
|
113
|
+ public int getCountPrice(Double price, String name, ArrayList<Item> itemList){
|
|
114
|
+ int count = 0;
|
|
115
|
+ for (Item item : itemList) {
|
|
116
|
+ if (item.getName().equals(name) && item.getPrice().equals(price)) count++;
|
|
117
|
+ }
|
|
118
|
+ return count;
|
|
119
|
+ }
|
|
120
|
+
|
|
121
|
+ public void printOutput(String rawData){
|
|
122
|
+ ArrayList<Item> itemList = makeItemList(rawData);
|
|
123
|
+ Set<String> nameSet = getUniqueNames(itemList);
|
|
124
|
+ for (String name : nameSet) {
|
|
125
|
+ Set<Double> priceList = getUniquePrices(name, itemList);
|
|
126
|
+ System.out.printf("%-13s %13s %n", "name: " + name, "seen: " + getCountName(name, itemList) + " times");
|
|
127
|
+ System.out.printf("%-13s %13s %n", "=============", "=============");
|
|
128
|
+ for (Double price : priceList) {
|
|
129
|
+ System.out.printf("%-13s %13s %n", "price: " + price,"seen: " + getCountPrice(price, name, itemList) + " times");
|
|
130
|
+ System.out.printf("%-13s %13s %n", "-------------", "-------------");
|
|
131
|
+ }
|
|
132
|
+ System.out.println();
|
|
133
|
+ }
|
|
134
|
+ System.out.printf("%-15s %15s %n","Errors", "seen: "+ getExceptionCount() + " times");
|
17
|
135
|
}
|
18
|
136
|
|
19
|
137
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
|
138
|
+ String stringPattern = "[;|^!%@*]";
|
21
|
139
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
22
|
140
|
return response;
|
23
|
141
|
}
|