Jennifer Chao 6 years ago
parent
commit
1c264620dc

+ 105
- 47
src/main/java/com/zipcodewilmington/gildedrose/Inventory.java View File

@@ -1,6 +1,8 @@
1 1
 package com.zipcodewilmington.gildedrose;
2 2
 
3 3
 
4
+import javax.xml.bind.SchemaOutputResolver;
5
+
4 6
 public class Inventory {
5 7
     private Item[] items;
6 8
 
@@ -9,58 +11,114 @@ public class Inventory {
9 11
         this.items = items;
10 12
     }
11 13
 
14
+    public boolean isAgedBrie(Item item) {
15
+        return item.getName().equals("Aged Brie");
16
+    }
12 17
 
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);
18
+    public boolean isBackstagePass(Item item) {
19
+        return item.getName().equals("Backstage passes to a TAFKAL80ETC concert");
20
+    }
25 21
 
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
-                        }
22
+    public boolean isSulfuras(Item item) {
23
+        return item.getName().equals("Sulfuras, Hand of Ragnaros");
24
+    }
32 25
 
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
-            }
26
+    public boolean isConjuredItem(Item item) {
27
+        return item.getName().equals("Sulfuros, Hand of Rognoros (not to be confused with Ragnaros)");
28
+    }
41 29
 
42
-            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
43
-                items[i].setSellIn(items[i].getSellIn() - 1);
30
+    public void updateQuality(Item item) {
31
+        if (isAgedBrie(item)) {
32
+            if (item.getSellIn() > 0 && item.getQuality() < 50) {
33
+                item.setQuality(item.getQuality() + 1);
34
+            } else if (item.getSellIn() < 0 && item.getQuality() < 50) {
35
+                item.setQuality(item.getQuality() + 2);
44 36
             }
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
-                }
37
+        } else if (isBackstagePass(item)) {
38
+            if (item.getSellIn() > 0 && item.getSellIn() > 10 && item.getQuality() >= 0) {
39
+                item.setQuality(item.getQuality() - 1);
40
+            } else if (item.getSellIn() <= 10 && item.getSellIn() > 5 && item.getQuality() < 50) {
41
+                item.setQuality(item.getQuality() + 2);
42
+            } else if (item.getSellIn() <= 5 && item.getSellIn() > 0 && item.getQuality() < 50) {
43
+                item.setQuality(item.getQuality() + 3);
44
+            } else {
45
+                item.setQuality(0);
46
+            }
47
+        } else if (isConjuredItem(item)) {
48
+            if (item.getSellIn() > 0 && item.getQuality() >= 0) {
49
+                item.setQuality(item.getQuality() - 2);
50
+            } else if (item.getQuality() >= 0) {
51
+                item.setQuality(item.getQuality() - 4);
63 52
             }
53
+        } else if (isSulfuras(item)) {
54
+            item.setQuality(80);
55
+        } else {
56
+            if (item.getSellIn() > 0 && item.getQuality() >= 0) {
57
+                item.setQuality(item.getQuality() - 1);
58
+            } else if (item.getQuality() >= 0) {
59
+                item.setQuality(item.getQuality() - 2);
60
+            }
61
+        }
62
+    }
63
+
64
+    public void updateSellIn(Item item) {
65
+        if (!isSulfuras(item)) {
66
+            item.setSellIn(item.getSellIn() - 1);
64 67
         }
65 68
     }
66
-}
69
+}
70
+
71
+
72
+//    public void updateQuality1() {
73
+//        for (int i = 0; i < items.length; i++) {
74
+//            if (!items[i].getName().equals("Aged Brie") && !items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
75
+//                if (items[i].getQuality() > 0) {
76
+//                    if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
77
+//                        items[i].setQuality(items[i].getQuality() - 1);
78
+//                    }
79
+//                }
80
+//            } else {
81
+//                if (items[i].getQuality() < 50) {
82
+//                    items[i].setQuality(items[i].getQuality() + 1);
83
+//
84
+//                    if (items[i].getName() == "Backstage passes to a TAFKAL80ETC concert") {
85
+//                        if (items[i].getSellIn() < 11) {
86
+//                            if (items[i].getQuality() < 50) {
87
+//                                items[i].setQuality(items[i].getQuality() + 1);
88
+//                            }
89
+//                        }
90
+//
91
+//                        if (items[i].getSellIn() < 6) {
92
+//                            if (items[i].getQuality() < 50) {
93
+//                                items[i].setQuality(items[i].getQuality() + 1);
94
+//                            }
95
+//                        }
96
+//                    }
97
+//                }
98
+//            }
99
+//
100
+//            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
101
+//                items[i].setSellIn(items[i].getSellIn() - 1);
102
+//            }
103
+//
104
+//            if (items[i].getSellIn() < 0) {
105
+//                if (!items[i].getName().equals("Aged Brie")) {
106
+//                    if (!items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
107
+//                        if (items[i].getQuality() > 0) {
108
+//                            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
109
+//                                items[i].setQuality(items[i].getQuality() - 1);
110
+//                            }
111
+//                        }
112
+//                    } else {
113
+//                        items[i].setQuality(items[i].getQuality()
114
+//                                - items[i].getQuality());
115
+//                    }
116
+//                } else {
117
+//                    if (items[i].getQuality() < 50) {
118
+//                        items[i].setQuality(items[i].getQuality() + 1);
119
+//                    }
120
+//                }
121
+//            }
122
+//        }
123
+//    }
124
+//}

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

