Ver código fonte

100% Test Coverage, and Refactored Code

Saurav Kamath 6 anos atrás
pai
commit
939af3950b

+ 92
- 49
src/main/java/com/zipcodewilmington/gildedrose/Inventory.java Ver arquivo

@@ -1,6 +1,5 @@
1 1
 package com.zipcodewilmington.gildedrose;
2 2
 
3
-
4 3
 public class Inventory {
5 4
     private Item[] items;
6 5
 
@@ -9,58 +8,102 @@ public class Inventory {
9 8
         this.items = items;
10 9
     }
11 10
 
11
+    private static void updateBackstage(Item[] items, int i){
12
+        String itemName = items[i].getName();
13
+        int sellIndays = items[i].getSellIn();
12 14
 
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);
25
-
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
-                        }
32
-
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
-            }
15
+        if (itemName.equals("Backstage Pass") && InventoryUtilities.ValidQualityCheck(items, i)){
41 16
 
42
-            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
43
-                items[i].setSellIn(items[i].getSellIn() - 1);
17
+            if(sellIndays <= 0){
18
+                items[i].setQuality(1);
19
+                InventoryUtilities.decreaseSellin(items, i);
44 20
             }
21
+            else if(sellIndays <= 5){
22
+                InventoryUtilities.increaseQuality(items,i,3);
23
+                InventoryUtilities.decreaseSellin(items, i);
45 24
 
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
-                }
63 25
             }
26
+            else if(sellIndays <= 10){
27
+                InventoryUtilities.increaseQuality(items, i, 2);
28
+                InventoryUtilities.decreaseSellin(items, i);
29
+            }
30
+            else{
31
+                InventoryUtilities.increaseQuality(items, i, 1);
32
+                InventoryUtilities.decreaseSellin(items, i);
33
+            }
34
+
35
+        }
36
+
37
+    }
38
+    private static void updateAgedBrie(Item[] items, int i){
39
+        String itemName = items[i].getName();
40
+
41
+        if(itemName.equals("Aged Brie") && InventoryUtilities.ValidQualityCheck(items, i)){
42
+            InventoryUtilities.increaseQuality(items,i,1);
43
+            InventoryUtilities.decreaseSellin(items, i);
44
+        }
45
+
46
+    }
47
+
48
+    private static void updatePastSellin(Item[] items, int i){
49
+        int itemSellin = items[i].getSellIn();
50
+        String itemName = items[i].getName();
51
+
52
+        if(itemSellin < 0 && InventoryUtilities.ValidQualityCheck(items, i) && !itemName.equals("Aged Brie")){
53
+            InventoryUtilities.decreaseQuality(items, i, 2);
54
+            InventoryUtilities.decreaseSellin(items, i);
64 55
         }
56
+
65 57
     }
66
-}
58
+    private static void updateConjured(Item[] items, int i){
59
+        int itemSellin = items[i].getSellIn();
60
+        String itemName = items[i].getName();
61
+
62
+        if(InventoryUtilities.ValidQualityCheck(items, i) && itemName.equals("Conjured Item")){
63
+            InventoryUtilities.decreaseQuality(items, i, 2);
64
+            InventoryUtilities.decreaseSellin(items, i);
65
+        }
66
+
67
+    }
68
+    private static void updateSulfuras(Item[] items, int i){
69
+        String itemName = items[i].getName();
70
+        if (itemName.equals("Sulfuras")){
71
+            items[i].setQuality(80);
72
+        }
73
+
74
+    }
75
+
76
+    private static void updateNormalItem(Item[] items, int i){
77
+        if(InventoryUtilities.ValidQualityCheck(items, i) && !checkUnique(items, i)){
78
+
79
+            InventoryUtilities.decreaseQuality(items, i, 1);
80
+            InventoryUtilities.decreaseSellin(items, i);
81
+        }
82
+
83
+    }
84
+
85
+    private static boolean checkUnique(Item[] items, int i){
86
+        String itemName = items[i].getName();
87
+        int itemSellin = items[i].getSellIn();
88
+
89
+        if(itemName.equals("Aged Brie") || itemName.equals("Backstage Pass") || itemName.equals("Sulfuras") || itemName.equals("Conjured Item") || itemSellin < 0)
90
+        {
91
+            return true;
92
+        }
93
+        else
94
+            {
95
+            return false;
96
+            }
97
+
98
+    }
99
+    public void updateQuality() {
100
+        for (int i = 0; i < items.length; i++) {
101
+            updateBackstage(items, i);
102
+            updateNormalItem(items, i);
103
+            updatePastSellin(items, i);
104
+            updateAgedBrie(items, i);
105
+            updateSulfuras(items, i);
106
+            updateConjured(items, i);
107
+            }
108
+        }
109
+    }

