Browse Source

Changed hyphenated line to print for each item after after the first price. If there are three different prices it will print a hyphenated in between prices, but not after the last occurence of price. Used tests to demonstrate ability to take additional input. If I wanted to create a set of acceptable item names I could set them with enums I am thinking.

PeterMcCormick 6 years ago
parent
commit
5a2fc8abe7

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

@@ -45,7 +45,7 @@ public class Main {
45 45
         StringBuilder sb = new StringBuilder();
46 46
         for (String item : groceryAccountedFor) {
47 47
             int timesSeen = hurtLocker.namePrice.get(item).size();
48
-            sb.append(String.format("name:%8s        seen:%2d %s \n", item, timesSeen, timeOrTimes(timesSeen)));
48
+            sb.append(String.format("name:%8s        seen:%2d %s\n", item, timesSeen, timeOrTimes(timesSeen)));
49 49
             sb.append(String.format("=============        =============\n"));
50 50
             sb.append(countOccurrences(item));
51 51
             sb.append("\n");
@@ -58,10 +58,13 @@ public class Main {
58 58
         StringBuilder sb = new StringBuilder();
59 59
         HashMap<Double, Integer> priceOccurrence = hurtLocker.getPriceOccurrence(item);
60 60
         Iterator<Double> itemPriceIterator = priceOccurrence.keySet().iterator();
61
+        int count = 0;
61 62
         while (itemPriceIterator.hasNext()) {
63
+            count++;
62 64
             Double price = itemPriceIterator.next();
63 65
             sb.append(String.format("Price:%7.2f        seen:%2d %s\n", price, priceOccurrence.get(price), timeOrTimes(priceOccurrence.get(price))));
64
-            if (itemPriceIterator.hasNext()) sb.append(String.format("-------------        -------------\n"));
66
+            if (itemPriceIterator.hasNext() && count > 1) sb.append(String.format("-------------        -------------\n"));
67
+            if (count == 1) sb.append(String.format("-------------        -------------\n"));
65 68
         }
66 69
         return sb.toString();
67 70
     }

+ 28
- 0
src/main/resources/TestData.txt View File

@@ -0,0 +1,28 @@
1
+naMe:MRShMll0;price:3.23;type:Food;expiration:10/25/2022
2
+                ##naME:c0cAC0la;price:1.23;type:Food;expiration:1/02/2016
3
+                ##NAMe:PREtzels;price:1.23;type:Food;expiration:2/25/2016
4
+                ##naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016
5
+                ##naMe:TwINKies;price:4.25;type:Food%expiration:1/25/2016
6
+                ##naMe:3DozEggs;price:2.25;type:Food*expiration:1/25/2016
7
+                ##naMe:PANCKEMX;price:1.25;type:Food;expiration:3/22/2016
8
+                ##naMe:COOkieS;price:2.25;type:Food;expiration:1/25/2016
9
+                ##NAME:2lbShrmp;price:3.23;type:Food;expiration:1/17/2016
10
+                ##naMe:Snpple24;price:10.23;type:Food!expiration:4/25/2016
11
+                ##naMe:aplJaX;price:0.25;type:Food;expiration:1/23/2016
12
+                ##naMe:aplJAx;price:0.23;type:Food;expiration:5/02/2016
13
+                ##NAMe:;price:1.23;type:Food;expiration:1/25/2016
14
+                ##naMe:;price:3.23;type:Food;expiration:1/04/2016
15
+                ##naMe:Milk;price:3.23;type:Food;expiration:1/25/2016
16
+                ##naME:BreaD;price:1.23;type:Food@expiration:1/02/2016
17
+                ##NAMe:BrEAD;price:1.23;type:Food@expiration:2/25/2016
18
+                ##naMe:MiLK;priCe:;type:Food;expiration:1/11/2016
19
+                ##naMe:Cookies;price:2.15;type:Food;expiration:1/25/2016
20
+                ##naMe:Co0kieS;pRice:2.25;type:Food;expiration:1/25/2016
21
+                ##naMe:COokIes;price:2.45;type:Food;expiration:3/22/2016
22
+                ##naMe:COOkieS;Price:2.25;type:Food;expiration:1/25/2016
23
+                ##NAME:MilK;price:3.23;type:Food;expiration:1/17/2016
24
+                ##naMe:MilK;priCe:;type:Food;expiration:4/25/2016
25
+                ##naMe:apPles;prIce:0.25;type:Food;expiration:1/23/2016
26
+                ##naMe:apPles;pRice:0.23;type:Food;expiration:5/02/2016
27
+                ##NAMe:BrEAD;price:1.23;type:Food;expiration:1/25/2016
28
+                ##naMe:;price:3.23;type:Food^expiration:1/04/2016##

+ 188
- 0
src/test/java/io/zipcoder/MainTest.java View File

@@ -0,0 +1,188 @@
1
+package io.zipcoder;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+public class MainTest {
8
+
9
+    private Main mainTest = new Main();
10
+    @Before
11
+    public void Setup() {
12
+
13
+
14
+
15
+        String originalRawData = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016" +
16
+                "##naME:BreaD;price:1.23;type:Food;expiration:1/02/2016" +
17
+                "##NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016" +
18
+                "##naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016" +
19
+                "##naMe:Cookies;price:2.25;type:Food%expiration:1/25/2016" +
20
+                "##naMe:CoOkieS;price:2.25;type:Food*expiration:1/25/2016" +
21
+                "##naMe:COokIes;price:2.25;type:Food;expiration:3/22/2016" +
22
+                "##naMe:COOkieS;price:2.25;type:Food;expiration:1/25/2016" +
23
+                "##NAME:MilK;price:3.23;type:Food;expiration:1/17/2016" +
24
+                "##naMe:MilK;price:1.23;type:Food!expiration:4/25/2016" +
25
+                "##naMe:apPles;price:0.25;type:Food;expiration:1/23/2016" +
26
+                "##naMe:apPles;price:0.23;type:Food;expiration:5/02/2016" +
27
+                "##NAMe:BrEAD;price:1.23;type:Food;expiration:1/25/2016" +
28
+                "##naMe:;price:3.23;type:Food;expiration:1/04/2016" +
29
+                "##naMe:Milk;price:3.23;type:Food;expiration:1/25/2016" +
30
+                "##naME:BreaD;price:1.23;type:Food@expiration:1/02/2016" +
31
+                "##NAMe:BrEAD;price:1.23;type:Food@expiration:2/25/2016" +
32
+                "##naMe:MiLK;priCe:;type:Food;expiration:1/11/2016" +
33
+                "##naMe:Cookies;price:2.25;type:Food;expiration:1/25/2016" +
34
+                "##naMe:Co0kieS;pRice:2.25;type:Food;expiration:1/25/2016" +
35
+                "##naMe:COokIes;price:2.25;type:Food;expiration:3/22/2016" +
36
+                "##naMe:COOkieS;Price:2.25;type:Food;expiration:1/25/2016" +
37
+                "##NAME:MilK;price:3.23;type:Food;expiration:1/17/2016" +
38
+                "##naMe:MilK;priCe:;type:Food;expiration:4/25/2016" +
39
+                "##naMe:apPles;prIce:0.25;type:Food;expiration:1/23/2016" +
40
+                "##naMe:apPles;pRice:0.23;type:Food;expiration:5/02/2016" +
41
+                "##NAMe:BrEAD;price:1.23;type:Food;expiration:1/25/2016" +
42
+                "##naMe:;price:3.23;type:Food^expiration:1/04/2016##";
43
+
44
+        String practice = "naMe:MaRShMAll0w;price:3.23;type:Food;expiration:10/25/2022" +
45
+                "##naME:c0cAC0la;price:1.23;type:Food;expiration:1/02/2016" +
46
+                "##NAMe:PREtzels;price:1.23;type:Food;expiration:2/25/2016" +
47
+                "##naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016" +
48
+                "##naMe:TwINKies;price:4.25;type:Food%expiration:1/25/2016" +
49
+                "##naMe:3DozEggs;price:2.25;type:Food*expiration:1/25/2016" +
50
+                "##naMe:PANCAKEMIX;price:1.25;type:Food;expiration:3/22/2016" +
51
+                "##naMe:COOkieS;price:2.25;type:Food;expiration:1/25/2016" +
52
+                "##NAME:2lbShrmp;price:3.23;type:Food;expiration:1/17/2016" +
53
+                "##naMe:Snpple24;price:10.23;type:Food!expiration:4/25/2016" +
54
+                "##naMe:aplJax;price:0.25;type:Food;expiration:1/23/2016" +
55
+                "##naMe:aplJax;price:0.23;type:Food;expiration:5/02/2016" +
56
+                "##NAMe:;price:1.23;type:Food;expiration:1/25/2016" +
57
+                "##naMe:;price:3.23;type:Food;expiration:1/04/2016" +
58
+                "##naMe:Milk;price:3.23;type:Food;expiration:1/25/2016" +
59
+                "##naME:BreaD;price:1.23;type:Food@expiration:1/02/2016" +
60
+                "##NAMe:BrEAD;price:1.23;type:Food@expiration:2/25/2016" +
61
+                "##naMe:MiLK;priCe:;type:Food;expiration:1/11/2016" +
62
+                "##naMe:Cookies;price:2.25;type:Food;expiration:1/25/2016" +
63
+                "##naMe:Co0kieS;pRice:2.25;type:Food;expiration:1/25/2016" +
64
+                "##naMe:COokIes;price:2.25;type:Food;expiration:3/22/2016" +
65
+                "##naMe:COOkieS;Price:2.25;type:Food;expiration:1/25/2016" +
66
+                "##NAME:MilK;price:3.23;type:Food;expiration:1/17/2016" +
67
+                "##naMe:MilK;priCe:;type:Food;expiration:4/25/2016" +
68
+                "##naMe:apPles;prIce:0.25;type:Food;expiration:1/23/2016" +
69
+                "##naMe:apPles;pRice:0.23;type:Food;expiration:5/02/2016" +
70
+                "##NAMe:BrEAD;price:1.23;type:Food;expiration:1/25/2016" +
71
+                "##naMe:;price:3.23;type:Food^expiration:1/04/2016##";
72
+
73
+
74
+
75
+    }
76
+    @Test
77
+    public void MainTest()throws Exception {
78
+        String expected = "name:    Milk \t\t seen: 6 times\n" +
79
+                "============= \t \t =============\n" +
80
+                "Price: \t 3.23\t\t seen: 5 times\n" +
81
+                "-------------\t\t -------------\n" +
82
+                "Price:   1.23\t\t seen: 1 time\n" +
83
+                "\n" +
84
+                "name:   Bread\t\t seen: 6 times\n" +
85
+                "=============\t\t =============\n" +
86
+                "Price:   1.23\t\t seen: 6 times\n" +
87
+                "-------------\t\t -------------\n" +
88
+                "\n" +
89
+                "name: Cookies     \t seen: 8 times\n" +
90
+                "=============     \t =============\n" +
91
+                "Price:   2.25        seen: 8 times\n" +
92
+                "-------------        -------------\n" +
93
+                "\n" +
94
+                "name:  Apples     \t seen: 4 times\n" +
95
+                "=============     \t =============\n" +
96
+                "Price:   0.25     \t seen: 2 times\n" +
97
+                "-------------     \t -------------\n" +
98
+                "Price:   0.23  \t \t seen: 2 times\n" +
99
+                "\n" +
100
+                "Errors         \t \t seen: 4 times\n";
101
+
102
+        String actual = mainTest.readRawDataToString("RawData.txt");
103
+
104
+
105
+        //This test is simply here to visually compare expected vs actual
106
+
107
+        Assert.assertEquals(expected, actual);
108
+    }
109
+
110
+    @Test
111
+    public void gettingCrazyTest() throws Exception {
112
+
113
+        String expected = "name:Mrshmllo        seen: 1 time\n" +
114
+                "=============        =============\n" +
115
+                "Price:   3.23        seen: 1 time\n" +
116
+                "-------------        -------------\n" +
117
+                "\n" +
118
+                "name:Cocacola        seen: 1 time\n" +
119
+                "=============        =============\n" +
120
+                "Price:   1.23        seen: 1 time\n" +
121
+                "-------------        -------------\n" +
122
+                "\n" +
123
+                "name:Pretzels        seen: 1 time\n" +
124
+                "=============        =============\n" +
125
+                "Price:   1.23        seen: 1 time\n" +
126
+                "-------------        -------------\n" +
127
+                "\n" +
128
+                "name:    Milk        seen: 3 times\n" +
129
+                "=============        =============\n" +
130
+                "Price:   3.23        seen: 3 times\n" +
131
+                "-------------        -------------\n" +
132
+                "\n" +
133
+                "name:Twinkies        seen: 1 time\n" +
134
+                "=============        =============\n" +
135
+                "Price:   4.25        seen: 1 time\n" +
136
+                "-------------        -------------\n" +
137
+                "\n" +
138
+                "name:3dozeggs        seen: 1 time\n" +
139
+                "=============        =============\n" +
140
+                "Price:   2.25        seen: 1 time\n" +
141
+                "-------------        -------------\n" +
142
+                "\n" +
143
+                "name:Panckemx        seen: 1 time\n" +
144
+                "=============        =============\n" +
145
+                "Price:   1.25        seen: 1 time\n" +
146
+                "-------------        -------------\n" +
147
+                "\n" +
148
+                "name: Cookies        seen: 5 times\n" +
149
+                "=============        =============\n" +
150
+                "Price:   2.25        seen: 3 times\n" +
151
+                "-------------        -------------\n" +
152
+                "Price:   2.15        seen: 1 time\n" +
153
+                "-------------        -------------\n" +
154
+                "Price:   2.45        seen: 1 time\n" +
155
+                "\n" +
156
+                "name:2lbshrmp        seen: 1 time\n" +
157
+                "=============        =============\n" +
158
+                "Price:   3.23        seen: 1 time\n" +
159
+                "-------------        -------------\n" +
160
+                "\n" +
161
+                "name:Snpple24        seen: 1 time\n" +
162
+                "=============        =============\n" +
163
+                "Price:  10.23        seen: 1 time\n" +
164
+                "-------------        -------------\n" +
165
+                "\n" +
166
+                "name:  Apljax        seen: 2 times\n" +
167
+                "=============        =============\n" +
168
+                "Price:   0.25        seen: 1 time\n" +
169
+                "-------------        -------------\n" +
170
+                "Price:   0.23        seen: 1 time\n" +
171
+                "\n" +
172
+                "name:   Bread        seen: 3 times\n" +
173
+                "=============        =============\n" +
174
+                "Price:   1.23        seen: 3 times\n" +
175
+                "-------------        -------------\n" +
176
+                "\n" +
177
+                "name:  Apples        seen: 2 times\n" +
178
+                "=============        =============\n" +
179
+                "Price:   0.25        seen: 1 time\n" +
180
+                "-------------        -------------\n" +
181
+                "Price:   0.23        seen: 1 time\n" +
182
+                "\n" +
183
+                "Errors               seen: 5 times\n";
184
+        String actual = mainTest.readRawDataToString("TestData.txt");
185
+        Assert.assertEquals(expected, actual);
186
+
187
+    }
188
+}