Browse Source

refactored codes

ThuyKhong 6 years ago
parent
commit
8711a5e6fb
1 changed files with 49 additions and 41 deletions
  1. 49
    41
      src/main/java/com/zipcodewilmington/gildedrose/Inventory.java

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

@@ -2,7 +2,12 @@ package com.zipcodewilmington.gildedrose;
2 2
 
3 3
 
4 4
 public class Inventory {
5
+    private static final int MAX_QUALITY = 50;
6
+    private static final int MIN_QUALITY = 0;
5 7
     private Item[] items;
8
+    protected static final String AGED_BRIE = "Aged Brie";
9
+    protected static final String BACKSTAGE_PASS = "Backstage passes to a TAFKAL80ETC concert";
10
+    protected static final String SULFURAS = "Sulfuras, Hand of Ragnaros";
6 11
 
7 12
     public Inventory(Item[] items) {
8 13
         super();
@@ -12,55 +17,58 @@ public class Inventory {
12 17
 
13 18
     public void updateQuality() {
14 19
         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);
20
+            Item item = items[i];
21
+            String itemName = item.getName();
25 22
 
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
-                        }
23
+            if (!SULFURAS.equals(itemName)) {
24
+                item.setSellIn(item.getSellIn() - 1);
25
+            }
32 26
 
33
-                        if (items[i].getSellIn() < 6) {
34
-                            if (items[i].getQuality() < 50) {
35
-                                items[i].setQuality(items[i].getQuality() + 1);
36
-                            }
37
-                        }
38
-                    }
39
-                }
27
+            if (AGED_BRIE.equals(itemName)) {
28
+                updateQuality(item, 1, 2);
29
+            } else if (BACKSTAGE_PASS.equals(itemName)){
30
+                updateBackstagePass(item, itemName);
31
+            } else if (SULFURAS.equals(itemName)){
32
+                // do nothing
33
+            } else {
34
+                updateQuality(item, -1, -2);
40 35
             }
36
+        }
37
+    }
41 38
 
42
-            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
43
-                items[i].setSellIn(items[i].getSellIn() - 1);
39
+    private void updateBackstagePass(Item item, String itemName) {
40
+        if (item.getSellIn() < 0) {
41
+            item.setQuality(0);
42
+        } else if (item.getQuality() < 50) {
43
+            item.setQuality(item.getQuality() + 1);
44
+            if (item.getSellIn() < 11) {
45
+                if (item.getQuality() < 50) {
46
+                    item.setQuality(item.getQuality() + 1);
47
+                }
44 48
             }
45 49
 
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
-                    }
50
+            if (item.getSellIn() < 6) {
51
+                if (item.getQuality() < 50) {
52
+                    item.setQuality(item.getQuality() + 1);
62 53
                 }
63 54
             }
64 55
         }
56
+
57
+    }
58
+
59
+    private void updateQuality(Item item, int quality, int passedSellInDateQuality) {
60
+        int itemQuality = item.getQuality();
61
+        if (item.getSellIn() < 0) {
62
+            item.setQuality(itemQuality + passedSellInDateQuality);
63
+        } else {
64
+            item.setQuality(itemQuality + quality);
65
+        }
66
+
67
+        if (item.getQuality() > MAX_QUALITY) {
68
+            item.setQuality(MAX_QUALITY);
69
+        } else if (item.getQuality() < MIN_QUALITY) {
70
+            item.setQuality(MIN_QUALITY);
71
+        }
72
+
65 73
     }
66 74
 }