Browse Source

Product Manager

Zavon Malone 6 years ago
parent
commit
2bc5999a1e

+ 71
- 46
src/main/java/com/zipcodewilmington/gildedrose/Inventory.java View File

3
 
3
 
4
 public class Inventory {
4
 public class Inventory {
5
     private Item[] items;
5
     private Item[] items;
6
+    private int maxQuality = 50;
7
+    private int legendaryQuality = 80;
6
 
8
 
7
     public Inventory(Item[] items) {
9
     public Inventory(Item[] items) {
8
         super();
10
         super();
10
     }
12
     }
11
 
13
 
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
 
15
 
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
-                        }
16
+    //insert method to set this.items = adjustItems(this.items)
32
 
17
 
33
-                        if (items[i].getSellIn() < 6) {
34
-                            if (items[i].getQuality() < 50) {
35
-                                items[i].setQuality(items[i].getQuality() + 1);
36
-                            }
37
-                        }
38
-                    }
39
-                }
18
+    public Item[] adjustItems(Item[] items) {
19
+        for (Item item : items) {
20
+            if (item.getName().equals("Sulfuras, Hand of Ragnaros")) {
21
+                item = sulfurasHandler(item);
22
+            } else if (item.getName().equals("Aged Brie")) {
23
+                item = agedBrieHandler(item);
24
+            } else if (item.getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
25
+                item = backstagePassHandler(item);
40
             }
26
             }
27
+        }
28
+        return items;
29
+    }
41
 
30
 
42
-            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
43
-                items[i].setSellIn(items[i].getSellIn() - 1);
44
-            }
31
+    public Item sulfurasHandler(Item legendaryItem) {
32
+        legendaryItem.setQuality(legendaryQuality);
33
+        return legendaryItem;
34
+    }
45
 
35
 
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
-            }
36
+    public Item agedBrieHandler(Item item) {
37
+        int x = 1;
38
+        int quality = item.getQuality();
39
+        if (isMaxQuality(quality) && !itemIsExpired(item)) {
40
+            return item;
41
+        } else if (itemIsExpired(item)) {
42
+            x = -2;
43
+        }
44
+        adjustItemQuality(item, x);
45
+        adjustItemSellIn(item);
46
+        return item;
47
+    }
48
+
49
+    public Item backstagePassHandler(Item item) {
50
+        int x = 0;
51
+        int sellIn = item.getSellIn();
52
+        if (isMaxQuality(item.getQuality()) && !itemIsExpired(item)) return item;
53
+        else if (sellIn >= 10) {
54
+            x = 1;
55
+        } else if (sellIn >= 5) {
56
+            x = 2;
57
+        } else if (sellIn > 0) {
58
+            x = 3;
59
+        } else {
60
+            x = item.getQuality() * -1;
61
+        }
62
+        adjustItemQuality(item, x);
63
+        adjustItemSellIn(item);
64
+        return item;
65
+    }
66
+
67
+    public boolean isMaxQuality(int n) {
68
+
69
+        return n == maxQuality;
70
+    }
71
+
72
+    public boolean itemIsExpired(Item i) {
73
+        return i.getSellIn() <= 0;
74
+    }
75
+
76
+    public void adjustItemQuality(Item i, Integer n) {
77
+        int q = i.getQuality();
78
+        int result = q + n;
79
+        if (result < 0) {
80
+            i.setQuality(0);
81
+        } else if (result <= maxQuality) {
82
+            i.setQuality(result);
83
+        }  else {
84
+            i.setQuality(maxQuality);
64
         }
85
         }
65
     }
86
     }
87
+
88
+    public void adjustItemSellIn(Item i) {
89
+        i.setSellIn(i.getSellIn() - 1);
90
+    }
66
 }
91
 }

+ 101
- 2
src/test/java/com/zipcodewilmington/gildedrose/InventoryTest.java View File

4
 import org.junit.Test;
4
 import org.junit.Test;
5
 
5
 
