Pārlūkot izejas kodu

Merge 2999f15f6e5343e4474d35318ac9631895a96d82 into 23c2c01bf07b924ac9b860fa2771bf6ceb246ad5

Jessica Campbell 6 gadus atpakaļ
vecāks
revīzija
2c50bdb0d2
Revīzijas autora e-pasts nav piesaistīts nevienam kontam

+ 4
- 0
src/main/java/io/zipcoder/Item.java Parādīt failu

@@ -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
 

+ 135
- 7
src/main/java/io/zipcoder/ItemParser.java Parādīt failu

@@ -2,30 +2,158 @@ package io.zipcoder;
2 2
 
3 3
 import java.util.ArrayList;
4 4
 import java.util.Arrays;
5
+import java.util.HashMap;
6
+import java.util.Map;
7
+import java.util.regex.Matcher;
8
+import java.util.regex.Pattern;
5 9
 
6 10
 public class ItemParser {
7 11
 
12
+    public static int exceptionCount = 0;
13
+    private HashMap<String, ArrayList<Item>> groceryList = new HashMap<String, ArrayList<Item>>();
8 14
 
9
-    public ArrayList<String> parseRawDataIntoStringArray(String rawData){
15
+
16
+    public ArrayList<String> parseRawDataIntoStringArray(String rawData){ // entries split by ## with name,price,type and exp Example: [naMe:Milk;price:3.23;type:Food;expiration:1/25/2016, naME:BreaD;price:1.23;type:Food;expiration:1/02/2016 ... etc.]
10 17
         String stringPattern = "##";
11 18
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
12 19
         return response;
13 20
     }
21
+    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){ // entries split by crazy characters Example: [naMe:Milk, price:3.23, type:Food, expiration:1/25/2016] [] [] etc.
22
+        String stringPattern = "[;|^]";
23
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
24
+        return response;
25
+    }
26
+
27
+    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){ // helper method that splits strings at characters above into ArrayLists
28
+        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
29
+    }
14 30
 
15 31
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
32
+        if (findName(rawItem) == null | findPrice(rawItem)== null) {
33
+            throw new ItemParseException();
34
+        }
35
+
36
+        String name = findName(rawItem);
37
+        Double price = Double.parseDouble(findPrice(rawItem));
38
+        String type = findType(rawItem);
39
+        String expDate = findExpiration(rawItem);
40
+
41
+        Item foundAnItem = new Item(name, price, type, expDate);
42
+        return foundAnItem;
43
+    }
44
+
45
+    public String findName(String rawItem) {
46
+        Pattern patternName = Pattern.compile("(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])"); // matches "MiLk" or "bReaD"
47
+        Matcher matcherName = patternName.matcher(rawItem);
48
+        if (matcherName.find()){
49
+            if(!matcherName.group().equals("")){ // if the name does not equal null --> bc some blank spaces and semicolons were being counted as names
50
+                String fixedName = matcherName.group().replaceAll("\\d", "o"); // will fix C00kie mistake - once you hit a number replace it with an o
51
+                return fixedName.toLowerCase(); // changed to lowercase bc tests require lowercase
52
+            }
53
+        }
16 54
         return null;
17 55
     }
18 56
 
19
-    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
21
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22
-        return response;
57
+    //group() --> Matcher method
58
+    //Returns the input subsequent matched by the previous match.
59
+
60
+    public String findPrice(String rawItem) {
61
+        Pattern patternPrice = Pattern.compile("\\d(\\.)\\d\\d"); // matches "3.23"
62
+        Matcher matcherPrice = patternPrice.matcher(rawItem);
63
+        if(matcherPrice.find()){
64
+            if (!matcherPrice.group().equals("")){ // some blank prices were getting returned so make sure they're not blank
65
+                return matcherPrice.group();
66
+            }
67
+        }
68
+        return null;
23 69
     }
24 70
 
