Просмотр исходного кода

Probably making it more complicated

Trinh Tong 6 лет назад
Родитель
Сommit
ec2209dded

+ 192
- 85
src/main/java/com/zipcodewilmington/gildedrose/Inventory.java Просмотреть файл

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

+ 79
- 45
src/test/java/com/zipcodewilmington/gildedrose/InventoryTest.java Просмотреть файл

@@ -14,64 +14,98 @@ import java.util.Arrays;
14 14
 public class InventoryTest {
15 15
     Inventory inventory;
16 16
 
17
-    public InventoryTest() {
18
-
19
-        Item wine = new Item("wine", 2, 25);
20
-        Item agedBrie = new Item("Aged Brie", 1, 45);
21
-        Item passes = new Item("Backstage passes to a TAFKAL80ETC concert", 11, 25);
22
-        Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 10, 80 );
17
+    Item wine = new Item("wine", 2, 25);
18
+    Item agedBrie = new Item("Aged Brie", 1, 45);
19
+    Item passes = new Item("Backstage passes to a TAFKAL80ETC concert", 11, 25);
20
+    Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 10, 80 );
23 21
 
24
-        Item[] items = {wine, agedBrie, passes, sulfuras};
22
+    Item[] items = {wine, agedBrie, passes, sulfuras};
25 23
 
24
+    public InventoryTest() {
26 25
         inventory = new Inventory(items);
27 26
     }
28 27
 
29
-    public void assertTestValues(Inventory inventory, int days, int wineSellIn, int wineQuality, int brieSellIn, int brieQuality,
30
-                                 int passSellIn, int passQuality, int sulfurasSellIn, int sulfurasQuality) {
31
-        for (int i = 0; i < days; i++) {
32
-            inventory.updateQuality();
33
-        }
34
-        Item[] items = inventory.getItems();
35
-        Item currentItem = items[0];
36
-        Assert.assertSame("wine", currentItem.getName());
37
-        Assert.assertEquals(wineSellIn, currentItem.getSellIn());
38
-        Assert.assertEquals(wineQuality, currentItem.getQuality());
39
-
40
-        currentItem = items[1];
41
-        Assert.assertSame("Aged Brie", currentItem.getName());
42
-        Assert.assertEquals(brieSellIn, currentItem.getSellIn());
43
-        Assert.assertEquals(brieQuality, currentItem.getQuality());
44
-
45
-        currentItem = items[2];
46
-        Assert.assertSame("Backstage passes to a TAFKAL80ETC concert", currentItem.getName());
47
-        Assert.assertEquals(passSellIn, currentItem.getSellIn());
48
-        Assert.assertEquals(passQuality, currentItem.getQuality());
49
-
50
-        currentItem = items[3];
51
-        Assert.assertSame("Sulfuras, Hand of Ragnaros", currentItem.getName());
52
-        Assert.assertEquals(sulfurasSellIn, currentItem.getSellIn());
53
-        Assert.assertEquals(sulfurasQuality, currentItem.getQuality());
54
-    }
55
-
56 28
     @Test
57
-    public void updateQualityTest(){
29
+    public void testBrieQuality() {
30
+        // Brie quality increases as sell in decreases
58 31
         // Given
59 32
 
60
-        // One day
61
-        assertTestValues(inventory, 1, 1, 24, 0, 46, 10, 26, 10, 80);
33
+        int expectedBrieQuality = 46;
34
+        String itemName = "agedBrie";
62 35
 
63
-        // One more day
64
-        assertTestValues(inventory, 1, 0, 23, -1, 48, 9, 28, 10, 80);
36
+        // When
37
+        agedBrie.setQuality(agedBrie.getQuality() + 1);
38
+        int actualBrieQuality = agedBrie.getQuality();
65 39
 
66
-        // Four more days
67
-        assertTestValues(inventory, 4, -4, 15, -5, 50, 5, 36, 10, 80);
40
+        // Then
41
+        Assert.assertEquals(expectedBrieQuality,actualBrieQuality);
42
+    }
68 43
 
69
-        // Three more days
70
-        assertTestValues(inventory, 3, -7, 9, -8, 50, 2, 45, 10, 80);
44
+    @Test
45
+    public void testPassQuality() {
71 46
 
72
-        // Five more days
73
-        assertTestValues(inventory, 5, -12, 0, -13, 50, -3, 0, 10, 80);
74 47
     }
75 48
 
76 49
 }
77 50
 
51
+// Test for previous code
52
+
53
+//
54
+//    public InventoryTest() {
55
+//
56
+//        Item wine = new Item("wine", 2, 25);
57
+//        Item agedBrie = new Item("Aged Brie", 1, 45);
58
+//        Item passes = new Item("Backstage passes to a TAFKAL80ETC concert", 11, 25);
59
+//        Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 10, 80 );
60
+//
61
+//        Item[] items = {wine, agedBrie, passes, sulfuras};
62
+//
63
+//        inventory = new Inventory(items);
64
+//    }
65
+//
66
+//    public void assertTestValues(Inventory inventory, int days, int wineSellIn, int wineQuality, int brieSellIn, int brieQuality,
67
+//                                 int passSellIn, int passQuality, int sulfurasSellIn, int sulfurasQuality) {
68
+//        for (int i = 0; i < days; i++) {
69
+//            inventory.updateQuality();
70
+//        }
71
+//        Item[] items = inventory.getItems();
72
+//        Item currentItem = items[0];
73
+//        Assert.assertSame("wine", currentItem.getName());
74
+//        Assert.assertEquals(wineSellIn, currentItem.getSellIn());
75
+//        Assert.assertEquals(wineQuality, currentItem.getQuality());
76
+//
77
+//        currentItem = items[1];
78
+//        Assert.assertSame("Aged Brie", currentItem.getName());
79
+//        Assert.assertEquals(brieSellIn, currentItem.getSellIn());
80
+//        Assert.assertEquals(brieQuality, currentItem.getQuality());
81
+//
82
+//        currentItem = items[2];
83
+//        Assert.assertSame("Backstage passes to a TAFKAL80ETC concert", currentItem.getName());
84
+//        Assert.assertEquals(passSellIn, currentItem.getSellIn());
85
+//        Assert.assertEquals(passQuality, currentItem.getQuality());
86
+//
87
+//        currentItem = items[3];
88
+//        Assert.assertSame("Sulfuras, Hand of Ragnaros", currentItem.getName());
89
+//        Assert.assertEquals(sulfurasSellIn, currentItem.getSellIn());
90
+//        Assert.assertEquals(sulfurasQuality, currentItem.getQuality());
91
+//    }
92
+//
93
+//    @Test
94
+//    public void updateQualityTest(){
95
+//        // Given
96
+//
97
+//        // One day
98
+//        assertTestValues(inventory, 1, 1, 24, 0, 46, 10, 26, 10, 80);
99
+//
100
+//        // One more day
101
+//        assertTestValues(inventory, 1, 0, 23, -1, 48, 9, 28, 10, 80);
102
+//
103
+//        // Four more days
104
+//        assertTestValues(inventory, 4, -4, 15, -5, 50, 5, 36, 10, 80);
105
+//
106
+//        // Three more days
107
+//        assertTestValues(inventory, 3, -7, 9, -8, 50, 2, 45, 10, 80);
108
+//
109
+//        // Five more days
110
+//        assertTestValues(inventory, 5, -12, 0, -13, 50, -3, 0, 10, 80);
111
+//    }