Selaa lähdekoodia

refactored code

Demetrius Murray 6 vuotta sitten
vanhempi
commit
7a223871c6

+ 32
- 34
src/main/java/com/zipcodewilmington/gildedrose/Inventory.java Näytä tiedosto

@@ -11,52 +11,50 @@ public class Inventory {
11 11
 
12 12
 
13 13
     public void updateQuality() {
14
+        /*
15
+        loop through items.
16
+
17
+        1. if name DNE "Aged" && DNE "Backstage
18
+            a. if quality > 0
19
+                1. if name is NOT "Sulfuras",  subtract 1 quality | *
20
+        2. If name is either "Aged" or "Backstage"
21
+            a. if quality < 50, add 1 quality | **
22
+                1. if name is "Backstage"
23
+                    a. if getSellIn < 11
24
+                        1. if quality < 50, then add 1 quality | *
25
+                    b. if get sellIn < 6
26
+                        1. if quality < 50, then add 1 quality | *
27
+        1. if name DNE "Sulfuras"
28
+            a. subtract one sellIn | **
29
+
30
+        1. If getSellIn < 0
31
+            a. if name is NOT "Aged"
32
+                1. if name is NOT "Backstage"
33
+                    a. if quality > 0
34
+                        1. if name is NOT "Sulfuras, then subtract 1 quality (useless);
35
+                2. else set quality to 0;
36
+            b. else if quality < 50, add 1 quality
37
+         */
14 38
         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 {
39
+            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
23 40
                 if (items[i].getQuality() < 50) {
24 41
                     items[i].setQuality(items[i].getQuality() + 1);
25
-
26
-                    if (items[i].getName() == "Backstage passes to a TAFKAL80ETC concert") {
42
+                    if (items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
27 43
                         if (items[i].getSellIn() < 11) {
28
-                            if (items[i].getQuality() < 50) {
29
-                                items[i].setQuality(items[i].getQuality() + 1);
30
-                            }
44
+                            items[i].setQuality(items[i].getQuality() + 1);
31 45
                         }
32
-
33 46
                         if (items[i].getSellIn() < 6) {
34
-                            if (items[i].getQuality() < 50) {
35
-                                items[i].setQuality(items[i].getQuality() + 1);
36
-                            }
47
+                            items[i].setQuality(items[i].getQuality() + 1);
37 48
                         }
38 49
                     }
39 50
                 }
40
-            }
41 51
 
42
-            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
43 52
                 items[i].setSellIn(items[i].getSellIn() - 1);
44
-            }
45 53
 
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) {
54
+                if (items[i].getSellIn() < 0) {
55
+                    if (items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
56
+                        items[i].setQuality(0);
57
+                    } else if (items[i].getName().equals("Aged Brie") && items[i].getQuality() < 50) {
60 58
                         items[i].setQuality(items[i].getQuality() + 1);
61 59
                     }
62 60
                 }

+ 88
- 1
src/test/java/com/zipcodewilmington/gildedrose/InventoryTest.java Näytä tiedosto

@@ -4,9 +4,96 @@ import org.junit.Assert;
4 4
 import org.junit.Test;
5 5
 
6 6
 public class InventoryTest {
7
+    Item agedBrie = new Item("Aged Brie", 7,40);
8
+    Item backStage = new Item("Backstage passes to a TAFKAL80ETC concert", 10,20);
9
+    Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 4,15);
10
+    Item[] item = {agedBrie, backStage, sulfuras};
11
+
12
+    Inventory inv = new Inventory(item);
13
+
7 14
     @Test
8 15
     public void updateQuantityTest(){
9
-        Assert.assertEquals(1, 1);
16
+        inv.updateQuality();
17
+        int actual = agedBrie.getQuality();
18
+        int expected = 41;
19
+        Assert.assertEquals(expected,actual);
20
+    }
21
+
22
+    @Test
23
+    public void updateQuantityTest2(){
24
+        inv.updateQuality();
25
+        int actual = backStage.getQuality();
26
+        int expected = 22;
27
+        Assert.assertEquals(expected,actual);
28
+    }
29
+
30
+    @Test
31
+    public void updateQuantityTest3(){
32
+        backStage.setSellIn(4);
33
+        inv.updateQuality();
34
+        int actual = backStage.getQuality();
35
+        int expected = 23;
36
+        Assert.assertEquals(expected,actual);
37
+    }
38
+
39
+    @Test
40
+    public void updateQuantityTest4(){
41
+        inv.updateQuality();
42
+        int actual = sulfuras.getQuality();
43
+        int expected = 15;
44
+        Assert.assertEquals(expected,actual);
45
+    }
46
+
47
+    @Test
48
+    public void updateQuantityTest5(){
49
+        inv.updateQuality();
50
+        int actual = backStage.getSellIn();
51
+        int expected = 9;
52
+        Assert.assertEquals(expected,actual);
53
+    }
54
+
55
+    @Test
56
+    public void updateQuantityTest6(){
57
+        inv.updateQuality();
58
+        int actual = agedBrie.getSellIn();
59
+        int expected = 6;
60
+        Assert.assertEquals(expected,actual);
61
+    }
62
+
63
+    @Test
64
+    public void updateQuantityTest7(){
65
+        agedBrie.setSellIn(-1);
66
+        inv.updateQuality();
67
+        int actual = agedBrie.getQuality();
68
+        int expected = 42;
69
+        Assert.assertEquals(expected,actual);
70
+    }
71
+
72
+    @Test
73
+    public void updateQuantityTest8(){
74
+        backStage.setSellIn(-1);
75
+        inv.updateQuality();
76
+        int actual = backStage.getQuality();
77
+        int expected = 0;
78
+        Assert.assertEquals(expected,actual);
79
+    }
80
+
81
+    @Test
82
+    public void updateQuantityTest9(){
83
+        sulfuras.setSellIn(-1);
84
+        inv.updateQuality();
85
+        int actual = sulfuras.getQuality();
86
+        int expected = 15;
87
+        Assert.assertEquals(expected,actual);
88
+    }
89
+
90
+    @Test
91
+    public void updateQuantityTest10(){
92
+        sulfuras.setName("Aged Brie");
93
+        inv.updateQuality();
94
+        int actual = sulfuras.getQuality();
95
+        int expected = 16;
96
+        Assert.assertEquals(expected,actual);
10 97
     }
11 98
 }
12 99