Stwillia94 6 年前
父节点
当前提交
fdd03b63fa

+ 51
- 46
src/main/java/com/zipcodewilmington/gildedrose/Inventory.java 查看文件

@@ -1,5 +1,8 @@
1 1
 package com.zipcodewilmington.gildedrose;
2 2
 
3
+import javax.xml.bind.SchemaOutputResolver;
4
+
5
+
3 6
 
4 7
 public class Inventory {
5 8
     private Item[] items;
@@ -10,57 +13,59 @@ public class Inventory {
10 13
     }
11 14
 
12 15
 
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);
16
+    public boolean isAgedBrie(Item item) {
17
+        return item.getName().equals("Aged Brie");
18
+    }
25 19
 
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
-                        }
20
+    public boolean isBackstagePass(Item item) {
21
+        return item.getName().equals("Backstage passes to a TAFKAL80ETC concert");
22
+    }
32 23
 
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
-            }
24
+    public boolean isSulfuras(Item item) {
25
+        return item.getName().equals("Sulfuras, Hand of Ragnaros");
26
+    }
41 27
 
42
-            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
43
-                items[i].setSellIn(items[i].getSellIn() - 1);
44
-            }
28
+    public boolean isConjuredItem(Item item) {
29
+        return item.getName().equals("Sulfuros, Hand of Rognoros (not to be confused with Ragnaros)");
30
+    }
45 31
 
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
-                }
32
+    public void updateQuality(Item item) {
33
+        if (isAgedBrie(item)) {
34
+            if (item.getSellIn() > 0 && item.getQuality() < 50) {
35
+                item.setQuality(item.getQuality() + 1);
36
+            } else if (item.getSellIn() < 0 && item.getQuality() < 50) {
37
+                item.setQuality(item.getQuality() + 2);
38
+            }
39
+        } else if (isBackstagePass(item)) {
40
+            if (item.getSellIn() > 0 && item.getSellIn() > 10 && item.getQuality() >= 0) {
41
+                item.setQuality(item.getQuality() - 1);
42
+            } else if (item.getSellIn() <= 10 && item.getSellIn() > 5 && item.getQuality() < 50) {
43
+                item.setQuality(item.getQuality() + 2);
44
+            } else if (item.getSellIn() <= 5 && item.getSellIn() > 0 && item.getQuality() < 50) {
45
+                item.setQuality(item.getQuality() + 3);
46
+            } else {
47
+                item.setQuality(0);
48
+            }
49
+        } else if (isConjuredItem(item)) {
50
+            if (item.getSellIn() > 0 && item.getQuality() >= 0) {
51
+                item.setQuality(item.getQuality() - 2);
52
+            } else if (item.getQuality() >= 0) {
53
+                item.setQuality(item.getQuality() - 4);
63 54
             }
55
+        } else if (isSulfuras(item)) {
56
+            item.setQuality(80);
57
+        } else {
58
+            if (item.getSellIn() > 0 && item.getQuality() >= 0) {
59
+                item.setQuality(item.getQuality() - 1);
60
+            } else if (item.getQuality() >= 0) {
61
+                item.setQuality(item.getQuality() - 2);
62
+            }
63
+        }
64
+    }
65
+
66
+    public void updateSellIn(Item item) {
67
+        if (!isSulfuras(item)) {
68
+            item.setSellIn(item.getSellIn() - 1);
64 69
         }
65 70
     }
66 71
 }

+ 180
- 2
src/test/java/com/zipcodewilmington/gildedrose/InventoryTest.java 查看文件

@@ -5,8 +5,186 @@ 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);
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);
10 188
     }
11 189
 }
12 190