Nicholas Satinover 6 years ago
parent
commit
761d0bf38c

+ 49
- 42
src/main/java/com/zipcodewilmington/gildedrose/Inventory.java View File

@@ -12,55 +12,62 @@ public class Inventory {
12 12
 
13 13
     public void updateQuality() {
14 14
         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
-                    }
15
+            if (items[i].getName().equals("Conjured")){
16
+                int temp = items[i].getQuality() == 1 ? 1 : 2;
17
+                items[i].setQuality(items[i].getQuality() - temp);
18
+            }
19
+            else{
20
+                if (items[i].getQuality() > 0 && !items[i].getName().matches("Aged Brie|Backstage passes to a TAFKAL80ETC concert|Sulfuras, Hand of Ragnaros")) {
21
+                    items[i].setQuality(items[i].getQuality() - 1);
22
+                } else {
23
+                    updateBasicORConcert(i);
21 24
                 }
22
-            } else {
23
-                if (items[i].getQuality() < 50) {
24
-                    items[i].setQuality(items[i].getQuality() + 1);
25 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
-                    }
26
+                if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
27
+                    items[i].setSellIn(items[i].getSellIn() - 1);
28
+                }
29
+                if (items[i].getSellIn() < 0) {
30
+                    sellInUnderZero(i);
39 31
                 }
40 32
             }
33
+        }//loop
34
+    }//updateQuality
41 35
 
42
-            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
43
-                items[i].setSellIn(items[i].getSellIn() - 1);
36
+    /**
37
+     * Helper method that will update any 'basic' item or a concert item
38
+     * @param i
39
+     */
40
+    private void updateBasicORConcert(int i){
41
+        if (items[i].getQuality() < 50) {
42
+            items[i].setQuality(items[i].getQuality() + 1);
43
+            if (items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert") && items[i].getQuality() < 50) {
44
+                if (items[i].getSellIn() < 11) {
45
+                    items[i].setQuality(items[i].getQuality() + 1);
46
+                }if (items[i].getSellIn() < 6) {
47
+                    items[i].setQuality(items[i].getQuality() + 1);
48
+                }
44 49
             }
50
+        }
51
+    }
45 52
 
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
-                }
53
+    /**
54
+     * Helper method to determine if Quality should be set to zero or incremented
55
+     * @param i
56
+     */
57
+    private void sellInUnderZero(int i){
58
+        if (!items[i].getName().equals("Aged Brie")) {
59
+            if (items[i].getQuality() > 0 && !items[i].getName().matches("Backstage passes to a TAFKAL80ETC concert|Sulfuras, Hand of Ragnaros")) {
60
+                items[i].setQuality(items[i].getQuality() - 1);
61
+            }
62
+            else {
63
+                items[i].setQuality(0);
63 64
             }
64 65
         }
66
+        else if (items[i].getQuality() < 50) {
67
+            items[i].setQuality(items[i].getQuality() + 1);
68
+        }
65 69
     }
66
-}
70
+}
71
+
72
+
73
+

+ 67
- 0
src/main/java/com/zipcodewilmington/gildedrose/InventoryOriginal.java View File

@@ -0,0 +1,67 @@
1
+package com.zipcodewilmington.gildedrose;
2
+
3
+/*
4
+public class InventoryOriginal {
5
+    private Item[] items;
6
+
7
+    public InventoryOriginal(Item[] items) {
8
+        super();
9
+        this.items = items;
10
+    }
11
+
12
+
13
+    public void updateQuality() {
14
+        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
+                }
22
+            } 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);
44
+            }
45
+
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
+*/

+ 248
- 0
src/test/java/com/zipcodewilmington/gildedrose/InventoryOriginalTest.java View File

