Преглед на файлове

Merge cc7d69ba0b9791297118acff7fda036670b289ca into 23c2c01bf07b924ac9b860fa2771bf6ceb246ad5

Mitch Taylor преди 6 години
родител
ревизия
467f377c44
No account linked to committer's email

+ 15
- 0
src/main/java/io/zipcoder/FoodData/AppleData.java Целия файл

@@ -0,0 +1,15 @@
1
+package io.zipcoder.FoodData;
2
+
3
+import java.util.ArrayList;
4
+
5
+public class AppleData implements FoodDataInterface {
6
+
7
+    public void addNewPriceToList(double priceToAdd) {
8
+        foodPrices.add(priceToAdd);
9
+    }
10
+
11
+    public ArrayList getFoodPrices() {
12
+        return foodPrices;
13
+    }
14
+
15
+}

+ 15
- 0
src/main/java/io/zipcoder/FoodData/BreadData.java Целия файл

@@ -0,0 +1,15 @@
1
+package io.zipcoder.FoodData;
2
+
3
+import java.util.ArrayList;
4
+
5
+public class BreadData implements FoodDataInterface {
6
+
7
+    public void addNewPriceToList(double priceToAdd) {
8
+        foodPrices.add(priceToAdd);
9
+    }
10
+
11
+    public ArrayList getFoodPrices() {
12
+        return foodPrices;
13
+    }
14
+
15
+}

+ 15
- 0
src/main/java/io/zipcoder/FoodData/CookieData.java Целия файл

@@ -0,0 +1,15 @@
1
+package io.zipcoder.FoodData;
2
+
3
+import java.util.ArrayList;
4
+
5
+public class CookieData implements FoodDataInterface{
6
+
7
+    public void addNewPriceToList(double priceToAdd) {
8
+        foodPrices.add(priceToAdd);
9
+    }
10
+
11
+    public ArrayList getFoodPrices() {
12
+        return foodPrices;
13
+    }
14
+
15
+}

+ 13
- 0
src/main/java/io/zipcoder/FoodData/FoodDataInterface.java Целия файл

@@ -0,0 +1,13 @@
1
+package io.zipcoder.FoodData;
2
+
3
+import java.util.ArrayList;
4
+
5
+public interface FoodDataInterface {
6
+
7
+    ArrayList foodPrices = new ArrayList();
8
+
9
+    public void addNewPriceToList(double priceToAdd);
10
+
11
+    public ArrayList getFoodPrices();
12
+
13
+}

+ 15
- 0
src/main/java/io/zipcoder/FoodData/MilkData.java Целия файл

@@ -0,0 +1,15 @@
1
+package io.zipcoder.FoodData;
2
+
3
+import java.util.ArrayList;
4
+
5
+public class MilkData implements FoodDataInterface {
6
+
7
+    public void addNewPriceToList(double priceToAdd) {
8
+        foodPrices.add(priceToAdd);
9
+    }
10
+
11
+    public ArrayList getFoodPrices() {
12
+        return foodPrices;
13
+    }
14
+
15
+}

+ 90
- 1
src/main/java/io/zipcoder/ItemParser.java Целия файл

@@ -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
 }

+ 25
- 0
src/test/java/io/zipcoder/ItemParserTest.java Целия файл

@@ -59,4 +59,29 @@ public class ItemParserTest {
59 59
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60 60
         assertEquals(expected, actual);
61 61
     }
62
+
63
+    @Test
64
+    public void nameFixerTest() {
65
+        String cookieExpected = "Cookies";
66
+        String milkExpected = "Milk";
67
+        String breadExpected = "Bread";
68
+        String applesExpected = "Apples";
69
+        String whatExpected = "What?";
70
+        String cookieActual = itemParser.nameFixer("CO0kie");
71
+        String milkActual = itemParser.nameFixer("MILK");
72
+        String breadActual = itemParser.nameFixer("BREad");
73
+        String applesActual = itemParser.nameFixer("AppLES");
74
+        String whatActual = itemParser.nameFixer("some bullshit");
75
+        Assert.assertEquals(cookieExpected, cookieActual);
76
+        Assert.assertEquals(milkExpected, milkActual);
77
+        Assert.assertEquals(breadExpected, breadActual);
78
+        Assert.assertEquals(applesExpected, applesActual);
79
+        Assert.assertEquals(whatExpected, whatActual);
80
+    }
81
+
82
+    @Test
83
+    public void runProductionInATestEnvironmentLol() {
84
+        itemParser.output();
85
+    }
86
+
62 87
 }