瀏覽代碼

Finished?

Trinh Tong 5 年之前
父節點
當前提交
f27e786836
共有 2 個檔案被更改,包括 95 行新增69 行删除
  1. 9
    69
      src/main/java/io/zipcoder/ItemParser.java
  2. 86
    0
      src/main/java/io/zipcoder/OutPutObject.java

+ 9
- 69
src/main/java/io/zipcoder/ItemParser.java 查看文件

1
 package io.zipcoder;
1
 package io.zipcoder;
2
 
2
 
3
-
4
-import com.sun.tools.javac.jvm.Items;
5
-
6
 import java.io.*;
3
 import java.io.*;
7
 import java.util.*;
4
 import java.util.*;
8
 import java.util.regex.Matcher;
5
 import java.util.regex.Matcher;
12
 
9
 
13
 public class ItemParser {
10
 public class ItemParser {
14
 
11
 
15
-    public boolean checkForValue(String itemString) {
12
+    public static boolean checkForValue(String itemString) {
16
         Pattern pattern = Pattern.compile("(:;)|(:%)|(:\\*)|(:@)|(:!)");
13
         Pattern pattern = Pattern.compile("(:;)|(:%)|(:\\*)|(:@)|(:!)");
17
         Matcher m = pattern.matcher(itemString);
14
         Matcher m = pattern.matcher(itemString);
18
 
15
 
19
         return m.find();
16
         return m.find();
20
     }
17
     }
21
 
18
 
22
-    public ArrayList<String> parseRawDataIntoStringArray(String rawData){
19
+    public static ArrayList<String> parseRawDataIntoStringArray(String rawData){
23
         String stringPattern = "##";
20
         String stringPattern = "##";
24
         return splitStringWithRegexPattern(stringPattern , rawData);
21
         return splitStringWithRegexPattern(stringPattern , rawData);
25
     }
22
     }
26
 
23
 
27
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
24
+    public static Item parseStringIntoItem(String rawItem) throws ItemParseException{
28
 
25
 
29
         if (checkForValue(rawItem)) {
26
         if (checkForValue(rawItem)) {
30
             throw new ItemParseException();
27
             throw new ItemParseException();
34
         }
31
         }
35
     }
32
     }
36
 
33
 
37
-    public String allValues(String rawItem) {
34
+    public static String allValues(String rawItem) {
38
         StringBuilder sb = new StringBuilder();
35
         StringBuilder sb = new StringBuilder();
39
         
36
         
40
         ArrayList<String> pairs = findKeyValuePairsInRawItemData(rawItem);
37
         ArrayList<String> pairs = findKeyValuePairsInRawItemData(rawItem);
44
         return sb.toString();
41
         return sb.toString();
45
     }
42
     }
46
 
43
 
47
-    public String returnValue (String pair) {
44
+    public static String returnValue (String pair) {
48
         return pair.substring(pair.lastIndexOf(":") + 1);
45
         return pair.substring(pair.lastIndexOf(":") + 1);
49
     }
46
     }
50
 
47
 
51
-    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
48
+    public static ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
52
         String stringPattern = "[;^%*@!]";
49
         String stringPattern = "[;^%*@!]";
53
         return splitStringWithRegexPattern(stringPattern , rawItem);
50
         return splitStringWithRegexPattern(stringPattern , rawItem);
54
     }
51
     }
55
 
52
 
56
-    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
53
+    private static ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
57
         return new ArrayList<>(Arrays.asList(inputString.split(stringPattern)));
54
         return new ArrayList<>(Arrays.asList(inputString.split(stringPattern)));
58
     }
55
     }
59
 
56
 
60
-    public static void main(String[] args) throws FileNotFoundException, ItemParseException {
61
-        ItemParser ip = new ItemParser();
62
-        String rawMultipleItems = "naMe:C00kies;price:3.23;type:Food;expiration:1/25/2016##"
63
-                +"naME:;price:1.23;type:Food;expiration:1/02/2016##"
64
-                +"NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##"
65
-                +"NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##";
66
-
67
-        ip.printToFile("list.txt" ,rawMultipleItems);
68
-    }
69
-
70
-
71
     public void printToFile(String fileName, String rawItemData) throws FileNotFoundException, ItemParseException {
57
     public void printToFile(String fileName, String rawItemData) throws FileNotFoundException, ItemParseException {
72
-        // Output to file
73
-        ArrayList<String> allItemsRaw = parseRawDataIntoStringArray(rawItemData);
74
-        ArrayList<Item> allItemObjects = new ArrayList<>();
75
-        int errors = 0;
76
-
77
-        for (String s:allItemsRaw) {
78
-            if (checkForValue(s)) {
79
-                errors++;
80
-            } else {
81
-                allItemObjects.add(parseStringIntoItem(s));
82
-            }
83
-        }
84
-
85
-        HashMap<String, SameItems> itemCounters = sameItemCounter(allItemObjects);
86
 
58
 
59
+        OutPutObject opo = new OutPutObject(parseRawDataIntoStringArray(rawItemData));
87
         PrintStream file = new PrintStream(new File(fileName));
60
         PrintStream file = new PrintStream(new File(fileName));
88
         PrintStream console = System.out;
61
         PrintStream console = System.out;
89
 
62
 
90
         System.setOut(file);
63
         System.setOut(file);
91
-
92
-        StringBuilder resultsOutput = new StringBuilder();
93
-
94
-        resultsOutput.append("Tariq's Messed up Grocery List...\n\n");
95
-
96
-        for (SameItems s : itemCounters.values()) {
97
-            resultsOutput.append(stringFormatName(s.getName(), s.getCount()));
98
-            resultsOutput.append(doubleLine());
99
-
100
-            for (Double d:s.getPrices().getPrices().keySet()) {
101
-                resultsOutput.append(stringFormatPrice(d, s.getPrices().getPrices().get(d)));
102
-                resultsOutput.append(singleLine());
103
-            }
104
-        }
105
-        resultsOutput.append(errorLine(errors));
106
-        System.out.println(resultsOutput.toString());
64
+        System.out.println(opo.outputString());
107
     }
65
     }
