Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[백지현] 1차 과제 제출 #11

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 6 additions & 49 deletions Java/src/main/java/com/gildedrose/GildedRose.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.gildedrose;

import com.gildedrose.update.Logic;
import com.gildedrose.update.LogicFactory;

class GildedRose {
Item[] items;

Expand All @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

향상된 for문으로 변경돼서 더 가독성이 좋네요 👍

Logic logic = LogicFactory.getLogic(item);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

팩토리 메소드를 활용 해 OCP를 지키는 코드를 짜신 것이 인상 깊었습니다! 다만LogicFactory나 getLogic 같은 클래스명, 메소드명은 이름을 통해 어떤 내용인지 유추하기 어려운 것 같습니다. ItemUpdateLogic 같이 명확한 메소드를 사용하는 것은 어떨까요?

logic.update(item);
}
}
}
24 changes: 24 additions & 0 deletions Java/src/main/java/com/gildedrose/update/AgedBrie.java
Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decreaseSellIn 메서드랑 increaseQuality 메서드가 중복되니 부모 클래스에 구현하고 자식클래스에서 상속받아서 사용해도 좋을 것 같습니다 😊

item.sellIn--;
}

private void increaseQuality(Item item) {
if (item.quality < 50) {
item.quality++;
}
}
}
33 changes: 33 additions & 0 deletions Java/src/main/java/com/gildedrose/update/BackstagePass.java
Original file line number Diff line number Diff line change
@@ -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++;
}
}
}
24 changes: 24 additions & 0 deletions Java/src/main/java/com/gildedrose/update/Conjured.java
Original file line number Diff line number Diff line change
@@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if문을 한번 더 사용하는 것이 아닌 Math.max 메소드를 통해 quality를 결정하는 방식 좋습니다!

}
}
}
7 changes: 7 additions & 0 deletions Java/src/main/java/com/gildedrose/update/Logic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.gildedrose.update;

import com.gildedrose.Item;

public interface Logic {
void update(Item item);
}
20 changes: 20 additions & 0 deletions Java/src/main/java/com/gildedrose/update/LogicFactory.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
24 changes: 24 additions & 0 deletions Java/src/main/java/com/gildedrose/update/NormalItem.java
Original file line number Diff line number Diff line change
@@ -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--;
}
}
}
10 changes: 10 additions & 0 deletions Java/src/main/java/com/gildedrose/update/Sulfuras.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.gildedrose.update;

import com.gildedrose.Item;

public class Sulfuras implements Logic{
@Override
public void update(Item item) {

}
}