Skip to content

Commit

Permalink
Introduce Strategy pattern for updating item quality
Browse files Browse the repository at this point in the history
  • Loading branch information
doxxx93 committed Oct 9, 2023
1 parent 39b5677 commit 87a76ed
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 58 deletions.
18 changes: 18 additions & 0 deletions Java/src/main/java/com/gildedrose/AgedBrieUpdateStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.gildedrose;

public class AgedBrieUpdateStrategy implements ItemUpdateStrategy {
@Override
public void update(Item item) {
if (item.quality < 50) {
item.quality = item.quality + 1;
}

item.sellIn = item.sellIn - 1;

if (item.sellIn < 0) {
if (item.quality < 50) {
item.quality = item.quality + 1;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.gildedrose;

public class BackstagePassesUpdateStrategy implements ItemUpdateStrategy {
@Override
public void update(Item item) {
if (item.quality < 50) {
item.quality = item.quality + 1;

if (item.sellIn < 11) {
if (item.quality < 50) {
item.quality = item.quality + 1;
}
}

if (item.sellIn < 6) {
if (item.quality < 50) {
item.quality = item.quality + 1;
}
}
}

item.sellIn = item.sellIn - 1;

if (item.sellIn < 0) {
item.quality = 0;
}
}
}
72 changes: 14 additions & 58 deletions Java/src/main/java/com/gildedrose/GildedRose.java
Original file line number Diff line number Diff line change
@@ -1,72 +1,28 @@
package com.gildedrose;

import java.util.HashMap;

class GildedRose {
public static final String DEFAULT_STRATEGY = "default";
Item[] items;
HashMap<String, ItemUpdateStrategy> strategies;

public GildedRose(Item[] items) {
this.items = items;
this.strategies = new HashMap<>();
this.strategies.put("Aged Brie", new AgedBrieUpdateStrategy());
this.strategies.put("Backstage passes to a TAFKAL80ETC concert", new BackstagePassesUpdateStrategy());
this.strategies.put("Sulfuras, Hand of Ragnaros", new SulfurasUpdateStrategy());
this.strategies.put(DEFAULT_STRATEGY, new StandardItemUpdateStrategy());
}

public void updateQuality() {
for (Item item : items) {
// "Aged Brie"와 "Backstage passes"가 아닌 경우
// 품질이 0보다 크면 "Sulfuras, Hand of Ragnaros" 제외 아이템 품질을 1 감소
if (!item.name.equals("Aged Brie")
&& !item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
if (item.quality > 0) {
if (!item.name.equals("Sulfuras, Hand of Ragnaros")) {
item.quality = item.quality - 1;
}
}
} else {
// "Aged Brie" 또는 "Backstage passes"인 경우 품질 50 미만이면 품질 1 증가
// "Backstage passes"의 경우 품질이 50 미만이고 판매일이 11일 미만인 경우 추가로 1 증가
// 판매일이 6일 미만인 경우 또한 추가로 1 증가
if (item.quality < 50) {
item.quality = item.quality + 1;

if (item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
if (item.sellIn < 11) {
if (item.quality < 50) {
item.quality = item.quality + 1;
}
}

if (item.sellIn < 6) {
if (item.quality < 50) {
item.quality = item.quality + 1;
}
}
}
}
}

// "Sulfuras, Hand of Ragnaros"가 아닌 경우 판매일을 1 줄임
if (!item.name.equals("Sulfuras, Hand of Ragnaros")) {
item.sellIn = item.sellIn - 1;
}

// 판매일이 지난 경우
// "Aged Brie"가 아니고, "Backstage passes"가 아닌 경우 품질이 0 보다 크면 줄임
// "Backstage passes"가 아닌 경우 품질을 0으로 변경
// "Aged Brie"인 경우 품질이 50 미만이면 증가
if (item.sellIn < 0) {
if (!item.name.equals("Aged Brie")) {
if (!item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
if (item.quality > 0) {
if (!item.name.equals("Sulfuras, Hand of Ragnaros")) {
item.quality = item.quality - 1;
}
}
} else {
item.quality = item.quality - item.quality;
}
} else {
if (item.quality < 50) {
item.quality = item.quality + 1;
}
}
}
getItemUpdateStrategy(item).update(item);
}
}

private ItemUpdateStrategy getItemUpdateStrategy(Item item) {
return strategies.getOrDefault(item.name, strategies.get(DEFAULT_STRATEGY));
}
}
5 changes: 5 additions & 0 deletions Java/src/main/java/com/gildedrose/ItemUpdateStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.gildedrose;

public interface ItemUpdateStrategy {
void update(Item item);
}
14 changes: 14 additions & 0 deletions Java/src/main/java/com/gildedrose/StandardItemUpdateStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.gildedrose;

public class StandardItemUpdateStrategy implements ItemUpdateStrategy {
@Override
public void update(Item item) {
item.sellIn -= 1;
if (item.quality > 0) {
item.quality = item.quality - 1;
}
if (item.sellIn < 0 && item.quality > 0) {
item.quality = item.quality - 1;
}
}
}
8 changes: 8 additions & 0 deletions Java/src/main/java/com/gildedrose/SulfurasUpdateStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.gildedrose;

public class SulfurasUpdateStrategy implements ItemUpdateStrategy {
@Override
public void update(Item item) {
// "Sulfuras, Hand of Ragnaros"는 판매일과 품질이 변하지 않음
}
}

0 comments on commit 87a76ed

Please sign in to comment.