+ 32
- 0
src/main/java/com/zipcodewilmington/gildedrose/InventoryUtilities.java Ver arquivo

@@ -0,0 +1,32 @@
1
+package com.zipcodewilmington.gildedrose;
2
+
3
+public class InventoryUtilities {
4
+    private Item[] items;
5
+
6
+
7
+
8
+    public static void increaseQuality(Item[] items, int i, int howMuch){
9
+        for(int idx = 0; idx <= howMuch; idx++){
10
+            items[i].setQuality(items[i].getQuality() + 1);
11
+        }
12
+    }
13
+
14
+    public static void decreaseQuality(Item[] items, int i, int howMuch){
15
+        for(int idx = 2; idx <= howMuch; idx++) {
16
+            items[i].setQuality(items[i].getQuality() - 1);
17
+        }
18
+    }
19
+    public static void decreaseSellin(Item[] items, int i){
20
+        items[i].setQuality(items[i].getQuality() - 1);
21
+    }
22
+
23
+
24
+    public static boolean ValidQualityCheck(Item[] items, int i){
25
+        int itemQuality = items[i].getQuality();
26
+
27
+        if(itemQuality > 0 && itemQuality < 50){
28
+            return true;
29
+        }
30
+        return false;
31
+    }
32
+}

+ 178
- 2
src/test/java/com/zipcodewilmington/gildedrose/InventoryTest.java Ver arquivo

@@ -5,8 +5,184 @@ 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 checkSetName(){
9
+
10
+        Item normalItem = new Item("Sock", 20, 9);
11
+        Item[] itemList = new Item[1];
12
+        itemList[0] = normalItem;
13
+        itemList[0].setName("Sulfuras");
14
+        String actual = itemList[0].getName();
15
+
16
+        Assert.assertEquals("Sulfuras",actual);
17
+    }
18
+
19
+    @Test
20
+    public void checkSetSellin(){
21
+
22
+        Item normalItem = new Item("Sock", 20, 9);
23
+        Item[] itemList = new Item[1];
24
+        itemList[0] = normalItem;
25
+        itemList[0].setSellIn(10);
26
+        int actual = itemList[0].getSellIn();
27
+
28
+        Assert.assertEquals(10,actual);
29
+    }
30
+
31
+    @Test
32
+    public void checkNormalQuality(){
33
+        //Given
34
+        Item normalItem = new Item("Sock", 20, 9);
35
+        Item[] itemList = new Item[1];
36
+        itemList[0] = normalItem;
37
+
38
+        Inventory testInventory = new Inventory(itemList);
39
+        testInventory.updateQuality();
40
+
41
+        int actual = itemList[0].getQuality();
42
+
43
+        Assert.assertEquals(8, actual);
44
+    }
45
+    @Test
46
+    public void checkPastSellin(){
47
+        //Given
48
+        Item normalItem = new Item("Sock", -1, 9);
49
+        Item[] itemList = new Item[1];
50
+        itemList[0] = normalItem;
51
+
52
+        Inventory testInventory = new Inventory(itemList);
53
+        testInventory.updateQuality();
54
+
55
+        int actual = itemList[0].getQuality();
56
+
57
+        Assert.assertEquals(7, actual);
58
+    }
59
+    @Test
60
+    public void checkSulfuras(){
61
+        //Given
62
+        Item normalItem = new Item("Sulfuras", -1, 9);
63
+        Item[] itemList = new Item[1];
64
+        itemList[0] = normalItem;
65
+
66
+        Inventory testInventory = new Inventory(itemList);
67
+        testInventory.updateQuality();
68
+
69
+        int actual = itemList[0].getQuality();
70
+
71
+        Assert.assertEquals(80, actual);
72
+    }
73
+    @Test
74
+    public void checkeAgedBrie(){
75
+        //Given
76
+        Item normalItem = new Item("Aged Brie", -1, 9);
77
+        Item[] itemList = new Item[1];
78
+        itemList[0] = normalItem;
79
+
80
+        Inventory testInventory = new Inventory(itemList);
81
+        testInventory.updateQuality();
82
+
83
+        int actual = itemList[0].getQuality();
84
+
85
+        Assert.assertEquals(10, actual);
86
+    }
87
+    @Test
88
+    public void checkBackStagePassesDay20(){
89
+        //Given
90
+        Item normalItem = new Item("Backstage Pass", 20, 9);
91
+        Item[] itemList = new Item[1];
92
+        itemList[0] = normalItem;
93
+
94
+        Inventory testInventory = new Inventory(itemList);
95
+        testInventory.updateQuality();
96
+
97
+        int actual = itemList[0].getQuality();
98
+
99
+        Assert.assertEquals(10, actual);
10 100
     }