6
 public class InventoryTest {
6
 public class InventoryTest {
7
+
7
     @Test
8
     @Test
8
-    public void updateQuantityTest(){
9
-        Assert.assertEquals(1, 1);
9
+    public void testAdjustItem(){
10
+        Item agedBrie = new Item("Aged Brie", 10, 30);
11
+        Item backstagePasses = new Item("Backstage passes to a TAFKAL80ETC concert", 10, 30);
12
+        Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 10, 80);
13
+        Item[] expected = {agedBrie, backstagePasses, sulfuras};
14
+
15
+        Item[] actual = expected;
16
+
17
+
18
+
19
+        Assert.assertEquals(expected, actual);
10
     }
20
     }
21
+
22
+
23
+    @Test
24
+    public void testSulfurasHandler() {
25
+        Item agedBrie = new Item("Aged Brie", 10, 30);
26
+        Item backstagePasses = new Item("Backstage passes to a TAFKAL80ETC concert", 10, 30);
27
+        Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 10, 80);
28
+        Item[] item = {agedBrie, backstagePasses, sulfuras};
29
+        Inventory inv = new Inventory(item);
30
+        int before = sulfuras.getQuality();
31
+        int expected = before;
32
+
33
+        int actual = before;
34
+
35
+        Assert.assertEquals(expected, actual);
36
+    }
37
+
38
+    @Test
39
+    public void testAgedBrieHandler() {
40
+        // given
41
+        Item agedBrie = new Item("Aged Brie", 10, 30);
42
+        Item backstagePasses = new Item("Backstage passes to a TAFKAL80ETC concert", 10, 30);
43
+        Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 10, 80);
44
+        Item[] item = {agedBrie, backstagePasses, sulfuras};
45
+        Inventory inv = new Inventory(item);
46
+        int expected = agedBrie.getQuality() + 1;
47
+
48
+        // when
49
+        inv.adjustItemQuality(agedBrie, 1);
50
+        int actual= agedBrie.getQuality();
51
+
52
+
53
+        //then
54
+        Assert.assertEquals(expected, actual);
55
+    }
56
+
57
+    @Test
58
+    public void testBackstagePassesHandler(){
59
+        // given
60
+        Item agedBrie = new Item("Aged Brie", 10, 30);
61
+        Item backstagePasses = new Item("Backstage passes to a TAFKAL80ETC concert", 10, 30);
62
+        Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 10, 80);
63
+        Item[] item = {agedBrie, backstagePasses, sulfuras};
64
+        Inventory inv = new Inventory(item);
65
+        int expected = backstagePasses.getQuality() + 2;
66
+
67
+        //when
68
+        inv.adjustItemQuality(backstagePasses, 2);
69
+        int actual = backstagePasses.getQuality();
70
+
71
+        //then
72
+        Assert.assertEquals(expected, actual);
73
+    }
74
+
75
+    @Test
76
+    public void testIsMaxQuality(){
77
+        //given
78
+        Item agedBrie = new Item("Aged Brie", 10, 50);
79
+        Item backstagePasses = new Item("Backstage passes to a TAFKAL80ETC concert", 10, 50);
80
+        Item[] item = {agedBrie, backstagePasses};
81
+        Inventory inv = new Inventory(item);
82
+
83
+
84
+        Assert.assertTrue(inv.isMaxQuality(50));
85
+    }
86
+
87
+    @Test
88
+    public void testItemisExpired(){
89
+        Item agedBrie = new Item("Aged Brie", 0, 50);
90
+        Item backstagePasses = new Item("Backstage passes to a TAFKAL80ETC concert", 0, 50);
91
+        Item[] item = {agedBrie, backstagePasses};
92
+        Inventory inv = new Inventory(item);
93
+
94
+        Assert.assertTrue(inv.itemIsExpired(agedBrie));
95
+    }
96
+
97
+    @Test
98
+    public void testAdjustItemSellIn(){
99
+        Item agedBrie = new Item("Aged Brie", 1, 50);
100
+        Item backstagePasses = new Item("Backstage passes to a TAFKAL80ETC concert", 1, 50);
101
+        Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 1, 80);
102
+        Item[] item = {agedBrie, backstagePasses, sulfuras};
103
+        Inventory inv = new Inventory(item);
104
+
105
+        inv.adjustItemSellIn(agedBrie);
106
+
107
+        Assert.assertTrue(inv.itemIsExpired(agedBrie));
108
+    }
109
+
11
 }
110
 }
12
 
111