瀏覽代碼

CleanCode with test

Jamez-s 6 年之前
父節點
當前提交
d05bf41623
共有 4 個文件被更改,包括 87 次插入28 次删除
  1. 31
    25
      Inventory.java
  2. 44
    3
      InventoryTest.java
  3. 12
    0
      README.MD
  4. 0
    0
      README.TXT

+ 31
- 25
Inventory.java 查看文件

@@ -8,32 +8,38 @@ public class Inventory {
8 8
     }
9 9
 
10 10
     public void updateQuality() {
11
-        for (int i = 0; i < items.length; i++) {
12
-            if (!items[i].getName().equals("Aged Brie")) {
13
-                if (items[i].getQuality() > 0) {
14
-                    if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
15
-                        items[i].setQuality(items[i].getQuality() - 1);
16
-                    }
17
-                }
18
-            } else {
19
-                if (items[i].getQuality() < 50) {
20
-                    items[i].setQuality(items[i].getQuality() + 1);
21
-                }
11
+        for (int i = 0; i < items.length; i++) { // each if/else handles each specific item
12
+            if (items[i].getName().equals("Sulfuras, Hand of Ragnoros")) {
13
+                continue;
22 14
             }
15
+            if (items[i].getName().equals("Aged Brie")) {
16
+                updateAgedBrie(i);
17
+            } else updateNormalItem(i);
18
+        }
19
+    }
23 20
 
24
-            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
25
-                items[i].setSellIn(items[i].getSellIn() - 1);
26
-            }
21
+    public void updateAgedBrie(int i) {
22
+        if (items[i].getSellIn() > 0 && items[i].getQuality() <= 49) {
23
+            items[i].setQuality(items[i].getQuality() + 1);
24
+        } else if (items[i].getSellIn() <= 0 && items[i].getQuality() <= 48) {
25
+            items[i].setQuality(items[i].getQuality() + 2);
26
+        }
27
+    }
27 28
 
28
-            if (items[i].getSellIn() < 0) {
29
-                if (!items[i].getName().equals("Aged Brie")) {
30
-                  items[i].setQuality(items[i].getQuality() - 1);
31
-                } else {
32
-                    if (items[i].getQuality() < 50) {
33
-                        items[i].setQuality(items[i].getQuality() + 1);
34
-                    }
35
-                }
36
-            }
29
+    public void updateNormalItem(int i) {
30
+        if (items[i].getSellIn() > 0 && items[i].getQuality() <= 49) {
31
+            items[i].setQuality(items[i].getQuality() - 1);
32
+        } else if (items[i].getSellIn() <= 0 && items[i].getQuality() <= 48) {
33
+            items[i].setQuality(items[i].getQuality() - 2);
37 34
         }
38
-}
39
-}
35
+    }
36
+
37
+    public void updateConjuredItem(int i) {
38
+        if (items[i].getSellIn() > 0 && items[i].getQuality() <= 50){
39
+            items[i].setQuality(items[i].getQuality() - 1);
40
+        }
41
+        if (items[i].getSellIn() <= 0 && items[i].getQuality() <= 50){
42
+            items[i].setQuality(items[i].getQuality() - 4);
43
+        }
44
+    }
45
+}

+ 44
- 3
InventoryTest.java 查看文件

@@ -8,14 +8,18 @@ public class InventoryTest {
8 8
     private static final String AGED_BRIE_NAME = "Aged Brie";
9 9
     private static final String BACKSTAGE_PASS_NAME = "Backstage passes to a TAFKAL80ETC concert";
10 10
     private static final String NORMAL_ITEM_NAME = "+5 Dexterity Vest";
11
-
11
+    private static final String CONJURED_ITEM_NAME = "Conjured Holy Water";
12
+    private static final int minimumQuality = 0;
13
+    
12 14
     private Item sulfuras;
13 15
     private Item normalItem;
14
-
16
+    private Item conjuredItem;
17
+    
15 18
     @Before
16 19
     public void setup(){
17 20
         sulfuras = new Item("Sulfuras, Hand of Ragnaros", 0, 80);
18 21
         normalItem = new Item(NORMAL_ITEM_NAME, 10, 20);
22
+        conjuredItem = new Item(CONJURED_ITEM_NAME, 10, MAX_QUALITY);
19 23
     }
20 24
 
21 25
     @Test
@@ -28,6 +32,7 @@ public class InventoryTest {
28 32
 
29 33
         assertEquals(expectedQuality, sulfuras.getQuality());
30 34
     }
35
+
31 36
     @Test
32 37
     public void testUpdate_ForSulfurasSellIn() throws Exception {
33 38
         int expectedSellIn = sulfuras.getSellIn();
@@ -119,4 +124,40 @@ public class InventoryTest {
119 124
 
120 125
         assertEquals(expectedQuality, agedBrie.getQuality());
121 126
     }
122
-}
127
+
128
+    @Test
129
+    public void testUpdate_WhenConjuredItem() throws Exception {
130
+        Item conjuredItem = new Item(CONJURED_ITEM_NAME, 10, 25);
131
+        int expectedQuality = conjuredItem.getQuality() - 1;
132
+        Item[] items = {conjuredItem};
133
+        Inventory sut = new Inventory(items);
134
+
135
+        sut.updateQuality();
136
+
137
+        assertEquals(expectedQuality, conjuredItem.getQuality());
138
+    }
139
+
140
+    @Test
141
+    public void testUpdate_WhenConjuredItemIsMaxQuality() throws Exception {
142
+        Item agedBrie = new Item(CONJURED_ITEM_NAME, 10, MAX_QUALITY);
143
+        int expectedQuality = conjuredItem.getQuality();
144
+        Item[] items = {conjuredItem};
145
+        Inventory sut = new Inventory(items);
146
+
147
+        sut.updateQuality();
148
+
149
+        assertEquals(expectedQuality, conjuredItem.getQuality());
150
+    }
151
+    
152
+     @Test
153
+    public void testUpdate_WhenConjuredItemOnceTheSellInDatePassed() throws Exception {
154
+        Item conjuredItem = new Item(CONJURED_ITEM_NAME, -1, 10);
155
+        int expectedQuality = conjuredItem.getQuality() - 2;
156
+        Item[] items = {conjuredItem};
157
+        Inventory sut = new Inventory(items);
158
+
159
+        sut.updateQuality();
160
+
161
+        assertEquals(expectedQuality, conjuredItem.getQuality());
162
+    }
163
+}

+ 12
- 0
README.MD 查看文件

@@ -71,3 +71,15 @@ new code as long as everything still works correctly.
71 71
 * Don't use the `!` for your if statement
72 72
 
73 73
 This was "borrowed" from [alexaitken](https://github.com/alexaitken/GildedRose_java) which originated from [Bobby Johnson's Blog](https://iamnotmyself.com/2011/02/13/refactor-this-the-gilded-rose-kata/).
74
+
75
+
76
+WHAT WE KNOW
77
+- all items have int SellIn
78
+- all items have int Quality
79
+- Regular items:
80
+    - QUality goes down by 1
81
+    - QUality goes down by 2 if SellIN < 0
82
+    
83
+AgedBrie
84
+    - QWuality goes up by 1
85
+    -Quality goes up by 2 if SellIn < 0

+ 0
- 0
README.TXT 查看文件