ソースを参照

Completed tests and methods

Trinh Tong 6 年 前
コミット
20b42ae56a
共有2 個のファイルを変更した202 個の追加151 個の削除を含む
  1. 46
    144
      src/main/java/com/zipcodewilmington/gildedrose/Inventory.java
  2. 156
    7
      src/test/java/com/zipcodewilmington/gildedrose/InventoryTest.java

+ 46
- 144
src/main/java/com/zipcodewilmington/gildedrose/Inventory.java ファイルの表示

@@ -6,6 +6,7 @@ public class Inventory {
6 6
     static final String BRIE = "Aged Brie";
7 7
     static final String BACK_PASS = "Backstage passes to a TAFKAL80ETC concert";
8 8
     static final String SULFURAS = "Sulfuras, Hand of Ragnaros";
9
+    static final String CONJURED = "Conjured items by the magnificent wizard";
9 10
 
10 11
 
11 12
     public Inventory(Item[] items) {
@@ -13,50 +14,62 @@ public class Inventory {
13 14
         this.items = items;
14 15
     }
15 16
 
16
-    // Updates the quality for 1 day
17 17
     public void updateQuality() {
18 18
 
19
-        for (Item item: items) {
20
-            // make a switch statement
19
+        for (Item item : items) {
21 20
 
22
-            switch (item.getName()){
21
+            switch (item.getName()) {
23 22
 
24
-            case BRIE:
23
+                case BRIE:
24
+                    if (!checkSellInNegative(item)) {
25
+                        adjustItemQuality(item, 1);
26
+                    } else {
27
+                        adjustItemQuality(item, 2);
28
+                    }
29
+                    decreaseSellIn(item);
30
+                    break;
25 31
 
26
-                adjustItemQuality(item, 1);
32
+                case BACK_PASS:
27 33
 
28
-                decreaseSellIn(item);
29
-                break;
34
+                    if (checkPassSellIn5(item)) {
35
+                        adjustItemQuality(item, 3);
30 36
 
31
-            case BACK_PASS:
37
+                    } else if (checkPassSellIn10(item)) {
38
+                        adjustItemQuality(item, 2);
32 39
 
33
-                // Check sell in because the sell in value dictates the quality modifier
40
+                    } else if (checkSellInNegative(item)) {
41
+                        setQualityZero(item);
34 42
 
35
-                if (checkPassSellIn5(item)) {
36
-                    adjustItemQuality(item, 3);
43
+                    } else {
44
+                        adjustItemQuality(item, 1);
45
+                    }
37 46
 
38
-                } else if (checkPassSellIn10(item)) {
39
-                    adjustItemQuality(item, 2);
47
+                    decreaseSellIn(item);
40 48
 
41
-                } else if (checkPassSellInNegative(item)) {
42
-                    setQualityZero(item);
43
-                } else {
44
-                    adjustItemQuality(item, 1);
45
-                }
46
-                decreaseSellIn(item);
49
+                    break;
47 50
 
48
-                break;
51
+                case SULFURAS:
52
+                    item.setQuality(80);
53
+                    item.setSellIn(item.getSellIn());
49 54
 
50
-            case SULFURAS:
51
-                item.setQuality(80);
52
-                item.setSellIn(item.getSellIn());
55
+                    break;
53 56
 
54
-                break;
57
+                case CONJURED:
58
+                    if (!checkSellInNegative(item)) {
59
+                        adjustItemQuality(item, -2);
60
+                    } else {
61
+                        adjustItemQuality(item, -4);
62
+                    }
63
+
64
+                    decreaseSellIn(item);
65
+                    break;
66
+
67
+                default:
68
+                    break;
55 69
             }
56 70
         }
57 71
     }
58 72
 
59
-
60 73
     public void decreaseSellIn(Item name) {
61 74
         name.setSellIn(name.getSellIn() - 1);
62 75
 
@@ -80,7 +93,7 @@ public class Inventory {
80 93
         return false;
81 94
     }
82 95
 
83
-    public boolean checkPassSellInNegative(Item item) {
96
+    public boolean checkSellInNegative(Item item) {
84 97
 
85 98
         if (item.getSellIn() < 0) {
86 99
             return true;
@@ -99,126 +112,15 @@ public class Inventory {
99 112
 
100 113
     public void adjustItemQuality(Item name, int qualityIncrement) {
101 114
 
102
-        if (name.getQuality() + qualityIncrement < 50) {
115
+        if (name.getQuality() + qualityIncrement < 0) {
116
+            setQualityZero(name);
117
+
118
+        } else if ((name.getQuality() + qualityIncrement < 50) && (name.getQuality() + qualityIncrement >= 0)) {
103 119
             name.setQuality(name.getQuality() + qualityIncrement);
120
+
104 121
         } else {
105 122
             setQualityMax(name);
106 123
         }
107 124
 
108 125
     }
109
-
110
-    public Item[] getItems(){
111
-        return this.items;
112
-    }
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-//    // Old Code
132
-//    public void updateQuality() {
133
-//        // Giant for loop representing the "day" each day, the quality's of items change.
134
-//
135
-//        for (int i = 0; i < items.length; i++) {
136
-//
137
-//            // This if statement checks if it is NOT aged brie and NOT backstage passes
138
-//            if (!items[i].getName().equals("Aged Brie")
139
-//                    && !items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
140
-//                // if it's not brie and bspasses
141
-//                if (items[i].getQuality() > 0) {
142
-//                    // checks if the quality is over 0
143
-//                    if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
144
-//                        // as long as it's not surlfuras...
145
-//                        // quantity is decreased by 1
146
-//                        items[i].setQuality(items[i].getQuality() - 1);
147
-//                    }
148
-//                }
149
-//
150
-//
151
-//
152
-//            } else {
153
-//
154
-//                // Next leg of IF statement quality < 50 then increase quality by 1
155
-//
156
-//                if (items[i].getQuality() < 50) {
157
-//                    // if the quality is below 50
158
-//                    items[i].setQuality(items[i].getQuality() + 1);
159
-//                    // the quality is increased by 1
160
-//
161
-//                    // stuff about backstage passes
162
-//                    if (items[i].getName() == "Backstage passes to a TAFKAL80ETC concert") {
163
-//                        // if the item is a backstagepass
164
-//
165
-//                        if (items[i].getSellIn() < 11) {
166
-//                            // and if the backstage pass has to sell in 11 days
167
-//                            if (items[i].getQuality() < 50) {
168
-//                                // and the quality is below 50
169
-//                                items[i].setQuality(items[i].getQuality() + 1);
170
-//                                // then add 1 to the quality
171
-//                            }
172
-//                        }
173
-//
174
-//                        if (items[i].getSellIn() < 6) {
175
-//                            // if the bspass has to sell in 6 days
176
-//                            if (items[i].getQuality() < 50) {
177
-//                                // and the quality is less than 50
178
-//                                items[i].setQuality(items[i].getQuality() + 1);
179
-//                                // then add 1 to the quality
180
-//                            }
181
-//                        }
182
-//                    }
183
-//                }
184
-//            }
185
-//
186
-//            // another if the item IS NOT SULFURAS
187
-//
188
-//            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
189
-//                items[i].setSellIn(items[i].getSellIn() - 1);
190
-//            }
191
-//            // then decrease the sellin day by 1
192
-//
193
-//
194
-//            // checks the items sell in
195
-//            // if the items sell in is less than 0 (no sellin is negative)
196
-//            if (items[i].getSellIn() < 0) {
197
-//
198
-//                if (!items[i].getName().equals("Aged Brie")) {
199
-//                    // and the item IS NOT Brie
200
-//                    if (!items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
201
-//                        // and the item IS NOT BSPASSES
202
-//                        if (items[i].getQuality() > 0) {
203
-//                            // if the item quality is greater than 0,
204
-//
205
-//                            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
206
-//                                // and the item is NOT SULFURAS,
207
-//                                items[i].setQuality(items[i].getQuality() - 1);
208
-//                                // the item quality -1
209
-//                            }
210
-//                        }
211
-//                    } else { // if the item IS a backstage pass
212
-//                        items[i].setQuality(items[i].getQuality() - items[i].getQuality());
213
-//                    }
214
-//                } else { // if the item IS BRIE
215
-//                    if (items[i].getQuality() < 50) {
216
-//                        // BRIE QUALITY is LESS THAN 50
217
-//                        items[i].setQuality(items[i].getQuality() + 1);
218
-//                        // Increase Brie's quality by 1
219
-//                    }
220
-//                }
221
-//            }
222
-//        }
223
-//    }
224
-}
126
+}

+ 156
- 7
src/test/java/com/zipcodewilmington/gildedrose/InventoryTest.java ファイルの表示

@@ -3,8 +3,6 @@ package com.zipcodewilmington.gildedrose;
3 3
 import org.junit.Assert;
4 4
 import org.junit.Test;
5 5
 
6
-import java.util.Arrays;
7
-
8 6
 /**
9 7
  * Creating tests for the Inventory Class
10 8
  * The Inventory class will keep track of SellIn & Quality
@@ -18,8 +16,9 @@ public class InventoryTest {
18 16
     Item agedBrie = new Item("Aged Brie", 1, 45);
19 17
     Item passes = new Item("Backstage passes to a TAFKAL80ETC concert", 11, 25);
20 18
     Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 10, 80 );
19
+    Item conjured = new Item("Conjured items by the magnificent wizard", 5, 4);
21 20
 
22
-    Item[] items = {wine, agedBrie, passes, sulfuras};
21
+    Item[] items = {wine, agedBrie, passes, sulfuras, conjured};
23 22
 
24 23
     public InventoryTest() {
25 24
         inventory = new Inventory(items);
@@ -27,23 +26,173 @@ public class InventoryTest {
27 26
 
28 27
     @Test
29 28
     public void testBrieQuality() {
30
-        // Brie quality increases as sell in decreases
31
-        // Given
32 29
 
33 30
         int expectedBrieQuality = 46;
34 31
         String itemName = "agedBrie";
35 32
 
36
-        // When
37 33
         inventory.adjustItemQuality(agedBrie, 1);
38 34
         int actualBrieQuality = agedBrie.getQuality();
39 35
 
40
-        // Then
41 36
         Assert.assertEquals(expectedBrieQuality,actualBrieQuality);
42 37
     }
43 38
 
39
+
44 40
     @Test
45 41
     public void testPassQuality() {
46 42
 
43
+        int expectedQuality = 26;
44
+
45
+        inventory.updateQuality();
46
+        int actualQuality = passes.getQuality();
47
+
48
+        Assert.assertEquals(expectedQuality, actualQuality);
49
+
50
+    }
51
+
52
+    @Test
53
+    public void testSulfurasQuality() {
54
+
55
+        int expectedQuality = 80;
56
+
57
+        inventory.updateQuality();
58
+        int actualQuality = sulfuras.getQuality();
59
+
60
+        Assert.assertEquals(expectedQuality, actualQuality);
61
+
62
+    }
63
+
64
+    @Test
65
+    public void testConjuredQuality() {
66
+
67
+    }
68
+
69
+    @Test
70
+    public void testReachMaxQuality(){
71
+
72
+        int expectedQuality = 50;
73
+
74
+        inventory.adjustItemQuality(wine, 45);
75
+
76
+        int actualQuality = wine.getQuality();
77
+
78
+        Assert.assertEquals(expectedQuality, actualQuality);
79
+    }
80
+
81
+    @Test
82
+    public void testDecreaseSellIn() {
83
+
84
+        int expectedSellin= 0;
85
+
86
+        inventory.decreaseSellIn(agedBrie);
87
+        int actualSellIn = agedBrie.getSellIn();
88
+
89
+        Assert.assertEquals(expectedSellin, actualSellIn);
90
+
91
+    }
92
+
93
+    @Test
94
+    public void testSellIn5() {
95
+
96
+        boolean actualSell = inventory.checkPassSellIn5(passes);
97
+
98
+        Assert.assertFalse(actualSell);
99
+
100
+    }
101
+
102
+    @Test
103
+    public void adjustPassQuality5() {
104
+
105
+        int expectedQuality = 39;
106
+
107
+        for (int i = 0; i < 7; i++) {
108
+            inventory.updateQuality();
109
+        }
110
+        int actualQuality = passes.getQuality();
111
+
112
+        Assert.assertEquals(expectedQuality,actualQuality);
113
+
114
+    }
115
+
116
+    @Test
117
+    public void adjustPassQualityNegative() {
118
+
119
+        int expectedQuality = 0;
120
+
121
+        for (int i = 0; i < 15; i++) {
122
+            inventory.updateQuality();
123
+        }
124
+
125
+        int actualQuality = passes.getQuality();
126
+
127
+        Assert.assertEquals(expectedQuality,actualQuality);
128
+    }
129
+
130
+    @Test
131
+    public void testSellIn10() {
132
+
133
+        inventory.decreaseSellIn(passes);
134
+        inventory.decreaseSellIn(passes);
135
+        boolean actualSell = inventory.checkPassSellIn10(passes);
136
+
137
+        Assert.assertTrue(actualSell);
138
+
139
+    }
140
+
141
+    @Test
142
+    public void testSellIn0() {
143
+
144
+        boolean actualSell = inventory.checkSellInNegative(passes);
145
+        Assert.assertFalse(actualSell);
146
+
147
+    }
148
+
149
+    @Test
150
+    public void testSetMaxQuality() {
151
+
152
+        int expectedMaxQuality = 50;
153
+
154
+        inventory.setQualityMax(wine);
155
+        int actualQuality = wine.getQuality();
156
+
157
+        Assert.assertEquals(expectedMaxQuality, actualQuality);
158
+
159
+    }
160
+
161
+    @Test
162
+    public void testSetQualityZero() {
163
+
164
+        int expectedQuality = 0;
165
+
166
+        inventory.setQualityZero(wine);
167
+        int actualQuality = wine.getQuality();
168
+
169
+        Assert.assertEquals(expectedQuality, actualQuality);
170
+    }
171
+
172
+    @Test
173
+    public void testNegativeQualityisZero() {
174
+
175
+        int expectedQuality = 0;
176
+
177
+        for (int i = 0; i < 5; i++) {
178
+            inventory.updateQuality();
179
+        }
180
+
181
+        int actualQuality = conjured.getQuality();
182
+
183
+        Assert.assertEquals(expectedQuality, actualQuality);
184
+    }
185
+
186
+    @Test
187
+    public void testGetName() {
188
+
189
+        String expectedName = "Conjured items by wizard";
190
+
191
+        conjured.setName(expectedName);
192
+        String actualName = conjured.getName();
193
+
194
+        Assert.assertEquals(expectedName, actualName);
195
+
47 196
     }
48 197
 
49 198
 }