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