Browse Source

GildedRoseKatagit add .

Chaitali Patel 6 years ago
parent
commit
5c9ccd27f0

+ 103
- 47
src/main/java/com/zipcodewilmington/gildedrose/Inventory.java View File

@@ -5,62 +5,118 @@ public class Inventory {
5 5
     private Item[] items;
6 6
 
7 7
     public Inventory(Item[] items) {
8
-        super();
9 8
         this.items = items;
10 9
     }
11 10
 
12
-
13 11
     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
-                }
12
+        for (Item item : items) {
13
+            ItemCategory category = categorize(item);
14
+            category.updateOneItem(item, this);
15
+        }
16
+    }
17
+
18
+    private ItemCategory categorize(Item item) {
19
+        if (item.getName().equals("Sulfuras, Hand of Ragnaros"))
20
+            return new Legendary();
21
+        if (item.getName().equals("Aged Brie"))
22
+            return new Cheese();
23
+        if (item.getName().equals("Backstage passes to a TAFKAL80ETC concert"))
24
+            return new BackstagePass();
25
+        if(item.getName().startsWith("Conjured"))
26
+            return new Conjured();
27
+        return new ItemCategory();
28
+    }
29
+
30
+    private boolean hasExpired(Item item) {
31
+        return item.getSellIn() < 0;
32
+    }
33
+
34
+
35
+    private class ItemCategory {
36
+        protected void incrementQuality(Item item) {
37
+            if (item.getQuality() < 50) {
38
+                item.setQuality(item.getQuality() + 1);
39
+            }
40
+        }
41
+
42
+        protected void decrementQuality(Item item) {
43
+            if (item.getQuality() > 0) {
44
+                item.setQuality(item.getQuality() - 1);
45
+            }
46
+        }
47
+
48
+        protected void updateExpired(Item item) {
49
+                decrementQuality(item);
50
+        }
51
+
52
+        protected void updateSellIn(Item item) {
53
+                item.setSellIn(item.getSellIn() - 1);
54
+
55
+        }
56
+
57
+        protected void updateQuality(Item item) {
58
+            decrementQuality(item);
59
+        }
60
+
61
+        protected void updateOneItem(Item item, Inventory inventory) {
62
+            updateQuality(item);
63
+
64
+            updateSellIn(item);
65
+
66
+            if (inventory.hasExpired(item)) {
67
+                updateExpired(item);
40 68
             }
69
+        }
70
+    }
71
+
72
+    private class Legendary extends ItemCategory {
73
+        protected void updateExpired(Item item) {}
74
+
75
+        protected void updateSellIn(Item item) {}
76
+
77
+        protected void updateQuality(Item item) {}
78
+    }
79
+
80
+    private class Cheese extends ItemCategory {
81
+        protected void updateExpired(Item item) {
82
+                incrementQuality(item);
83
+        }
41 84
 
42
-            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
43
-                items[i].setSellIn(items[i].getSellIn() - 1);
85
+        protected void updateQuality(Item item) {
86
+                incrementQuality(item);
87
+
88
+        }
89
+    }
90
+
91
+    private class BackstagePass extends ItemCategory {
92
+        protected void updateExpired(Item item) {
93
+            item.setQuality(0);
94
+        }
95
+
96
+        protected void updateQuality(Item item) {
97
+            incrementQuality(item);
98
+
99
+            if (item.getSellIn() < 11) {
100
+                incrementQuality(item);
44 101
             }
45 102
 
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
-                }
103
+            if (item.getSellIn() < 6) {
104
+                incrementQuality(item);
63 105
             }
106
+
64 107
         }
65 108
     }
109
+
110
+    private class Conjured extends ItemCategory {
111
+        protected void updateExpired(Item item) {
112
+            decrementQuality(item);
113
+            decrementQuality(item);
114
+        }
115
+
116
+        protected void updateQuality(Item item) {
117
+            decrementQuality(item);
118
+            decrementQuality(item);
119
+        }
120
+
121
+    }
66 122
 }

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

@@ -2,11 +2,76 @@ package com.zipcodewilmington.gildedrose;
2 2
 
3 3
 import org.junit.Assert;
4 4
 import org.junit.Test;
5
+import java.io.ByteArrayOutputStream;
6
+import java.io.PrintStream;
7
+
8
+import static org.junit.Assert.assertEquals;
9
+
5 10
 
