NiraParikh hace 6 años
padre
commit
977a40a4ea

+ 80
- 46
src/main/java/com/zipcodewilmington/gildedrose/Inventory.java Ver fichero

@@ -9,58 +9,92 @@ public class Inventory {
9 9
         this.items = items;
10 10
     }
11 11
 
12
+    public void updateQuality02() {
12 13
 
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
-                }
14
+        for(int i = 0; i < items.length; i ++){
15
+
16
+            switch(items[i].getName()){
17
+
18
+                case "Aged Brie":
19
+                    increaseQuality(items[i]);
20
+                    passedSellin(items[i]);
21
+                    break;
22
+
23
+                case "Backstage passes to a TAFKAL80ETC concert":
24
+                    increaseQuality(items[i]);
25
+                    checkSellinTickets(items[i]);
26
+                    passedSellin(items[i]);
27
+                    break;
28
+
29
+                case "Sulfuras, Hand of Ragnaros":
30
+                    break;
31
+
32
+                case "Conjured Item":
33
+                    decreaseQuality(items[i]);
34
+                    decreaseQuality(items[i]);
35
+                    passedSellin(items[i]);
36
+                    break;
37
+
38
+                default:
39
+                    decreaseQuality(items[i]);
40
+                    passedSellin(items[i]);
41
+                    break;
40 42
             }
43
+        }
44
+    }
45
+
46
+    private static void increaseQuality(Item item){
47
+        if(item.getQuality() < 50){
48
+            item.setQuality(item.getQuality() + 1);
49
+        }
50
+    }
41 51
 
42
-            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
43
-                items[i].setSellIn(items[i].getSellIn() - 1);
52
+    private static void decreaseQuality(Item item){
53
+        if(item.getQuality() > 0){
54
+            item.setQuality(item.getQuality() -1);
55
+        }
56
+    }
57
+
58
+    private static void checkSellinTickets(Item item){
59
+        //if the items sell in is less then 11
60
+        if(item.getSellIn() < 11){
61
+            //decrease quality
62
+            increaseQuality(item);
63
+            //if it is also less then 6
64
+            if(item.getSellIn() < 6){
65
+                //decrease quality
66
+                increaseQuality(item);
44 67
             }
68
+        }
69
+    }
70
+
71
+    private static void passedSellin(Item item){
72
+        switch(item.getName()) {
73
+            case "Aged Brie":
74
+                increaseQuality(item);
75
+                break;
45 76
 
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
-                    }
77
+            case "Backstage passes to a TAFKAL80ETC concert":
78
+                if(item.getSellIn() < 0) {
79
+                    item.setQuality(0);
62 80
                 }
63
-            }
81
+                break;
82
+
83
+            case "Conjured Item":
84
+                checkPassedDate(item);
85
+                checkPassedDate(item);
86
+                break;
87
+
88
+            default:
89
+                checkPassedDate(item);
90
+                break;
91
+        }
92
+    }
93
+
94
+    private static void checkPassedDate(Item item){
95
+        if(item.getSellIn() < 0){
96
+            decreaseQuality(item);
64 97
         }
65 98
     }
99
+
66 100
 }

+ 88
- 2
src/test/java/com/zipcodewilmington/gildedrose/InventoryTest.java Ver fichero

@@ -4,9 +4,95 @@ import org.junit.Assert;
4 4
 import org.junit.Test;
5 5
 
6 6
 public class InventoryTest {
7
+
7 8
     @Test
8 9
     public void updateQuantityTest(){
9
-        Assert.assertEquals(1, 1);
10
+        //test item that should go down 1 quality in 1 day
11
+        Item item = new Item("A book", 12, 20);
12
+        Item[] items = {item};
13
+
14
+        Inventory inv = new Inventory(items);
15
+        inv.updateQuality02();
16
+        Assert.assertEquals(19, item.getQuality());
17
+    }
18
+
19
+    @Test
20
+    public void updateQuantityTest02(){
21
+        //test item that should go down two quality in 1 day
22
+        Item item = new Item("A book", -1, 20);
23
+        Item[] items = {item};
24
+
25
+        Inventory inv = new Inventory(items);
26
+        inv.updateQuality02();
27
+        System.out.println(item.getQuality());
28
+        Assert.assertEquals(18, item.getQuality());
10 29
     }
11
-}
12 30
 
31
+    @Test
32
+    public void updateQuantityTest03(){
33
+        //test aged brie that should increase in quality twice
34
+        Item item = new Item("Aged Brie", -1, 15);
35
+        Item[] items = {item};
36
+
37
+        Inventory inv = new Inventory(items);
38
+        inv.updateQuality02();
39
+        Assert.assertEquals(17, item.getQuality());
40
+    }
41
+
42
+    @Test
43
+    public void updateQuantityTest04() {
44
+        //test that tickets for TAFKAL80ETC goes up three times
45
+        Item item = new Item("Backstage passes to a TAFKAL80ETC concert", 5, 15);
46
+        Item[] items = {item};
47
+
48
+        Inventory inv = new Inventory(items);
49
+        inv.updateQuality02();
50
+        Assert.assertEquals(18, item.getQuality());
51
+    }
52
+
53
+    @Test
54
+    public void updateQuantityTest05() {
55
+        //test that tickets for TAFKAL80ETC goes up three times
56
+        Item item = new Item("Aged Brie", 0, 15);
57
+        item.setName("A book");
58
+        item.setSellIn(50);
59
+        Item[] items = {item};
60
+
61
+        Inventory inv = new Inventory(items);
62
+        inv.updateQuality02();
63
+        Assert.assertEquals(14, item.getQuality());
64
+    }
65
+
66
+    @Test
67
+    public void updateQuantityTest06() {
68
+        //test that tickets for TAFKAL80ETC goes up three times
69
+        Item item = new Item("Backstage passes to a TAFKAL80ETC concert", -3, 15);
70
+        Item[] items = {item};
71
+
72
+        Inventory inv = new Inventory(items);
73
+        inv.updateQuality02();
74
+        Assert.assertEquals(0, item.getQuality());
75
+    }
76
+
77
+    @Test
78
+    public void updateQuantityTest07() {
79
+        //test that tickets for TAFKAL80ETC goes up three times
80
+        Item item = new Item("Sulfuras, Hand of Ragnaros", 0, 15);
81
+        Item[] items = {item};
82
+
83
+        Inventory inv = new Inventory(items);
84
+        inv.updateQuality02();
85
+        Assert.assertEquals(15, item.getQuality());
86
+    }
87
+
88
+    @Test
89
+    public void updateQuantityTest08() {
90
+        //test that tickets for TAFKAL80ETC goes up three times
91
+        Item item = new Item("Conjured Item", -1, 15);
92
+        Item[] items = {item};
93
+
94
+        Inventory inv = new Inventory(items);
95
+        inv.updateQuality02();
96
+        Assert.assertEquals(11, item.getQuality());
97
+    }
98
+}