Browse Source

Merge pull request #1 from gjarant/formattingOutput

Formatting output
gjarant 6 years ago
parent
commit
1c78ed4c3e
No account linked to committer's email

+ 61
- 3
src/main/java/io/zipcoder/ItemParser.java View File

@@ -66,8 +66,67 @@ public class ItemParser {
66 66
         return priceTotals;
67 67
     }
68 68
 
69
-    public Integer totalTimesItemSeen(ArrayList<Item> filteredArrayList){
70
-        return filteredArrayList.size();
69
+    public Map<Double, Integer> itemTypeMapWithCounts(String rawData, String filterType) throws ItemParseException {
70
+        ArrayList<Item> itemArrayList = createItemArrayList(rawData);
71
+        ArrayList<Item> filterItemArrayList = filterItemArrayList(itemArrayList, filterType);
72
+        Map<Double, Integer> priceTotals = individualItemCount(filterItemArrayList);
73
+        return priceTotals;
74
+    }
75
+
76
+    public Integer totalTimesItemSeen(String rawData, String filterType) throws ItemParseException {
77
+        ArrayList<Item> itemArrayList = createItemArrayList(rawData);
78
+        ArrayList<Item> filterItemArrayList = filterItemArrayList(itemArrayList, filterType);
79
+        return filterItemArrayList.size();
80
+    }
81
+
82
+    public ArrayList<String> filterTypeArrayList(String rawData) throws ItemParseException {
83
+        ArrayList<Item> itemArrayList = createItemArrayList(rawData);
84
+        ArrayList<String> filterType = new ArrayList<String>();
85
+
86
+        for(int i = 0; i < itemArrayList.size(); i++) {
87
+            if (!filterType.contains(itemArrayList.get(i).getName())) {
88
+                filterType.add(itemArrayList.get(i).getName());
89
+            }
90
+        }
91
+        return filterType;
92
+    }
93
+
94
+    public String formatText(String rawData) throws ItemParseException {
95
+        ArrayList<String> filterType = filterTypeArrayList(rawData);
96
+        StringBuilder sb = new StringBuilder();
97
+
98
+            for (int i = 0; i < filterType.size(); i++) {
99
+                String name = filterType.get(i);
100
+                Integer total = totalTimesItemSeen(rawData, filterType.get(i));
101
+                String priceFormated = formatPriceField(rawData, filterType.get(i));
102
+                sb.append("name: ");
103
+                sb.append(String.format("%7s", name.substring(0, 1).toUpperCase() + name.substring(1)));
104
+                sb.append("\t \t");
105
+                sb.append(" seen: " + total + " times\n");
106
+                sb.append("============= \t \t =============\n");
107
+                sb.append(priceFormated + "\n");
108
+            }
109
+
110
+        sb.append("Errors              " + " seen: " + getExceptionsThrown(rawData) + " times");
111
+
112
+        return sb.toString();
113
+    }
114
+
115
+    public String formatPriceField(String rawData, String filterType) throws ItemParseException {
116
+        Map<Double, Integer> priceTotals = itemTypeMapWithCounts(rawData, filterType);
117
+        StringBuilder sb = new StringBuilder();
118
+        Set mapSet = (Set) priceTotals.entrySet();
119
+        Iterator mapIterator = mapSet.iterator();
120
+        while (mapIterator.hasNext()) {
121
+            Map.Entry mapEntry = (Map.Entry) mapIterator.next();
122
+            // getKey Method of HashMap access a key of map
123
+            Object keyValue = mapEntry.getKey();
124
+            //getValue method returns corresponding key's value
125
+            Object value = mapEntry.getValue();
126
+            sb.append("Price:   " + keyValue + "\t\t seen: " + value + " times" + "\n");
127
+            sb.append("-------------\t\t -------------\n");
128
+        }
129
+        return sb.toString();
71 130
     }
72 131
 
73 132
     public String checkName(String input) throws ItemParseException {
@@ -114,7 +173,6 @@ public class ItemParser {
114 173
     }
115 174
 
116 175
     public Integer getExceptionsThrown(String rawData)  {
117
-
118 176
         ArrayList<String> parsedRawData = parseRawDataIntoStringArray(rawData);
119 177
         for (int i = 0; i < parsedRawData.size(); i++)
120 178
             try {

+ 2
- 1
src/main/java/io/zipcoder/Main.java View File

@@ -13,7 +13,8 @@ public class Main {
13 13
 
14 14
     public static void main(String[] args) throws Exception{
15 15
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
16
+        ItemParser item = new ItemParser();
17
+        System.out.println(item.formatText(output));
17 18
         // TODO: parse the data in output into items, and display to console.
18 19
     }
19 20
 }

+ 48
- 2
src/test/java/io/zipcoder/ItemParserTest.java View File

@@ -108,10 +108,56 @@ public class ItemParserTest {
108 108
     }
109 109
 
110 110
     @Test
111
+    public void itemTypeMapWithCountsTest() throws ItemParseException {
112
+        String expected ="{3.23=2, 2.23=1, 1.23=2}";
113
+        String actual  = itemParser.itemTypeMapWithCounts(rawMultipleItems2,"bread").toString();
114
+        assertEquals(expected, actual);
115
+    }
116
+
117
+    @Test
111 118
     public void totalTimesItemSeen() throws ItemParseException {
112
-        ArrayList<Item> individualItemCountTester = itemParser.filterItemArrayList(itemParser.createItemArrayList(rawMultipleItems2),"bread");
113 119
         Integer expected = 5;
114
-        Integer actual  = itemParser.totalTimesItemSeen(individualItemCountTester);
120
+        Integer actual  = itemParser.totalTimesItemSeen(rawMultipleItems2, "bread");
121
+        assertEquals(expected, actual);
122
+    }
123
+
124
+    @Test
125
+    public void filterTypeArrayListTest() throws ItemParseException {
126
+        String expected = "[milk, bread]";
127
+        String actual = itemParser.filterTypeArrayList(rawMultipleItems2).toString();
128
+        assertEquals(expected, actual);
129
+    }
130
+
131
+    @Test
132
+    public void formatTextTest() throws ItemParseException {
133
+        String expected = "name:    Milk\t \t seen: 1 times\n" +
134
+                "============= \t \t =============\n" +
135
+                "Price:   3.23\t\t seen: 1 times\n" +
136
+                "-------------\t\t -------------\n" +
137
+                "\n" +
138
+                "name:   Bread\t \t seen: 5 times\n" +
139
+                "============= \t \t =============\n" +
140
+                "Price:   3.23\t\t seen: 2 times\n" +
141
+                "-------------\t\t -------------\n" +
142
+                "Price:   2.23\t\t seen: 1 times\n" +
143
+                "-------------\t\t -------------\n" +
144
+                "Price:   1.23\t\t seen: 2 times\n" +
145
+                "-------------\t\t -------------\n" +
146
+                "\n" +
147
+                "Errors               seen: 0 times";
148
+        String actual = itemParser.formatText(rawMultipleItems2).toString();
149
+        assertEquals(expected, actual);
150
+    }
151
+
152
+    @Test
153
+    public void formatPriceFieldTest() throws ItemParseException {
154
+        String expected = "Price:   3.23\t\t seen: 2 times\n" +
155
+                "-------------\t\t -------------\n" +
156
+                "Price:   2.23\t\t seen: 1 times\n" +
157
+                "-------------\t\t -------------\n" +
158
+                "Price:   1.23\t\t seen: 2 times\n" +
159
+                "-------------\t\t -------------\n";
160
+        String actual = itemParser.formatPriceField(rawMultipleItems2, "bread").toString();
115 161
         assertEquals(expected, actual);
116 162
     }
117 163