瀏覽代碼

Finished Part 3: Add a new item

NedRedmond 6 年之前
父節點
當前提交
3e972bb5dc

+ 17
- 62
src/main/java/com/zipcodewilmington/gildedrose/Inventory.java 查看文件

@@ -19,6 +19,14 @@ public class Inventory {
19 19
 
20 20
     public void updateQuality() {
21 21
         for (Item item : items) {
22
+
23
+            int minuend = 0;
24
+            int subtrahend;
25
+
26
+            if (item.getName().equals("Conjured")) {
27
+                minuend = item.getQuality();
28
+            }
29
+
22 30
             if (item.getQuality() < 50) {
23 31
                 switch (item.getName()) {
24 32
                     case "Aged Brie":
@@ -44,72 +52,19 @@ public class Inventory {
44 52
                         break;
45 53
                 }
46 54
             }
55
+
56
+            if (item.getName().equals("Conjured")) {
57
+                subtrahend = item.getQuality();
58
+                int qualityDifference = minuend - subtrahend;
59
+                if (qualityDifference > 0) {
60
+                    item.setQuality(item.getQuality() - qualityDifference);
61
+                }
62
+            }
63
+
47 64
             if (item.getQuality() < 0) {
48 65
                 item.setQuality(0);
49 66
             }
50
-//                    default:
51
-//                        if (item.getQuality() > 0)
52
-//                            if (item.getSellIn() < 0 && item.getQuality() > 1))
53
-//                                changeQuality(item, -1);
54
-//                            if (item.getSellIn() < 0 && item.getQuality() > 1)
55
-//                                changeQuality(item, -2);
56
-//                        break;
57
-//                }
58
-//            }
59 67
             updateSellIn(item);
60 68
         }
61
-
62
-
63
-//        for (int i = 0; i < items.length; i++) {
64
-//            if (!items[i].getName().equals("Aged Brie") && !items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
65
-//                if (items[i].getQuality() > 0) {
66
-//                    if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
67
-//                        items[i].setQuality(items[i].getQuality() - 1);
68
-//                    }
69
-//                }
70
-//            } else {
71
-//                if (items[i].getQuality() < 50 && !items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
72
-//                    items[i].setQuality(items[i].getQuality() + 1);
73
-//
74
-////                    if (items[i].getName() == "Backstage passes to a TAFKAL80ETC concert") {
75
-////                        if (items[i].getSellIn() < 11) {
76
-////                            if (items[i].getQuality() < 50) {
77
-////                                items[i].setQuality(items[i].getQuality() + 1);
78
-////                            }
79
-////                        }
80
-////
81
-////                        if (items[i].getSellIn() < 6) {
82
-////                            if (items[i].getQuality() < 50) {
83
-////                                items[i].setQuality(items[i].getQuality() + 1);
84
-////                            }
85
-////                        }
86
-////                    }
87
-//                }
88
-//            }
89
-//
90
-//            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros") && !items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
91
-//                items[i].setSellIn(items[i].getSellIn() - 1);
92
-//            }
93
-//
94
-//            if (items[i].getSellIn() < 0 && !items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
95
-//                if (!items[i].getName().equals("Aged Brie")) {
96
-//                    if (!items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
97
-//                        if (items[i].getQuality() > 0) {
98
-//                            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
99
-//                                items[i].setQuality(items[i].getQuality() - 1);
100
-//                            }
101
-//                        }
102
-//                    } else {
103
-//                        items[i].setQuality(items[i].getQuality()
104
-//                                - items[i].getQuality());
105
-//                    }
106
-//                }
107
-//////                else {
108
-////////                    if (items[i].getQuality() < 50) {
109
-////////                        items[i].setQuality(items[i].getQuality() + 1);
110
-//////                    }
111
-////                }
112
-//            }
113
-//        }
114 69
     }
115 70
 }

+ 44
- 1
src/test/java/com/zipcodewilmington/gildedrose/InventoryTest.java 查看文件

@@ -9,7 +9,8 @@ public class InventoryTest {
9 9
     Item agedBrie = new Item("Aged Brie",7,10);
10 10
     Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 7, 10);
11 11
     Item backstagePasses = new Item("Backstage passes to a TAFKAL80ETC concert", 14, 10);
12
-    Item[] items = {genericItem, agedBrie, sulfuras, backstagePasses};
12
+    Item conjured = new Item("Conjured", 7, 25);
13
+    Item[] items = {genericItem, agedBrie, sulfuras, backstagePasses, conjured};
13 14
 
14 15
     @Test
15 16
     public void updateQuality0() {
@@ -150,5 +151,47 @@ public class InventoryTest {
150 151
         Assert.assertTrue(testItem.getName().equals("Health Potion"));
151 152
     }
152 153
 
154
+    @Test
155
+    public void updateQuality8() {
156
+        Inventory inventory = new Inventory(items);
157
+
158
+        // test that conjured quality goes down by double each day
159
+        int beforeConjured = items[4].getQuality();
160
+        int beforeGeneric = items[0].getQuality();
161
+        inventory.updateQuality();
162
+        int afterConjured = items[4].getQuality();
163
+        int afterGeneric = items[0].getQuality();
164
+
165
+        int differenceConjured = beforeConjured - afterConjured;
166
+        int differenceGeneric = beforeGeneric - afterGeneric;
167
+
168
+//        System.out.println(before);
169
+//        System.out.println(after);
170
+
171
+        Assert.assertTrue(differenceConjured/2 == differenceGeneric);
172
+    }
173
+
174
+    @Test
175
+    public void updateQuality9() {
176
+        Inventory inventory = new Inventory(items);
177
+
178
+        // test that conjured quality goes down by double each day
179
+        int beforeConjured = items[4].getQuality();
180
+        int beforeGeneric = items[0].getQuality();
181
+        for (int i = 0; i < 5; i++) {
182
+            inventory.updateQuality();
183
+        }
184
+        int afterConjured = items[4].getQuality();
185
+        int afterGeneric = items[0].getQuality();
186
+
187
+        int differenceConjured = beforeConjured - afterConjured;
188
+        int differenceGeneric = beforeGeneric - afterGeneric;
189
+
190
+//        System.out.println(before);
191
+//        System.out.println(after);
192
+
193
+        Assert.assertTrue(differenceConjured/2 == differenceGeneric);
194
+    }
195
+
153 196
 }
154 197