101
+    @Test
102
+    public void checkBackStagePassesDay10(){
103
+        //Given
104
+        Item normalItem = new Item("Backstage Pass", 10, 9);
105
+        Item[] itemList = new Item[1];
106
+        itemList[0] = normalItem;
107
+
108
+        Inventory testInventory = new Inventory(itemList);
109
+        testInventory.updateQuality();
110
+
111
+        int actual = itemList[0].getQuality();
112
+
113
+        Assert.assertEquals(11, actual);
114
+    }
115
+    @Test
116
+    public void checkBackStagePassesDay5(){
117
+        //Given
118
+        Item normalItem = new Item("Backstage Pass", 5, 9);
119
+        Item[] itemList = new Item[1];
120
+        itemList[0] = normalItem;
121
+
122
+        Inventory testInventory = new Inventory(itemList);
123
+        testInventory.updateQuality();
124
+
125
+        int actual = itemList[0].getQuality();
126
+
127
+        Assert.assertEquals(12, actual);
128
+    }
129
+    @Test
130
+    public void checkBackStagePassesDay0(){
131
+        //Given
132
+        Item normalItem = new Item("Backstage Pass", 0, 9);
133
+        Item[] itemList = new Item[1];
134
+        itemList[0] = normalItem;
135
+
136
+        Inventory testInventory = new Inventory(itemList);
137
+        testInventory.updateQuality();
138
+
139
+        int actual = itemList[0].getQuality();
140
+
141
+        Assert.assertEquals(0, actual);
142
+    }
143
+    @Test
144
+    public void checkConjured(){
145
+        //Given
146
+        Item normalItem = new Item("Conjured Item", 4, 9);
147
+        Item[] itemList = new Item[1];
148
+        itemList[0] = normalItem;
149
+
150
+        Inventory testInventory = new Inventory(itemList);
151
+        testInventory.updateQuality();
152
+
153
+        int actual = itemList[0].getQuality();
154
+
155
+        Assert.assertEquals(7, actual);
156
+    }
157
+
158
+    @Test
159
+    public void checkMin(){
160
+        //Given
161
+        Item normalItem = new Item("Conjured Item", 4, 0);
162
+        Item[] itemList = new Item[1];
163
+        itemList[0] = normalItem;
164
+
165
+        Inventory testInventory = new Inventory(itemList);
166
+        testInventory.updateQuality();
167
+
168
+        int actual = itemList[0].getQuality();
169
+
170
+        Assert.assertEquals(0, actual);
171
+    }
172
+    @Test
173
+    public void checkMax(){
174
+        //Given
175
+        Item normalItem = new Item("Backstage Pass", 4, 50);
176
+        Item[] itemList = new Item[1];
177
+        itemList[0] = normalItem;
178
+
179
+        Inventory testInventory = new Inventory(itemList);
180
+        testInventory.updateQuality();
181
+
182
+        int actual = itemList[0].getQuality();
183
+
184
+        Assert.assertEquals(50, actual);
185
+    }
186
+
11 187
 }
12 188