Kaynağa Gözat

Wrote cleaner code and had 100% test coverage for the inventory class.

William Brown 6 yıl önce
ebeveyn
işleme
0c00112e1b

+ 26
- 0
src/main/java/com/zipcodewilmington/gildedrose/Inventory.java Dosyayı Görüntüle

@@ -13,6 +13,8 @@ public class Inventory {
13 13
         agedBrie();
14 14
         sulfuras();
15 15
         backstagePasses();
16
+        conjured();
17
+        normalItem();
16 18
     }
17 19
 
18 20
     public void agedBrie(){
@@ -46,4 +48,28 @@ public class Inventory {
46 48
             }
47 49
         }
48 50
     }
51
+
52
+    public void conjured(){
53
+        for(int i = 0; i < items.length; i++){
54
+            if(items[i].getName().equals("conjured")){
55
+                if(items[i].getSellIn() <= 0 && items[i].getQuality() >= 4){
56
+                    items[i].setQuality(items[i].getQuality() - 4);
57
+                } else if(items[i].getSellIn() > 0 && items[i].getQuality() >= 2){
58
+                    items[i].setQuality(items[i].getQuality() - 2);
59
+                }
60
+            }
61
+        }
62
+    }
63
+
64
+    public void normalItem(){
65
+        for(int i = 0; i < items.length; i++){
66
+            if(items[i].getName().equals("normal")){
67
+                if(items[i].getSellIn() <= 0 && items[i].getQuality() >= 2){
68
+                    items[i].setQuality(items[i].getQuality() - 2);
69
+                } else if(items[i].getSellIn() > 0 && items[i].getQuality() >= 1){
70
+                    items[i].setQuality(items[i].getQuality() - 1);
71
+                }
72
+            }
73
+        }
74
+    }
49 75
 }

+ 97
- 9
src/test/java/com/zipcodewilmington/gildedrose/InventoryTest.java Dosyayı Görüntüle

