Parcourir la source

practicing enum w/ template methods

Jared Norris il y a 8 ans
Parent
révision
768cf1b965
1 fichiers modifiés avec 70 ajouts et 48 suppressions
  1. 70
    48
      Inventory.java

+ 70
- 48
Inventory.java Voir le fichier

@@ -1,63 +1,85 @@
1 1
 
2
-public class Inventory {
2
+public class Inventory
3
+{
3 4
     private Item[] items;
4
-
5 5
     public Inventory(Item[] items) {
6 6
         super();
7 7
         this.items = items;
8 8
     }
9 9
 
10
-    public void updateQuality() {//<---------------------------------------------------method to update an item's quality
11
-        for (int i = 0; i < items.length; i++) {//<------------------------------------iterating over the "items" array
12
-            if (!items[i].getName().equals("Aged Brie")
13
-            && !items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {//<-if items[i] ISNT Aged Brie or BSP
14
-                if (items[i].getQuality() > 0) {//<------------------------------------if items[i]'s current quality is greater than 0
15
-                    if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {//<-if items[i] isn't Sulfuras
16
-                        items[i].setQuality(items[i].getQuality() - 1);//<-------------reduce the quality of items[i] by 1
17
-                    }
18
-                }
19
-            } else {//<---------------------------------------------------------------else
20
-                if (items[i].getQuality() < 50) {//<----------------------------------if items[i] quality < 50(max)
21
-                    items[i].setQuality(items[i].getQuality() + 1);//<----------------increase quality of item[i] by 1
22
-
23
-                    if (items[i].getName() == "Backstage passes to a TAFKAL80ETC concert") {//<-if items[i]'s name is BSP
24
-                        if (items[i].getSellIn() < 11) {//<---------------------------if items[i]'s sell by date is less than 11 days from now
25
-                            if (items[i].getQuality() < 50) {//<----------------------if items[i]'s quality is less than 50
26
-                                items[i].setQuality(items[i].getQuality() + 1);//<----increase the quality of items[i] by 1
27
-                            }
28
-                        }
10
+    public void updateQuality() {
11
+        for (int i = 0; i < items.length; i++)
12
+        {
13
+           String itemName = items[i].getName().toUpperCase();
14
+           String[] nameArr = itemName.replaceAll("[^A-Z0-9\\s]", "").split(" ");
15
+           String output = (nameArr[0].equals("5")) ? "FIVE" : nameArr[0];
29 16
 
30
-                        if (items[i].getSellIn() < 6) {
31
-                            if (items[i].getQuality() < 50) {
32
-                                items[i].setQuality(items[i].getQuality() + 1);
33
-                            }
34
-                        }
35
-                    }
17
+           BigSellers.valueOf(output).quality(items[i]);
18
+        }
19
+    }
20
+//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvBIG OL ENUMvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
21
+    public enum BigSellers {
22
+//------------------------------SULFURAS---------------------------------------
23
+        SULFURAS() {
24
+            @Override public void quality(Item x) {
25
+              return;
26
+            }
27
+        },
28
+//----------------------------------------------------------------------------
29
+//----------------------------AGED BRIE---------------------------------------
30
+        AGED() {
31
+            @Override public void quality(Item x) {
32
+              int currentQuality = x.getQuality();
33
+              int sellIn = x.getSellIn();
34
+              int adjustPrice = sellIn > 0 ? 1 : 2;
35
+              //---------------------------------------
36
+              if (currentQuality < 50) {
37
+                x.setQuality(currentQuality + adjustPrice);
38
+              }
39
+              //--------------------------------------
40
+              x.setSellIn(sellIn - 1);
41
+            }
42
+        },
43
+//-----------------------------------------------------------------------------
44
+//-------------------------BACKSTAGE PASSES------------------------------------
45
+        BACKSTAGE() {
46
+            @Override public void quality(Item x) {
47
+              int currentQuality = x.getQuality();
48
+              int sellIn = x.getSellIn();
49
+              int adjustPrice = sellIn <= 5  ? 3 :
50
+                                sellIn <= 10 ? 2 :
51
+                                               1 ;
52
+              //-------------------------------------
53
+              if (sellIn >= 0) {
54
+                if (currentQuality + adjustPrice >= 50) {
55
+                  x.setQuality(50);
36 56
                 }
37
-            }//<-----------------------------------------------------------------------if items[i] ISNT Aged Brie, or BSP
38
-
39
-            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
40
-                items[i].setSellIn(items[i].getSellIn() - 1);
57
+                else x.setQuality(currentQuality + adjustPrice);
58
+              }
59
+              else x.setQuality(0);
60
+              //--------------------------------------
61
+              x.setSellIn(sellIn - 1);
41 62
             }
42
-
43
-            if (items[i].getSellIn() < 0) {
44
-                if (!items[i].getName().equals("Aged Brie")) {
45
-                    if (!items[i].getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
46
-                        if (items[i].getQuality() > 0) {
47
-                            if (!items[i].getName().equals("Sulfuras, Hand of Ragnaros")) {
48
-                                items[i].setQuality(items[i].getQuality() - 1);
49
-                            }
50
-                        }
51
-                    } else {
52
-                        items[i].setQuality(items[i].getQuality()
53
-                            - items[i].getQuality());
54
-                    }
55
-                } else {
56
-                    if (items[i].getQuality() < 50) {
57
-                        items[i].setQuality(items[i].getQuality() + 1);
63
+        },
64
+//-----------------------------------------------------------------------------
65
+//-----------------------------+5 DEX------------------------------------------
66
+        FIVE() {
67
+            @Override public void quality(Item x) {
68
+                int currentQuality = x.getQuality();
69
+                int sellIn = x.getSellIn();
70
+                //------------------------------------------
71
+                if (currentQuality > 0) {
72
+                    if(sellIn >= 0) {
73
+                      x.setQuality(currentQuality - 1);
58 74
                     }
75
+                    else x.setQuality(currentQuality - 2);
59 76
                 }
77
+                //------------------------------------------
78
+                x.setSellIn(sellIn - 1);
60 79
             }
61
-        }
80
+        };
81
+//-----------------------------------------------------------------------------
82
+        public abstract void quality(Item x); //<--------template method
62 83
     }
84
+//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^END OF ENUM^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
63 85
 }