Refactored Code- Moved implementation of sellin days and quality to specific classes.

This commit is contained in:
lekshmysasidhar 2023-03-31 12:01:16 +02:00
parent 95e7036824
commit cec7b6ad04
10 changed files with 246 additions and 42 deletions

View File

@ -1,5 +1,17 @@
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++;
}
}
}

View File

@ -1,5 +1,25 @@
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++;
}
}
}

View File

@ -1,5 +1,10 @@
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--;
}
}
}

View File

@ -1,5 +1,7 @@
package com.gildedrose;
import com.gildedrose.factory.GoodsFactory;
class GildedRose {
Item[] items;
@ -9,44 +11,14 @@ class GildedRose {
public void updateQuality() {
for (Item item : items) {
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")) {
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);
}
Goods goods = GoodsFactory.getGoods(item.name);
goods.updateQuality(item);
goods.updateSellInDays(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) {
if (item.quality < 50) {
@ -80,7 +52,4 @@ class GildedRose {
item.quality--;
}
private void updateSellInDays(Item item) {
item.sellIn--;
}
}

View File

@ -1,5 +1,10 @@
package com.gildedrose;
public abstract class Goods {
public interface Goods {
public void updateQuality(Item item);
default void updateSellInDays(Item item) {
item.sellIn--;
}
}

View File

@ -1,5 +1,18 @@
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.
}
}

View 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);
}
}

View 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);
}
}

View 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);
}
}

View 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);
}
}