Xcuello 6 years ago
parent
commit
481dd3706f

+ 64
- 49
src/main/java/com/zipcodewilmington/gildedrose/Inventory.java View File

1
 package com.zipcodewilmington.gildedrose;
1
 package com.zipcodewilmington.gildedrose;
2
 
2
 
3
 
3
 
4
+import com.sun.tools.javac.jvm.Items;
5
+
4
 public class Inventory {
6
 public class Inventory {
7
+
5
     private static Item[] items;
8
     private static Item[] items;
6
 
9
 
7
     public Inventory(Item[] items) {
10
     public Inventory(Item[] items) {
8
 
11
 
9
         super();
12
         super();
10
-        this.items = items;
13
+
14
+        Inventory.items = items;
11
     }
15
     }
12
 
16
 
17
+    public void updateQuality(Item[] items) {
13
 
18
 
14
-    public static void updateQuality() {
19
+        for (Item item : items) {
15
 
20
 
16
-        for (int i = 0; i < items.length; i++) {
21
+            String name = item.getName();
17
 
22
 
18
-            if (!items[i].getName().equals("Aged Brie")
19
-                    && !items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
23
+            int sellin = item.getSellIn();
20
 
24
 
21
-                if (items[i].getQuality() > 0) {
25
+            int quality = item.getQuality();
22
 
26
 
23
-                    if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
24
-                        items[i].setQuality(items[i].getQuality() - 1);
25
-                    }
26
-                }
27
-            } else {
27
+            if (quality >= 0 || quality <= 50) {
28
 
28
 
29
-                if (items[i].getQuality() < 50) {
30
-                    items[i].setQuality(items[i].getQuality() + 1);
29
+                setQuality(name, item, quality, setSellIn(name, sellin, item));
31
 
30
 
32
-                    if (items[i].getName() == "Backstage passes to a TAFKAL80ETC concert") {
31
+            }
33
 
32
 
34
-                        if (items[i].getSellIn() < 11) {
33
+        }
34
+    }
35
 
35
 
36
-                            if (items[i].getQuality() < 50) {
37
-                                items[i].setQuality(items[i].getQuality() + 1);
38
-                            }
39
-                        }
36
+    public void setQuality(String name, Item item, int quality, int sellinAdjustment) {
40
 
37
 
41
-                        if (items[i].getSellIn() < 6) {
38
+        if (name.equals("Aged Brie")) {
42
 
39
 
43
-                            if (items[i].getQuality() < 50) {
44
-                                items[i].setQuality(items[i].getQuality() + 1);
45
-                            }
46
-                        }
47
-                    }
48
-                }
49
-            }
40
+            item.setQuality(quality + sellinAdjustment);
50
 
41
 
51
-            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
52
-                items[i].setSellIn(items[i].getSellIn() - 1);
53
-            }
42
+        } else if (name.equals("Backstage passes to a TAFKAL80ETC concert")) {
54
 
43
 
55
-            if (items[i].getSellIn() < 0) {
44
+            setQualityForPasses(item,quality);
45
+        } else {
56
 
46
 
57
-                if (!items[i].getName().equals("Aged Brie")) {
47
+            item.setQuality(quality - sellinAdjustment);
48
+        }
58
 
49
 
59
-                    if (!items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
60
 
50
 
61
-                        if (items[i].getQuality() > 0) {
51
+    }
62
 
52
 
63
-                            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
64
-                                items[i].setQuality(items[i].getQuality() - 1);
65
-                            }
66
-                        }
67
-                    } else {
53
+    public int setSellIn(String name, int sellin, Item item) {
68
 
54
 
69
-                        items[i].setQuality(0);
70
-                    }
71
-                } else {
55
+        int newSellin = sellin - 1;
56
+
57
+        item.setSellIn(newSellin);
58
+
59
+        int sellinAdjustment = (newSellin <= 0) ? 2 : 1;
60
+
61
+        return sellinAdjustment;
72
 
62
 
73
-                    if (items[i].getQuality() < 50) {
74
-                        items[i].setQuality(items[i].getQuality() + 1);
75
-                    }
76
-                }
77
-            }
78
-        }
79
     }
63
     }
