Bläddra i källkod

Merge 33d3b2a8c1e0e9e25ec43e0b9bd2eb2986b7e0a0 into 23c2c01bf07b924ac9b860fa2771bf6ceb246ad5

carolynnmarie 6 år sedan
förälder
incheckning
969011bb9f
No account linked to committer's email

+ 1
- 6
src/main/java/io/zipcoder/Item.java Visa fil

@@ -26,17 +26,12 @@ public class Item {
26 26
         return name;
27 27
     }
28 28
 
29
-
30
-    public Double getPrice() {
31
-        return price;
32
-    }
33
-
29
+    public Double getPrice() { return price; }
34 30
 
35 31
     public String getType() {
36 32
         return type;
37 33
     }
38 34
 
39
-
40 35
     public String getExpiration() {
41 36
         return expiration;
42 37
     }

+ 4
- 0
src/main/java/io/zipcoder/ItemParseException.java Visa fil

@@ -1,4 +1,8 @@
1 1
 package io.zipcoder;
2 2
 
3 3
 public class ItemParseException extends Exception {
4
+
5
+    public ItemParseException(){
6
+
7
+    }
4 8
 }

+ 89
- 7
src/main/java/io/zipcoder/ItemParser.java Visa fil

@@ -2,30 +2,112 @@ package io.zipcoder;
2 2
 
3 3
 import java.util.ArrayList;
4 4
 import java.util.Arrays;
5
+import java.util.regex.Matcher;
6
+import java.util.regex.Pattern;
7
+import static java.util.regex.Pattern.CASE_INSENSITIVE;
8
+import static java.util.regex.Pattern.LITERAL;
5 9
 
6 10
 public class ItemParser {
7 11
 
12
+    private static int exceptionCounter;
13
+
14
+    public ItemParser(){
15
+        exceptionCounter = 0;
16
+    }
17
+
18
+    public int getExceptionCounter(){
19
+        return exceptionCounter;
20
+    }
8 21
 
9 22
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
23
+        String normalizedRawData = normalizeRawData(rawData);
10 24
         String stringPattern = "##";
11
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
25
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , normalizedRawData);
12 26
         return response;
13 27
     }
14 28
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
29
+    public ArrayList<Item> stringArrayToItemArray(ArrayList<String> rawItems)throws ItemParseException{
30
+        ArrayList<Item> itemArray = new ArrayList<>();
31
+        for(String item: rawItems){
32
+            Item it = parseStringIntoItem(item);
33
+            if(!it.getName().equals("")) {
34
+                itemArray.add(it);
35
+            }
36
+        }
37
+        return itemArray;
17 38
     }
18 39
 
19
-    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
21
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22
-        return response;
40
+    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
41
+        String name = "";
42
+        String priceString = "";
43
+        Double price = 0.00;
44
+        String type = "";
45
+        String expiration = "";
46
+        Pattern pattern = Pattern.compile("name:(\\w*);price:(\\d*.\\d*\\d*);type:(\\w*);expiration:(\\w*/\\w*/\\w*)");
47
+        Matcher matcher = pattern.matcher(rawItem);
48
+        if(matcher.find()) {
49
+            try {
50
+                name = matcher.group(1);
51
+                priceString = matcher.group(2);
52
+                price = Double.parseDouble(priceString);
53
+                type = matcher.group(3);
54
+                expiration = matcher.group(4);
55
+                if (name.length() == 0 || priceString.length() == 0 || type.length() == 0 || expiration.length() == 0) {
56
+                    exceptionCounter++;
57
+                    throw new ItemParseException();
58
+                }
59
+            } catch (ItemParseException e) {
60
+                exceptionCounter++;
61
+            }
62
+        }
63
+        return new Item(name, price, type, expiration);
23 64
     }
24 65
 
25 66
     private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
26 67
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
27 68
     }
28 69
 
