Browse Source

passing tests

Whitney Martinez 6 years ago
parent
commit
94b48807fe

+ 4
- 0
src/main/java/io/zipcoder/Item.java View File

@@ -23,21 +23,25 @@ public class Item {
23 23
     }
24 24
 
25 25
     public String getName() {
26
+
26 27
         return name;
27 28
     }
28 29
 
29 30
 
30 31
     public Double getPrice() {
32
+
31 33
         return price;
32 34
     }
33 35
 
34 36
 
35 37
     public String getType() {
38
+
36 39
         return type;
37 40
     }
38 41
 
39 42
 
40 43
     public String getExpiration() {
44
+
41 45
         return expiration;
42 46
     }
43 47
 

+ 142
- 2
src/main/java/io/zipcoder/ItemParser.java View File

@@ -1,7 +1,12 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.lang.reflect.Array;
3 4
 import java.util.ArrayList;
4 5
 import java.util.Arrays;
6
+import java.util.HashMap;
7
+import java.util.Map;
8
+import java.util.regex.Matcher;
9
+import java.util.regex.Pattern;
5 10
 
6 11
 public class ItemParser {
7 12
 
@@ -13,19 +18,154 @@ public class ItemParser {
13 18
     }
14 19
 
15 20
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
21
+
22
+        ArrayList<String> values = getValues(rawItem);
23
+         String name = values.get(0);
24
+         Double price = Double.valueOf(values.get(1));
25
+        String type = values.get(2);
26
+        String expiration = values.get(3);
27
+
28
+        return new Item(toLowerCase(name),price,toLowerCase(type),expiration);
29
+
17 30
     }
18 31
 
19 32
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
33
+        String stringPattern = "[^a-zA-Z0-9\\:\\.\\/]";
21 34
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22 35
         return response;
23 36
     }
24 37
 
25 38
     private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
26 39
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
40
+
41
+    }
42
+    public String getValue(String input) throws ItemParseException {
43
+    ArrayList<String>  kv = splitStringWithRegexPattern(":", input);
44
+       if(kv.size() < 2){
45
+           throw new ItemParseException();
46
+       }else if(kv.size() == 2){
47
+           return kv.get(1);
48
+    }
49
+     return null;
50
+    }
51
+
52
+    public ArrayList<String> getValues(String rawItem) throws ItemParseException{
53
+        ArrayList<String> pairing = findKeyValuePairsInRawItemData(rawItem);
54
+        ArrayList<String> values = new ArrayList<String>();
55
+        for(String pair : pairing) {
56
+            String value = getValue(pair);
57
+            if (value.equals("")) {
58
+                throw new ItemParseException();
59
+
60
+            } else {
61
+                values.add((value));
62
+            }
63
+        }
64
+
65
+        return values;
66
+    }
67
+    public String toLowerCase(String test){
68
+        String[] lower = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
69
+        String[] upper = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
70
+
71
+        StringBuilder lowercase = new StringBuilder();
72
+
73
+        Pattern pattern = Pattern.compile(".");
74
+        Matcher matcher = pattern.matcher(test);
75
+
76
+
77
+        while(matcher.find()){
78
+            String character = matcher.group();
79
+            if(isUpper(character)){
80
+                int i = indexOf(upper,character);
81
+                lowercase.append(lower[i]);
82
+            }else{
83
+                lowercase.append(character);
84
+            }
85
+        }
86
+        return lowercase.toString();
87
+    }
88
+
89
+    public boolean isUpper(String c){
90
+        Pattern pattern = Pattern.compile("[A-Z]");
91
+        Matcher matcher = pattern.matcher(c);
92
+        return matcher.find();
93
+    }
94
+
95
+    public int indexOf(String[] array, String c){
96
+        for (int i = 0; i < array.length ; i++) {
97
+            if(array[i].equals(c)) {
98
+                return i;
99
+            }
100
+        }
101
+        return -1;
102
+    }
103
+
104
+    public int findNumberOfErrors(String input){
105
+
106
+        ArrayList<String> badFormat = new ArrayList<String>();
107
+        ArrayList<String> rawData = parseRawDataIntoStringArray(input);
108
+
109
+        for(String data : rawData){
110
+            try{
111
+                Item item = parseStringIntoItem(data);
112
+
113
+
114
+            }catch(ItemParseException e ){
115
+                badFormat.add(data);
116
+
117
+            }
118
+        }
119
+      return badFormat.size();
120
+    }
121
+
122
+    public ArrayList<Item> findItems(String input){
123
+        ArrayList<Item> items = new ArrayList<Item>();
124
+        ArrayList<String> rawData = parseRawDataIntoStringArray(input);
125
+
126
+        for(String data : rawData){
127
+            try{
128
+                Item item = parseStringIntoItem(data);
129
+                items.add(item);
130
+//                System.out.format("%s %.2f \n", item.getName(), item.getPrice());
131
+            }catch(ItemParseException e ){
132
+                e.printStackTrace();
133
+
134
+            }
135
+        }
136
+        return items;
137
+    }
138
+
139
+    public Map<String, Integer> getUniqueItemNames(ArrayList<Item> items){
140
+
141
+        Map<String,Integer> uniqueNames = new HashMap<String,Integer>();
142
+
143
+        for(Item item : items) {
144
+            if (!uniqueNames.containsKey(item.getName())) {
145
+                uniqueNames.put(item.getName(), 1);
146
+            } else {
147
+                int amount = uniqueNames.get(item.getName());
148
+                amount += 1;
149
+                uniqueNames.put(item.getName(), amount);
150
+            }
151
+        }
152
+        return uniqueNames;
27 153
     }