@@ -0,0 +1,248 @@
1
+/*
2
+package com.zipcodewilmington.gildedrose;
3
+
4
+import org.junit.Assert;
5
+import org.junit.Test;
6
+
7
+public class InventoryOriginalTest {
8
+    @Test
9
+    public void agedBrieTen(){
10
+        //Given
11
+        Item agedBrie = new Item("Aged Brie", 10, 47);
12
+            Item[] basicItems = new Item[1];
13
+            basicItems[0] = agedBrie;
14
+            InventoryOriginal inventoryOriginal = new InventoryOriginal(basicItems);
15
+        int expected = 48;
16
+        inventoryOriginal.updateQuality();
17
+
18
+        //When
19
+        int actual = agedBrie.getQuality();
20
+
21
+        //Then
22
+        Assert.assertEquals(expected, actual);
23
+    }
24
+
25
+    @Test
26
+    public void agedBrieQuality50(){
27
+        //Given
28
+        Item agedBrie = new Item("Aged Brie", 11, 50);
29
+        Item[] basicItems = new Item[1];
30
+        basicItems[0] = agedBrie;
31
+        InventoryOriginal inventoryOriginal = new InventoryOriginal(basicItems);
32
+        int expected = 50;
33
+        inventoryOriginal.updateQuality();
34
+
35
+        //When
36
+        int actual = agedBrie.getQuality();
37
+
38
+        //Then
39
+        Assert.assertEquals(expected, actual);
40
+    }
41
+
42
+    @Test
43
+    public void agedBrieExpired(){
44
+        //Given
45
+        Item agedBrie = new Item("Aged Brie", -1, 48);
46
+        Item[] basicItems = new Item[1];
47
+        basicItems[0] = agedBrie;
48
+        InventoryOriginal inventoryOriginal = new InventoryOriginal(basicItems);
49
+        int expected = 50;
50
+        inventoryOriginal.updateQuality();
51
+
52
+        //When
53
+        int actual = agedBrie.getQuality();
54
+
55
+        //Then
56
+        Assert.assertEquals(expected, actual);
57
+    }
58
+
59
+    @Test
60
+    public void agedBrieExpiredMaxQuality(){
61
+        //Given
62
+        Item agedBrie = new Item("Aged Brie", -1, 50);
63
+        Item[] basicItems = new Item[1];
64
+        basicItems[0] = agedBrie;
65
+        InventoryOriginal inventoryOriginal = new InventoryOriginal(basicItems);
66
+        int expected = 50;
67
+        inventoryOriginal.updateQuality();
68
+
69
+        //When
70
+        int actual = agedBrie.getQuality();
71
+
72
+        //Then
73
+        Assert.assertEquals(expected, actual);
74
+    }
75
+
76
+
77
+
78
+    @Test
79
+    public void stagePassQualityOne(){
80
+        //Given
81
+        Item stagePass = new Item("Backstage passes to a TAFKAL80ETC concert", 11, 20);
82
+            Item[] basicItems = new Item[1];
83
+            basicItems[0] = stagePass;
84
+            InventoryOriginal inventoryOriginal = new InventoryOriginal(basicItems);
85
+        int expected = 21;
86
+        inventoryOriginal.updateQuality();
87
+
88
+        //When
89
+        int actual = stagePass.getQuality();
90
+
91
+        //Then
92
+        Assert.assertEquals(expected, actual);
93
+    }
94
+
95
+    @Test
96
+    public void stagePassQualityTwo(){
97
+        //Given
98
+        Item stagePass = new Item("Backstage passes to a TAFKAL80ETC concert", 10, 20);
99
+        Item[] basicItems = new Item[1];
100
+        basicItems[0] = stagePass;
101
+        InventoryOriginal inventoryOriginal = new InventoryOriginal(basicItems);
102
+        int expected = 22;
103
+        inventoryOriginal.updateQuality();
104
+
105
+        //When
106
+        int actual = stagePass.getQuality();
107
+
108
+        //Then
109
+        Assert.assertEquals(expected, actual);
110
+    }
111
+
112
+    @Test
113
+    public void stagePassQualityThree(){
114
+        //Given
115
+        Item stagePass = new Item("Backstage passes to a TAFKAL80ETC concert", 5, 20);
116
+        Item[] basicItems = new Item[1];
117
+        basicItems[0] = stagePass;
118
+        InventoryOriginal inventoryOriginal = new InventoryOriginal(basicItems);
119
+        int expected = 23;
120
+        inventoryOriginal.updateQuality();
121
+
122
+        //When
123
+        int actual = stagePass.getQuality();
124
+
125
+        //Then
126
+        Assert.assertEquals(expected, actual);
127
+    }
128
+
129
+    @Test
130
+    public void stagePassQualityZero(){
131
+        //Given
132
+        Item stagePass = new Item("Backstage passes to a TAFKAL80ETC concert", 0, 20);
133
+        Item[] basicItems = new Item[1];
134
+        basicItems[0] = stagePass;
135
+        InventoryOriginal inventoryOriginal = new InventoryOriginal(basicItems);
136
+        int expected = 0;
137
+        inventoryOriginal.updateQuality();
138
+
139
+        //When
140
+        int actual = stagePass.getQuality();
141
+
142
+        //Then
143
+        Assert.assertEquals(expected, actual);
144
+    }
145
+
146
+    @Test
147
+    public void sulfuras(){
148
+        //Given
149
+        Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 05, 80);
150
+            Item[] basicItems = new Item[1];
151
+            basicItems[0] = sulfuras;
152
+            InventoryOriginal inventoryOriginal = new InventoryOriginal(basicItems);
153
+        int expected = 80;
154
+        inventoryOriginal.updateQuality();
155
+
156
+        //When
157
+        int actual = sulfuras.getQuality();
158
+
159
+        //Then
160
+        Assert.assertEquals(expected, actual);
161
+    }
162
+
163
+    @Test
164
+    public void newItem(){
165
+        //Given
166
+        Item newItem = new Item("New Item", 05, 49);
167
+        Item[] basicItems = new Item[1];
168
+        basicItems[0] = newItem;
169
+        InventoryOriginal inventoryOriginal = new InventoryOriginal(basicItems);
170
+        int expected = 48;
171
+        inventoryOriginal.updateQuality();
172
+
173
+        //When
174
+        int actual = newItem.getQuality();
175
+
176
+        //Then
177
+        Assert.assertEquals(expected, actual);
178
+    }
179
+
180
+    @Test
181
+    public void changeItemName(){
182
+        //Given
183
+        Item newItem = new Item("New Item", 02, 49);
184
+        Item[] basicItems = new Item[1];
185
+        basicItems[0] = newItem;
186
+        InventoryOriginal inventoryOriginal = new InventoryOriginal(basicItems);
187
+        newItem.setName("Changed Name");
188
+        String expected = "Changed Name";
189
+        inventoryOriginal.updateQuality();
190
+
191
+        //When
192
+        String actual = newItem.getName();
193
+
194
+        //Then
195
+        Assert.assertEquals(expected, actual);
196
+    }
197
+
198
+    @Test
199
+    public void passedSellIn(){
200
+        //Given
201
+        Item newItem = new Item("Expired", -1, 10);
202
+        Item[] basicItems = new Item[1];
203
+        basicItems[0] = newItem;
204
+        InventoryOriginal inventoryOriginal = new InventoryOriginal(basicItems);
205
+        int expected = 8;
206
+        inventoryOriginal.updateQuality();
207
+
208
+        //When
209
+        int actual = newItem.getQuality();
210
+
211
+        //Then
212
+        Assert.assertEquals(expected, actual);
213
+    }
214
+}
215
+
216
+
217
+
218
+
219
+
220
+
221
+
222
+First an introduction to our system:
223
+
224
+All items have
225
+    SellIn value
226
+        number of days we have to sell the item
227
+    Quality value
228
+        how valuable the item is
229
+    End of each day
230
+        lower both values for every item
231
+
232
+Quality of an item is never more than 50
233
+Sell by date has passed
234
+    Quality degrades twice as fast
235
+
236
+Quality
237
+    never negative
238
+
239
+"Aged Brie"
240
+    increases in Quality the older it gets
241
+"Sulfuras"
242
+    "Sulfuras" is a legendary item and as such its Quality is 80
243
+    never has to be sold or decreases in Quality
244
+"Backstage passes"
245
+    like aged brie, increases in Quality as it's SellIn value approaches;
246
+        Quality increases by 2 when there are 10 days or less and by 3 when there are 5 days or less but Quality drops to 0 after the concert
247
+ */
248
+

