|
@@ -1,14 +1,29 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
|
3
|
+import io.zipcoder.FoodData.AppleData;
|
|
4
|
+import io.zipcoder.FoodData.BreadData;
|
|
5
|
+import io.zipcoder.FoodData.CookieData;
|
|
6
|
+import io.zipcoder.FoodData.MilkData;
|
|
7
|
+
|
|
8
|
+import java.io.File;
|
|
9
|
+import java.io.IOException;
|
3
|
10
|
import java.util.ArrayList;
|
4
|
11
|
import java.util.Arrays;
|
|
12
|
+import java.util.Scanner;
|
5
|
13
|
|
6
|
14
|
public class ItemParser {
|
7
|
15
|
|
|
16
|
+ private int errorCounter = 0;
|
|
17
|
+ private MilkData milkData = new MilkData();
|
|
18
|
+ private CookieData cookieData = new CookieData();
|
|
19
|
+ private BreadData breadData = new BreadData();
|
|
20
|
+ private AppleData appleData = new AppleData();
|
|
21
|
+ String fileName = "RawData.txt";
|
8
|
22
|
|
9
|
23
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
|
24
|
+ String rawDataFix = rawData.substring(0, rawData.length() - 2);
|
10
|
25
|
String stringPattern = "##";
|
11
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
|
26
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawDataFix);
|
12
|
27
|
return response;
|
13
|
28
|
}
|
14
|
29
|
|
|
@@ -26,6 +41,80 @@ public class ItemParser {
|
26
|
41
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
42
|
}
|
28
|
43
|
|
|
44
|
+ public String nameFixer(String name) {
|
|
45
|
+ try {
|
|
46
|
+ char firstLetter = name.toLowerCase().charAt(0);
|
|
47
|
+ if (firstLetter == 'm') return "Milk";
|
|
48
|
+ if (firstLetter == 'c') return "Cookies";
|
|
49
|
+ if (firstLetter == 'a') return "Apples";
|
|
50
|
+ if (firstLetter == 'b') return "Bread";
|
|
51
|
+ else return "What?";
|
|
52
|
+ } catch (Exception e) {
|
|
53
|
+ errorCounter++;
|
|
54
|
+ return "What??";
|
|
55
|
+ }
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+ public String fileToString(String filename) {
|
|
59
|
+ StringBuilder result = new StringBuilder("");
|
|
60
|
+ ClassLoader classLoader = ItemParser.class.getClassLoader();
|
|
61
|
+ File file = new File(classLoader.getResource(filename).getFile());
|
|
62
|
+ try (Scanner scanner = new Scanner(file)) {
|
|
63
|
+ while (scanner.hasNextLine()) {
|
|
64
|
+ String line = scanner.nextLine();
|
|
65
|
+ result.append(line).append("\n");
|
|
66
|
+ }
|
|
67
|
+ scanner.close();
|
|
68
|
+ } catch (IOException e) {
|
|
69
|
+ e.printStackTrace();
|
|
70
|
+ }
|
|
71
|
+ return result.toString();
|
|
72
|
+ }
|
29
|
73
|
|
|
74
|
+ public void doEverythingBesidesOutput() {
|
|
75
|
+ String input = fileToString(fileName);
|
|
76
|
+ ArrayList<String> arrayList = parseRawDataIntoStringArray(input);
|
|
77
|
+ for (String anArrayList : arrayList) {
|
|
78
|
+ // everything happens here lol
|
|
79
|
+ String[] temp = anArrayList.split("([;:|^@!*%])");
|
|
80
|
+ if (!temp[1].equals("") && !temp[3].equals("") && !temp[5].equals("") && !temp[7].equals("")) {
|
|
81
|
+ if (nameFixer(temp[1]).equals("Cookies")) {
|
|
82
|
+ cookieData.addNewPriceToList(Double.parseDouble(temp[3]));
|
|
83
|
+ }
|
|
84
|
+ if (nameFixer(temp[1]).equals("Milk")) {
|
|
85
|
+ milkData.addNewPriceToList(Double.parseDouble(temp[3]));
|
|
86
|
+ }
|
|
87
|
+ if (nameFixer(temp[1]).equals("Bread")) {
|
|
88
|
+ breadData.addNewPriceToList(Double.parseDouble(temp[3]));
|
|
89
|
+ }
|
|
90
|
+ if (nameFixer(temp[1]).equals("Apples")) {
|
|
91
|
+ appleData.addNewPriceToList(Double.parseDouble(temp[3]));
|
|
92
|
+ }
|
|
93
|
+ } else {
|
|
94
|
+ errorCounter++;
|
|
95
|
+ }
|
|
96
|
+ }
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+ public void output() {
|
|
100
|
+ doEverythingBesidesOutput();
|
|
101
|
+ for (int i = 0; i < milkData.getFoodPrices().size(); i++) {
|
|
102
|
+ System.out.println(String.format("%-15s%10s%15s%2d%5s", "name:", "Milk", "seen: ", milkData.getFoodPrices().size(), " times"));
|
|
103
|
+ System.out.println("================================================");
|
|
104
|
+ }
|
|
105
|
+ for (int j = 0; j < cookieData.getFoodPrices().size(); j++) {
|
|
106
|
+ System.out.println(String.format("%-15s%10s%15s%2d%5s", "name:", "Cookies", "seen: ", cookieData.getFoodPrices().size(), " times"));
|
|
107
|
+ System.out.println("================================================");
|
|
108
|
+ }
|
|
109
|
+ for (int k = 0; k < breadData.getFoodPrices().size(); k++) {
|
|
110
|
+ System.out.println(String.format("%-15s%10s%15s%2d%5s", "name:", "Bread", "seen: ", breadData.getFoodPrices().size(), " times"));
|
|
111
|
+ System.out.println("================================================");
|
|
112
|
+ }
|
|
113
|
+ for (int l = 0; l < appleData.getFoodPrices().size(); l++) {
|
|
114
|
+ System.out.println(String.format("%-15s%10s%15s%2d%5s", "name:", "Apple", "seen: ", appleData.getFoodPrices().size(), " times"));
|
|
115
|
+ System.out.println("================================================");
|
|
116
|
+ }
|
|
117
|
+ System.out.println("Exceptions: " + errorCounter);
|
|
118
|
+ }
|
30
|
119
|
|
31
|
120
|
}
|