70
+    public String normalizeRawData(String rawData){
71
+        String normal = convertWeirdCharactersToSemiColon(rawData);
72
+        String normal2 = toLowerCaseString(normal);
73
+        return remove0FromCookie(normal2);
74
+    }
75
+
76
+    public String convertWeirdCharactersToSemiColon(String rawData) {
77
+        ArrayList<String> wierdCharacters = new ArrayList<>(Arrays.asList("^", "!", "*", "%"));
78
+        Pattern pattern = Pattern.compile("@");
79
+        Matcher matcher = pattern.matcher(rawData);
80
+        String unWeird = matcher.replaceAll(";");
81
+        for (String character : wierdCharacters) {
82
+            matcher.reset(unWeird);
83
+            matcher.usePattern(Pattern.compile(character, Pattern.LITERAL));
84
+            unWeird = matcher.replaceAll(";");
85
+        }
86
+        return unWeird;
87
+    }
88
+
89
+    public String toLowerCaseString(String rawData){
90
+        ArrayList<String> upperCase = new ArrayList<>(Arrays.asList("A","B","C","D","E","F","G","H","I","J",
91
+                "K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"));
92
+        ArrayList<String> lowerCase = new ArrayList<>(Arrays.asList("a","b","c","d","e","f","g","h","i","j",
93
+                "k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"));
94
+        Pattern pattern = Pattern.compile("A");
95
+        Matcher matcher = pattern.matcher(rawData);
96
+        String lower = matcher.replaceAll("a");
97
+        for(int i = 1; i<upperCase.size(); i++){
98
+            matcher.reset(lower);
99
+            matcher.usePattern(Pattern.compile(upperCase.get(i)));
100
+            lower = matcher.replaceAll(lowerCase.get(i));
101
+        }
102
+        return lower;
103
+    }
104
+
105
+    public String remove0FromCookie(String rawData){
106
+        Pattern pattern = Pattern.compile("c..kie");
107
+        Matcher matcher = pattern.matcher(rawData);
108
+        return matcher.replaceAll("cookie");
109
+
110
+    }
29 111
 
30 112
 
31 113
 }

+ 110
- 0
src/main/java/io/zipcoder/ListCreation.java Visa fil

@@ -0,0 +1,110 @@
1
+package io.zipcoder;
2
+
3
+import java.util.*;
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
6
+
7
+public class ListCreation {
8
+
9
+    private ItemParser parser;
10
+
11
+    public ListCreation() {
12
+        this.parser = new ItemParser();
13
+    }
14
+
15
+    public String rawStringToFinishedGroceryList(String raw) throws Exception {
16
+        ArrayList<String> rawItems = parser.parseRawDataIntoStringArray(raw);
17
+        ArrayList<Item> groceryList = parser.stringArrayToItemArray(rawItems);
18
+        return formattedGroceryListString(groceryList);
19
+    }
20
+
21
+    public String formattedGroceryListString(ArrayList<Item> groceryList) {
22
+        LinkedHashMap<Double,Integer> priceOccur = new LinkedHashMap<>();
23
+        String list = "";
24
+        for (String key : keySet(groceryList)) {
25
+            Pattern p = Pattern.compile(key);
26
+            Matcher matcher = p.matcher(keyString(groceryList));
27
+            int x = 0;
28
+            while (matcher.find()) {
29
+                x++;
30
+            }
31
+            if (x > 0) {
32
+                if(x == 1){
33
+                    list += String.format("name:%1$8s       seen: %2$d time \n=============       =============\n", key, x);
34
+                }else{
35
+                    list += String.format("name:%1$8s       seen: %2$d times\n=============       =============\n", key, x);
36
+                }
37
+                priceOccur = valueCount(groceryList, key);
38
+                list += printValues(priceOccur);
39
+            }
40
+        }
41
+        int errors = parser.getExceptionCounter();
42
+        list += String.format("Errors              seen: %d times",errors);
43
+        list = namesToCaps(list);
44
+        return list;
45
+    }
46
+
47
+    public LinkedHashSet<String> keySet(ArrayList<Item> groceryList) {
48
+        LinkedHashSet<String> keys = new LinkedHashSet<>();
49
+        for (Item item : groceryList) {
50
+            keys.add(item.getName());
51
+        }
52
+        return keys;
53
+    }
54
+
55
+    public String keyString(ArrayList<Item> groceryList) {
56
+        String keys = "";
57
+        for (Item item : groceryList) {
58
+            keys += item.getName();
59
+        }
60
+        return keys;
61
+    }
62
+
63
+    public LinkedHashMap<Double, Integer> valueCount(ArrayList<Item> groceryList,String key) {
64
+        LinkedHashMap<Double, Integer> priceOccurrences = new LinkedHashMap<>();
65
+        for (Item item : groceryList) {
66
+            Double price = item.getPrice();
67
+            if (key.equals(item.getName())) {
68
+                if (!priceOccurrences.containsKey(item.getPrice())) {
69
+                    priceOccurrences.put(price, 1);
70
+                } else if (priceOccurrences.containsKey(item.getPrice())) {
71
+                    priceOccurrences.replace(price, (priceOccurrences.get(price) + 1));
72
+                }
73
+            }
74
+        }
75
+        return priceOccurrences;
76
+    }
77
+
78
+    private String printValues(LinkedHashMap<Double, Integer> priceOccurrences) {
79
+        String valuesAndOccurrences = "";
80
+        for (Map.Entry<Double, Integer> entry : priceOccurrences.entrySet()) {
81
+            if(entry.getValue().equals(1)) {
82
+                valuesAndOccurrences += String.format("Price:   %1$.2f       seen: %2$d time \n", entry.getKey(), entry.getValue());
83
+            } else {
84
+                valuesAndOccurrences += String.format("Price:   %1$.2f       seen: %2$d times\n", entry.getKey(), entry.getValue());
85
+            }
86
+            valuesAndOccurrences += "-------------       -------------\n";
87
+
88
+        }
89
+        valuesAndOccurrences += "\n";
90
+        return valuesAndOccurrences;
91
+    }
92
+
93
+    public String namesToCaps(String groceryList){
94
+        String groceries = "";
95
+        ArrayList<String> itemsLower = new ArrayList<>(Arrays.asList("bread","cookies","apples"));
96
+        ArrayList<String> itemsUpper = new ArrayList<>(Arrays.asList("Bread","Cookies","Apples"));
97
+        Pattern p = Pattern.compile("milk");
98
+        Matcher m = p.matcher(groceryList);
99
+        groceries = m.replaceAll("Milk");
100
+        for(int i = 0; i<itemsLower.size(); i++){
101
+            m.reset(groceries);
102
+            m.usePattern(Pattern.compile(itemsLower.get(i)));
103
+            groceries = m.replaceAll(itemsUpper.get(i));
104
+        }
105
+        return groceries;
106
+    }
107
+
108
+}
109
+
110
+