25
-    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
26
-        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
71
+    public String  findExpiration(String rawItem) {
72
+        Pattern patternExp = Pattern.compile("(?<=([Ee][Xx][Pp][Ii][Rr][Aa][Tt][Ii][Oo][Nn][^A-Za-z]))(.)+[^#]"); // matches "1/17/2016"
73
+        Matcher matcherExp = patternExp.matcher(rawItem);
74
+        if (matcherExp.find()){
75
+            return matcherExp.group();
76
+        }
77
+        else return null;
78
+    }
79
+
80
+    public String findType(String rawItem){
81
+        Pattern patternType = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])"); // matches "type: Food"
82
+        Matcher matcherType = patternType.matcher(rawItem);
83
+        if (matcherType.find()){
84
+            return matcherType.group().toLowerCase();
85
+        }
86
+        else return null;
27 87
     }
28 88
 
89
+    public HashMap<String, ArrayList<Item>> getGroceryList() throws Exception {
90
+        Main main = new Main();
91
+
92
+        ArrayList<String> listOfItems = parseRawDataIntoStringArray(main.readRawDataToString());
93
+
94
+        for(String anItem : listOfItems){
95
+            try {
96
+                Item newItem = parseStringIntoItem(anItem);
97
+                if (!groceryList.containsKey(newItem.getName())){ // if we do not have this key
98
+                    ArrayList<Item> myItemArrayList = new ArrayList<Item>(); // we will have to create a new arrayList to hold our items
99
+                    myItemArrayList.add(newItem); // then we can add our item to our arrayList
100
+                    groceryList.put(newItem.getName(), myItemArrayList); // we will add it to our map next
101
+                } else {
102
+                    groceryList.get(newItem.getName()).add(newItem); // if we already have the item, add the value
103
+                }
104
+            } catch (ItemParseException e){
105
+                exceptionCount++;
106
+            }
107
+        }
108
+        return groceryList;
109
+    }
110
+
111
+    public String displayGroceryListToString() throws Exception{
112
+        groceryList = getGroceryList();
113
+        StringBuilder displayGroceryList = new StringBuilder();
114
+
115
+        for (Map.Entry<String, ArrayList<Item>> nameAndItem : groceryList.entrySet()){
116
+            String makeUpperCase = nameAndItem.getKey().substring(0,1).toUpperCase() + nameAndItem.getKey().substring(1); // capitalize the first letter of the name ex: M and then add the rest of the work ex: ilk
117
+
118
+            displayGroceryList.append("\n" + "name: " + makeUpperCase + "\t\t\t\t" + "seen: " + nameAndItem.getValue().size() + " times");
119
+            displayGroceryList.append("\n" + "------------------------------------------");
29 120
 
121
+            ArrayList<Double> getDiffPrices = getDifferentPrices(nameAndItem);
122
+            for (int i = 0; i < getDiffPrices.size(); i++) {
123
+                if (getPriceOccurrences(nameAndItem.getValue(), getDiffPrices.get(i)) == 1) {
124
+                    String time = " time";
125
+                } else {
126
+                    String time = " times";
127
+                    displayGroceryList.append("\n" + "Price: " + getDiffPrices.get(i) + "\t\t\t\t" + " seen: " + getPriceOccurrences(nameAndItem.getValue(), getDiffPrices.get(i)) + " "+time);
128
+                    displayGroceryList.append("\n" + "==========================================");
129
+                }
130
+            }
30 131
 
132
+        }
133
+        displayGroceryList.append("\n\n" + "Errors: " + exceptionCount + " times\n\n");
134
+        displayGroceryList.append("\n" + "------------------------------------------");
135
+        return displayGroceryList.toString();
136
+
137
+    }
138
+
139
+    public Integer getPriceOccurrences(ArrayList<Item> listOfItems, Double price){
140
+        int counter = 0;
141
+
142
+        for (int i = 0; i < listOfItems.size(); i++){
143
+           if (listOfItems.get(i).getPrice().equals(price)) // if our arrayList of items have the value of our price add it to the count
144
+               counter++;
145
+        }
146
+        return counter;
147
+    }
148
+
149
+    public ArrayList<Double> getDifferentPrices(Map.Entry<String, ArrayList<Item>> item){
150
+        ArrayList<Double> diffPrices = new ArrayList<Double>();
151
+
152
+        for (int i = 0; i < item.getValue().size(); i ++){
153
+            if (!diffPrices.contains(item.getValue().get(i).getPrice())){ // get the size of our arrayList of items and if the prices != the other prices add them to our list
154
+                diffPrices.add(item.getValue().get(i).getPrice());
155
+            }
156
+        }
157
+        return diffPrices;
158
+    }
31 159
 }

