Quellcode durchsuchen

Finished Part 2: Refactor the code

NedRedmond vor 6 Jahren
Ursprung
Commit
94b2b9495f

+ 94
- 45
src/main/java/com/zipcodewilmington/gildedrose/Inventory.java Datei anzeigen

@@ -9,58 +9,107 @@ public class Inventory {
9 9
         this.items = items;
10 10
     }
11 11
 
12
+    private void updateSellIn(Item item) {
13
+        item.setSellIn(item.getSellIn() - 1);
14
+    }
12 15
 
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);
16
+    private void changeQuality(Item item, int change) {
17
+        item.setQuality(item.getQuality() + change);
18
+    }
25 19
 
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
-                            }
20
+    public void updateQuality() {
21
+        for (Item item : items) {
22
+            if (item.getQuality() < 50) {
23
+                switch (item.getName()) {
24
+                    case "Aged Brie":
25
+                        changeQuality(item, 1);
26
+                        break;
27
+                    case "Backstage passes to a TAFKAL80ETC concert":
28
+                        if (item.getSellIn() < 6) {
29
+                            changeQuality(item, 3);
30
+                        } else if (item.getSellIn() < 11) {
31
+                            changeQuality(item, 2);
32
+                        } else {
33
+                            changeQuality(item, 1);
31 34
                         }
32
-
33
-                        if (items[i].getSellIn() < 6) {
34
-                            if (items[i].getQuality() < 50) {
35
-                                items[i].setQuality(items[i].getQuality() + 1);
36
-                            }
35
+                        break;
36
+                    case "Sulfuras, Hand of Ragnaros":
37
+                        continue;
38
+                    default:
39
+                        if (item.getSellIn() < 1) {
40
+                            changeQuality(item, -2);
41
+                        } else {
42
+                            changeQuality(item, -1);
37 43
                         }
38
-                    }
44
+                        break;
39 45
                 }
40 46
             }
41
-
42
-            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
43
-                items[i].setSellIn(items[i].getSellIn() - 1);
44
-            }
45
-
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
-                }
47
+            if (item.getQuality() < 0) {
48
+                item.setQuality(0);
63 49
             }
50
+//                    default:
51
+//                        if (item.getQuality() > 0)
52
+//                            if (item.getSellIn() < 0 && item.getQuality() > 1))
53
+//                                changeQuality(item, -1);
54
+//                            if (item.getSellIn() < 0 && item.getQuality() > 1)
55
+//                                changeQuality(item, -2);
56
+//                        break;
57
+//                }
58
+//            }
59
+            updateSellIn(item);
64 60
         }
61
+
62
+
63
+//        for (int i = 0; i < items.length; i++) {
64
+//            if (!items[i].getName().equals("Aged Brie") && !items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
65
+//                if (items[i].getQuality() > 0) {
66
+//                    if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
67
+//                        items[i].setQuality(items[i].getQuality() - 1);
68
+//                    }
69
+//                }
70
+//            } else {
71
+//                if (items[i].getQuality() < 50 && !items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
72
+//                    items[i].setQuality(items[i].getQuality() + 1);
73
+//
74
+////                    if (items[i].getName() == "Backstage passes to a TAFKAL80ETC concert") {
75
+////                        if (items[i].getSellIn() < 11) {
76
+////                            if (items[i].getQuality() < 50) {
77
+////                                items[i].setQuality(items[i].getQuality() + 1);
78
+////                            }
79
+////                        }
80
+////
81
+////                        if (items[i].getSellIn() < 6) {
82
+////                            if (items[i].getQuality() < 50) {
83
+////                                items[i].setQuality(items[i].getQuality() + 1);
84
+////                            }
85
+////                        }
86
+////                    }
87
+//                }
88
+//            }
89
+//
90
+//            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros") && !items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
91
+//                items[i].setSellIn(items[i].getSellIn() - 1);
92
+//            }
93
+//
94
+//            if (items[i].getSellIn() < 0 && !items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
95
+//                if (!items[i].getName().equals("Aged Brie")) {
96
+//                    if (!items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
97
+//                        if (items[i].getQuality() > 0) {
98
+//                            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
99
+//                                items[i].setQuality(items[i].getQuality() - 1);
100
+//                            }
101
+//                        }
102
+//                    } else {
103
+//                        items[i].setQuality(items[i].getQuality()
104
+//                                - items[i].getQuality());
105
+//                    }
106
+//                }
107
+//////                else {
108
+////////                    if (items[i].getQuality() < 50) {
109
+////////                        items[i].setQuality(items[i].getQuality() + 1);
110
+//////                    }
111
+////                }
112
+//            }
113
+//        }
65 114
     }
66 115
 }

+ 13
- 5
src/test/java/com/zipcodewilmington/gildedrose/InventoryTest.java Datei anzeigen

@@ -20,8 +20,8 @@ public class InventoryTest {
20 20
         inventory.updateQuality();
21 21
         int after = items[0].getQuality();
22 22
 
23
-        System.out.println(before);
24
-        System.out.println(after);
23
+//        System.out.println(before);
24
+//        System.out.println(after);
25 25
 
26 26
         Assert.assertTrue(before > after);
27 27
     }
@@ -37,7 +37,7 @@ public class InventoryTest {
37 37
         }
38 38
         int after = items[0].getQuality();
39 39
 
40
-        System.out.println(after);
40
+//        System.out.println(after);
41 41
 
42 42
         Assert.assertTrue(after >= 0);
43 43
     }
@@ -51,8 +51,8 @@ public class InventoryTest {
51 51
         inventory.updateQuality();
52 52
         int after = items[1].getQuality();
53 53
 
54
-        System.out.println(before);
55
-        System.out.println(after);
54
+//        System.out.println(before);
55
+//        System.out.println(after);
56 56
 
57 57
         Assert.assertTrue(before < after);
58 58
     }
@@ -64,11 +64,13 @@ public class InventoryTest {
64 64
         // test that quality degrades 2x fast after sellIn expires
65 65
         for (int i = 0; i < 8; i++) {
66 66
             inventory.updateQuality();
67
+            System.out.println(items[0].getQuality());
67 68
         }
68 69
 
69 70
         int expectedValue = 25 - 7 - 2;
70 71
         int actualValue = items[0].getQuality();
71 72
 
73
+
72 74
         Assert.assertTrue(expectedValue == actualValue);
73 75
     }
74 76
 
@@ -116,6 +118,9 @@ public class InventoryTest {
116 118
         int expectedValue = 10 + 1*4 + 2;
117 119
         int actualValue = items[3].getQuality();
118 120
 
121
+        System.out.println(actualValue);
122
+        System.out.println(expectedValue);
123
+
119 124
         Assert.assertTrue(actualValue == expectedValue);
120 125
     }
121 126
 
@@ -131,6 +136,9 @@ public class InventoryTest {
131 136
         int expectedValue = 10 + 1*4 + 2*5 + 3;
132 137
         int actualValue = items[3].getQuality();
133 138
 
139
+        System.out.println(actualValue);
140
+        System.out.println(expectedValue);
141
+
134 142
         Assert.assertTrue(actualValue == expectedValue);
135 143
     }
136 144