28 154
 
29 155
 
156
+//    private String[] getKeyValue(String input){
157
+//        Pattern pattern = Pattern.compile("([a-zA-Z0-9\\:\\.\\/]+)(?=[^\\/])");
158
+//        Matcher matcher = pattern.matcher(input);
159
+//
160
+//    }
161
+//    public String toLowerCase(String test){
162
+//        ArrayList<String> characters = new ArrayList<String>();
163
+//
164
+//        Pattern pattern = Pattern.compile("\\w");
165
+//        Matcher matcher = pattern.matcher(test);
166
+//
167
+//
168
+//
169
+//    }
30 170
 
31 171
 }

+ 17
- 4
src/main/java/io/zipcoder/Main.java View File

@@ -2,18 +2,31 @@ package io.zipcoder;
2 2
 
3 3
 import org.apache.commons.io.IOUtils;
4 4
 
5
+import java.util.ArrayList;
6
+import java.util.HashMap;
7
+import java.util.Map;
8
+
5 9
 
6 10
 public class Main {
7 11
 
12
+    private static ItemParser itm = new ItemParser();
13
+
8 14
     public String readRawDataToString() throws Exception{
9 15
         ClassLoader classLoader = getClass().getClassLoader();
10 16
         String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
11 17
         return result;
12 18
     }
13 19
 
14
-    public static void main(String[] args) throws Exception{
20
+    public static void main(String[] args) throws Exception {
15 21
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
17
-        // TODO: parse the data in output into items, and display to console.
18
-    }
22
+
23
+        int errs = itm.findNumberOfErrors(output);
24
+    //    ArrayList<Item> items = itm.findItems(output);
25
+
26
+
27
+//            System.out.format("%s %.2f \n", item.getName(), item.getPrice());
28
+
29
+        }
30
+
31
+
19 32
 }

+ 15
- 1
src/test/java/io/zipcoder/ItemParserTest.java View File

@@ -14,7 +14,7 @@ public class ItemParserTest {
14 14
 
15 15
     private String rawSingleItemIrregularSeperatorSample = "naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##";
16 16
 
17
-    private String rawBrokenSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
17
+    private String rawBrokenSingleItem =    "naMe:Milk;price:3.23;Food;expiration:1/25/2016##";
18 18
 
19 19
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
20 20
                                       +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
@@ -59,4 +59,18 @@ public class ItemParserTest {
59 59
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60 60
         assertEquals(expected, actual);
61 61
     }
62
+
63
+    @Test
64
+    public void toLowerCaseTest(){
65
+        String test = "MILK";
66
+        String expect = "milk";
67
+        String actual = itemParser.toLowerCase(test);
68
+        Assert.assertEquals(expect,actual);
69
+    }
70
+
71
+    @Test
72
+    public void isUpperTest(){
73
+        String test = "E";
74
+        Assert.assertTrue(itemParser.isUpper(test));
75
+    }
62 76
 }