+ 31
- 4
src/main/java/io/zipcoder/Main.java Parādīt failu

@@ -2,6 +2,10 @@ package io.zipcoder;
2 2
 
3 3
 import org.apache.commons.io.IOUtils;
4 4
 
5
+import java.lang.reflect.Array;
6
+import java.util.ArrayList;
7
+import java.util.Map;
8
+
5 9
 
6 10
 public class Main {
7 11
 
@@ -13,7 +17,30 @@ public class Main {
13 17
 
14 18
     public static void main(String[] args) throws Exception{
15 19
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
17
-        // TODO: parse the data in output into items, and display to console.
18
-    }
19
-}
20
+        ItemParser parser = new ItemParser();
21
+
22
+        System.out.println(parser.displayGroceryListToString());
23
+
24
+
25
+//        for (Map.Entry<String, ArrayList<Item>> mapKey : parser.getGroceryList().entrySet()) {
26
+//            System.out.println(mapKey.getKey());
27
+//            for (Item item : mapKey.getValue()) {
28
+//                System.out.println(item.getPrice());
29
+
30
+            }
31
+        }
32
+
33
+//        System.out.println(output);
34
+//        // TODO: parse the data in output into items, and display to console.
35
+//        ItemParser itemParser = new ItemParser();
36
+//        ArrayList<String> arrayListWithoutHashTags = itemParser.parseRawDataIntoStringArray(output);
37
+//        for (String s : arrayListWithoutHashTags)
38
+//        {
39
+//            //System.out.println(s);
40
+//            ItemParser itemParser2 = new ItemParser();
41
+//            //System.out.println(itemParser2.findKeyValuePairsInRawItemData(s));
42
+//            ArrayList<String> arrayWithoutVariousCharacters = itemParser2.splitStringsAtSemiColon(s);
43
+//                for (String s1 : arrayWithoutVariousCharacters){
44
+//                    System.out.println(s1);
45
+//                }
46
+

+ 24
- 0
src/main/resources/displayOfGroceryList.txt Parādīt failu

@@ -0,0 +1,24 @@
1
+
2
+name: Bread				seen: 6 times
3
+------------------------------------------
4
+Price: 1.23				 seen: 6  times
5
+==========================================
6
+name: Milk				seen: 6 times
7
+------------------------------------------
8
+Price: 3.23				 seen: 5  times
9
+==========================================
10
+name: Apples				seen: 4 times
11
+------------------------------------------
12
+Price: 0.25				 seen: 2  times
13
+==========================================
14
+Price: 0.23				 seen: 2  times
15
+==========================================
16
+name: Cookies				seen: 8 times
17
+------------------------------------------
18
+Price: 2.25				 seen: 8  times
19
+==========================================
20
+
21
+Errors: 4 times
22
+
23
+
24
+------------------------------------------

+ 2
- 1
src/test/java/io/zipcoder/ItemParserTest.java Parādīt failu

@@ -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:;price:3.23;type: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##"
@@ -23,6 +23,7 @@ public class ItemParserTest {
23 23
 
24 24
     @Before
25 25
     public void setUp(){
26
+
26 27
         itemParser = new ItemParser();
27 28
     }
28 29