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

cleaned up code and added more tests

Carolynn Vansant преди 6 години
родител
ревизия
33d3b2a8c1

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

@@ -94,7 +94,7 @@ public class ItemParser {
94 94
         Pattern pattern = Pattern.compile("A");
95 95
         Matcher matcher = pattern.matcher(rawData);
96 96
         String lower = matcher.replaceAll("a");
97
-        for(int i = 0; i<upperCase.size(); i++){
97
+        for(int i = 1; i<upperCase.size(); i++){
98 98
             matcher.reset(lower);
99 99
             matcher.usePattern(Pattern.compile(upperCase.get(i)));
100 100
             lower = matcher.replaceAll(lowerCase.get(i));

+ 19
- 39
src/main/java/io/zipcoder/ListCreation.java Целия файл

@@ -6,40 +6,24 @@ import java.util.regex.Pattern;
6 6
 
7 7
 public class ListCreation {
8 8
 
9
-    private ListEntryPair entry;
10
-    private ArrayList<ListEntryPair> groceryList;
11 9
     private ItemParser parser;
12 10
 
13 11
     public ListCreation() {
14
-        this.entry = new ListEntryPair("", 0.00);
15
-        this.groceryList = new ArrayList<>();
16 12
         this.parser = new ItemParser();
17 13
     }
18 14
 
19 15
     public String rawStringToFinishedGroceryList(String raw) throws Exception {
20 16
         ArrayList<String> rawItems = parser.parseRawDataIntoStringArray(raw);
21
-        ArrayList<Item> itemArray = parser.stringArrayToItemArray(rawItems);
22
-        ArrayList<ListEntryPair> groceryListArray = createNameAndPriceList(itemArray);
23
-        String finish = formattedGroceryListString(groceryListArray);
24
-        return finish;
17
+        ArrayList<Item> groceryList = parser.stringArrayToItemArray(rawItems);
18
+        return formattedGroceryListString(groceryList);
25 19
     }
26 20
 
27
-    public ArrayList<ListEntryPair> createNameAndPriceList(ArrayList<Item> itemObjects) {
28
-        for (Item item : itemObjects) {
29
-            String name = item.getName();
30
-            Double price = item.getPrice();
31
-            entry = new ListEntryPair(name, price);
32
-            groceryList.add(entry);
33
-        }
34
-        return groceryList;
35
-    }
36
-
37
-    public String formattedGroceryListString(ArrayList<ListEntryPair> groceryList) {
21
+    public String formattedGroceryListString(ArrayList<Item> groceryList) {
38 22
         LinkedHashMap<Double,Integer> priceOccur = new LinkedHashMap<>();
39 23
         String list = "";
40 24
         for (String key : keySet(groceryList)) {
41 25
             Pattern p = Pattern.compile(key);
42
-            Matcher matcher = p.matcher(keyAndValueString(groceryList));
26
+            Matcher matcher = p.matcher(keyString(groceryList));
43 27
             int x = 0;
44 28
             while (matcher.find()) {
45 29
                 x++;
@@ -55,39 +39,35 @@ public class ListCreation {
55 39
             }
56 40
         }
57 41
         int errors = parser.getExceptionCounter();
58
-        if(errors == 1){
59
-            list += String.format("Errors              seen: %d time",errors);
60
-        }else {
61
-            list += String.format("Errors              seen: %d times",errors);
62
-        }
42
+        list += String.format("Errors              seen: %d times",errors);
63 43
         list = namesToCaps(list);
64 44
         return list;
65 45
     }
66 46
 
67
-    private LinkedHashSet<String> keySet(ArrayList<ListEntryPair> groceryList) {
47
+    public LinkedHashSet<String> keySet(ArrayList<Item> groceryList) {
68 48
         LinkedHashSet<String> keys = new LinkedHashSet<>();
69
-        for (ListEntryPair pair : groceryList) {
70
-            keys.add(pair.getName());
49
+        for (Item item : groceryList) {
50
+            keys.add(item.getName());
71 51
         }
72 52
         return keys;
73 53
     }
74 54
 
75
-    private String keyAndValueString(ArrayList<ListEntryPair> groceryList) {
76
-        String keysAndValues = "";
77
-        for (ListEntryPair pair : groceryList) {
78
-            keysAndValues += pair.pairToString();
55
+    public String keyString(ArrayList<Item> groceryList) {
56
+        String keys = "";
57
+        for (Item item : groceryList) {
58
+            keys += item.getName();
79 59
         }
80
-        return keysAndValues;
60
+        return keys;
81 61
     }
82 62
 
83
-    private LinkedHashMap<Double, Integer> valueCount(ArrayList<ListEntryPair> groceryList,String key) {
63
+    public LinkedHashMap<Double, Integer> valueCount(ArrayList<Item> groceryList,String key) {
84 64
         LinkedHashMap<Double, Integer> priceOccurrences = new LinkedHashMap<>();
85
-        for (ListEntryPair pair : groceryList) {
86
-            Double price = pair.getPrice();
87
-            if (key.equals(pair.getName())) {
88
-                if (!priceOccurrences.containsKey(pair.getPrice())) {
65
+        for (Item item : groceryList) {
66
+            Double price = item.getPrice();
67
+            if (key.equals(item.getName())) {
68
+                if (!priceOccurrences.containsKey(item.getPrice())) {
89 69
                     priceOccurrences.put(price, 1);
90
-                } else if (priceOccurrences.containsKey(pair.getPrice())) {
70
+                } else if (priceOccurrences.containsKey(item.getPrice())) {
91 71
                     priceOccurrences.replace(price, (priceOccurrences.get(price) + 1));
92 72
                 }
93 73
             }

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

@@ -1,25 +0,0 @@
1
-package io.zipcoder;
2
-
3
-public class ListEntryPair {
4
-
5
-
6
-    private String name;
7
-    private Double price;
8
-
9
-    public ListEntryPair(String name, Double price){
10
-        this.name = name;
11
-        this.price = price;
12
-    }
13
-
14
-    public String getName() {
15
-        return name;
16
-    }
17
-
18
-    public Double getPrice() {
19
-        return price;
20
-    }
21
-
22
-    public String pairToString(){
23
-        return name+price;
24
-    }
25
-}

+ 41
- 24
src/test/java/io/zipcoder/ListCreationTest.java Целия файл

@@ -3,7 +3,7 @@ package io.zipcoder;
3 3
 import org.junit.Assert;
4 4
 import org.junit.Test;
5 5
 
6
-import java.util.ArrayList;
6
+import java.util.*;
7 7
 
8 8
 public class ListCreationTest {
9 9
 
@@ -20,44 +20,61 @@ public class ListCreationTest {
20 20
     }
21 21
 
22 22
     @Test
23
-    public void testCreateGroceryList1() {
23
+    public void testFormattedGroceryListString(){
24 24
         //Given
25 25
         ListCreation list = new ListCreation();
26
-        ListEntryPair entry = new ListEntryPair("", 0.00);
27 26
         Item item1 = new Item("milk", 3.23, "food", "1/25/2016");
28 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");
29 31
         //When
30
-        ArrayList<Item> itemObjects = new ArrayList<>();
31
-        itemObjects.add(item1);
32
-        itemObjects.add(item2);
33
-        String expectedName = "milk";
32
+        ArrayList<Item> itemObjects = new ArrayList<>(Arrays.asList(item1,item2,item3,item4,item5));
34 33
         //Then
35
-        ArrayList<ListEntryPair> groceries = list.createNameAndPriceList(itemObjects);
36
-        entry = groceries.get(0);
37
-        String actualName = entry.getName();
38
-        Assert.assertEquals(expectedName, actualName);
34
+        String groceryList = list.formattedGroceryListString(itemObjects);
35
+        boolean expected = true;
36
+        boolean actual = groceryList.contains("Milk");
37
+        Assert.assertEquals(expected, actual);
39 38
     }
40 39
 
41 40
     @Test
42
-    public void testFormattedGroceryListString(){
43
-        //Given
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(){
44 66
         ListCreation list = new ListCreation();
45
-        ArrayList<Item> itemObjects = new ArrayList<>();
46 67
         Item item1 = new Item("milk", 3.23, "food", "1/25/2016");
47 68
         Item item2 = new Item("cookies", 2.50, "food", "12/03/2018");
48 69
         Item item3 = new Item("apples", 0.35, "food","04/23/2018");
49 70
         Item item4 = new Item("milk", 3.23, "food", "1/25/2016");
50 71
         Item item5 = new Item("milk", 2.00, "food", "1/25/2016");
51
-        //When
52
-        itemObjects.add(item1);
53
-        itemObjects.add(item2);
54
-        itemObjects.add(item3);
55
-        itemObjects.add(item4);
56
-        itemObjects.add(item5);
57
-        //Then
58
-        ArrayList<ListEntryPair> groceries = list.createNameAndPriceList(itemObjects);
59
-        String actual = list.formattedGroceryListString(groceries);
60
-        System.out.println(actual);
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);
61 78
     }
62 79
 
63 80
     @Test