Parcourir la source

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

William Brown il y a 6 ans
Parent
révision
0c00112e1b

+ 26
- 0
src/main/java/com/zipcodewilmington/gildedrose/Inventory.java Voir le fichier

13
         agedBrie();
13
         agedBrie();
14
         sulfuras();
14
         sulfuras();
15
         backstagePasses();
15
         backstagePasses();
16
+        conjured();
17
+        normalItem();
16
     }
18
     }
17
 
19
 
18
     public void agedBrie(){
20
     public void agedBrie(){
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 Voir le fichier

8
 public class InventoryTest {
8
 public class InventoryTest {
9
 
9
 
10
     @Test
10
     @Test
11
-    public void testAgedBrie1() {
11
+    public void testAgedBrieIncreaseTo11() {
12
         Item agedBrie = new Item("Aged Brie", 10, 10);
12
         Item agedBrie = new Item("Aged Brie", 10, 10);
13
         Item[] item = {agedBrie};
13
         Item[] item = {agedBrie};
14
         Inventory inventory = new Inventory(item);
14
         Inventory inventory = new Inventory(item);
19
     }
19
     }
20
 
20
 
21
     @Test
21
     @Test
22
-    public void testAgedBrie2() {
22
+    public void testAgedBrieIncreaseTo50() {
23
         Item agedBrie = new Item("Aged Brie", 10, 49);
23
         Item agedBrie = new Item("Aged Brie", 10, 49);
24
         Item[] item = {agedBrie};
24
         Item[] item = {agedBrie};
25
         Inventory inventory = new Inventory(item);
25
         Inventory inventory = new Inventory(item);
30
     }
30
     }
31
 
31
 
32
     @Test
32
     @Test
33
-    public void testAgedBrie3() {
33
+    public void testAgedBrieOver50() {
34
         Item agedBrie = new Item("Aged Brie", 10, 50);
34
         Item agedBrie = new Item("Aged Brie", 10, 50);
35
         Item[] item = {agedBrie};
35
         Item[] item = {agedBrie};
36
         Inventory inventory = new Inventory(item);
36
         Inventory inventory = new Inventory(item);
41
     }
41
     }
42
 
42
 
43
     @Test
43
     @Test
44
-    public void testBackstagePass1() {
44
+    public void testBackstagePass10BeforeSellDay() {
45
         Item backstage = new Item("Backstage passes to a TAFKAL80ETC concert", 11, 40);
45
         Item backstage = new Item("Backstage passes to a TAFKAL80ETC concert", 11, 40);
46
         Item[] item = {backstage};
46
         Item[] item = {backstage};
47
         Inventory inventory = new Inventory(item);
47
         Inventory inventory = new Inventory(item);
52
     }
52
     }
53
 
53
 
54
     @Test
54
     @Test
55
-    public void testBackstagePass2() {
55
+    public void testBackstagePass7BeforeSellDay() {
56
         Item backstage = new Item("Backstage passes to a TAFKAL80ETC concert", 7, 40);
56
         Item backstage = new Item("Backstage passes to a TAFKAL80ETC concert", 7, 40);
57
         Item[] item = {backstage};
57
         Item[] item = {backstage};
58
         Inventory inventory = new Inventory(item);
58
         Inventory inventory = new Inventory(item);
63
     }
63
     }
64
 
64
 
65
     @Test
65
     @Test
66
-    public void testBackstagePass3() {
66
+    public void testBackstagePass3BeforeSellDay() {
67
         Item backstage = new Item("Backstage passes to a TAFKAL80ETC concert", 3, 40);
67
         Item backstage = new Item("Backstage passes to a TAFKAL80ETC concert", 3, 40);
68
         Item[] item = {backstage};
68
         Item[] item = {backstage};
69
         Inventory inventory = new Inventory(item);
69
         Inventory inventory = new Inventory(item);
74
     }
74
     }
75
 
75
 
76
     @Test
76
     @Test
77
-    public void testBackstagePass4() {
77
+    public void testBackstagePassOnSellDay() {
78
         Item backstage = new Item("Backstage passes to a TAFKAL80ETC concert", 0, 40);
78
         Item backstage = new Item("Backstage passes to a TAFKAL80ETC concert", 0, 40);
79
         Item[] item = {backstage};
79
         Item[] item = {backstage};
80
         Inventory inventory = new Inventory(item);
80
         Inventory inventory = new Inventory(item);
85
     }
85
     }
86
 
86
 
87
     @Test
87
     @Test
88
-    public void testBackstagePass5() {
88
+    public void testBackstagePassAfterSellDay() {
89
         Item backstage = new Item("Backstage passes to a TAFKAL80ETC concert", -1, 40);
89
         Item backstage = new Item("Backstage passes to a TAFKAL80ETC concert", -1, 40);
90
         Item[] item = {backstage};
90
         Item[] item = {backstage};
91
         Inventory inventory = new Inventory(item);
91
         Inventory inventory = new Inventory(item);
96
     }
96
     }
97
 
97
 
98
     @Test
98
     @Test
99
-    public void testSulfurasPass1() {
99
+    public void testSulfuras() {
100
         Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 10, 40);
100
         Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 10, 40);
101
         Item[] item = {sulfuras};
101
         Item[] item = {sulfuras};
102
         Inventory inventory = new Inventory(item);
102
         Inventory inventory = new Inventory(item);
106
         Assert.assertEquals(expected, actual);
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