Browse Source

finished lab with 100% line coverage

Connor Dunnigan 6 years ago
parent
commit
0f8374da41

+ 72
- 44
src/main/java/com/zipcodewilmington/gildedrose/Inventory.java View File

@@ -12,55 +12,83 @@ 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
-                    }
21
-                }
22
-            } else {
23
-                if (items[i].getQuality() < 50) {
24
-                    items[i].setQuality(items[i].getQuality() + 1);
25 15
 
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
-                        }
16
+            switch(items[i].getName()){
17
+                case "Aged Brie":
18
+                        updateBrie(items[i]);
19
+                        break;
20
+                case "Backstage passes to a TAFKAL80ETC concert" :
21
+                        updateBackstagePass(items[i]);
22
+                        break;
23
+                case "Sulfuras, Hand of Ragnaros" :
24
+                        updateSulfra(items[i]);
25
+                        break;
32 26
 
33
-                        if (items[i].getSellIn() < 6) {
34
-                            if (items[i].getQuality() < 50) {
35
-                                items[i].setQuality(items[i].getQuality() + 1);
36
-                            }
37
-                        }
38
-                    }
39
-                }
27
+                case "Conjured Item" :
28
+                        updateConjure(items[i]);
29
+                        break;
30
+                default :
31
+                        updateDefault(items[i]);
32
+                        break;
40 33
             }
34
+        }
35
+    }
41 36
 
42
-            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
43
-                items[i].setSellIn(items[i].getSellIn() - 1);
44
-            }
37
+    public void updateBrie(Item item){
38
+        if(!sellInExpired(item)){
39
+            incrementQuality(item,1);
40
+        } else{
41
+            incrementQuality(item,2);
42
+        }
43
+        decrementSellIn(item);
44
+    }
45 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
-            }
46
+    public void updateBackstagePass(Item item){
47
+        if(sellInExpired(item)){
48
+            item.setQuality(0);
49
+        } else if(item.getSellIn() <= 5){
50
+            incrementQuality(item, 3);
51
+        } else if(item.getSellIn() <= 10){
52
+            incrementQuality(item, 2);
53
+        } else{
54
+            incrementQuality(item, 1);
55
+        }
56
+        decrementSellIn(item);
57
+    }
58
+
59
+    public void updateSulfra(Item item){
60
+        item.setQuality(80);
61
+        item.setSellIn(item.getSellIn());
62
+    }
63
+
64
+    public void updateConjure(Item item){
65
+        incrementQuality(item, -2);
66
+        decrementSellIn(item);
67
+    }
68
+
69
+    public void updateDefault(Item item){
70
+        incrementQuality(item, -1);
71
+        decrementSellIn(item);
72
+    }
73
+
74
+    public boolean sellInExpired(Item item){
75
+        if(item.getSellIn()<=0){
76
+            return true;
77
+        } else {
78
+            return false;
64 79
         }
65 80
     }
81
+
82
+    public void incrementQuality(Item item, int incr){
83
+        int currentQual = item.getQuality() + incr;
84
+        if(currentQual < 50 && currentQual >= 0) {
85
+            item.setQuality(currentQual);
86
+        } else{
87
+            item.setQuality(50);
88
+        }
89
+    }
90
+
91
+    public void decrementSellIn(Item item){
92
+        item.setSellIn(item.getSellIn()-1);
93
+    }
66 94
 }

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

@@ -4,9 +4,189 @@ import org.junit.Assert;
4 4
 import org.junit.Test;
5 5
 