@@ -5,8 +5,234 @@ 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 testBrieQuality() {
9
+        Item brie = new Item("Aged Brie", 10, 45);
10
+        Item[] item = {brie};
11
+        Inventory inventory = new Inventory(item);
12
+
13
+        inventory.updateQuality(brie);
14
+
15
+        int expected = 46;
16
+        int actual = brie.getQuality();
17
+
18
+        Assert.assertEquals(expected, actual);
19
+    }
20
+
21
+    @Test
22
+    public void testBrieQuality_PastSellIn() {
23
+        Item brie = new Item("Aged Brie", -2, 45);
24
+        Item[] item = {brie};
25
+        Inventory inventory = new Inventory(item);
26
+
27
+        inventory.updateQuality(brie);
28
+
29
+        int expected = 47;
30
+        int actual = brie.getQuality();
31
+
32
+        Assert.assertEquals(expected, actual);
33
+    }
34
+
35
+    @Test
36
+    public void testBackstageQuality() {
37
+        Item backstage = new Item("Backstage passes to a TAFKAL80ETC concert", 15, 45);
38
+        Item[] item = {backstage};
39
+        Inventory inventory = new Inventory(item);
40
+
41
+        inventory.updateQuality(backstage);
42
+
43
+        int expected = 44;
44
+        int actual = backstage.getQuality();
45
+
46
+        Assert.assertEquals(expected, actual);
47
+    }
48
+
49
+    @Test
50
+    public void testBackstageQuality_LessThanTenDays() {
51
+        Item backstage = new Item("Backstage passes to a TAFKAL80ETC concert", 8, 45);
52
+        Item[] item = {backstage};
53
+        Inventory inventory = new Inventory(item);
54
+
55
+        inventory.updateQuality(backstage);
56
+
57
+        int expected = 47;
58
+        int actual = backstage.getQuality();
59
+
60
+        Assert.assertEquals(expected, actual);
61
+    }
62
+
63
+    @Test
64
+    public void testBackstageQuality_LessThanFiveDays() {
65
+        Item backstage = new Item("Backstage passes to a TAFKAL80ETC concert", 4, 45);
66
+        Item[] item = {backstage};
67
+        Inventory inventory = new Inventory(item);
68
+
69
+        inventory.updateQuality(backstage);
70
+
71
+        int expected = 48;
72
+        int actual = backstage.getQuality();
73
+
74
+        Assert.assertEquals(expected, actual);
75
+    }
76
+
77
+    @Test
78
+    public void testBackstageQuality_AfterConcert() {
79
+        Item backstage = new Item("Backstage passes to a TAFKAL80ETC concert", 0, 50);
80
+        Item[] item = {backstage};
81
+        Inventory inventory = new Inventory(item);
82
+
83
+        inventory.updateQuality(backstage);
84
+
85
+        int expected = 0;
86
+        int actual = backstage.getQuality();
87
+
88
+        Assert.assertEquals(expected, actual);
89
+    }
90
+
91
+    @Test
92
+    public void testSulfurasQuality() {
93
+        Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 10, 80);
94
+        Item[] item = {sulfuras};
95
+        Inventory inventory = new Inventory(item);
96
+
97
+        inventory.updateQuality(sulfuras);
98
+
99
+        int expected = 80;
100
+        int actual = sulfuras.getQuality();
101
+
102
+        Assert.assertEquals(expected, actual);
10 103
     }