80
-}
64
+
65
+    public void setQualityForPasses(Item item, int quality) {
66
+
67
+        int sellin = item.getSellIn();
68
+
69
+        int newSellin = sellin - 1;
70
+
71
+        item.setSellIn(newSellin);
72
+
73
+        if(newSellin <= 0) {
74
+
75
+            item.setQuality(quality + 2);
76
+
77
+        }else if (newSellin <= 5) {
78
+
79
+            item.setQuality(quality + 3);
80
+
81
+        }else if (newSellin <= 10) {
82
+
83
+            item.setQuality(quality + 2);
84
+
85
+        }else{
86
+
87
+            item.setQuality(quality + 1);
88
+        }
89
+
90
+}
91
+
92
+}
93
+
94
+
95
+

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

5
 
5
 
6
 public class InventoryTest {
6
 public class InventoryTest {
7
     @Test
7
     @Test
8
-    public void updateQuantityTest(){
9
-        Assert.assertEquals(1, 1);
8
+    public void updateQualityPt1() {
9
+
10
+        //Given
11
+        Item item1 = new Item("food", 2, 1);
12
+        Item item2 = new Item("food", 2, 1);
13
+        Item[] items = new Item[]{item1, item2};
14
+        //System.out.println(items);
15
+        Inventory unventory = new Inventory(items);
16
+        // when
17
+        unventory.updateQuality();
18
+        //Then
19
+        int actual = item1.getQuality();
20
+        int expected = 0;
21
+        Assert.assertEquals(expected, actual);
22
+    }
23
+
24
+    @Test
25
+    public void updateQualityPt2() {
26
+        //Given
27
+        Item item1 = new Item("Backstage passes to a TAFKAL80ETC concert", 10, 46);
28
+        Item item2 = new Item("Backstage passes to a TAFKAL80ETC concert", 8, 39);
29
+        Item item3 = new Item("Backstage passes to a TAFKAL80ETC concert", 5, 30);
30
+        Item[] items = new Item[]{item1, item2, item3};
31
+        Inventory newStuff = new Inventory(items);
32
+        //When
33
+        newStuff.updateQuality();
34
+
35
+        //Then
36
+        int actual = item1.getQuality();
37
+        int expected = 48;
38
+        Assert.assertEquals(expected, actual);
39
+    }
40
+
41
+    @Test
42
+    public void updateQualityPt3() {
43
+        //Given
44
+        Item item1 = new Item("This is new name", -2, 5);
45
+        Item item2 = new Item("Another name mayne", -6, 10);
46
+        Item[] items = new Item[]{item1, item2};
47
+        Inventory otherStuff = new Inventory(items);
48
+
49
+        //When
50
+
51
+        otherStuff.updateQuality();
52
+
53
+        //Then
54
+        int actual = item1.getQuality();
55
+        int expected = 3;
56
+        Assert.assertEquals(expected, actual);
57
+    }
58
+
59
+    @Test
60
+    public void updateQualityPt4() {
61
+        //Given
62
+        Item item1 = new Item("Aged Brie", -1, 41);
63
+        Item[] items = new Item[]{item1};
64
+        Inventory differentStuff = new Inventory(items);
65
+
66
+        //When
67
+
68
+        differentStuff.updateQuality();
69
+
70
+        //Then
71
+
72
+        int actual = item1.getQuality();
73
+        int expected = 43;
74
+        Assert.assertEquals(expected, actual);
75
+    }
76
+
77
+    @Test
78
+    public void updateQualityPt5() {
79
+        //Given
80
+        Item item1 = new Item("Backstage passes to a TAFKAL80ETC concert", -2, 2);
81
+        Item[] items = new Item[]{item1};
82
+        Inventory finalVariable = new Inventory(items);
83
+
84
+        //When
85
+        finalVariable.updateQuality();
86
+        //Then
87
+        int actual = item1.getQuality();
88
+        int expected = 0;
89
+        Assert.assertEquals(expected, actual);
90
+
91
+    }
92
+
93
+    @Test
94
+    public void updateQualityPt6() {
95
+        //Given
96
+        Item item1 = new Item("Backstage passes to a TAFKAL80ETC concert", -2, 2);
97
+        Item[] items = new Item[]{item1};
98
+        Inventory variable = new Inventory(items);
99
+
100
+        //When
101
+        item1.setName("Final Name");
102
+
103
+        //Then
104
+        String expected = "Final Name";
105
+        String actual = item1.getName();
106
+        Assert.assertEquals(expected, actual);
107
+        
10
     }
108
     }
11
 }
109
 }
12
 
110