108
 
66
 
109
 
67
 
110
-    private HashMap<String, SameItems> sameItemCounter(ArrayList<Item> allItems) {
111
-
112
-        HashMap<String, SameItems> itemCounters = new HashMap<>();
113
-        for (Item i: allItems) {
114
-            SameItems temp;
115
-            if (!itemCounters.keySet().contains(i.getName())) {
116
-                temp = new SameItems(i.getName());
117
-                temp.getPrices().addNewPrice(i.getPrice());
118
-                itemCounters.put(i.getName(), temp);
119
-            } else {
120
 
68
 
121
-                temp = itemCounters.get(i.getName());
122
-                temp.getPrices().addNewPrice(i.getPrice());
123
-                temp.setCount(temp.getCount()+1);
124
-                itemCounters.put(i.getName(),temp);
125
-            }
126
-        }
127
-        return itemCounters;
128
-    }
129
 
69
 
130
 }
70
 }

+ 86
- 0
src/main/java/io/zipcoder/OutPutObject.java 查看文件

1
+package io.zipcoder;
2
+
3
+import java.util.ArrayList;
4
+import java.util.HashMap;
5
+
6
+import static io.zipcoder.ItemParser.checkForValue;
7
+import static io.zipcoder.ItemParser.parseStringIntoItem;
8
+import static io.zipcoder.LeetUtils.*;
9
+import static io.zipcoder.LeetUtils.errorLine;
10
+
11
+public class OutPutObject {
12
+    private int errorCount;
13
+    private HashMap<String, SameItems> allItemCounts;
14
+    private ArrayList<String> rawInputArray;
15
+    private ArrayList<Item> allItems;
16
+
17
+    public OutPutObject(ArrayList<String> rawInputArray) throws ItemParseException {
18
+        this.rawInputArray = rawInputArray;
19
+        this.allItems = new ArrayList<>();
20
+        errorCount = 0;
21
+
22
+        errorsAndCounts();
23
+        sameItemCounter(this.allItems);
24
+        allItemCounts = sameItemCounter(this.allItems);
25
+    }
26
+
27
+    public int getErrorCount() {
28
+        return errorCount;
29
+    }
30
+
31
+    public HashMap<String, SameItems> getAllItemCounts() {
32
+        return allItemCounts;
33
+    }
34
+
35
+    private void addErrorCount() {
36
+        this.errorCount = getErrorCount() + 1;
37
+    }
38
+
39
+    private void errorsAndCounts() throws ItemParseException {
40
+        for (String s:this.rawInputArray) {
41
+            if (checkForValue(s)) {
42
+                addErrorCount();
43
+            } else {
44
+                allItems.add(parseStringIntoItem(s));
45
+            }
46
+        }
47
+    }
48
+
49
+    private HashMap<String, SameItems> sameItemCounter(ArrayList<Item> allItems) {
50
+
51
+        HashMap<String, SameItems> itemCounters = new HashMap<>();
52
+        for (Item i: allItems) {
53
+            SameItems temp;
54
+            if (!itemCounters.keySet().contains(i.getName())) {
55
+                temp = new SameItems(i.getName());
56
+                temp.getPrices().addNewPrice(i.getPrice());
57
+                itemCounters.put(i.getName(), temp);
58
+            } else {
59
+
60
+                temp = itemCounters.get(i.getName());
61
+                temp.getPrices().addNewPrice(i.getPrice());
62
+                temp.setCount(temp.getCount()+1);
63
+                itemCounters.put(i.getName(),temp);
64
+            }
65
+        }
66
+        return itemCounters;
67
+    }
68
+
69
+    public String outputString() {
70
+        StringBuilder resultsOutput = new StringBuilder();
71
+        resultsOutput.append("Tariq's Messed up Grocery List...\n\n");
72
+
73
+        for (SameItems s : this.getAllItemCounts().values()) {
74
+            resultsOutput.append(stringFormatName(s.getName(), s.getCount()));
75
+            resultsOutput.append(doubleLine());
76
+
77
+            for (Double d:s.getPrices().getPrices().keySet()) {
78
+                resultsOutput.append(stringFormatPrice(d, s.getPrices().getPrices().get(d)));
79
+                resultsOutput.append(singleLine());
80
+            }
81
+        }
82
+
83
+        resultsOutput.append(errorLine(this.getErrorCount()));
84
+        return resultsOutput.toString();
85
+    }
86
+}