Jennifer Chao преди 6 години
родител
ревизия
89ff6c63ba
променени са 3 файла, в които са добавени 191 реда и са изтрити 82 реда
  1. 96
    70
      src/main/java/io/zipcoder/ItemParser.java
  2. 1
    0
      src/main/resources/TestFile.txt
  3. 94
    12
      src/test/java/io/zipcoder/ItemParserTest.java

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

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

+ 1
- 0
src/main/resources/TestFile.txt Целия файл

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

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

@@ -5,31 +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##";
18 19
 
19
-    private String rawBrokenSingleItem1 =    "naMe:Milk;price:3.23;type:;expiration:1/25/2016##";
20
+    private String rawBrokenSingleItem1 = "naMe:Milk;price:3.23;type:;expiration:1/25/2016##";
20 21
 
21 22
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
22
-                                      +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
23
-                                      +"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##";
24 25
     private ItemParser itemParser;
25 26
 
26 27
     @Before
27
-    public void setUp(){
28
+    public void setUp() {
28 29
         itemParser = new ItemParser();
29 30
     }
30 31
 
31 32
     @Test
32
-    public void parseRawDataIntoStringArrayTest(){
33
+    public void parseRawDataIntoStringArrayTest() {
33 34
         Integer expectedArraySize = 3;
34 35
         ArrayList<String> items = itemParser.parseRawDataIntoStringArray(rawMultipleItems);
35 36
         Integer actualArraySize = items.size();
@@ -37,8 +38,8 @@ public class ItemParserTest {
37 38
     }
38 39
 
39 40
     @Test
40
-    public void parseStringIntoItemTest() throws ItemParseException{
41
-        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");
42 43
         Item actual = itemParser.parseStringIntoItem(rawSingleItem);
43 44
         assertEquals(expected.toString(), actual.toString());
44 45
     }
@@ -49,21 +50,102 @@ public class ItemParserTest {
49 50
 //    }
50 51
 
51 52
     @Test(expected = ItemParseException.class)
52
-    public void parseBrokenStringIntoItemTest1() throws ItemParseException{
53
+    public void parseBrokenStringIntoItemTest1() throws ItemParseException {
53 54
         itemParser.parseStringIntoItem(rawBrokenSingleItem1);
54 55
     }
55 56
 
56 57
     @Test
57
-    public void findKeyValuePairsInRawItemDataTest(){
58
+    public void findKeyValuePairsInRawItemDataTest() {
58 59
         Integer expected = 4;
59 60
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItem).size();
60 61
         assertEquals(expected, actual);
61 62
     }
62 63
 
63 64
     @Test
64
-    public void findKeyValuePairsInRawItemDataTestIrregular(){
65
+    public void findKeyValuePairsInRawItemDataTestIrregular() {
65 66
         Integer expected = 4;
66 67
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
67 68
         assertEquals(expected, actual);
68 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
+    }
69 151
 }