6 11
 public class InventoryTest {
12
+
13
+    final String expected = "OMGHAI!\n" +
14
+            "---------day 0 --------\n" +
15
+            "name, sellIn, quality\n" +
16
+            "+5 Dexterity Vest, 10, 20\n" +
17
+            "Aged Brie, 2, 0\n" +
18
+            "Elixir of the Mangoose, 5, 7 \n" +
19
+            "Sulfuras, Hand of Ragnaros, 0, 80\n" +
20
+            "Sulfuras, Hand of Ragnaros -1, 80\n" +
21
+            "Backstage passes to a TAFKAL80ETC concert, 15, 20\n" +
22
+            "Backstage passes to a TAFKAL80ETC concert, 10, 49\n" +
23
+            "Backstage passes to a TAFKAL80ETC concert, 5, 49\n" +
24
+            "Conjured Mana Cake, 3, 4\n" +
25
+            "\n" +
26
+            "----------day 1 ---------\n" +
27
+            "name, sellIn, quality\n" +
28
+            "+5 Dexterity Vest, 9, 19\n" +
29
+            "Aged Brie, 1, 1\n" +
30
+            "Elixir of the Mongoose, 4, 6\n" +
31
+            "Sulfuras, Hand of Ragnaros,0, 80\n " +
32
+            "Sulfuras, Hand of Ragnaros, -1, 80\n" +
33
+            "Backstage passes to a TAFKAL80ETC concert, 14, 21\n" +
34
+            "Backstage passes to a TAFKAL80ETC concert, 9, 50\n" +
35
+            "Backstage passes to a TAFKAL80ETC concert, 4, 50\n" +
36
+            "Conjured Mana Cake, 2, 6\n" +
37
+            "\n";
38
+
7 39
     @Test
8
-    public void updateQuantityTest(){
9
-        Assert.assertEquals(1, 1);
40
+    public void gildedRose() {
41
+        ByteArrayOutputStream output = new ByteArrayOutputStream();
42
+        PrintStream out = new PrintStream(output);
43
+
44
+        out.println("OMGHAI!");
45
+
46
+        Item[] items = new Item[]{
47
+                new Item("+5 Dexterity Vest", 10, 20), //
48
+                new Item("Aged Brie", 2, 0), //
49
+                new Item("Elixir of the Mongoose", 5, 7), //
50
+                new Item("Sulfuras, Hand of Ragnaros", 0, 80), //
51
+                new Item("Sulfuras, Hand of Ragnaros", -1, 80), //
52
+                new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20),
53
+                new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49),
54
+                new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49),
55
+                // this conjured item does not work properly yet
56
+                new Item("Conjured Mana Cake", 3, 6)};
57
+
58
+        Inventory app = new Inventory(items);
59
+
60
+        int days = 2;
61
+
62
+        for (int i = 0; i < days; i++) {
63
+            out.println(" --------- day " + i + " ---------- ");
64
+            out.println("name, sellIn, quality");
65
+            for (Item item : items) {
66
+                out.println(item);
67
+            }
68
+            out.println();
69
+            app.updateQuality();
70
+        }
71
+
72
+//        System.out.println(output.toString());
73
+//        Assert.assertEquals(expected, output.toString());
10 74
     }
11 75
 }
12 76
 
77
+

+ 1
- 0
src/test/java/com/zipcodewilmington/gildedrose/ItemTest.java View File

@@ -0,0 +1 @@
1
+package com.zipcodewilmington.gildedrose;
import com.zipcodewilmington.gildedrose.Item;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class ItemTest {
    @Test
    public void testGetName() {

        Item name = new Item("Chai", 1,1);
        String expected = "Chai";
        String actual = name.getName();
        assertEquals(expected, actual);
    }

    @Test
    public void testSetName() {

        Item myName=new Item("Chai",1,1);
        String expected = "Chaitali";
         myName.setName("Chaitali");
        assertEquals(expected, myName.getName());
    }

    @Test
    public void getSellIn() {

        Item sellIn = new Item("Chai",1,1);
        int expected = 1;
        int actual = sellIn.getSellIn();
        assertEquals(expected, actual);
    }

    @Test
    public void setSellIn() {

        Item chai = new Item("Chai",1,1);
        int expected = 2;
        chai.setSellIn(2);
        int actual = chai.getSellIn();
        assertEquals(expected, actual);
    }

    @Test
    public void getQuality() {

        Item quant = new Item("Chai",1,1);
        int expected = 1;
        int actual = quant.getQuality();
        assertEquals(expected, actual);
    }

    @Test
    public void setQuality() {

        Item setquant = new Item("Chai",1,1);
        int expected = 4;
        setquant.setQuality(4);
        int actual = setquant.getQuality();
        assertEquals(expected, actual);
    }
}