From ffbbf13590ea8dbe263cd45a3b0f5c30134ef8a5 Mon Sep 17 00:00:00 2001 From: jihyeon baek Date: Wed, 2 Oct 2024 15:45:22 +0900 Subject: [PATCH 1/6] =?UTF-8?q?feat:=EC=BD=94=EB=93=9C=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/gildedrose/GildedRose.java | 55 ++----------------- .../java/com/gildedrose/update/AgedBrie.java | 24 ++++++++ .../com/gildedrose/update/BackstagePass.java | 33 +++++++++++ .../java/com/gildedrose/update/Conjured.java | 24 ++++++++ .../java/com/gildedrose/update/Logic.java | 7 +++ .../com/gildedrose/update/LogicFactory.java | 20 +++++++ .../com/gildedrose/update/NormalItem.java | 24 ++++++++ .../java/com/gildedrose/update/Sulfuras.java | 10 ++++ 8 files changed, 148 insertions(+), 49 deletions(-) create mode 100644 Java/src/main/java/com/gildedrose/update/AgedBrie.java create mode 100644 Java/src/main/java/com/gildedrose/update/BackstagePass.java create mode 100644 Java/src/main/java/com/gildedrose/update/Conjured.java create mode 100644 Java/src/main/java/com/gildedrose/update/Logic.java create mode 100644 Java/src/main/java/com/gildedrose/update/LogicFactory.java create mode 100644 Java/src/main/java/com/gildedrose/update/NormalItem.java create mode 100644 Java/src/main/java/com/gildedrose/update/Sulfuras.java diff --git a/Java/src/main/java/com/gildedrose/GildedRose.java b/Java/src/main/java/com/gildedrose/GildedRose.java index 87a3b92..4712e8b 100644 --- a/Java/src/main/java/com/gildedrose/GildedRose.java +++ b/Java/src/main/java/com/gildedrose/GildedRose.java @@ -1,5 +1,8 @@ package com.gildedrose; +import com.gildedrose.update.Logic; +import com.gildedrose.update.LogicFactory; + class GildedRose { Item[] items; @@ -8,55 +11,9 @@ public GildedRose(Item[] items) { } public void updateQuality() { - for (int i = 0; i < items.length; i++) { - if (!items[i].name.equals("Aged Brie") - && !items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) { - if (items[i].quality > 0) { - if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) { - items[i].quality = items[i].quality - 1; - } - } - } else { - if (items[i].quality < 50) { - items[i].quality = items[i].quality + 1; - - if (items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) { - if (items[i].sellIn < 11) { - if (items[i].quality < 50) { - items[i].quality = items[i].quality + 1; - } - } - - if (items[i].sellIn < 6) { - if (items[i].quality < 50) { - items[i].quality = items[i].quality + 1; - } - } - } - } - } - - if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) { - items[i].sellIn = items[i].sellIn - 1; - } - - if (items[i].sellIn < 0) { - if (!items[i].name.equals("Aged Brie")) { - if (!items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) { - if (items[i].quality > 0) { - if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) { - items[i].quality = items[i].quality - 1; - } - } - } else { - items[i].quality = items[i].quality - items[i].quality; - } - } else { - if (items[i].quality < 50) { - items[i].quality = items[i].quality + 1; - } - } - } + for (Item item : items) { + Logic logic = LogicFactory.getLogic(item); + logic.update(item); } } } diff --git a/Java/src/main/java/com/gildedrose/update/AgedBrie.java b/Java/src/main/java/com/gildedrose/update/AgedBrie.java new file mode 100644 index 0000000..68441a9 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/update/AgedBrie.java @@ -0,0 +1,24 @@ +package com.gildedrose.update; + +import com.gildedrose.Item; + +public class AgedBrie implements Logic{ + @Override + public void update(Item item) { + decreaseSellIn(item); + increaseQuality(item); + if (item.sellIn < 0) { + increaseQuality(item); + } + } + + private void decreaseSellIn(Item item) { + item.sellIn--; + } + + private void increaseQuality(Item item) { + if (item.quality < 50) { + item.quality++; + } + } +} diff --git a/Java/src/main/java/com/gildedrose/update/BackstagePass.java b/Java/src/main/java/com/gildedrose/update/BackstagePass.java new file mode 100644 index 0000000..2931ea5 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/update/BackstagePass.java @@ -0,0 +1,33 @@ +package com.gildedrose.update; + +import com.gildedrose.Item; + +public class BackstagePass implements Logic{ + @Override + public void update(Item item) { + decreaseSellIn(item); + increaseQuality(item); + + if (item.sellIn <= 10) { + increaseQuality(item); + } + + if (item.sellIn <= 5) { + increaseQuality(item); + } + + if (item.sellIn < 0) { + item.quality = 0; + } + } + + private void decreaseSellIn(Item item) { + item.sellIn--; + } + + private void increaseQuality(Item item) { + if (item.quality < 50) { + item.quality++; + } + } +} diff --git a/Java/src/main/java/com/gildedrose/update/Conjured.java b/Java/src/main/java/com/gildedrose/update/Conjured.java new file mode 100644 index 0000000..a720be7 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/update/Conjured.java @@ -0,0 +1,24 @@ +package com.gildedrose.update; + +import com.gildedrose.Item; + +public class Conjured implements Logic{ + @Override + public void update(Item item) { + decreaseSellIn(item); + decreaseQualityTwice(item); + if (item.sellIn < 0) { + decreaseQualityTwice(item); + } + } + + private void decreaseSellIn(Item item) { + item.sellIn--; + } + + private void decreaseQualityTwice(Item item) { + if (item.quality > 0) { + item.quality = Math.max(0, item.quality - 2); + } + } +} diff --git a/Java/src/main/java/com/gildedrose/update/Logic.java b/Java/src/main/java/com/gildedrose/update/Logic.java new file mode 100644 index 0000000..74584a3 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/update/Logic.java @@ -0,0 +1,7 @@ +package com.gildedrose.update; + +import com.gildedrose.Item; + +public interface Logic { + void update(Item item); +} diff --git a/Java/src/main/java/com/gildedrose/update/LogicFactory.java b/Java/src/main/java/com/gildedrose/update/LogicFactory.java new file mode 100644 index 0000000..74e8ca6 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/update/LogicFactory.java @@ -0,0 +1,20 @@ +package com.gildedrose.update; + +import com.gildedrose.Item; + +public class LogicFactory { + public static Logic getLogic(Item item) { + switch (item.name) { + case "Aged Brie": + return new AgedBrie(); + case "Backstage passes to a TAFKAL80ETC concert": + return new BackstagePass(); + case "Sulfuras, Hand of Ragnaros": + return new Sulfuras(); + case "Conjured Mana Cake": + return new Conjured(); + default: + return new NormalItem(); + } + } +} diff --git a/Java/src/main/java/com/gildedrose/update/NormalItem.java b/Java/src/main/java/com/gildedrose/update/NormalItem.java new file mode 100644 index 0000000..e3346fa --- /dev/null +++ b/Java/src/main/java/com/gildedrose/update/NormalItem.java @@ -0,0 +1,24 @@ +package com.gildedrose.update; + +import com.gildedrose.Item; + +public class NormalItem implements Logic{ + @Override + public void update(Item item) { + decreaseSellIn(item); + decreaseQuality(item); + if (item.sellIn < 0) { + decreaseQuality(item); + } + } + + private void decreaseSellIn(Item item) { + item.sellIn--; + } + + private void decreaseQuality(Item item) { + if (item.quality > 0) { + item.quality--; + } + } +} diff --git a/Java/src/main/java/com/gildedrose/update/Sulfuras.java b/Java/src/main/java/com/gildedrose/update/Sulfuras.java new file mode 100644 index 0000000..8b370a8 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/update/Sulfuras.java @@ -0,0 +1,10 @@ +package com.gildedrose.update; + +import com.gildedrose.Item; + +public class Sulfuras implements Logic{ + @Override + public void update(Item item) { + + } +} From eb7adb51337db305b2eeb173fcb0bfaa8581f3b3 Mon Sep 17 00:00:00 2001 From: jihyeon baek Date: Thu, 31 Oct 2024 14:29:00 +0900 Subject: [PATCH 2/6] =?UTF-8?q?refactor:=20logicFactory=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0-=EC=95=84=EC=9D=B4=ED=85=9C=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EB=A7=A4=ED=95=91=EC=97=90=20map=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=97=AC=20=EC=9C=A0=EC=A7=80=EB=B3=B4=EC=88=98=EC=84=B1=20?= =?UTF-8?q?=ED=96=A5=EC=83=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/gildedrose/update/LogicFactory.java | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/Java/src/main/java/com/gildedrose/update/LogicFactory.java b/Java/src/main/java/com/gildedrose/update/LogicFactory.java index 74e8ca6..cde3834 100644 --- a/Java/src/main/java/com/gildedrose/update/LogicFactory.java +++ b/Java/src/main/java/com/gildedrose/update/LogicFactory.java @@ -1,20 +1,21 @@ package com.gildedrose.update; +import java.util.HashMap; +import java.util.Map; + import com.gildedrose.Item; public class LogicFactory { + private static final Map logicMap = new HashMap<>(); + + static { + logicMap.put("Aged Brie", new AgedBrie()); + logicMap.put("Backstage passes to a TAFKAL80ETC concert", new BackstagePass()); + logicMap.put("Sulfuras, Hand of Ragnaros", new Sulfuras()); + logicMap.put("Conjured Mana Cake", new Conjured()); + } + public static Logic getLogic(Item item) { - switch (item.name) { - case "Aged Brie": - return new AgedBrie(); - case "Backstage passes to a TAFKAL80ETC concert": - return new BackstagePass(); - case "Sulfuras, Hand of Ragnaros": - return new Sulfuras(); - case "Conjured Mana Cake": - return new Conjured(); - default: - return new NormalItem(); - } + return logicMap.getOrDefault(item.name, new NormalItem()); } } From 55ff50618fcf514b0a25e3c2df97148d005d69d3 Mon Sep 17 00:00:00 2001 From: jihyeon baek Date: Thu, 31 Oct 2024 15:26:44 +0900 Subject: [PATCH 3/6] =?UTF-8?q?refactor:=20=ED=81=B4=EB=9E=98=EC=8A=A4?= =?UTF-8?q?=EB=AA=85,=20=EB=A9=94=EC=86=8C=EB=93=9C=EB=AA=85=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Java/src/main/java/com/gildedrose/GildedRose.java | 8 ++++---- Java/src/main/java/com/gildedrose/update/AgedBrie.java | 2 +- .../main/java/com/gildedrose/update/BackstagePass.java | 2 +- Java/src/main/java/com/gildedrose/update/Conjured.java | 2 +- .../update/{Logic.java => ItemUpdateLogic.java} | 2 +- .../{LogicFactory.java => ItemUpdateLogicFactory.java} | 6 +++--- Java/src/main/java/com/gildedrose/update/NormalItem.java | 2 +- Java/src/main/java/com/gildedrose/update/Sulfuras.java | 2 +- 8 files changed, 13 insertions(+), 13 deletions(-) rename Java/src/main/java/com/gildedrose/update/{Logic.java => ItemUpdateLogic.java} (72%) rename Java/src/main/java/com/gildedrose/update/{LogicFactory.java => ItemUpdateLogicFactory.java} (72%) diff --git a/Java/src/main/java/com/gildedrose/GildedRose.java b/Java/src/main/java/com/gildedrose/GildedRose.java index 4712e8b..3a00569 100644 --- a/Java/src/main/java/com/gildedrose/GildedRose.java +++ b/Java/src/main/java/com/gildedrose/GildedRose.java @@ -1,7 +1,7 @@ package com.gildedrose; -import com.gildedrose.update.Logic; -import com.gildedrose.update.LogicFactory; +import com.gildedrose.update.ItemUpdateLogic; +import com.gildedrose.update.ItemUpdateLogicFactory; class GildedRose { Item[] items; @@ -12,8 +12,8 @@ public GildedRose(Item[] items) { public void updateQuality() { for (Item item : items) { - Logic logic = LogicFactory.getLogic(item); - logic.update(item); + ItemUpdateLogic itemUpdateLogic = ItemUpdateLogicFactory.ItemUpdateLogic(item); + itemUpdateLogic.update(item); } } } diff --git a/Java/src/main/java/com/gildedrose/update/AgedBrie.java b/Java/src/main/java/com/gildedrose/update/AgedBrie.java index 68441a9..0c73db2 100644 --- a/Java/src/main/java/com/gildedrose/update/AgedBrie.java +++ b/Java/src/main/java/com/gildedrose/update/AgedBrie.java @@ -2,7 +2,7 @@ import com.gildedrose.Item; -public class AgedBrie implements Logic{ +public class AgedBrie implements ItemUpdateLogic { @Override public void update(Item item) { decreaseSellIn(item); diff --git a/Java/src/main/java/com/gildedrose/update/BackstagePass.java b/Java/src/main/java/com/gildedrose/update/BackstagePass.java index 2931ea5..04aedb8 100644 --- a/Java/src/main/java/com/gildedrose/update/BackstagePass.java +++ b/Java/src/main/java/com/gildedrose/update/BackstagePass.java @@ -2,7 +2,7 @@ import com.gildedrose.Item; -public class BackstagePass implements Logic{ +public class BackstagePass implements ItemUpdateLogic { @Override public void update(Item item) { decreaseSellIn(item); diff --git a/Java/src/main/java/com/gildedrose/update/Conjured.java b/Java/src/main/java/com/gildedrose/update/Conjured.java index a720be7..405d0e3 100644 --- a/Java/src/main/java/com/gildedrose/update/Conjured.java +++ b/Java/src/main/java/com/gildedrose/update/Conjured.java @@ -2,7 +2,7 @@ import com.gildedrose.Item; -public class Conjured implements Logic{ +public class Conjured implements ItemUpdateLogic { @Override public void update(Item item) { decreaseSellIn(item); diff --git a/Java/src/main/java/com/gildedrose/update/Logic.java b/Java/src/main/java/com/gildedrose/update/ItemUpdateLogic.java similarity index 72% rename from Java/src/main/java/com/gildedrose/update/Logic.java rename to Java/src/main/java/com/gildedrose/update/ItemUpdateLogic.java index 74584a3..27c08d8 100644 --- a/Java/src/main/java/com/gildedrose/update/Logic.java +++ b/Java/src/main/java/com/gildedrose/update/ItemUpdateLogic.java @@ -2,6 +2,6 @@ import com.gildedrose.Item; -public interface Logic { +public interface ItemUpdateLogic { void update(Item item); } diff --git a/Java/src/main/java/com/gildedrose/update/LogicFactory.java b/Java/src/main/java/com/gildedrose/update/ItemUpdateLogicFactory.java similarity index 72% rename from Java/src/main/java/com/gildedrose/update/LogicFactory.java rename to Java/src/main/java/com/gildedrose/update/ItemUpdateLogicFactory.java index cde3834..f3793eb 100644 --- a/Java/src/main/java/com/gildedrose/update/LogicFactory.java +++ b/Java/src/main/java/com/gildedrose/update/ItemUpdateLogicFactory.java @@ -5,8 +5,8 @@ import com.gildedrose.Item; -public class LogicFactory { - private static final Map logicMap = new HashMap<>(); +public class ItemUpdateLogicFactory { + private static final Map logicMap = new HashMap<>(); static { logicMap.put("Aged Brie", new AgedBrie()); @@ -15,7 +15,7 @@ public class LogicFactory { logicMap.put("Conjured Mana Cake", new Conjured()); } - public static Logic getLogic(Item item) { + public static ItemUpdateLogic ItemUpdateLogic(Item item) { return logicMap.getOrDefault(item.name, new NormalItem()); } } diff --git a/Java/src/main/java/com/gildedrose/update/NormalItem.java b/Java/src/main/java/com/gildedrose/update/NormalItem.java index e3346fa..7f16599 100644 --- a/Java/src/main/java/com/gildedrose/update/NormalItem.java +++ b/Java/src/main/java/com/gildedrose/update/NormalItem.java @@ -2,7 +2,7 @@ import com.gildedrose.Item; -public class NormalItem implements Logic{ +public class NormalItem implements ItemUpdateLogic { @Override public void update(Item item) { decreaseSellIn(item); diff --git a/Java/src/main/java/com/gildedrose/update/Sulfuras.java b/Java/src/main/java/com/gildedrose/update/Sulfuras.java index 8b370a8..4ae0931 100644 --- a/Java/src/main/java/com/gildedrose/update/Sulfuras.java +++ b/Java/src/main/java/com/gildedrose/update/Sulfuras.java @@ -2,7 +2,7 @@ import com.gildedrose.Item; -public class Sulfuras implements Logic{ +public class Sulfuras implements ItemUpdateLogic { @Override public void update(Item item) { From 6a6c660089d68ddf7cb0b05c0b7f6a327a82ff0e Mon Sep 17 00:00:00 2001 From: jihyeon baek Date: Thu, 31 Oct 2024 16:00:20 +0900 Subject: [PATCH 4/6] =?UTF-8?q?refactor:=20=EC=9D=B8=ED=84=B0=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=8A=A4=EB=A5=BC=20=EC=B6=94=EC=83=81=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/gildedrose/GildedRose.java | 2 +- .../java/com/gildedrose/update/AgedBrie.java | 9 +++++++-- .../com/gildedrose/update/BackstagePass.java | 9 +++++++-- .../java/com/gildedrose/update/Conjured.java | 9 +++++++-- .../com/gildedrose/update/ItemUpdateLogic.java | 10 ++++++++-- .../update/ItemUpdateLogicFactory.java | 18 ++++++++++++------ .../java/com/gildedrose/update/NormalItem.java | 9 +++++++-- .../java/com/gildedrose/update/Sulfuras.java | 8 ++++++-- 8 files changed, 55 insertions(+), 19 deletions(-) diff --git a/Java/src/main/java/com/gildedrose/GildedRose.java b/Java/src/main/java/com/gildedrose/GildedRose.java index 3a00569..555b1e1 100644 --- a/Java/src/main/java/com/gildedrose/GildedRose.java +++ b/Java/src/main/java/com/gildedrose/GildedRose.java @@ -13,7 +13,7 @@ public GildedRose(Item[] items) { public void updateQuality() { for (Item item : items) { ItemUpdateLogic itemUpdateLogic = ItemUpdateLogicFactory.ItemUpdateLogic(item); - itemUpdateLogic.update(item); + itemUpdateLogic.update(); } } } diff --git a/Java/src/main/java/com/gildedrose/update/AgedBrie.java b/Java/src/main/java/com/gildedrose/update/AgedBrie.java index 0c73db2..3bbf0de 100644 --- a/Java/src/main/java/com/gildedrose/update/AgedBrie.java +++ b/Java/src/main/java/com/gildedrose/update/AgedBrie.java @@ -2,9 +2,14 @@ import com.gildedrose.Item; -public class AgedBrie implements ItemUpdateLogic { +public class AgedBrie extends ItemUpdateLogic { + + public AgedBrie(Item item) { + super(item); + } + @Override - public void update(Item item) { + public void update() { decreaseSellIn(item); increaseQuality(item); if (item.sellIn < 0) { diff --git a/Java/src/main/java/com/gildedrose/update/BackstagePass.java b/Java/src/main/java/com/gildedrose/update/BackstagePass.java index 04aedb8..71ce80e 100644 --- a/Java/src/main/java/com/gildedrose/update/BackstagePass.java +++ b/Java/src/main/java/com/gildedrose/update/BackstagePass.java @@ -2,9 +2,14 @@ import com.gildedrose.Item; -public class BackstagePass implements ItemUpdateLogic { +public class BackstagePass extends ItemUpdateLogic { + + public BackstagePass(Item item) { + super(item); + } + @Override - public void update(Item item) { + public void update() { decreaseSellIn(item); increaseQuality(item); diff --git a/Java/src/main/java/com/gildedrose/update/Conjured.java b/Java/src/main/java/com/gildedrose/update/Conjured.java index 405d0e3..818364a 100644 --- a/Java/src/main/java/com/gildedrose/update/Conjured.java +++ b/Java/src/main/java/com/gildedrose/update/Conjured.java @@ -2,9 +2,14 @@ import com.gildedrose.Item; -public class Conjured implements ItemUpdateLogic { +public class Conjured extends ItemUpdateLogic { + + public Conjured(Item item) { + super(item); + } + @Override - public void update(Item item) { + public void update() { decreaseSellIn(item); decreaseQualityTwice(item); if (item.sellIn < 0) { diff --git a/Java/src/main/java/com/gildedrose/update/ItemUpdateLogic.java b/Java/src/main/java/com/gildedrose/update/ItemUpdateLogic.java index 27c08d8..83635a4 100644 --- a/Java/src/main/java/com/gildedrose/update/ItemUpdateLogic.java +++ b/Java/src/main/java/com/gildedrose/update/ItemUpdateLogic.java @@ -2,6 +2,12 @@ import com.gildedrose.Item; -public interface ItemUpdateLogic { - void update(Item item); +public abstract class ItemUpdateLogic { + protected Item item; + + public ItemUpdateLogic(Item item) { + this.item = item; + } + + public abstract void update(); } diff --git a/Java/src/main/java/com/gildedrose/update/ItemUpdateLogicFactory.java b/Java/src/main/java/com/gildedrose/update/ItemUpdateLogicFactory.java index f3793eb..29e7c3d 100644 --- a/Java/src/main/java/com/gildedrose/update/ItemUpdateLogicFactory.java +++ b/Java/src/main/java/com/gildedrose/update/ItemUpdateLogicFactory.java @@ -6,16 +6,22 @@ import com.gildedrose.Item; public class ItemUpdateLogicFactory { - private static final Map logicMap = new HashMap<>(); + private static final Map> logicMap = new HashMap<>(); static { - logicMap.put("Aged Brie", new AgedBrie()); - logicMap.put("Backstage passes to a TAFKAL80ETC concert", new BackstagePass()); - logicMap.put("Sulfuras, Hand of Ragnaros", new Sulfuras()); - logicMap.put("Conjured Mana Cake", new Conjured()); + logicMap.put("Aged Brie", AgedBrie.class); + logicMap.put("Backstage passes to a TAFKAL80ETC concert", BackstagePass.class); + logicMap.put("Sulfuras, Hand of Ragnaros", Sulfuras.class); + logicMap.put("Conjured Mana Cake", Conjured.class); } public static ItemUpdateLogic ItemUpdateLogic(Item item) { - return logicMap.getOrDefault(item.name, new NormalItem()); + Class logicClass = logicMap.getOrDefault(item.name, NormalItem.class); + try { + // 생성자에 Item을 매개변수로 전달하여 인스턴스를 생성 + return logicClass.getDeclaredConstructor(Item.class).newInstance(item); + } catch (Exception e) { + throw new RuntimeException("Failed to create ItemUpdateLogic for item: " + item.name, e); + } } } diff --git a/Java/src/main/java/com/gildedrose/update/NormalItem.java b/Java/src/main/java/com/gildedrose/update/NormalItem.java index 7f16599..8be98ff 100644 --- a/Java/src/main/java/com/gildedrose/update/NormalItem.java +++ b/Java/src/main/java/com/gildedrose/update/NormalItem.java @@ -2,9 +2,14 @@ import com.gildedrose.Item; -public class NormalItem implements ItemUpdateLogic { +public class NormalItem extends ItemUpdateLogic { + + public NormalItem(Item item) { + super(item); + } + @Override - public void update(Item item) { + public void update() { decreaseSellIn(item); decreaseQuality(item); if (item.sellIn < 0) { diff --git a/Java/src/main/java/com/gildedrose/update/Sulfuras.java b/Java/src/main/java/com/gildedrose/update/Sulfuras.java index 4ae0931..4f7dded 100644 --- a/Java/src/main/java/com/gildedrose/update/Sulfuras.java +++ b/Java/src/main/java/com/gildedrose/update/Sulfuras.java @@ -2,9 +2,13 @@ import com.gildedrose.Item; -public class Sulfuras implements ItemUpdateLogic { +public class Sulfuras extends ItemUpdateLogic { + public Sulfuras(Item item) { + super(item); + } + @Override - public void update(Item item) { + public void update() { } } From ac18da2d81bd9057341873a14d5046a20867f478 Mon Sep 17 00:00:00 2001 From: jihyeon baek Date: Thu, 31 Oct 2024 18:01:31 +0900 Subject: [PATCH 5/6] =?UTF-8?q?refactor:=20=EB=B0=98=EB=B3=B5=20=EB=A9=94?= =?UTF-8?q?=EC=86=8C=EB=93=9C=20=ED=86=B5=ED=95=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/gildedrose/update/AgedBrie.java | 16 +++-------- .../com/gildedrose/update/BackstagePass.java | 28 +++++++------------ .../java/com/gildedrose/update/Conjured.java | 17 +++-------- .../gildedrose/update/ItemUpdateLogic.java | 17 +++++++++++ .../com/gildedrose/update/NormalItem.java | 16 +++-------- .../java/com/gildedrose/update/Sulfuras.java | 1 + 6 files changed, 40 insertions(+), 55 deletions(-) diff --git a/Java/src/main/java/com/gildedrose/update/AgedBrie.java b/Java/src/main/java/com/gildedrose/update/AgedBrie.java index 3bbf0de..cf25d3b 100644 --- a/Java/src/main/java/com/gildedrose/update/AgedBrie.java +++ b/Java/src/main/java/com/gildedrose/update/AgedBrie.java @@ -10,20 +10,12 @@ public AgedBrie(Item item) { @Override public void update() { - decreaseSellIn(item); - increaseQuality(item); + decreaseSellIn(); + increaseQuality(1); + if (item.sellIn < 0) { - increaseQuality(item); + increaseQuality(1); } } - private void decreaseSellIn(Item item) { - item.sellIn--; - } - - private void increaseQuality(Item item) { - if (item.quality < 50) { - item.quality++; - } - } } diff --git a/Java/src/main/java/com/gildedrose/update/BackstagePass.java b/Java/src/main/java/com/gildedrose/update/BackstagePass.java index 71ce80e..7162154 100644 --- a/Java/src/main/java/com/gildedrose/update/BackstagePass.java +++ b/Java/src/main/java/com/gildedrose/update/BackstagePass.java @@ -4,35 +4,27 @@ public class BackstagePass extends ItemUpdateLogic { + private static final int BACKSTAGE_THRESHOLD_10_DAYS = 10; + private static final int BACKSTAGE_THRESHOLD_5_DAYS = 5; + public BackstagePass(Item item) { super(item); } @Override public void update() { - decreaseSellIn(item); - increaseQuality(item); + decreaseSellIn(); + increaseQuality(1); - if (item.sellIn <= 10) { - increaseQuality(item); + if (item.sellIn < BACKSTAGE_THRESHOLD_10_DAYS) { + increaseQuality(1); } - - if (item.sellIn <= 5) { - increaseQuality(item); + if (item.sellIn < BACKSTAGE_THRESHOLD_5_DAYS) { + increaseQuality(1); } if (item.sellIn < 0) { - item.quality = 0; - } - } - - private void decreaseSellIn(Item item) { - item.sellIn--; - } - - private void increaseQuality(Item item) { - if (item.quality < 50) { - item.quality++; + item.quality = RESET_QUALITY; } } } diff --git a/Java/src/main/java/com/gildedrose/update/Conjured.java b/Java/src/main/java/com/gildedrose/update/Conjured.java index 818364a..bfaca92 100644 --- a/Java/src/main/java/com/gildedrose/update/Conjured.java +++ b/Java/src/main/java/com/gildedrose/update/Conjured.java @@ -10,20 +10,11 @@ public Conjured(Item item) { @Override public void update() { - decreaseSellIn(item); - decreaseQualityTwice(item); - if (item.sellIn < 0) { - decreaseQualityTwice(item); - } - } + decreaseSellIn(); + decreaseQuality(2); - private void decreaseSellIn(Item item) { - item.sellIn--; - } - - private void decreaseQualityTwice(Item item) { - if (item.quality > 0) { - item.quality = Math.max(0, item.quality - 2); + if (item.sellIn < 0) { + decreaseQuality(2); } } } diff --git a/Java/src/main/java/com/gildedrose/update/ItemUpdateLogic.java b/Java/src/main/java/com/gildedrose/update/ItemUpdateLogic.java index 83635a4..94372a9 100644 --- a/Java/src/main/java/com/gildedrose/update/ItemUpdateLogic.java +++ b/Java/src/main/java/com/gildedrose/update/ItemUpdateLogic.java @@ -3,6 +3,11 @@ import com.gildedrose.Item; public abstract class ItemUpdateLogic { + protected static final int MAX_QUALITY = 50; + protected static final int MIN_QUALITY = 0; + protected static final int RESET_QUALITY = 0; + protected static final int SELL_IN_DECREASE_RATE = 1; + protected Item item; public ItemUpdateLogic(Item item) { @@ -10,4 +15,16 @@ public ItemUpdateLogic(Item item) { } public abstract void update(); + + protected void decreaseSellIn() { + item.sellIn -= SELL_IN_DECREASE_RATE; + } + + protected void increaseQuality(int amount) { + item.quality = Math.min(MAX_QUALITY, item.quality + amount); + } + + protected void decreaseQuality(int amount) { + item.quality = Math.max(MIN_QUALITY, item.quality - amount); + } } diff --git a/Java/src/main/java/com/gildedrose/update/NormalItem.java b/Java/src/main/java/com/gildedrose/update/NormalItem.java index 8be98ff..c984191 100644 --- a/Java/src/main/java/com/gildedrose/update/NormalItem.java +++ b/Java/src/main/java/com/gildedrose/update/NormalItem.java @@ -10,20 +10,12 @@ public NormalItem(Item item) { @Override public void update() { - decreaseSellIn(item); - decreaseQuality(item); + decreaseSellIn(); + decreaseQuality(1); + if (item.sellIn < 0) { - decreaseQuality(item); + decreaseQuality(1); } } - private void decreaseSellIn(Item item) { - item.sellIn--; - } - - private void decreaseQuality(Item item) { - if (item.quality > 0) { - item.quality--; - } - } } diff --git a/Java/src/main/java/com/gildedrose/update/Sulfuras.java b/Java/src/main/java/com/gildedrose/update/Sulfuras.java index 4f7dded..e73c22c 100644 --- a/Java/src/main/java/com/gildedrose/update/Sulfuras.java +++ b/Java/src/main/java/com/gildedrose/update/Sulfuras.java @@ -3,6 +3,7 @@ import com.gildedrose.Item; public class Sulfuras extends ItemUpdateLogic { + public Sulfuras(Item item) { super(item); } From 9cff0c832f3edd7c4a3a838b9201173ddfa5aa86 Mon Sep 17 00:00:00 2001 From: jihyeon baek Date: Thu, 31 Oct 2024 18:04:40 +0900 Subject: [PATCH 6/6] =?UTF-8?q?refactor:=20=EC=A3=BC=EC=84=9D=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Java/src/main/java/com/gildedrose/update/AgedBrie.java | 1 + Java/src/main/java/com/gildedrose/update/BackstagePass.java | 2 ++ Java/src/main/java/com/gildedrose/update/Conjured.java | 3 ++- Java/src/main/java/com/gildedrose/update/NormalItem.java | 1 + Java/src/main/java/com/gildedrose/update/Sulfuras.java | 2 +- 5 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Java/src/main/java/com/gildedrose/update/AgedBrie.java b/Java/src/main/java/com/gildedrose/update/AgedBrie.java index cf25d3b..0d0fafb 100644 --- a/Java/src/main/java/com/gildedrose/update/AgedBrie.java +++ b/Java/src/main/java/com/gildedrose/update/AgedBrie.java @@ -13,6 +13,7 @@ public void update() { decreaseSellIn(); increaseQuality(1); + // 판매일이 지났을 경우 추가 품질 증가 if (item.sellIn < 0) { increaseQuality(1); } diff --git a/Java/src/main/java/com/gildedrose/update/BackstagePass.java b/Java/src/main/java/com/gildedrose/update/BackstagePass.java index 7162154..4b35de3 100644 --- a/Java/src/main/java/com/gildedrose/update/BackstagePass.java +++ b/Java/src/main/java/com/gildedrose/update/BackstagePass.java @@ -16,6 +16,7 @@ public void update() { decreaseSellIn(); increaseQuality(1); + // 판매일에 따라 추가 품질 증가 if (item.sellIn < BACKSTAGE_THRESHOLD_10_DAYS) { increaseQuality(1); } @@ -23,6 +24,7 @@ public void update() { increaseQuality(1); } + // 판매일이 지나면 품질 초기화 if (item.sellIn < 0) { item.quality = RESET_QUALITY; } diff --git a/Java/src/main/java/com/gildedrose/update/Conjured.java b/Java/src/main/java/com/gildedrose/update/Conjured.java index bfaca92..31a4de0 100644 --- a/Java/src/main/java/com/gildedrose/update/Conjured.java +++ b/Java/src/main/java/com/gildedrose/update/Conjured.java @@ -11,8 +11,9 @@ public Conjured(Item item) { @Override public void update() { decreaseSellIn(); - decreaseQuality(2); + decreaseQuality(2); // 품질이 일반 아이템보다 2배 빨리 감소 + // 판매일이 지나면 추가 감소 if (item.sellIn < 0) { decreaseQuality(2); } diff --git a/Java/src/main/java/com/gildedrose/update/NormalItem.java b/Java/src/main/java/com/gildedrose/update/NormalItem.java index c984191..6c7e74b 100644 --- a/Java/src/main/java/com/gildedrose/update/NormalItem.java +++ b/Java/src/main/java/com/gildedrose/update/NormalItem.java @@ -13,6 +13,7 @@ public void update() { decreaseSellIn(); decreaseQuality(1); + // 판매일이 지나면 추가 감소 if (item.sellIn < 0) { decreaseQuality(1); } diff --git a/Java/src/main/java/com/gildedrose/update/Sulfuras.java b/Java/src/main/java/com/gildedrose/update/Sulfuras.java index e73c22c..50f8ca9 100644 --- a/Java/src/main/java/com/gildedrose/update/Sulfuras.java +++ b/Java/src/main/java/com/gildedrose/update/Sulfuras.java @@ -10,6 +10,6 @@ public Sulfuras(Item item) { @Override public void update() { - + // Sulfuras는 품질과 판매일이 변하지 않음 } }