+ 238
- 3
src/test/java/com/zipcodewilmington/gildedrose/InventoryTest.java View File

@@ -5,8 +5,243 @@ import org.junit.Test;
5 5
 
6 6
 public class InventoryTest {
7 7
     @Test
8
-    public void updateQuantityTest(){
9
-        Assert.assertEquals(1, 1);
8
+    public void agedBrieTen(){
9
+        //Given
10
+        Item agedBrie = new Item("Aged Brie", 10, 47);
11
+        Item[] basicItems = new Item[1];
12
+        basicItems[0] = agedBrie;
13
+        Inventory inventory = new Inventory(basicItems);
14
+        int expected = 48;
15
+        inventory.updateQuality();
16
+
17
+        //When
18
+        int actual = agedBrie.getQuality();
19
+
20
+        //Then
21
+        Assert.assertEquals(expected, actual);
22
+    }
23
+
24
+    @Test
25
+    public void agedBrieQuality50(){
26
+        //Given
27
+        Item agedBrie = new Item("Aged Brie", 11, 50);
28
+        Item[] basicItems = new Item[1];
29
+        basicItems[0] = agedBrie;
30
+        Inventory inventory = new Inventory(basicItems);
31
+        int expected = 50;
32
+        inventory.updateQuality();
33
+
34
+        //When
35
+        int actual = agedBrie.getQuality();
36
+
37
+        //Then
38
+        Assert.assertEquals(expected, actual);
39
+    }
40
+
41
+    @Test
42
+    public void agedBrieExpired(){
43
+        //Given
44
+        Item agedBrie = new Item("Aged Brie", -1, 48);
45
+        Item[] basicItems = new Item[1];
46
+        basicItems[0] = agedBrie;
47
+        Inventory inventory = new Inventory(basicItems);
48
+        int expected = 50;
49
+        inventory.updateQuality();
50
+
51
+        //When
52
+        int actual = agedBrie.getQuality();
53
+
54
+        //Then
55
+        Assert.assertEquals(expected, actual);
56
+    }
57
+
58
+    @Test
59
+    public void agedBrieExpiredMaxQuality(){
60
+        //Given
61
+        Item agedBrie = new Item("Aged Brie", -1, 50);
62
+        Item[] basicItems = new Item[1];
63
+        basicItems[0] = agedBrie;
64
+        Inventory inventory = new Inventory(basicItems);
65
+        int expected = 50;
66
+        inventory.updateQuality();
67
+
68
+        //When
69
+        int actual = agedBrie.getQuality();
70
+
71
+        //Then
72
+        Assert.assertEquals(expected, actual);
73
+    }
74
+
75
+
76
+
77
+    @Test
78
+    public void stagePassQualityOne(){
79
+        //Given
80
+        Item stagePass = new Item("Backstage passes to a TAFKAL80ETC concert", 11, 20);
81
+        Item[] basicItems = new Item[1];
82
+        basicItems[0] = stagePass;
83
+        Inventory inventory = new Inventory(basicItems);
84
+        int expected = 21;
85
+        inventory.updateQuality();
86
+
87
+        //When
88
+        int actual = stagePass.getQuality();
89
+
90
+        //Then
91
+        Assert.assertEquals(expected, actual);
92
+    }
93
+
94
+    @Test
95
+    public void stagePassQualityTwo(){
96
+        //Given
97
+        Item stagePass = new Item("Backstage passes to a TAFKAL80ETC concert", 10, 20);
98
+        Item[] basicItems = new Item[1];
99
+        basicItems[0] = stagePass;
100
+        Inventory inventory = new Inventory(basicItems);
101
+        int expected = 22;
102
+        inventory.updateQuality();
103
+
104
+        //When
105
+        int actual = stagePass.getQuality();
106
+
107
+        //Then
108
+        Assert.assertEquals(expected, actual);
109
+    }
110
+
111
+    @Test
112
+    public void stagePassQualityThree(){
113
+        //Given
114
+        Item stagePass = new Item("Backstage passes to a TAFKAL80ETC concert", 5, 20);
115
+        Item[] basicItems = new Item[1];
116
+        basicItems[0] = stagePass;
117
+        Inventory inventory = new Inventory(basicItems);
118
+        int expected = 23;
119
+        inventory.updateQuality();
120
+
121
+        //When
122
+        int actual = stagePass.getQuality();
123
+
124
+        //Then
125
+        Assert.assertEquals(expected, actual);
10 126
     }
11
-}
12 127
 
128
+    @Test
129
+    public void stagePassQualityZero(){
130
+        //Given
131
+        Item stagePass = new Item("Backstage passes to a TAFKAL80ETC concert", 0, 20);
132
+        Item[] basicItems = new Item[1];
133
+        basicItems[0] = stagePass;
134
+        Inventory inventory = new Inventory(basicItems);
135
+        int expected = 0;
136
+        inventory.updateQuality();
137
+
138
+        //When
139
+        int actual = stagePass.getQuality();
140
+
141
+        //Then
142
+        Assert.assertEquals(expected, actual);
143
+    }
144
+
145
+    @Test
146
+    public void sulfuras(){
147
+        //Given
148
+        Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 05, 80);
149
+        Item[] basicItems = new Item[1];
150
+        basicItems[0] = sulfuras;
151
+        Inventory inventory = new Inventory(basicItems);
152
+        int expected = 80;
153
+        inventory.updateQuality();
154
+
155
+        //When
156
+        int actual = sulfuras.getQuality();
157
+
158
+        //Then
159
+        Assert.assertEquals(expected, actual);
160
+    }
161
+
162
+    @Test
163
+    public void newItem(){
164
+        //Given
165
+        Item newItem = new Item("New Item", 05, 49);
166
+        Item[] basicItems = new Item[1];
167
+        basicItems[0] = newItem;
168
+        Inventory inventory = new Inventory(basicItems);
169
+        int expected = 48;
170
+        inventory.updateQuality();
171
+
172
+        //When
173
+        int actual = newItem.getQuality();
174
+
175
+        //Then
176
+        Assert.assertEquals(expected, actual);
177
+    }
178
+
179
+    @Test
180
+    public void changeItemName(){
181
+        //Given
182
+        Item newItem = new Item("New Item", 02, 49);
183
+        Item[] basicItems = new Item[1];
184
+        basicItems[0] = newItem;
185
+        Inventory inventory = new Inventory(basicItems);
186
+        newItem.setName("Changed Name");
187
+        String expected = "Changed Name";
188
+        inventory.updateQuality();
189
+
190
+        //When
191
+        String actual = newItem.getName();
192
+
193
+        //Then
194
+        Assert.assertEquals(expected, actual);
195
+    }
196
+
197
+    @Test
198
+    public void passedSellIn(){
199
+        //Given
200
+        Item newItem = new Item("Expired", -1, 10);
201
+        Item[] basicItems = new Item[1];
202
+        basicItems[0] = newItem;
203
+        Inventory inventory = new Inventory(basicItems);
204
+        int expected = 8;
205
+        inventory.updateQuality();
206
+
207
+        //When
208
+        int actual = newItem.getQuality();
209
+
210
+        //Then
211
+        Assert.assertEquals(expected, actual);
212
+    }
213
+
214
+    @Test
215
+    public void conjured(){
216
+        //Given
217
+        Item conjured = new Item("Conjured", 10, 25);
218
+        Item[] basicItems = new Item[1];
219
+        basicItems[0] = conjured;
220
+        Inventory inventory = new Inventory(basicItems);
221
+        int expected = 23;
222
+        inventory.updateQuality();
223
+
224
+        //When
225
+        int actual = conjured.getQuality();
226
+
227
+        //Then
228
+        Assert.assertEquals(expected, actual);
229
+    }
230
+
231
+    @Test
232
+    public void conjuredatOne(){
233
+        //Given
234
+        Item conjured = new Item("Conjured", 10, 1);
235
+        Item[] basicItems = new Item[1];
236
+        basicItems[0] = conjured;
237
+        Inventory inventory = new Inventory(basicItems);
238
+        int expected = 0;
239
+        inventory.updateQuality();
240
+
241
+        //When
242
+        int actual = conjured.getQuality();
243
+
244
+        //Then
245
+        Assert.assertEquals(expected, actual);
246
+    }
247
+}