#8 JenniferChao4 - JerkSON (almost finished...)

오픈
JenniferChao4 JenniferChao4/JerkSON-Parser:master 에서 master 로 2 commits 를 머지하려 합니다
3개의 변경된 파일278개의 추가작업 그리고 23개의 파일을 삭제
  1. 176
    11
      src/main/java/io/zipcoder/ItemParser.java
  2. 1
    0
      src/main/resources/TestFile.txt
  3. 101
    12
      src/test/java/io/zipcoder/ItemParserTest.java

+ 176
- 11
src/main/java/io/zipcoder/ItemParser.java 파일 보기

@@ -1,31 +1,196 @@
1 1
 package io.zipcoder;
2 2
 
3
-import java.util.ArrayList;
4
-import java.util.Arrays;
3
+import java.io.BufferedReader;
4
+import java.io.File;
5
+import java.io.FileReader;
6
+import java.io.IOException;
7
+import java.lang.reflect.Array;
8
+import java.util.*;
9
+import java.util.regex.Matcher;
10
+import java.util.regex.Pattern;
5 11
 
6 12
 public class ItemParser {
7 13
 
14
+    private int numberOfExceptions;
8 15
 
9
-    public ArrayList<String> parseRawDataIntoStringArray(String rawData){
16
+    public String readFromFile(String filePath) {
17
+        try {
18
+            File file = new File(filePath);
19
+            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
20
+            String stringFile = "";
21
+            String line;
22
+            while ((line = bufferedReader.readLine()) != null) {
23
+                stringFile += line + "\n";
24
+            }
25
+            bufferedReader.close();
26
+            return stringFile.trim();
27
+        } catch (IOException e) {
28
+            e.printStackTrace();
29
+            return null;
30
+        }
31
+    }
32
+
33
+    public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
10 34
         String stringPattern = "##";
11
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
35
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
12 36
         return response;
13 37
     }
14 38
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
39
+    public Item parseStringIntoItem(String rawItem) throws ItemParseException {
40
+        List<String> keyValues = findKeyValuePairsInRawItemData(rawItem);
41
+
42
+        String name = findValue(keyValues.get(0));
43
+        double price = Double.valueOf(findValue(keyValues.get(1)));
44
+        String type = findValue(keyValues.get(2));
45
+        String expiration = findValue(keyValues.get(3));
46
+
47
+        return new Item(convertZeroTo0(name), price, type, expiration);
48
+    }
49
+
50
+    public List<Item> parseRawDataIntoItems(String rawData) {
51
+        List<Item> itemList = new ArrayList<>();
52
+
53
+        List<String> items = parseRawDataIntoStringArray(rawData);
54
+
55
+        for (String item : items) {
56
+            try {
57
+                itemList.add(parseStringIntoItem(item));
58
+            } catch (ItemParseException e) {
59
+                numberOfExceptions++;
60
+            }
61
+        }
62
+        return itemList;
63
+    }
64
+
65
+    public String findValue(String keyValue) throws ItemParseException {
66
+        List<String> keyAndValue = splitStringWithRegexPattern(":", keyValue);
67
+
68
+        if (keyAndValue.size() > 1) {
69
+            return convertToLowerCase(keyAndValue.get(1));
70
+        } else {
71
+            throw new ItemParseException();
72
+        }
73
+    }
74
+
75
+    public Map<String, Integer> itemCounts(List<Item> items) {
76
+        Map<String, Integer> itemMap = new HashMap<>();
77
+
78
+        for (Item item : items) {
79
+            if (!itemMap.containsKey(item.getName())) {
80
+                itemMap.put(item.getName(), 1);
81
+            } else {
82
+                itemMap.put(item.getName(), itemMap.get(item.getName()) + 1);
83
+            }
84
+        }
85
+
86
+        return itemMap;
87
+    }
88
+
89
+    public Map<Map<String, Double>, Integer> priceCounts(List<Item> items) {
90
+        Map<String, Double> namePrice = new HashMap<>();
91
+        Map<Map<String, Double>, Integer> itemMap = new HashMap<>();
92
+
93
+        for (Item item : items) {
94
+            if (!namePrice.containsKey(item.getName())) {
95
+                namePrice.put(item.getName(), item.getPrice());
96
+            }
97
+        }
98
+        // im tired
99
+        return itemMap;
100
+    }
101
+
102
+    public void printOutput(Map<String, Integer> itemMap) {
103
+        for (String name : itemMap.keySet()) {
104
+            System.out.println(String.format("name: %7s \t\t seen: %s times", capitalizeFirst(name), itemMap.get(name)));
105
+            System.out.println("============= \t\t =============");
106
+        }
107
+        System.out.println(String.format("\nErrors \t\t\t\t seen: %s times", getNumberOfExceptions()));
17 108
     }
18 109
 
19
-    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
21
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
110
+    public String convertToLowerCase(String input) {
111
+        String output = "";
112
+
113
+        List<String> upperAlpha = splitStringWithRegexPattern("", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
114
+        List<String> lowerAlpha = splitStringWithRegexPattern("", "abcdefghijklmnopqrstuvwxyz");
115
+        ArrayList<String> stringCharacters = stringToArrayList(input);
116
+
117
+        for (int i = 0; i < stringCharacters.size(); i++) {
118
+            for (int j = 0; j < upperAlpha.size(); j++) {
119
+                if (stringCharacters.get(i).equals(upperAlpha.get(j))) {
120
+                    stringCharacters.set(i, lowerAlpha.get(j));
121
+                }
122
+            }
123
+            output += stringCharacters.get(i);
124
+        }
125
+        return output;
126
+    }
127
+
128
+    public String convertZeroTo0(String input) {
129
+        String output = "";
130
+        ArrayList<String> stringCharacters = stringToArrayList(input);
131
+
132
+        for (int i = 0; i < stringCharacters.size(); i++) {
133
+            if (stringCharacters.get(i).equals("0")) {
134
+                stringCharacters.set(i, "o");
135
+            }
136
+            output += stringCharacters.get(i);
137
+        }
138
+        return output;
139
+    }
140
+
141
+    public String capitalizeFirst(String input) {
142
+        String output = "";
143
+        List<String> upperAlpha = splitStringWithRegexPattern("", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
144
+        List<String> lowerAlpha = splitStringWithRegexPattern("", "abcdefghijklmnopqrstuvwxyz");
145
+        ArrayList<String> stringCharacters = stringToArrayList(input);
146
+
147
+        for (int i = 0; i < stringCharacters.size(); i++) {
148
+            for (int j = 0; j < upperAlpha.size(); j++) {
149
+                if (stringCharacters.get(0).equals(lowerAlpha.get(j))) {
150
+                    stringCharacters.set(0, upperAlpha.get(j));
151
+                }
152
+            }
153
+            output += stringCharacters.get(i);
154
+        }
155
+        return output;
156
+    }
157
+
158
+    public ArrayList<String> stringToArrayList(String input) {
159
+        ArrayList<String> stringCharacters = new ArrayList<>();
160
+
161
+        Pattern pattern = Pattern.compile(".");
162
+        Matcher matcher = pattern.matcher(input);
163
+
164
+        while (matcher.find()) {
165
+            stringCharacters.add(matcher.group());
166
+        }
167
+
168
+        return stringCharacters;
169
+    }
170
+
171
+    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
172
+        String stringPattern = "[^a-zA-Z0-9\\:\\.\\/]";
173
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
22 174
         return response;
23 175
     }
24 176
 
25
-    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
26
-        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
177
+    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
178
+        return new ArrayList<>(Arrays.asList(inputString.split(stringPattern)));
27 179
     }
28 180
 
181
+    public int getNumberOfExceptions() {
182
+        return numberOfExceptions;
183
+    }
184
+
185
+    public static void main(String[] args) {
186
+        ItemParser itemParser = new ItemParser();
187
+
188
+        String rawData = itemParser.readFromFile("src/main/resources/RawData.txt");
29 189
 
190
+        List<Item> items = itemParser.parseRawDataIntoItems(rawData);
30 191
 
192
+        Map<String, Integer> itemMap = itemParser.itemCounts(items);
193
+
194
+        itemParser.printOutput(itemMap);
195
+    }
31 196
 }

