Browse Source

Code cleaned up and all tests written and passed with 100% coverage

Lauren Green 6 years ago
parent
commit
bd3ce27eb2

+ 56
- 50
src/main/java/com/zipcodewilmington/gildedrose/Inventory.java View File

@@ -2,65 +2,71 @@ package com.zipcodewilmington.gildedrose;
2 2
 
3 3
 
4 4
 public class Inventory {
5
-    private Item[] items;
6 5
 
7
-    public Inventory(Item[] items) {
6
+    private static Item[] items;
7
+
8
+    public Inventory(Item[] arr) {
8 9
         super();
9
-        this.items = items;
10
+        this.items = arr;
11
+    }
12
+
13
+    public void updateSellIn() {
14
+        int j = 0;
15
+
16
+        if (items[j].getSellIn() > 0) {
17
+            if (!items[j].getName().equals("Sulfuras"))
18
+                items[j].setSellIn(items[j].getSellIn() - 1);
19
+                j++;
20
+        }
10 21
     }
11 22
 
23
+    private void decreaseQualityByOne(int i) {
24
+
25
+        items[i].setQuality(items[i].getQuality() - 1);
26
+
27
+    }
28
+
29
+    private void decreaseQualityByTwo(int i) {
30
+
31
+        decreaseQualityByOne(i);
32
+        decreaseQualityByOne(i);
33
+
34
+    }
35
+
36
+    private void increaseQualityByOne(int i) {
37
+
38
+        items[i].setQuality(items[i].getQuality() + 1);
39
+
40
+    }
41
+
42
+    private void increaseQualityByTwo(int i) {
43
+
44
+        increaseQualityByOne(i);
45
+        increaseQualityByOne(i);
46
+
47
+    }
12 48
 
13 49
     public void updateQuality() {
50
+
14 51
         for (int i = 0; i < items.length; i++) {
15
-            if (!items[i].getName().equals("Aged Brie")
16
-                    && !items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
17
-                if (items[i].getQuality() > 0) {
18
-                    if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
19
-                        items[i].setQuality(items[i].getQuality() - 1);
20
-                    }
21
-                }
52
+        if (!items[i].getName().equals("Sulfuras") && !(items[i].getQuality() == 0) && !(items[i].getQuality() >= 50)) {
53
+            if (items[i].getName().equals("Aged Brie")) {
54
+                increaseQualityByOne(i);
55
+            } else if (items[i].getName().equals("Backstage Passes") && (items[i].getSellIn() <= 10)) {
56
+                increaseQualityByTwo(i);
57
+                if ((items[i].getSellIn() <= 10)) {
58
+                increaseQualityByOne(i);
59
+            } } else if (items[i].getSellIn() == 0) {
60
+                decreaseQualityByTwo(i);
22 61
             } else {
23
-                if (items[i].getQuality() < 50) {
24
-                    items[i].setQuality(items[i].getQuality() + 1);
25
-
26
-                    if (items[i].getName() == "Backstage passes to a TAFKAL80ETC concert") {
27
-                        if (items[i].getSellIn() < 11) {
28
-                            if (items[i].getQuality() < 50) {
29
-                                items[i].setQuality(items[i].getQuality() + 1);
30
-                            }
31
-                        }
32
-
33
-                        if (items[i].getSellIn() < 6) {
34
-                            if (items[i].getQuality() < 50) {
35
-                                items[i].setQuality(items[i].getQuality() + 1);
36
-                            }
37
-                        }
38
-                    }
39
-                }
40
-            }
41
-
42
-            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
43
-                items[i].setSellIn(items[i].getSellIn() - 1);
62
+                decreaseQualityByOne(i);
44 63
             }
45 64
 
46
-            if (items[i].getSellIn() < 0) {
47
-                if (!items[i].getName().equals("Aged Brie")) {
48
-                    if (!items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
49
-                        if (items[i].getQuality() > 0) {
50
-                            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
51
-                                items[i].setQuality(items[i].getQuality() - 1);
52
-                            }
53
-                        }
54
-                    } else {
55
-                        items[i].setQuality(items[i].getQuality()
56
-                                - items[i].getQuality());
57
-                    }
58
-                } else {
59
-                    if (items[i].getQuality() < 50) {
60
-                        items[i].setQuality(items[i].getQuality() + 1);
61
-                    }
62
-                }
63
-            }
64 65
         }
66
+
67
+
65 68
     }
66
-}
69
+    }
70
+
71
+}
72
+