6 6
 public class InventoryTest {
7
+
8
+    @Test
9
+    public void setQualityTest(){
10
+        //Given
11
+        Item rand = new Item("rand", 10, 10);
12
+        int expVal = 42;
13
+
14
+        //When
15
+        rand.setQuality(42);
16
+
17
+        //Then
18
+        int actVal = rand.getQuality();
19
+        Assert.assertEquals(expVal, actVal);
20
+    }
21
+
22
+    @Test
23
+    public void setNameTest(){
24
+        //Given
25
+        Item rand = new Item("rand", 10, 10);
26
+        String expString = "random";
27
+
28
+        //When
29
+        rand.setName(expString);
30
+
31
+        //Then
32
+        String actString = rand.getName();
33
+        Assert.assertEquals(expString, actString);
34
+    }
35
+
36
+    @Test
37
+    public void agedBrie50Quality(){
38
+        //Given
39
+        Item brie = new Item("Aged Brie", 2, 50);
40
+        Item[] items = new Item[1];
41
+        items[0] = brie;
42
+        Inventory inv = new Inventory(items);
43
+        int exp = 50;
44
+
45
+        //When
46
+        inv.updateQuality();
47
+        int act = brie.getQuality();
48
+
49
+        //Then
50
+        Assert.assertEquals(exp, act);
51
+    }
52
+
53
+
54
+    @Test
55
+    public void agedBrieExpireTest(){
56
+        //Given
57
+        Item brie = new Item("Aged Brie", -1, 39);
58
+        Item[] items = new Item[1];
59
+        items[0] = brie;
60
+        Inventory inv = new Inventory(items);
61
+        int exp = 41;
62
+
63
+        //When
64
+        inv.updateQuality();
65
+        int act = brie.getQuality();
66
+
67
+        //Then
68
+        Assert.assertEquals(exp, act);
69
+    }
70
+
71
+    @Test
72
+    public void passQualityWith5Days(){
73
+        //Given
74
+        Item pass = new Item("Backstage passes to a TAFKAL80ETC concert", 5, 39);
75
+        Item[] items = new Item[1];
76
+        items[0] = pass;
77
+        Inventory inv = new Inventory(items);
78
+        int exp = 42;
79
+
80
+        //When
81
+        inv.updateQuality();
82
+        int act = pass.getQuality();
83
+
84
+        //Then
85
+        Assert.assertEquals(exp, act);
86
+    }
87
+
88
+    @Test
89
+    public void passQualityWith11Days(){
90
+        //Given
91
+        Item pass = new Item("Backstage passes to a TAFKAL80ETC concert", 11, 39);
92
+        Item[] items = new Item[1];
93
+        items[0] = pass;
94
+        Inventory inv = new Inventory(items);
95
+        int exp = 40;
96
+
97
+        //When
98
+        inv.updateQuality();
99
+        int act = pass.getQuality();
100
+
101
+        //Then
102
+        Assert.assertEquals(exp, act);
103
+    }
104
+
105
+    @Test
106
+    public void passQualityWith0Days(){
107
+        //Given
108
+        Item pass = new Item("Backstage passes to a TAFKAL80ETC concert", 0, 39);
109
+        Item[] items = new Item[1];
110
+        items[0] = pass;
111
+        Inventory inv = new Inventory(items);
112
+        int exp = 0;
113
+
114
+        //When
115
+        inv.updateQuality();
116
+        int act = pass.getQuality();
117
+
118
+        //Then
119
+        Assert.assertEquals(exp, act);
120
+    }
121
+
122
+    @Test
123
+    public void passQualityWith10Days(){
124
+        //Given
125
+        Item pass = new Item("Backstage passes to a TAFKAL80ETC concert", 10, 39);
126
+        Item[] items = new Item[1];
127
+        items[0] = pass;
128
+        Inventory inv = new Inventory(items);
129
+        int exp = 41;
130
+
131
+        //When
132
+        inv.updateQuality();
133
+        int act = pass.getQuality();
134
+
135
+        //Then
136
+        Assert.assertEquals(exp, act);
137
+    }
138
+
7 139
     @Test
8
-    public void updateQuantityTest(){
9
-        Assert.assertEquals(1, 1);
140
+    public void sulfurasTest(){
141
+        //Given
142
+        Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 17, 80);
143
+        Item[] items = new Item[1];
144
+        items[0] =sulfuras;
145
+        Inventory inv = new Inventory(items);
146
+        int exp = 80;
147
+
148
+        //When
149
+        inv.updateQuality();
150
+        int act = sulfuras.getQuality();
151
+
152
+        //Then
153
+        Assert.assertEquals(exp, act);
154
+    }
155
+
156
+    @Test
157
+
158
+    public void conjureTest(){
159
+        //Given
160
+        Item conjure = new Item("Conjured Item", 17, 25);
161
+        Item[] items = new Item[1];
162
+        items[0] = conjure;
163
+        Inventory inv = new Inventory(items);
164
+        int exp = 23;
165
+
166
+        //When
167
+        inv.updateQuality();
168
+        int act = conjure.getQuality();
169
+
170
+        //Then
171
+        Assert.assertEquals(exp, act);
172
+    }
173
+
174
+    @Test
175
+
176
+    public void defaultTest(){
177
+        //Given
178
+        Item rand = new Item("randName", 17, 25);
179
+        Item[] items = new Item[1];
180
+        items[0] = rand;
181
+        Inventory inv = new Inventory(items);
182
+        int exp = 24;
183
+
184
+        //When
185
+        inv.updateQuality();
186
+        int act = rand.getQuality();
187
+
188
+        //Then
189
+        Assert.assertEquals(exp, act);
10 190
     }
11 191
 }
12 192