+ 1
- 0
src/main/resources/TestFile.txt 파일 보기

@@ -0,0 +1 @@
1
+meow meow

+ 101
- 12
src/test/java/io/zipcoder/ItemParserTest.java 파일 보기

@@ -5,29 +5,32 @@ import org.junit.Before;
5 5
 import org.junit.Test;
6 6
 
7 7
 import java.util.ArrayList;
8
+import java.util.Map;
8 9
 
9 10
 import static org.junit.Assert.*;
10 11
 
11 12
 public class ItemParserTest {
12 13
 
13
-    private String rawSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
14
+    private String rawSingleItem = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
14 15
 
15 16
     private String rawSingleItemIrregularSeperatorSample = "naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##";
16 17
 
17
-    private String rawBrokenSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
18
+    private String rawBrokenSingleItem = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
19
+
20
+    private String rawBrokenSingleItem1 = "naMe:Milk;price:3.23;type:;expiration:1/25/2016##";
18 21
 
19 22
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
20
-                                      +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
21
-                                      +"NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##";
23
+            + "naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
24
+            + "NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##";
22 25
     private ItemParser itemParser;
23 26
 
24 27
     @Before
25
-    public void setUp(){
28
+    public void setUp() {
26 29
         itemParser = new ItemParser();
27 30
     }
28 31
 
29 32
     @Test
30
-    public void parseRawDataIntoStringArrayTest(){
33
+    public void parseRawDataIntoStringArrayTest() {
31 34
         Integer expectedArraySize = 3;
32 35
         ArrayList<String> items = itemParser.parseRawDataIntoStringArray(rawMultipleItems);
33 36
         Integer actualArraySize = items.size();
@@ -35,28 +38,114 @@ public class ItemParserTest {
35 38
     }
36 39
 
37 40
     @Test
38
-    public void parseStringIntoItemTest() throws ItemParseException{
39
-        Item expected = new Item("milk", 3.23, "food","1/25/2016");
41
+    public void parseStringIntoItemTest() throws ItemParseException {
42
+        Item expected = new Item("milk", 3.23, "food", "1/25/2016");
40 43
         Item actual = itemParser.parseStringIntoItem(rawSingleItem);
41 44
         assertEquals(expected.toString(), actual.toString());
42 45
     }
43 46
 
47
+//    @Test(expected = ItemParseException.class)
48
+//    public void parseBrokenStringIntoItemTest() throws ItemParseException{
49
+//        itemParser.parseStringIntoItem(rawBrokenSingleItem);
50
+//    }
51
+
44 52
     @Test(expected = ItemParseException.class)
45
-    public void parseBrokenStringIntoItemTest() throws ItemParseException{
46
-        itemParser.parseStringIntoItem(rawBrokenSingleItem);
53
+    public void parseBrokenStringIntoItemTest1() throws ItemParseException {
54
+        itemParser.parseStringIntoItem(rawBrokenSingleItem1);
47 55
     }
48 56
 
49 57
     @Test
50
-    public void findKeyValuePairsInRawItemDataTest(){
58
+    public void findKeyValuePairsInRawItemDataTest() {
51 59
         Integer expected = 4;
52 60
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItem).size();
53 61
         assertEquals(expected, actual);
54 62
     }
55 63
 
56 64
     @Test
57
-    public void findKeyValuePairsInRawItemDataTestIrregular(){
65
+    public void findKeyValuePairsInRawItemDataTestIrregular() {
58 66
         Integer expected = 4;
59 67
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60 68
         assertEquals(expected, actual);
61 69
     }
70
+
71
+    @Test
72
+    public void readFromFile() {
73
+        String expected = "meow meow";
74
+        String actual = itemParser.readFromFile("src/main/resources/TestFile.txt");
75
+
76
+        Assert.assertEquals(expected, actual);
77
+    }
78
+
79
+    @Test
80
+    public void parseRawDataIntoItemsTest() {
81
+        int expected = 3;
82
+        int actual = itemParser.parseRawDataIntoItems(rawMultipleItems).size();
83
+
84
+        Assert.assertEquals(expected, actual);
85
+    }
86
+
87
+    @Test
88
+    public void findValue() throws ItemParseException {
89
+        String expected = "milk";
90
+        String actual = itemParser.findValue(itemParser.findKeyValuePairsInRawItemData(rawSingleItem).get(0));
91
+
92
+        Double expected1 = 3.23;
93
+        Double actual1 = Double.valueOf(itemParser.findValue(itemParser.findKeyValuePairsInRawItemData(rawSingleItem).get(1)));
94
+
95
+        String expected2 = "food";
96
+        String actual2 = itemParser.findValue(itemParser.findKeyValuePairsInRawItemData(rawSingleItem).get(2));
97
+
98
+        String expected3 = "1/25/2016";
99
+        String actual3 = itemParser.findValue(itemParser.findKeyValuePairsInRawItemData(rawSingleItem).get(3));
100
+
101
+        Assert.assertEquals(expected, actual);
102
+        Assert.assertEquals(expected1, actual1);
103
+        Assert.assertEquals(expected2, actual2);
104
+        Assert.assertEquals(expected3, actual3);
105
+    }
106
+
107
+    @Test
108
+    public void countItem() {
109
+        Map<String, Integer> itemMap = itemParser.itemCounts(itemParser.parseRawDataIntoItems(rawMultipleItems));
110
+
111
+        int expected = 2;
112
+        int actual = itemMap.get("bread");
113
+
114
+        Assert.assertEquals(expected, actual);
115
+    }
116
+
117
+    @Test
118
+    public void convertToLowerCase() {
119
+        String expected = "meow meow meow";
120
+        String actual = itemParser.convertToLowerCase("Meow mEOW meOw");
121
+
122
+        Assert.assertEquals(expected, actual);
123
+    }
124
+
125
+    @Test
126
+    public void convertZeroTo0() {
127
+        String expected = "meow";
128
+        String actual = itemParser.convertZeroTo0("me0w");
129
+
130
+        Assert.assertEquals(expected, actual);
131
+    }
132
+
133
+    @Test
134
+    public void capitalizeFirst() {
135
+        String expected = "Meow";
136
+        String actual = itemParser.capitalizeFirst("meow");
137
+
138
+        Assert.assertEquals(expected, actual);
139
+    }
140
+
141
+    @Test
142
+    public void stringToArrayList() {
143
+        ArrayList<String> expected = new ArrayList<>();
144
+        expected.add("a");
145
+        expected.add("b");
146
+        expected.add("c");
147
+        ArrayList<String> actual = itemParser.stringToArrayList("abc");
148
+
149
+        Assert.assertEquals(expected, actual);
150
+    }
62 151
 }