+ 6
- 0
src/main/java/io/zipcoder/Main.java Visa fil

@@ -2,12 +2,18 @@ package io.zipcoder;
2 2
 
3 3
 import org.apache.commons.io.IOUtils;
4 4
 
5
+import java.util.ArrayList;
6
+
5 7
 
6 8
 public class Main {
7 9
 
10
+    protected ListCreation listCreation = new ListCreation();
11
+
12
+
8 13
     public String readRawDataToString() throws Exception{
9 14
         ClassLoader classLoader = getClass().getClassLoader();
10 15
         String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
16
+        result = listCreation.rawStringToFinishedGroceryList(result);
11 17
         return result;
12 18
     }
13 19
 

+ 31
- 10
src/test/java/io/zipcoder/ItemParserTest.java Visa fil

@@ -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;type:;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##"
@@ -26,6 +26,7 @@ public class ItemParserTest {
26 26
         itemParser = new ItemParser();
27 27
     }
28 28
 
29
+
29 30
     @Test
30 31
     public void parseRawDataIntoStringArrayTest(){
31 32
         Integer expectedArraySize = 3;
@@ -37,7 +38,8 @@ public class ItemParserTest {
37 38
     @Test
38 39
     public void parseStringIntoItemTest() throws ItemParseException{
39 40
         Item expected = new Item("milk", 3.23, "food","1/25/2016");
40
-        Item actual = itemParser.parseStringIntoItem(rawSingleItem);
41
+        String myNormalized = itemParser.normalizeRawData(rawSingleItem);
42
+        Item actual = itemParser.parseStringIntoItem(myNormalized);
41 43
         assertEquals(expected.toString(), actual.toString());
42 44
     }
43 45
 
@@ -47,16 +49,35 @@ public class ItemParserTest {
47 49
     }
48 50
 
49 51
     @Test
50
-    public void findKeyValuePairsInRawItemDataTest(){
51
-        Integer expected = 4;
52
-        Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItem).size();
53
-        assertEquals(expected, actual);
52
+    public void testNormalizeRawData(){
53
+        String initial = "naMe:Co0kie@price:3.23%type:Food^expiration:1/11/2016##";
54
+        String expected = "name:cookie;price:3.23;type:food;expiration:1/11/2016##";
55
+        String actual = itemParser.normalizeRawData(initial);
56
+        Assert.assertEquals(expected, actual);
54 57
     }
55 58
 
56 59
     @Test
57
-    public void findKeyValuePairsInRawItemDataTestIrregular(){
58
-        Integer expected = 4;
59
-        Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60
-        assertEquals(expected, actual);
60
+    public void testToLowerCaseItem(){
61
+        String expected = rawSingleItem.toLowerCase();
62
+        String actual = itemParser.toLowerCaseString(rawSingleItem);
63
+        Assert.assertEquals(expected, actual);
61 64
     }
65
+
66
+    @Test
67
+    public void testConvertWeirdCharactersToSemiColon(){
68
+        String initial = "^!@*%";
69
+        String expected = ";;;;;";
70
+        String actual = itemParser.convertWeirdCharactersToSemiColon(initial);
71
+        Assert.assertEquals(expected, actual);
72
+    }
73
+
74
+    @Test
75
+    public void testRemove0FromCookie(){
76
+        String initial = "c00kie";
77
+        String expected = "cookie";
78
+        String actual = itemParser.remove0FromCookie(initial);
79
+        Assert.assertEquals(expected, actual);
80
+    }
81
+
82
+
62 83
 }

+ 89
- 0
src/test/java/io/zipcoder/ListCreationTest.java Visa fil

@@ -0,0 +1,89 @@
1
+package io.zipcoder;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+import java.util.*;
7
+
8
+public class ListCreationTest {
9
+
10
+    @Test
11
+    public void testRawToFinish() throws Exception {
12
+        ListCreation list = new ListCreation();
13
+        String raw = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016";
14
+        try {
15
+            String actual = list.rawStringToFinishedGroceryList(raw);
16
+            System.out.println(actual);
17
+        } catch (Exception e) {
18
+            e.printStackTrace();
19
+        }
20
+    }
21
+
22
+    @Test
23
+    public void testFormattedGroceryListString(){
24
+        //Given
25
+        ListCreation list = new ListCreation();
26
+        Item item1 = new Item("milk", 3.23, "food", "1/25/2016");
27
+        Item item2 = new Item("cookies", 2.50, "food", "12/03/2018");
28
+        Item item3 = new Item("apples", 0.35, "food","04/23/2018");
29
+        Item item4 = new Item("milk", 3.23, "food", "1/25/2016");
30
+        Item item5 = new Item("milk", 2.00, "food", "1/25/2016");
31
+        //When
32
+        ArrayList<Item> itemObjects = new ArrayList<>(Arrays.asList(item1,item2,item3,item4,item5));
33
+        //Then
34
+        String groceryList = list.formattedGroceryListString(itemObjects);
35
+        boolean expected = true;
36
+        boolean actual = groceryList.contains("Milk");
37
+        Assert.assertEquals(expected, actual);
38
+    }
39
+
40
+    @Test
41
+    public void testKeySet(){
42
+        ListCreation list = new ListCreation();
43
+        Item item1 = new Item("apples", 0.35, "food","04/23/2018");
44
+        Item item2 = new Item("milk", 3.23, "food", "1/25/2016");
45
+        Item item3 = new Item("milk", 2.00, "food", "1/25/2016");
46
+        ArrayList<Item> itemObjects = new ArrayList<>(Arrays.asList(item1,item2,item3));
47
+        LinkedHashSet<String> actual = list.keySet(itemObjects);
48
+        LinkedHashSet<String> expected = new LinkedHashSet<>(Arrays.asList("apples","milk"));
49
+        Assert.assertEquals(expected,actual);
50
+    }
51
+
52
+    @Test
53
+    public void testKeyString(){
54
+        ListCreation list = new ListCreation();
55
+        Item item1 = new Item("apples", 0.35, "food","04/23/2018");
56
+        Item item2 = new Item("milk", 3.23, "food", "1/25/2016");
57
+        Item item3 = new Item("milk", 2.00, "food", "1/25/2016");
58
+        ArrayList<Item> itemObjects = new ArrayList<>(Arrays.asList(item1, item2,item3));
59
+        String expected = "applesmilkmilk";
60
+        String actual = list.keyString(itemObjects);
61
+        Assert.assertEquals(expected, actual);
62
+    }
63
+
64
+    @Test
65
+    public void testValueCount(){
66
+        ListCreation list = new ListCreation();
67
+        Item item1 = new Item("milk", 3.23, "food", "1/25/2016");
68
+        Item item2 = new Item("cookies", 2.50, "food", "12/03/2018");
69
+        Item item3 = new Item("apples", 0.35, "food","04/23/2018");
70
+        Item item4 = new Item("milk", 3.23, "food", "1/25/2016");
71
+        Item item5 = new Item("milk", 2.00, "food", "1/25/2016");
72
+        ArrayList<Item> itemObjects = new ArrayList<>(Arrays.asList(item1, item2, item3, item4, item5));
73
+        LinkedHashMap<Double,Integer> expected = new LinkedHashMap<>();
74
+        expected.put(3.23,2);
75
+        expected.put(2.00,1);
76
+        LinkedHashMap<Double,Integer> actual = list.valueCount(itemObjects, "milk");
77
+        Assert.assertEquals(expected, actual);
78
+    }
79
+
80
+    @Test
81
+    public void testNamesToCaps(){
82
+        ListCreation list = new ListCreation();
83
+        String initial = "cookies";
84
+        String expected = "Cookies";
85
+        String actual = list.namesToCaps(initial);
86
+        Assert.assertEquals(expected, actual);
87
+    }
88
+
89
+}

+ 18
- 0
src/test/java/io/zipcoder/ListEntryPairTest.java Visa fil

@@ -0,0 +1,18 @@
1
+package io.zipcoder;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class ListEntryPairTest {
7
+
8
+    @Test
9
+    public void constructorTest(){
10
+        Item item = new Item("milk", 3.23, "food","1/25/2016");
11
+        String expectedName = "milk";
12
+        Double expectedPrice = 3.23;
13
+        String actualName = item.getName();
14
+        Double actualPrice = item.getPrice();
15
+        Assert.assertEquals(expectedName, actualName);
16
+        Assert.assertEquals(expectedPrice, actualPrice);
17
+    }
18
+}