+ 247
- 2
src/test/java/com/zipcodewilmington/gildedrose/InventoryTest.java View File

@@ -1,12 +1,257 @@
1 1
 package com.zipcodewilmington.gildedrose;
2 2
 
3 3
 import org.junit.Assert;
4
+import org.junit.Before;
4 5
 import org.junit.Test;
5 6
 
6 7
 public class InventoryTest {
8
+
7 9
     @Test
8
-    public void updateQuantityTest(){
9
-        Assert.assertEquals(1, 1);
10
+    public void setNameTest(){
11
+        //Given
12
+        Item candle = new Item("Scented Candle", 30, 40);
13
+        String expected = "Candle";
14
+
15
+        //When
16
+        candle.setName("Candle");
17
+
18
+        //Then
19
+        String actual = candle.getName();
20
+        Assert.assertEquals(expected, actual);
10 21
     }
22
+
23
+    @Test
24
+    public void setQualityTest(){
25
+        //Given
26
+        Item candle = new Item("Scented Candle", 30, 40);
27
+        int expected = 25;
28
+
29
+        //When
30
+        candle.setQuality(25);
31
+
32
+        //Then
33
+        int actual = candle.getQuality();
34
+        Assert.assertEquals(expected, actual);
35
+    }
36
+
37
+
38
+    @Test
39
+    public void getNameTest(){
40
+        //Given
41
+        String expected = "Scented Candle";
42
+
43
+        //When
44
+        Item candle = new Item("Scented Candle", 30, 40);
45
+
46
+        //Then
47
+        String actual = candle.getName();
48
+        Assert.assertEquals(expected, actual);
49
+    }
50
+
51
+    @Test
52
+    public void getSellInTest(){
53
+        //Given
54
+        int expected = 30;
55
+
56
+        //When
57
+        Item candle = new Item("Scented Candle", 30, 40);
58
+
59
+        //Then
60
+        int actual = candle.getSellIn();
61
+        Assert.assertEquals(expected, actual);
62
+    }
63
+
64
+    @Test
65
+    public void getQualityTest(){
66
+        //Given
67
+        int expected = 40;
68
+
69
+        //When
70
+        Item candle = new Item("Scented Candle", 30, 40);
71
+
72
+        //Then
73
+        int actual = candle.getQuality();
74
+        Assert.assertEquals(expected, actual);
75
+    }
76
+
77
+    @Test
78
+    public void sulfurasUpdateSellInTest(){
79
+        //Given
80
+        Item sulfuras = new Item("Sulfuras", 0, 80);
81
+        Item[] itemArr = new Item[1];
82
+        itemArr[0] = sulfuras;
83
+        Inventory newInv = new Inventory(itemArr);
84
+        int expected = 0;
85
+
86
+        //When
87
+        newInv.updateSellIn();
88
+
89
+        //Then
90
+        int actual = sulfuras.getSellIn();
91
+
92
+        Assert.assertEquals(expected, actual);
93
+    }
94
+
95
+    @Test
96
+    public void itemAtZeroUpdateSellInTest(){
97
+        //Given
98
+        Item wine = new Item("Wine", 0, 40);
99
+        Item[] itemArr = new Item[1];
100
+        itemArr[0] = wine;
101
+        Inventory newInv = new Inventory(itemArr);
102
+        int expected = 0;
103
+
104
+        //When
105
+        newInv.updateSellIn();
106
+
107
+        //Then
108
+        int actual = wine.getSellIn();
109
+
110
+        Assert.assertEquals(expected, actual);
111
+    }
112
+
113
+
114
+
115
+    @Test
116
+    public void allOthersUpdateSellInTest(){
117
+        //Given
118
+        Item candle = new Item("Scented Candle", 30, 40);
119
+        Item[] itemArr = new Item[1];
120
+        itemArr[0] = candle;
121
+        Inventory newInv = new Inventory(itemArr);
122
+        int expected = 29;
123
+
124
+        //When
125
+        newInv.updateSellIn();
126
+
127
+        //
128
+        int actual = candle.getSellIn();
129
+
130
+        Assert.assertEquals(expected, actual);
131
+    }
132
+
133
+    @Test
134
+    public void sulfurasUpdateQualityTest(){
135
+        //Given
136
+        Item sulfuras = new Item("Sulfuras", 0, 80);
137
+        Item[] itemArr = new Item[1];
138
+        itemArr[0] = sulfuras;
139
+        Inventory newInv = new Inventory(itemArr);
140
+        int expected = 80;
141
+
142
+        //When
143
+        newInv.updateSellIn();
144
+        newInv.updateQuality();
145
+
146
+        //Then
147
+        int actual = sulfuras.getQuality();
148
+
149
+        Assert.assertEquals(expected, actual);
150
+    }
151
+
152
+    @Test
153
+    public void backstagePasses9DaysUpdateQualityTest(){
154
+        //Given
155
+        Item backstagePass = new Item("Backstage Passes", 9, 10);
156
+        Item[] itemArr = new Item[1];
157
+        itemArr[0] = backstagePass;
158
+        Inventory newInv = new Inventory(itemArr);
159
+        int expected = 12;
160
+
161
+        //When
162
+        newInv.updateSellIn();
163
+        newInv.updateQuality();
164
+
165
+        //Then
166
+        int actual = backstagePass.getQuality();
167
+
168
+        Assert.assertEquals(expected, actual);
169
+    }
170
+
171
+    @Test
172
+    public void backstagePasses3DaysUpdateQualityTest(){
173
+        //Given
174
+        Item backstagePass = new Item("Backstage Passes", 3, 10);
175
+        Item[] itemArr = new Item[1];
176
+        itemArr[0] = backstagePass;
177
+        Inventory newInv = new Inventory(itemArr);
178
+        int expected = 13;
179
+
180
+        //When
181
+        newInv.updateSellIn();
182
+        newInv.updateQuality();
183
+
184
+        //Then
185
+        int actual = backstagePass.getQuality();
186
+
187
+        Assert.assertEquals(expected, actual);
188
+    }
189
+
190
+    @Test
191
+    public void agedBrieUpdateQualityTest(){
192
+        //Given
193
+        Item agedBrie = new Item("Aged Brie", 20, 5);
194
+        Item[] itemArr = new Item[1];
195
+        itemArr[0] = agedBrie;
196
+        Inventory newInv = new Inventory(itemArr);
197
+        int expected = 6;
198
+
199
+        //When
200
+        newInv.updateSellIn();
201
+        newInv.updateQuality();
202
+
203
+        //Then
204
+        int actual = agedBrie.getQuality();
205
+
206
+        Assert.assertEquals(expected, actual);
207
+    }
208
+
209
+    @Test
210
+    public void sellinIs0UpdateQualityTest(){
211
+        //Given
212
+        Item candle = new Item("Scented Candle", 0, 40);
213
+        Item[] itemArr = new Item[1];
214
+        itemArr[0] = candle;
215
+        Inventory newInv = new Inventory(itemArr);
216
+        int expected = 38;
217
+
218
+        //When
219
+        newInv.updateSellIn();
220
+        newInv.updateQuality();
221
+
222
+        //Then
223
+        int actual = candle.getQuality();
224
+
225
+        Assert.assertEquals(expected, actual);
226
+    }
227
+
228
+    @Test
229
+    public void defaultUpdateQualityTest(){
230
+        //Given
231
+        Item candle = new Item("Scented Candle", 30, 40);
232
+        Item[] itemArr = new Item[1];
233
+        itemArr[0] = candle;
234
+        Inventory newInv = new Inventory(itemArr);
235
+        int expected = 39;
236
+
237
+        //When
238
+        newInv.updateSellIn();
239
+        newInv.updateQuality();
240
+
241
+        //Then
242
+        int actual = candle.getQuality();
243
+
244
+        Assert.assertEquals(expected, actual);
245
+    }
246
+
11 247
 }
12 248
 
249
+/*
250
+Item agedBrie = new Item("Aged Brie", 20, 5);
251
+        itemArr[1] = agedBrie;
252
+        Item backstagePass = new Item("Backstage Passes", 15, 10);
253
+        itemArr[2] = backstagePass;
254
+        Item candle = new Item("Scented Candle", 30, 40);
255
+        itemArr[3] = candle;
256
+        Item avocado = new Item("Avocado", 0, 0);
257
+        itemArr[4] = avocado;*/