-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathMain.java
More file actions
252 lines (210 loc) · 8.76 KB
/
Main.java
File metadata and controls
252 lines (210 loc) · 8.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public String readRawDataToString(){
ClassLoader classLoader = getClass().getClassLoader();
String result = null;
try {
result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) throws Exception{
Main newMain = new Main();
String output = newMain.readRawDataToString();
System.out.println(newMain.getList(output));
}
public String findAlphabetical() throws Exception {
String jerkText = readRawDataToString();
Pattern pattern = Pattern.compile("/^[A-Z]",Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(jerkText);
return null;
}
public String getList(String input) {
Pattern pattern = Pattern.compile("##");
Matcher matcher = pattern.matcher(input);
String result = matcher.replaceAll("\n");
return result;
}
public String correctSeparator(){
String correctMe = readRawDataToString();
Pattern pattern = Pattern.compile("[!@^%*]");
Matcher matcher = pattern.matcher(correctMe);
return matcher.replaceAll(";");
}
public String findSeparatorCorrected(){
String list = correctSeparator();
Pattern pattern = Pattern.compile(";");
Matcher matcher = pattern.matcher(list);
return matcher.replaceAll("\n");
}
public String reformatAfterCorrectSeparator(){
String list = findSeparatorCorrected();
Pattern pattern = Pattern.compile("2016");
Matcher matcher = pattern.matcher(list);
return matcher.replaceAll("2016\n");
}
public Integer countMilk323(){
List<Integer> indexes = new ArrayList<>();
String jerkText = readRawDataToString();
Pattern pattern = Pattern.compile("milk",Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(jerkText);
while(matcher.find()){
indexes.add(matcher.start());
}
return indexes.size();
}
public Integer countCookies(){
List<Integer> indexes = new ArrayList<>();
String jerkText = readRawDataToString();
Pattern pattern = Pattern.compile("c[oO0][oO0]kies",Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(jerkText);
while(matcher.find()){
indexes.add(matcher.start());
}
return indexes.size();
}
public Integer countBread(){
List<Integer> indexes = new ArrayList<>();
String jerkText = readRawDataToString();
Pattern pattern = Pattern.compile("bread",Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(jerkText);
while(matcher.find()){
indexes.add(matcher.start());
}
return indexes.size();
}
public Integer countApple(){
List<Integer> indexes = new ArrayList<>();
String jerkText = readRawDataToString();
Pattern pattern = Pattern.compile("apples",Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(jerkText);
while(matcher.find()){
indexes.add(matcher.start());
}
return indexes.size();
}
//-------------Formatting String Into Correct List----------------
public String changeMilk (String input) {
try {
Pattern pattern = Pattern.compile("milk", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
String milk = matcher.replaceAll("Milk");
return milk;
} catch (Exception e) {
throw new UnsupportedOperationException();
}
}
public String changeBread (String input) {
try {
Pattern pattern = Pattern.compile("bread", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
String milk = matcher.replaceAll("Bread");
return milk;
} catch (Exception e) {
throw new UnsupportedOperationException();
}
}
public String changeCookies (String input) {
try {
Pattern pattern = Pattern.compile("c[o0][o0]kies", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
String milk = matcher.replaceAll("Cookies");
return milk;
} catch (Exception e) {
throw new UnsupportedOperationException();
}
}
public String changeApples (String input) {
try {
Pattern pattern = Pattern.compile("apples", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
String milk = matcher.replaceAll("Apples");
return milk;
} catch (Exception e) {
throw new UnsupportedOperationException();
}
}
public String nameChange(String input) {
try {
Pattern pattern = Pattern.compile("name", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
String milk = matcher.replaceAll("Name");
return milk;
} catch (Exception e) {
throw new UnsupportedOperationException();
}
}
public String priceChange(String input) {
try {
Pattern pattern = Pattern.compile("price", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
String milk = matcher.replaceAll("Price");
return milk;
} catch (Exception e) {
throw new UnsupportedOperationException();
}
}
public String readyForFormatting() {
String result = getList(correctSeparator());
String result1 = changeApples(result);
String result2 = changeBread(result1);
String result3 = changeCookies(result2);
String result4 = changeMilk(result3);
String result5 = nameChange(result4);
String result6 = priceChange(result5);
return result6;
}
public String doingTheFormatting() {
String result =
"name: Milk seen: " + findGroceries("milk") + " times\n" +
"============= =============\n" +
"Price: 3.23 seen: " + findGroceries("milk;price:3.23") + " times\n" +
"------------- -------------\n" +
"Price: 1.23 seen: " + findGroceries("milk;price:1.23") + " times\n\n" +
"name: Bread seen: " + findGroceries("bread") + " times\n" +
"============= =============\n" +
"Price: 1.23 seen: " + findGroceries("bread") + " times\n" +
"------------- -------------\n\n" +
"name: Cookies seen: " + findGroceries("cookies") + " times\n" +
"============= =============\n" +
"Price: 2.25 seen: " + findGroceries("cookies") + " times\n" +
"------------- -------------\n\n" +
"name: Apples seen: " + (findGroceries("apples;price:0.25") + findGroceries("apples;price:0.23")) + " times\n" +
"============= =============\n" +
"Price: 0.25 seen: " + findGroceries("price:0.25") + " times\n" +
"------------- -------------\n" +
"Price: 0.23 seen: " + findGroceries("price:0.23") + " times\n\n" +
"Errors seen: " + countingErrors() + " times";
return result;
}
// we can use a "scanner" something to check when the (';' <- those bois) come up
public int findGroceries(String input) {
Integer holdingValue = 0;
Boolean checkVal = false;
Pattern pattern = Pattern.compile(input, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(readyForFormatting());
while(!checkVal) {
if (!matcher.find()) {
checkVal = true;
continue;
}
holdingValue++;
}
return holdingValue;
}
public int countingErrors() {
int counter = 0;
// beware
// counter += findGroceries("apples") - (findGroceries("apples;price:0.25") + findGroceries("apples;price:0.23"));
counter += findGroceries("Name:;");
counter += findGroceries("milk") - (findGroceries("milk;price:3.23") + findGroceries("milk;price:1.23"));
return counter;
}
}