mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-16 06:51:27 +00:00
Refactored Code- Moved implementation of sellin days and quality to specific classes.
This commit is contained in:
parent
95e7036824
commit
cec7b6ad04
@ -1,5 +1,17 @@
|
|||||||
package com.gildedrose;
|
package com.gildedrose;
|
||||||
|
|
||||||
public class AgedBrie extends Goods {
|
public class AgedBrie implements Goods {
|
||||||
|
private static final int MAX_ALLOWED_QUALITY = 50;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateQuality(Item item) {
|
||||||
|
addQualityWhenWithInLimit(item);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addQualityWhenWithInLimit(Item item) {
|
||||||
|
if (item.quality < MAX_ALLOWED_QUALITY) {
|
||||||
|
item.quality++;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,25 @@
|
|||||||
package com.gildedrose;
|
package com.gildedrose;
|
||||||
|
|
||||||
public class BackStagePasses extends Goods {
|
public class BackStagePasses implements Goods {
|
||||||
|
|
||||||
|
private static final int MAX_ALLOWED_QUALITY = 50;
|
||||||
|
private static final int SELL_IN_MAX_THRESHOLD_DAY = 11;
|
||||||
|
private static final int SELL_IN_MIN_THRESHOLD_DAY = 6;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateQuality(Item item) {
|
||||||
|
addQualityWhenWithInLimit(item);
|
||||||
|
if (item.sellIn < SELL_IN_MAX_THRESHOLD_DAY) {
|
||||||
|
addQualityWhenWithInLimit(item);
|
||||||
|
}
|
||||||
|
if (item.sellIn < SELL_IN_MIN_THRESHOLD_DAY) {
|
||||||
|
addQualityWhenWithInLimit(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addQualityWhenWithInLimit(Item item) {
|
||||||
|
if (item.quality < MAX_ALLOWED_QUALITY) {
|
||||||
|
item.quality++;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,10 @@
|
|||||||
package com.gildedrose;
|
package com.gildedrose;
|
||||||
|
|
||||||
public class Generic extends Goods {
|
public class Generic implements Goods {
|
||||||
|
@Override
|
||||||
|
public void updateQuality(Item item) {
|
||||||
|
if (item.quality > 0) {
|
||||||
|
item.quality--;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
package com.gildedrose;
|
package com.gildedrose;
|
||||||
|
|
||||||
|
import com.gildedrose.factory.GoodsFactory;
|
||||||
|
|
||||||
class GildedRose {
|
class GildedRose {
|
||||||
Item[] items;
|
Item[] items;
|
||||||
|
|
||||||
@ -9,44 +11,14 @@ class GildedRose {
|
|||||||
|
|
||||||
public void updateQuality() {
|
public void updateQuality() {
|
||||||
for (Item item : items) {
|
for (Item item : items) {
|
||||||
if (!item.name.equals("Aged Brie")
|
Goods goods = GoodsFactory.getGoods(item.name);
|
||||||
&& !item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
goods.updateQuality(item);
|
||||||
if (item.quality > 0) {
|
goods.updateSellInDays(item);
|
||||||
if (!item.name.equals("Sulfuras, Hand of Ragnaros")) {
|
|
||||||
updateQualityForNormalItem(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (item.quality < 50) {
|
|
||||||
item.quality = item.quality + 1;
|
|
||||||
|
|
||||||
if (item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
|
||||||
updateQualityForBackstagePasses(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!item.name.equals("Sulfuras, Hand of Ragnaros")) {
|
|
||||||
updateSellInDays(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
updateQualityForExpiredItem(item);
|
updateQualityForExpiredItem(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateQualityForBackstagePasses(Item item) {
|
|
||||||
if (item.sellIn < 11) {
|
|
||||||
addQualityWhenWithInLimit(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.sellIn < 6) {
|
|
||||||
addQualityWhenWithInLimit(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateQualityForNormalItem(Item item) {
|
|
||||||
item.quality--;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addQualityWhenWithInLimit(Item item) {
|
private void addQualityWhenWithInLimit(Item item) {
|
||||||
if (item.quality < 50) {
|
if (item.quality < 50) {
|
||||||
@ -80,7 +52,4 @@ class GildedRose {
|
|||||||
item.quality--;
|
item.quality--;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateSellInDays(Item item) {
|
|
||||||
item.sellIn--;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -1,5 +1,10 @@
|
|||||||
package com.gildedrose;
|
package com.gildedrose;
|
||||||
|
|
||||||
public abstract class Goods {
|
public interface Goods {
|
||||||
|
|
||||||
|
public void updateQuality(Item item);
|
||||||
|
|
||||||
|
default void updateSellInDays(Item item) {
|
||||||
|
item.sellIn--;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,18 @@
|
|||||||
package com.gildedrose;
|
package com.gildedrose;
|
||||||
|
|
||||||
public class Sulfuras extends Goods {
|
public class Sulfuras implements Goods {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateQuality(Item item) {
|
||||||
|
// Do nothing because Sulfuras being a legendary item, never has to be sold or
|
||||||
|
// decreases in quality.
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateSellInDays(Item item) {
|
||||||
|
// Do nothing because Sulfuras being a legendary item, never has to be sold or
|
||||||
|
// decreases in quality.
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
45
Java/src/test/java/com/gildedrose/AgedBrieTest.java
Normal file
45
Java/src/test/java/com/gildedrose/AgedBrieTest.java
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
package com.gildedrose;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
|
class AgedBrieTest {
|
||||||
|
|
||||||
|
private static Stream<Arguments> getTestItemsWithExpectedQuality() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of(new Item(GoodsType.AGED_BRIE.getGoodsName(), 2, 2), 3),
|
||||||
|
Arguments.of(new Item(GoodsType.AGED_BRIE.getGoodsName(), 1, 50), 50)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Stream<Arguments> getTestItemsWithExpectedSellin() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of(new Item(GoodsType.AGED_BRIE.getGoodsName(), 2, 2), 1),
|
||||||
|
Arguments.of(new Item(GoodsType.AGED_BRIE.getGoodsName(), 1, 50), 0)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Test updateQuality - {index}")
|
||||||
|
@MethodSource("getTestItemsWithExpectedQuality")
|
||||||
|
void testUpdateQuality(Item item, int expectedQuality) {
|
||||||
|
AgedBrie agedBrie = new AgedBrie();
|
||||||
|
agedBrie.updateQuality(item);
|
||||||
|
assertEquals(expectedQuality, item.quality);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Test updateSellIn - {index}")
|
||||||
|
@MethodSource("getTestItemsWithExpectedSellin")
|
||||||
|
void testUpdateSellInDaysItem(Item item, int expectedSellin) {
|
||||||
|
AgedBrie agedBrie = new AgedBrie();
|
||||||
|
agedBrie.updateSellInDays(item);
|
||||||
|
assertEquals(expectedSellin, item.sellIn);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
48
Java/src/test/java/com/gildedrose/BackStagePassesTest.java
Normal file
48
Java/src/test/java/com/gildedrose/BackStagePassesTest.java
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package com.gildedrose;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
|
class BackStagePassesTest {
|
||||||
|
private static Stream<Arguments> getTestItemsWithExpectedQuality() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of(new Item(GoodsType.BACK_STAGE_PASSES.getGoodsName(), 2, 2), 5),
|
||||||
|
Arguments.of(new Item(GoodsType.BACK_STAGE_PASSES.getGoodsName(), 10, 45), 47),
|
||||||
|
Arguments.of(new Item(GoodsType.BACK_STAGE_PASSES.getGoodsName(), 0, 49), 50),
|
||||||
|
Arguments.of(new Item(GoodsType.BACK_STAGE_PASSES.getGoodsName(), 12, 46), 47),
|
||||||
|
Arguments.of(new Item(GoodsType.BACK_STAGE_PASSES.getGoodsName(), 3, 50), 50));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Stream<Arguments> getTestItemsWithExpectedSellin() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of(new Item(GoodsType.BACK_STAGE_PASSES.getGoodsName(), 2, 2), 1),
|
||||||
|
Arguments.of(new Item(GoodsType.BACK_STAGE_PASSES.getGoodsName(), 1, 50), 0)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Test updateQuality - {index}")
|
||||||
|
@MethodSource("getTestItemsWithExpectedQuality")
|
||||||
|
void testUpdateQuality(Item item, int expectedQuality) {
|
||||||
|
BackStagePasses backStagePasses = new BackStagePasses();
|
||||||
|
backStagePasses.updateQuality(item);
|
||||||
|
assertEquals(expectedQuality, item.quality);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Test updateSellIn - {index}")
|
||||||
|
@MethodSource("getTestItemsWithExpectedSellin")
|
||||||
|
void testUpdateSellInDaysItem(Item item, int expectedSellin) {
|
||||||
|
BackStagePasses backStagePasses = new BackStagePasses();
|
||||||
|
backStagePasses.updateSellInDays(item);
|
||||||
|
assertEquals(expectedSellin, item.sellIn);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
46
Java/src/test/java/com/gildedrose/GenericTest.java
Normal file
46
Java/src/test/java/com/gildedrose/GenericTest.java
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package com.gildedrose;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
|
class GenericTest {
|
||||||
|
|
||||||
|
private static Stream<Arguments> getTestItemsWithExpectedQuality() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of(new Item("Generic Item", 2, 0), 0),
|
||||||
|
Arguments.of(new Item("Generic Item", 2, 2), 1)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Stream<Arguments> getTestItemsWithExpectedSellin() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of(new Item("Generic Item", 2, 0), 1),
|
||||||
|
Arguments.of(new Item("Generic Item", 2, 2), 1)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Test updateQuality - {index}")
|
||||||
|
@MethodSource("getTestItemsWithExpectedQuality")
|
||||||
|
void testUpdateQuality(Item item, int expectedQuality) {
|
||||||
|
Generic generic = new Generic();
|
||||||
|
generic.updateQuality(item);
|
||||||
|
assertEquals(expectedQuality, item.quality);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Test updateSellIn - {index}")
|
||||||
|
@MethodSource("getTestItemsWithExpectedSellin")
|
||||||
|
void testUpdateSellInDaysItem(Item item, int expectedSellin) {
|
||||||
|
Generic generic = new Generic();
|
||||||
|
generic.updateSellInDays(item);
|
||||||
|
assertEquals(expectedSellin, item.sellIn);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
41
Java/src/test/java/com/gildedrose/SulfurasTest.java
Normal file
41
Java/src/test/java/com/gildedrose/SulfurasTest.java
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package com.gildedrose;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
|
class SulfurasTest {
|
||||||
|
|
||||||
|
private static Stream<Arguments> getTestItemsWithExpectedQuality() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of(new Item(GoodsType.SULFURAS.getGoodsName(), 2, 2), 2),
|
||||||
|
Arguments.of(new Item(GoodsType.SULFURAS.getGoodsName(), 1, 50), 50));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Stream<Arguments> getTestItemsWithExpectedSellin() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of(new Item(GoodsType.SULFURAS.getGoodsName(), 2, 2), 2),
|
||||||
|
Arguments.of(new Item(GoodsType.SULFURAS.getGoodsName(), 1, 50), 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Test updateQuality - {index}")
|
||||||
|
@MethodSource("getTestItemsWithExpectedQuality")
|
||||||
|
void testUpdateQuality(Item item, int expectedQuality) {
|
||||||
|
Sulfuras sulfurus = new Sulfuras();
|
||||||
|
sulfurus.updateQuality(item);
|
||||||
|
assertEquals(expectedQuality, item.quality);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Test updateSellin - {index}")
|
||||||
|
@MethodSource("getTestItemsWithExpectedSellin")
|
||||||
|
void testUpdateSellInDays(Item item, int expectedSellin) {
|
||||||
|
Sulfuras sulfurus = new Sulfuras();
|
||||||
|
sulfurus.updateSellInDays(item);
|
||||||
|
assertEquals(expectedSellin, item.sellIn);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user