@@ -8,7 +8,7 @@ import java.util.ArrayList;
8 8
 public class InventoryTest {
9 9
 
10 10
     @Test
11
-    public void testAgedBrie1() {
11
+    public void testAgedBrieIncreaseTo11() {
12 12
         Item agedBrie = new Item("Aged Brie", 10, 10);
13 13
         Item[] item = {agedBrie};
14 14
         Inventory inventory = new Inventory(item);
@@ -19,7 +19,7 @@ public class InventoryTest {
19 19
     }
20 20
 
21 21
     @Test
22
-    public void testAgedBrie2() {
22
+    public void testAgedBrieIncreaseTo50() {
23 23
         Item agedBrie = new Item("Aged Brie", 10, 49);
24 24
         Item[] item = {agedBrie};
25 25
         Inventory inventory = new Inventory(item);
@@ -30,7 +30,7 @@ public class InventoryTest {
30 30
     }
31 31
 
32 32
     @Test
33
-    public void testAgedBrie3() {
33
+    public void testAgedBrieOver50() {
34 34
         Item agedBrie = new Item("Aged Brie", 10, 50);
35 35
         Item[] item = {agedBrie};
36 36
         Inventory inventory = new Inventory(item);
@@ -41,7 +41,7 @@ public class InventoryTest {
41 41
     }
42 42
 
43 43
     @Test
44
-    public void testBackstagePass1() {
44
+    public void testBackstagePass10BeforeSellDay() {
45 45
         Item backstage = new Item("Backstage passes to a TAFKAL80ETC concert", 11, 40);
46 46
         Item[] item = {backstage};
47 47
         Inventory inventory = new Inventory(item);
@@ -52,7 +52,7 @@ public class InventoryTest {
52 52
     }
53 53
 
54 54
     @Test
55
-    public void testBackstagePass2() {
55
+    public void testBackstagePass7BeforeSellDay() {
56 56
         Item backstage = new Item("Backstage passes to a TAFKAL80ETC concert", 7, 40);
57 57
         Item[] item = {backstage};
58 58
         Inventory inventory = new Inventory(item);
@@ -63,7 +63,7 @@ public class InventoryTest {
63 63
     }
64 64
 
65 65
     @Test
66
-    public void testBackstagePass3() {
66
+    public void testBackstagePass3BeforeSellDay() {
67 67
         Item backstage = new Item("Backstage passes to a TAFKAL80ETC concert", 3, 40);
68 68
         Item[] item = {backstage};
69 69
         Inventory inventory = new Inventory(item);
@@ -74,7 +74,7 @@ public class InventoryTest {
74 74
     }
75 75
 
76 76
     @Test
77
-    public void testBackstagePass4() {
77
+    public void testBackstagePassOnSellDay() {
78 78
         Item backstage = new Item("Backstage passes to a TAFKAL80ETC concert", 0, 40);
79 79
         Item[] item = {backstage};
80 80
         Inventory inventory = new Inventory(item);
@@ -85,7 +85,7 @@ public class InventoryTest {
85 85
     }
86 86
 
87 87
     @Test
88
-    public void testBackstagePass5() {
88
+    public void testBackstagePassAfterSellDay() {
89 89
         Item backstage = new Item("Backstage passes to a TAFKAL80ETC concert", -1, 40);
90 90
         Item[] item = {backstage};
91 91
         Inventory inventory = new Inventory(item);
@@ -96,7 +96,7 @@ public class InventoryTest {
96 96
     }
97 97
 
98 98
     @Test
99
-    public void testSulfurasPass1() {
99
+    public void testSulfuras() {
100 100
         Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 10, 40);
101 101
         Item[] item = {sulfuras};
102 102
         Inventory inventory = new Inventory(item);
@@ -106,5 +106,93 @@ public class InventoryTest {
106 106
         Assert.assertEquals(expected, actual);
107 107
     }
108 108
 
109
+    @Test
110
+    public void testConjuredBeforeSellDay() {
111
+        Item conjured = new Item("conjured", 5, 40);
112
+        Item[] item = {conjured};
113
+        Inventory inventory = new Inventory(item);
114
+        inventory.updateQuality();
115
+        int expected = 38;
116
+        int actual = conjured.getQuality();
117
+        Assert.assertEquals(expected, actual);
118
+    }
119
+
120
+    @Test
121
+    public void testConjuredAfterSellDay() {
122
+        Item conjured = new Item("conjured", -1, 40);
123
+        Item[] item = {conjured};
124
+        Inventory inventory = new Inventory(item);
125
+        inventory.updateQuality();
126
+        int expected = 36;
127
+        int actual = conjured.getQuality();
128
+        Assert.assertEquals(expected, actual);
129
+    }
130
+
131
+    @Test
132
+    public void testConjuredQualityZero1() {
133
+        Item conjured = new Item("conjured", 5, 0);
134
+        Item[] item = {conjured};
135
+        Inventory inventory = new Inventory(item);
136
+        inventory.updateQuality();
137
+        int expected = 0;
138
+        int actual = conjured.getQuality();
139
+        Assert.assertEquals(expected, actual);
140
+    }
141
+
142
+    @Test
143
+    public void testConjuredQualityZero2() {
144
+        Item conjured = new Item("conjured", -1, 0);
145
+        Item[] item = {conjured};
146
+        Inventory inventory = new Inventory(item);
147
+        inventory.updateQuality();
148
+        int expected = 0;
149
+        int actual = conjured.getQuality();
150
+        Assert.assertEquals(expected, actual);
151
+    }
152
+
153
+    @Test
154
+    public void testNormal1() {
155
+        Item normal = new Item("normal", 5, 40);
156
+        Item[] item = {normal};
157
+        Inventory inventory = new Inventory(item);
158
+        inventory.updateQuality();
159
+        int expected = 39;
160
+        int actual = normal.getQuality();
161
+        Assert.assertEquals(expected, actual);
162
+    }
163
+
164
+    @Test
165
+    public void testNormal2() {
166
+        Item normal = new Item("normal", -1, 40);
167
+        Item[] item = {normal};
168
+        Inventory inventory = new Inventory(item);
169
+        inventory.updateQuality();
170
+        int expected = 38;
171
+        int actual = normal.getQuality();
172
+        Assert.assertEquals(expected, actual);
173
+    }
174
+
175
+    @Test
176
+    public void testNormal3() {
177
+        Item normal = new Item("normal", -1, 0);
178
+        Item[] item = {normal};
179
+        Inventory inventory = new Inventory(item);
180
+        inventory.updateQuality();
181
+        int expected = 0;
182
+        int actual = normal.getQuality();
183
+        Assert.assertEquals(expected, actual);
184
+    }
185
+
186
+    @Test
187
+    public void testNormal4() {
188
+        Item normal = new Item("normal", 20, 0);
189
+        Item[] item = {normal};
190
+        Inventory inventory = new Inventory(item);
191
+        inventory.updateQuality();
192
+        int expected = 0;
193
+        int actual = normal.getQuality();
194
+        Assert.assertEquals(expected, actual);
195
+    }
196
+
109 197
 }
110 198