From a8e3ebd565c403354f4e53b6281cc8055502b322 Mon Sep 17 00:00:00 2001 From: 0-x-14 <0x14@daum.net> Date: Mon, 30 Sep 2024 15:59:40 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[Feat]=20=EB=A1=9C=EC=A7=81=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 --- .../main/java/com/gildedrose/GildedRose.java | 57 +++---------------- .../Update/UpdateAgedBrieLogic.java | 22 +++++++ .../Update/UpdateBackstagePassesLogic.java | 26 +++++++++ .../Update/UpdateDefaultItemLogic.java | 19 +++++++ .../com/gildedrose/Update/UpdateLogic.java | 7 +++ .../gildedrose/Update/UpdateLogicFactory.java | 18 ++++++ .../Update/UpdateSulfurasLogic.java | 11 ++++ .../java/com/gildedrose/TexttestFixture.java | 20 +++---- 8 files changed, 121 insertions(+), 59 deletions(-) create mode 100644 Java/src/main/java/com/gildedrose/Update/UpdateAgedBrieLogic.java create mode 100644 Java/src/main/java/com/gildedrose/Update/UpdateBackstagePassesLogic.java create mode 100644 Java/src/main/java/com/gildedrose/Update/UpdateDefaultItemLogic.java create mode 100644 Java/src/main/java/com/gildedrose/Update/UpdateLogic.java create mode 100644 Java/src/main/java/com/gildedrose/Update/UpdateLogicFactory.java create mode 100644 Java/src/main/java/com/gildedrose/Update/UpdateSulfurasLogic.java diff --git a/Java/src/main/java/com/gildedrose/GildedRose.java b/Java/src/main/java/com/gildedrose/GildedRose.java index 87a3b92..5c5cd90 100644 --- a/Java/src/main/java/com/gildedrose/GildedRose.java +++ b/Java/src/main/java/com/gildedrose/GildedRose.java @@ -1,62 +1,21 @@ package com.gildedrose; +import com.gildedrose.Update.UpdateLogic; +import com.gildedrose.Update.UpdateLogicFactory; + class GildedRose { Item[] items; + UpdateLogicFactory updateLogicFactory; public GildedRose(Item[] items) { this.items = items; + updateLogicFactory = new UpdateLogicFactory(); } 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) { + UpdateLogic updateLogic = updateLogicFactory.getUpdateLogic(item); + updateLogic.update(item); } } } diff --git a/Java/src/main/java/com/gildedrose/Update/UpdateAgedBrieLogic.java b/Java/src/main/java/com/gildedrose/Update/UpdateAgedBrieLogic.java new file mode 100644 index 0000000..20f6fbe --- /dev/null +++ b/Java/src/main/java/com/gildedrose/Update/UpdateAgedBrieLogic.java @@ -0,0 +1,22 @@ +package com.gildedrose.Update; + +import com.gildedrose.Item; + +public class UpdateAgedBrieLogic implements UpdateLogic { + + @Override + public void update(Item item) { + + if (item.quality < 50) { + item.quality++; + } + + // 기존 시스템도 quality값 50 미만인지 검사 후 quality++ -> sellIn-- -> 판매 기한 지났는지 검사 후 quality++이므로 해당 구조 유지 + item.sellIn--; + + if (item.sellIn < 0 && item.quality < 50) { + item.quality++; + } + + } +} diff --git a/Java/src/main/java/com/gildedrose/Update/UpdateBackstagePassesLogic.java b/Java/src/main/java/com/gildedrose/Update/UpdateBackstagePassesLogic.java new file mode 100644 index 0000000..5e64c9f --- /dev/null +++ b/Java/src/main/java/com/gildedrose/Update/UpdateBackstagePassesLogic.java @@ -0,0 +1,26 @@ +package com.gildedrose.Update; + +import com.gildedrose.Item; + +public class UpdateBackstagePassesLogic implements UpdateLogic { + + @Override + public void update(Item item) { + + if (item.quality < 50) { + item.quality++; + } + if (item.sellIn <= 10 && item.quality < 50) { + item.quality++; + } + if (item.sellIn <= 5 && item.quality < 50) { + item.quality++; + } + + item.sellIn--; + + if (item.sellIn < 0) { + item.quality = 0; + } + } +} diff --git a/Java/src/main/java/com/gildedrose/Update/UpdateDefaultItemLogic.java b/Java/src/main/java/com/gildedrose/Update/UpdateDefaultItemLogic.java new file mode 100644 index 0000000..b0af0b5 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/Update/UpdateDefaultItemLogic.java @@ -0,0 +1,19 @@ +package com.gildedrose.Update; + +import com.gildedrose.Item; + +public class UpdateDefaultItemLogic implements UpdateLogic { + + @Override + public void update(Item item) { + if (item.quality > 0) { + item.quality--; + } + + item.sellIn--; + + if (item.sellIn < 0 && item.quality > 0) { + item.quality--; + } + } +} diff --git a/Java/src/main/java/com/gildedrose/Update/UpdateLogic.java b/Java/src/main/java/com/gildedrose/Update/UpdateLogic.java new file mode 100644 index 0000000..a1af9bd --- /dev/null +++ b/Java/src/main/java/com/gildedrose/Update/UpdateLogic.java @@ -0,0 +1,7 @@ +package com.gildedrose.Update; + +import com.gildedrose.Item; + +public interface UpdateLogic { + void update(Item item); +} diff --git a/Java/src/main/java/com/gildedrose/Update/UpdateLogicFactory.java b/Java/src/main/java/com/gildedrose/Update/UpdateLogicFactory.java new file mode 100644 index 0000000..b33a6ee --- /dev/null +++ b/Java/src/main/java/com/gildedrose/Update/UpdateLogicFactory.java @@ -0,0 +1,18 @@ +package com.gildedrose.Update; + +import com.gildedrose.Item; + +public class UpdateLogicFactory { + public static UpdateLogic getUpdateLogic(Item item) { + switch (item.name) { + case "Aged Brie": + return new UpdateAgedBrieLogic(); + case "Backstage passes to a TAFKAL80ETC concert": + return new UpdateBackstagePassesLogic(); + case "Sulfuras, Hand of Ragnaros": + return new UpdateSulfurasLogic(); + default: + return new UpdateDefaultItemLogic(); + } + } +} diff --git a/Java/src/main/java/com/gildedrose/Update/UpdateSulfurasLogic.java b/Java/src/main/java/com/gildedrose/Update/UpdateSulfurasLogic.java new file mode 100644 index 0000000..97a5077 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/Update/UpdateSulfurasLogic.java @@ -0,0 +1,11 @@ +package com.gildedrose.Update; + +import com.gildedrose.Item; + +public class UpdateSulfurasLogic implements UpdateLogic { + + @Override + public void update(Item item) { + + } +} diff --git a/Java/src/test/java/com/gildedrose/TexttestFixture.java b/Java/src/test/java/com/gildedrose/TexttestFixture.java index d059c88..509e290 100644 --- a/Java/src/test/java/com/gildedrose/TexttestFixture.java +++ b/Java/src/test/java/com/gildedrose/TexttestFixture.java @@ -5,16 +5,16 @@ public static void main(String[] args) { System.out.println("OMGHAI!"); Item[] items = new Item[] { - new Item("+5 Dexterity Vest", 10, 20), // - new Item("Aged Brie", 2, 0), // - new Item("Elixir of the Mongoose", 5, 7), // - new Item("Sulfuras, Hand of Ragnaros", 0, 80), // - new Item("Sulfuras, Hand of Ragnaros", -1, 80), - new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20), - new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49), - new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49), - // this conjured item does not work properly yet - new Item("Conjured Mana Cake", 3, 6) }; + new Item("+5 Dexterity Vest", 10, 20), // + new Item("Aged Brie", 2, 0), // + new Item("Elixir of the Mongoose", 5, 7), // + new Item("Sulfuras, Hand of Ragnaros", 0, 80), // + new Item("Sulfuras, Hand of Ragnaros", -1, 80), + new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20), + new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49), + new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49)}; + // this conjured item does not work properly yet + // new Item("Conjured Mana Cake", 3, 6)}; GildedRose app = new GildedRose(items); From 867df955d1fc6b83d55762853be8f5f2e57a1436 Mon Sep 17 00:00:00 2001 From: 0-x-14 <0x14@daum.net> Date: Mon, 30 Sep 2024 16:18:20 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[Feat]=20Conjured=20=EC=95=84=EC=9D=B4?= =?UTF-8?q?=ED=85=9C=20=EC=B2=98=EB=A6=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Update/UpdateConjuredItemLogic.java | 26 +++++++++++++++++++ .../gildedrose/Update/UpdateLogicFactory.java | 5 ++++ .../java/com/gildedrose/TexttestFixture.java | 5 ++-- 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 Java/src/main/java/com/gildedrose/Update/UpdateConjuredItemLogic.java diff --git a/Java/src/main/java/com/gildedrose/Update/UpdateConjuredItemLogic.java b/Java/src/main/java/com/gildedrose/Update/UpdateConjuredItemLogic.java new file mode 100644 index 0000000..20a3769 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/Update/UpdateConjuredItemLogic.java @@ -0,0 +1,26 @@ +package com.gildedrose.Update; + +import com.gildedrose.Item; + +public class UpdateConjuredItemLogic implements UpdateLogic { + + @Override + public void update(Item item) { + + if (item.quality > 0) { + item.quality -= 2; + } + + item.sellIn--; + + if (item.sellIn < 0 && item.quality > 0) { + item.quality -= 2; + } + + if (item.quality < 0) { + // quality가 음수가 될수도 있으므로 + // sellIn > 0, quality = 1인 경우, sellIn < 0, quality = 3인 경우 해당 + item.quality = 0; + } + } +} diff --git a/Java/src/main/java/com/gildedrose/Update/UpdateLogicFactory.java b/Java/src/main/java/com/gildedrose/Update/UpdateLogicFactory.java index b33a6ee..55f9789 100644 --- a/Java/src/main/java/com/gildedrose/Update/UpdateLogicFactory.java +++ b/Java/src/main/java/com/gildedrose/Update/UpdateLogicFactory.java @@ -4,6 +4,11 @@ public class UpdateLogicFactory { public static UpdateLogic getUpdateLogic(Item item) { + if (item.name.contains("Conjured")) { + // Conjured 아이템인 경우 switch문에서 판별 불가능하므로 별도 처리 + return new UpdateConjuredItemLogic(); + } + switch (item.name) { case "Aged Brie": return new UpdateAgedBrieLogic(); diff --git a/Java/src/test/java/com/gildedrose/TexttestFixture.java b/Java/src/test/java/com/gildedrose/TexttestFixture.java index 509e290..808a899 100644 --- a/Java/src/test/java/com/gildedrose/TexttestFixture.java +++ b/Java/src/test/java/com/gildedrose/TexttestFixture.java @@ -12,9 +12,8 @@ public static void main(String[] args) { new Item("Sulfuras, Hand of Ragnaros", -1, 80), new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20), new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49), - new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49)}; - // this conjured item does not work properly yet - // new Item("Conjured Mana Cake", 3, 6)}; + new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49), + new Item("Conjured Mana Cake", 3, 6)}; GildedRose app = new GildedRose(items);