104
+
105
+    @Test
106
+    public void testNormalItemQuality() {
107
+        Item normal = new Item("Generic Mana Potion", 10, 45);
108
+        Item[] item = {normal};
109
+        Inventory inventory = new Inventory(item);
110
+
111
+        inventory.updateQuality(normal);
112
+
113
+        int expected = 44;
114
+        int actual = normal.getQuality();
115
+
116
+        Assert.assertEquals(expected, actual);
117
+    }
118
+
119
+
120
+    @Test
121
+    public void testNormalItemQuality_PastSellIn() {
122
+        Item normal = new Item("Generic Mana Potion", -1, 10);
123
+        Item[] item = {normal};
124
+        Inventory inventory = new Inventory(item);
125
+
126
+        inventory.updateQuality(normal);
127
+
128
+        int expected = 8;
129
+        int actual = normal.getQuality();
130
+
131
+        Assert.assertEquals(expected, actual);
132
+    }
133
+
134
+    @Test
135
+    public void testConjuredItemQuality () {
136
+        Item conjured = new Item("Sulfuros, Hand of Rognoros (not to be confused with Ragnaros)", 5, 45);
137
+        Item[] item = {conjured};
138
+        Inventory inventory = new Inventory(item);
139
+
140
+        inventory.updateQuality(conjured);
141
+
142
+        int expected = 43;
143
+        int actual = conjured.getQuality();
144
+
145
+        Assert.assertEquals(expected, actual);
146
+    }
147
+
148
+    @Test
149
+    public void testConjuredItemQuality_PastSellIn () {
150
+        Item conjured = new Item("Sulfuros, Hand of Rognoros (not to be confused with Ragnaros)", -1, 10);
151
+        Item[] item = {conjured};
152
+        Inventory inventory = new Inventory(item);
153
+
154
+        inventory.updateQuality(conjured);
155
+
156
+        int expected = 6;
157
+        int actual = conjured.getQuality();
158
+
159
+        Assert.assertEquals(expected, actual);
160
+    }
161
+
162
+    @Test
163
+    public void testNormalItemSellIn () {
164
+        Item normal = new Item("Generic Mana Potion", 5, 45);
165
+        Item[] item = {normal};
166
+        Inventory inventory = new Inventory(item);
167
+
168
+        inventory.updateSellIn(normal);
169
+
170
+        int expected = 4;
171
+        int actual = normal.getSellIn();
172
+
173
+        Assert.assertEquals(expected, actual);
174
+    }
175
+
176
+    @Test
177
+    public void testSulfurasItemSellIn () {
178
+        Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 15, 45);
179
+        Item[] item = {sulfuras};
180
+        Inventory inventory = new Inventory(item);
181
+
182
+        inventory.updateSellIn(sulfuras);
183
+
184
+        int expected = 15;
185
+        int actual = sulfuras.getSellIn();
186
+
187
+        Assert.assertEquals(expected, actual);
188
+    }
189
+
190
+//    @Test
191
+//    public void updateBrieQualityTest_PastSellIn() {
192
+//        Item brie = new Item("Aged Brie", -2, 45);
193
+//        Item[] item = {brie};
194
+//        Inventory inventory = new Inventory(item);
195
+//
196
+//        inventory.updateQuality();
197
+//
198
+//        int expected = 47;
199
+//        int actual = brie.getQuality();
200
+//
201
+//        Assert.assertEquals(expected, actual);
202
+//    }
203
+
204
+//    @Test
205
+//    public void testIsAgedBrie() {
206
+//        Item item = new Item("Aged Brie", 6, 50);
207
+//
208
+//        String expected = "Aged Brie";
209
+//
210
+//        String actual = item.getName();
211
+//
212
+//        Assert.assertEquals(expected, actual);
213
+//    }
214
+//
215
+//    @Test
216
+//    public void testIsBackstagePass() {
217
+//        Item item = new Item("Backstage passes to a TAFKAL80ETC concert", 6, 50);
218
+//
219
+//        String expected = "Backstage passes to a TAFKAL80ETC concert";
220
+//
221
+//        String actual = item.getName();
222
+//
223
+//        Assert.assertEquals(expected, actual);
224
+//    }
225
+//
226
+//    @Test
227
+//    public void testIsSulfuras() {
228
+//        Item item = new Item("Sulfuras, Hand of Ragnaros", 6, 50);
229
+//
230
+//        String expected = "Sulfuras, Hand of Ragnaros";
231
+//
232
+//        String actual = item.getName();
233
+//
234
+//        Assert.assertEquals(expected, actual);
235
+//
236
+//    }
11 237
 }
12 238