|
@@ -4,6 +4,8 @@ import com.sun.xml.internal.fastinfoset.util.CharArray;
|
4
|
4
|
|
5
|
5
|
import java.util.ArrayList;
|
6
|
6
|
import java.util.Arrays;
|
|
7
|
+import java.util.HashSet;
|
|
8
|
+import java.util.Set;
|
7
|
9
|
import java.util.logging.Level;
|
8
|
10
|
import java.util.logging.Logger;
|
9
|
11
|
import java.util.regex.Matcher;
|
|
@@ -12,7 +14,17 @@ import java.util.regex.Pattern;
|
12
|
14
|
public class ItemParser {
|
13
|
15
|
|
14
|
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");
|
15
|
20
|
|
|
21
|
+ public int getExceptionCount() {
|
|
22
|
+ return exceptionCount-1;
|
|
23
|
+ }
|
|
24
|
+
|
|
25
|
+ public void setExceptionCount(int exceptionCount) {
|
|
26
|
+ this.exceptionCount = exceptionCount;
|
|
27
|
+ }
|
16
|
28
|
|
17
|
29
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
18
|
30
|
String stringPattern = "##";
|
|
@@ -21,34 +33,33 @@ public class ItemParser {
|
21
|
33
|
}
|
22
|
34
|
|
23
|
35
|
public String toLowerCase(String str){
|
24
|
|
-
|
25
|
36
|
StringBuilder inputLineT = new StringBuilder(str);
|
26
|
|
-
|
27
|
|
- for(int i = 0 ; i < inputLineT.length() ; i++)
|
28
|
|
- {
|
|
37
|
+ for(int i = 0 ; i < inputLineT.length() ; i++) {
|
29
|
38
|
if(inputLineT.charAt(i) >= 65 && inputLineT.charAt(i) <=91)
|
30
|
|
- {
|
31
|
|
- inputLineT.setCharAt(i, (char)(inputLineT.charAt(i)+32));
|
32
|
|
- }
|
|
39
|
+ { inputLineT.setCharAt(i, (char)(inputLineT.charAt(i)+32)); }
|
33
|
40
|
}
|
34
|
41
|
return inputLineT.toString();
|
35
|
42
|
}
|
36
|
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
|
+
|
37
|
53
|
|
38
|
54
|
public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
39
|
|
- ArrayList<String> properties = findKeyValuePairsInRawItemData(toLowerCase(toLowerCase(rawItem)));
|
40
|
|
- Pattern patternAlpha = Pattern.compile("(?<=:)[^\\s].*");
|
41
|
|
- Pattern patternDate = Pattern.compile("\\d/\\d\\d/\\d\\d\\d\\d");
|
42
|
|
- Pattern patternDouble = Pattern.compile("\\d.\\d\\d");
|
|
55
|
+ ArrayList<String> properties = findKeyValuePairsInRawItemData(toLowerCase(toLowerCase(rawItem)));
|
43
|
56
|
Matcher mName = patternAlpha.matcher(properties.get(0));
|
44
|
|
- Matcher mPrice = patternDouble.matcher(properties.get(1));
|
|
57
|
+ Matcher mPrice = patternAlpha.matcher(properties.get(1));
|
45
|
58
|
Matcher mDescription = patternAlpha.matcher(properties.get(2));
|
46
|
59
|
Matcher mExpiration = patternDate.matcher(properties.get(3));
|
47
|
|
- int exceptionCount=0;
|
48
|
|
-
|
49
|
60
|
|
50
|
61
|
if (mName.find() && mDescription.find() && mPrice.find() && mExpiration.find()) {
|
51
|
|
- String name = mName.group();
|
|
62
|
+ String name = fixCookies(mName.group());
|
52
|
63
|
Double price = Double.parseDouble(mPrice.group());
|
53
|
64
|
String type = mDescription.group();
|
54
|
65
|
String expiration = mExpiration.group();
|
|
@@ -57,7 +68,71 @@ public class ItemParser {
|
57
|
68
|
throw new ItemParseException(); }
|
58
|
69
|
}
|
59
|
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
|
+ }
|
60
|
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("%-15s %15s %n", "name: " + name, "seen: " + getCountName(name, itemList) + " times");
|
|
127
|
+ System.out.printf("%-15s %15s %n", "==============", "==============");
|
|
128
|
+ for (Double price : priceList) {
|
|
129
|
+ System.out.printf("%-15s %15s %n", "price: " + price,"seen: " + getCountPrice(price, name, itemList) + " times");
|
|
130
|
+ System.out.printf("%-15s %15s %n", "--------------", "--------------");
|
|
131
|
+ }
|
|
132
|
+ System.out.println();
|
|
133
|
+ }
|
|
134
|
+ System.out.printf("%-15s %15s %n","Errors", "seen: "+ getExceptionCount() + " times");
|
|
135
|
+ }
|
61
|
136
|
|
62
|
137
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
63
|
138
|
String stringPattern = "[;|^!%@*]";
|