-
Notifications
You must be signed in to change notification settings - Fork 14
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
base: develop
Are you sure you want to change the base?
[백지현] 1차 과제 제출 #11
Changes from 1 commit
ffbbf13
eb7adb5
55ff506
6a6c660
ac18da2
9cff0c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 팩토리 메소드를 활용 해 OCP를 지키는 코드를 짜신 것이 인상 깊었습니다! 다만LogicFactory나 getLogic 같은 클래스명, 메소드명은 이름을 통해 어떤 내용인지 유추하기 어려운 것 같습니다. |
||
logic.update(item); | ||
} | ||
} | ||
} |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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++; | ||
} | ||
} | ||
} |
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++; | ||
} | ||
} | ||
} |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if문을 한번 더 사용하는 것이 아닌 Math.max 메소드를 통해 quality를 결정하는 방식 좋습니다! |
||
} | ||
} | ||
} |
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); | ||
} |
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(); | ||
} | ||
} | ||
} |
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--; | ||
} | ||
} | ||
} |
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) { | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
향상된 for문으로 변경돼서 더 가독성이 좋네요 👍