Procházet zdrojové kódy

here is what i have so far.. alot of help from my classmates

Jordan Elderidge před 6 roky
rodič
revize
8be850ab2d

+ 137
- 8
src/main/java/io/zipcoder/ItemParser.java Zobrazit soubor

@@ -2,30 +2,159 @@ 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
 
8
-
12
+    private Pattern pattern;
13
+    private Matcher matcher;
14
+    private int counter = 0;
15
+    private Main main = new Main();
16
+    private Map<String ,ArrayList<Item>> groceryListMap = new HashMap<String, ArrayList<Item>>();
17
+    // Splits string based on ##
9 18
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
10 19
         String stringPattern = "##";
11 20
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
12 21
         return response;
13 22
     }
14 23
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
17
-    }
18
-
19 24
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
25
+        String stringPattern = "[^|*|@|!|$|%|&|;]";
21 26
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22 27
         return response;
23 28
     }
24
-
25
-    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
29
+    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
26 30
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
27 31
     }
28 32
 
33
+    public Item parseStringIntoItem(String rawItem) throws ItemParseException {
34
+        if (findName(rawItem) == null || findPrice(rawItem) == null){
35
+            throw new ItemParseException();
36
+
37
+        }
38
+        String itemName = findName(rawItem);
39
+        Double ittemPrice = Double.parseDouble(findPrice(rawItem));
40
+        String itemType = findType(rawItem);
41
+        String itemExpiration = findExpiration(rawItem);
42
+
43
+        return new Item(itemName,ittemPrice,itemType,itemExpiration);
44
+
45
+
46
+
47
+    }
48
+    public String findName(String rawItem){
49
+        String search = "(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])";
50
+        pattern = Pattern.compile(search);
51
+        matcher = pattern.matcher(rawItem);
52
+
53
+        if (matcher.find()){
54
+            if (!matcher.group().equals("")){
55
+                String name = matcher.group().replaceAll("\\d","o");
56
+                return name.toLowerCase();
57
+            }
58
+        }
59
+        return null;
60
+    }
61
+    public String findPrice(String rawItem){
62
+        pattern =Pattern.compile("(?<=([Pp][Rr][Ii][Cc][Ee][^A-Za-z])).*?(?=[^0-9.])");
63
+        matcher = pattern.matcher(rawItem);
64
+
65
+        if (matcher.find()){
66
+            if (!matcher.group().equals("")){
67
+                return matcher.group();
68
+            }
69
+        }
70
+        return null;
71
+    }
72
+    public String findType(String rawItem){
73
+        pattern =Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
74
+        matcher.pattern().matcher(rawItem);
75
+
76
+        if (matcher.find()){
77
+            return matcher.group().toLowerCase();
78
+
79
+            }
80
+        return null;
81
+        }
82
+
83
+
84
+
85
+    public String findExpiration(String rawItem){
86
+        pattern = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])(.) + [^#]");
87
+        matcher = pattern.matcher(rawItem);
88
+
89
+        if (matcher.find()){
90
+            return matcher.group();
91
+        }
92
+
93
+        return null;
94
+    }
95
+    public Map<String, ArrayList<Item>> getMap() throws Exception{
96
+        ArrayList<String> listOfItems = parseRawDataIntoStringArray(main.readRawDataToString());
97
+
98
+        for (String item : listOfItems){
99
+            try {
100
+                Item newItem = parseStringIntoItem(item);
101
+                if (!groceryListMap.containsKey(newItem.getName())){
102
+                    ArrayList<Item> myItem = new ArrayList<Item>();
103
+                    myItem.add(newItem);
104
+                    groceryListMap.put(newItem.getName(),myItem);
105
+                }else {
106
+                    groceryListMap.get(newItem.getName()).add(newItem);
107
+                }
108
+            }catch (ItemParseException e){
109
+                counter++;
110
+            }
111
+        }
112
+        return groceryListMap;
113
+    }
114
+
115
+    public String display() throws Exception{
116
+        groceryListMap = getMap();
117
+        StringBuilder displayBuild = new StringBuilder();
118
+
119
+        for (Map.Entry<String,ArrayList<Item>> item: groceryListMap.entrySet()){
120
+            String upperCase = item.getKey().substring(0,1).toUpperCase() + item.getKey().substring(1);
121
+
122
+            displayBuild.append("\n" + String.format("%-5s%10s%15s%2d%5s", "name:", upperCase, "seen: ", item.getValue().size(), "  times"));
123
+            displayBuild.append("\n" + String.format("%15s%3s%5s", "===============", "\t\t\t", "===============") + "\n");
124
+
125
+            ArrayList<Double> uniquePriceList = getUniquePrices(item);
126
+            for (int i = 0; i < uniquePriceList.size(); i++) {
127
+                displayBuild.append(String.format("%-11s%.2f%15s%2d%5s", "Price:", uniquePriceList.get(i), "seen: ", seenPriceOccurences(item.getValue(), uniquePriceList.get(i)), "  times"));
128
+                displayBuild.append("\n" + String.format("%15s%3s%5s", "---------------", "\t\t\t", "---------------") + "\n");
129
+            }
130
+
131
+        }
132
+        displayBuild.append("\n" + String.format("%-20s%10s%2d%5s", "Errors", "seen: ", counter, "  times"));
133
+
134
+        return displayBuild.toString();
135
+
136
+    }
137
+    public int seenPriceOccurences(ArrayList<Item> list, Double price) {
138
+        int countPrice =0;
139
+        for (int i =0; i < list.size();i++){
140
+            if (list.get(i).getPrice().equals(price)){
141
+                countPrice++;
142
+            }
143
+        }
144
+        return countPrice;
145
+    }
146
+
147
+
148
+    private ArrayList<Double> getUniquePrices(Map.Entry<String, ArrayList<Item>> item) {
149
+        ArrayList<Double> uniquePrice = new ArrayList<Double>();
150
+        for (int i = 0; i < item.getValue().size();i++){
151
+            if (!uniquePrice.contains(item.getValue().get(i).getPrice()));
152
+            uniquePrice.add(item.getValue().get(i).getPrice());
153
+
154
+        }
155
+        return uniquePrice;
156
+
157
+    }
29 158
 
30 159
 
31 160
 }

+ 2
- 0
src/main/java/io/zipcoder/Main.java Zobrazit soubor

@@ -15,5 +15,7 @@ public class Main {
15 15
         String output = (new Main()).readRawDataToString();
16 16
         System.out.println(output);
17 17
         // TODO: parse the data in output into items, and display to console.
18
+        ItemParser itemParser = new ItemParser();
19
+        System.out.println(itemParser.display());
18 20
     }
19 21
 }