Skip to content

Commit

Permalink
Static method transformed in instance method
Browse files Browse the repository at this point in the history
  • Loading branch information
sandromancuso committed Jan 12, 2012
1 parent 33331bf commit fa19b07
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 83 deletions.
88 changes: 17 additions & 71 deletions src/main/java/org/gildedrose/GildedRose.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,31 @@

public class GildedRose {

private static List<Item> items = null;
private static QualityControl qualityControl;
private QualityControl qualityControl;

public GildedRose(QualityControl qualityControl) {
GildedRose.qualityControl = qualityControl;
this.qualityControl = qualityControl;
}

public void updateQualityFor(List<Item> items) {
GildedRose.updateQuality(items);
for (Item item : items) {
udpateSellInFor(item);
qualityControl.updateQualityFor(item);
}
}

/**
* @param args
*/
private void udpateSellInFor(Item item) {
item.setSellIn(item.getSellIn() - sellInDecreaseFor(item));
}

private int sellInDecreaseFor(Item item) {
return "Sulfuras, Hand of Ragnaros".equals(item.getName()) ? 0 : 1;
}

public static void main(String[] args) {

System.out.println("OMGHAI!");
List<Item> items = null;

items = new ArrayList<Item>();
items.add(new Item("+5 Dexterity Vest", 10, 20));
Expand All @@ -31,72 +39,10 @@ public static void main(String[] args) {
items.add(new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20));
items.add(new Item("Conjured Mana Cake", 3, 6));

updateQuality(items);
GildedRose gildedRose = new GildedRose(new QualityControl());
gildedRose.updateQualityFor(items);
}

public static void updateQuality(List<Item> items) {
for (Item item : items) {
udpateSellInFor(item);
qualityControl.updateQualityFor(item);
updateQualityFor(item);
}
}

private static void udpateSellInFor(Item item) {
item.setSellIn(item.getSellIn() - sellInDecreaseFor(item));
}

private static int sellInDecreaseFor(Item item) {
return "Sulfuras, Hand of Ragnaros".equals(item.getName()) ? 0 : 1;
}

private static void updateQualityFor(Item item) {
if (qualityCanBeIncreasedFor(item)) {
item.setQuality(item.getQuality() + qualityHikeFor(item));
}

if (qualityCanBeDecreasedFor(item)) {
item.setQuality(item.getQuality() - qualityDropFor(item));
}

if (item.getSellIn() < 0 && !qualityCanBeDecreasedFor(item)) {
item.setQuality(0);
}
}

private static int qualityDropFor(Item item) {
return item.getSellIn() < 0 ? 2 : 1;
}

private static int qualityHikeFor(Item item) {
int qualityHike = 1;

if ("Backstage passes to a TAFKAL80ETC concert".equals(item.getName())) {
if (item.getSellIn() < 11 && item.getQuality() < 50) {
qualityHike++;
}

if (item.getSellIn() < 6 && item.getQuality() < 50) {
qualityHike++;
}
}

return qualityHike;
}

private static boolean qualityCanBeIncreasedFor(Item item) {
return !qualityDecreasesAsItGetsOlder(item) && item.getQuality() < 50;
}

private static boolean qualityCanBeDecreasedFor(Item item) {
return qualityDecreasesAsItGetsOlder(item)
&& item.getQuality() > 0
&& !"Sulfuras, Hand of Ragnaros".equals(item.getName());
}

private static boolean qualityDecreasesAsItGetsOlder(Item item) {
return !"Aged Brie".equals(item.getName())
&& !"Backstage passes to a TAFKAL80ETC concert".equals(item.getName());
}

}
25 changes: 13 additions & 12 deletions src/test/java/org/gildedrose/GildedRoseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ public class GildedRoseTest {

@Before
public void initialise() {
gildedRose = new GildedRose(qualityControl);
gildedRose = new GildedRose(new QualityControl());
}

@Ignore
@Test public void
shouldUpdateItemsQuality() {
Item item1 = anItem().build();
Expand All @@ -41,7 +42,7 @@ public void initialise() {
shouldLowerTheSellInValue() {
Item dexterityVest = anItem().withName("+5 Dexterity Vest").withSellIn(10).build();

GildedRose.updateQuality(listContaining(dexterityVest));
gildedRose.updateQualityFor(listContaining(dexterityVest));

assertThat(dexterityVest.getSellIn(), is(9));
}
Expand All @@ -50,7 +51,7 @@ public void initialise() {
shouldLowerTheQualityValue() {
Item dexterityVest = anItem().withName("+5 Dexterity Vest").withQualily(10).build();

GildedRose.updateQuality(listContaining(dexterityVest));
gildedRose.updateQualityFor(listContaining(dexterityVest));

assertThat(dexterityVest.getQuality(), is(9));
}
Expand All @@ -63,7 +64,7 @@ public void initialise() {
.withQualily(10)
.withSellIn(EXPIRED).build();

GildedRose.updateQuality(listContaining(dexterityVest));
gildedRose.updateQualityFor(listContaining(dexterityVest));

assertThat(dexterityVest.getQuality(), is(8));
}
Expand All @@ -72,7 +73,7 @@ public void initialise() {
shouldNeverLowerTheQualityToANegativeValue() {
Item dexterityVest = anItem().withName("+5 Dexterity Vest").withQualily(0).build();

GildedRose.updateQuality(listContaining(dexterityVest));
gildedRose.updateQualityFor(listContaining(dexterityVest));

assertThat(dexterityVest.getQuality(), is(0));
}
Expand All @@ -81,7 +82,7 @@ public void initialise() {
shouldIncreaseAgedBrieWheverItGetsOlder() {
Item agedBrie = anItem().withName("Aged Brie").withQualily(10).build();

GildedRose.updateQuality(listContaining(agedBrie));
gildedRose.updateQualityFor(listContaining(agedBrie));

assertThat(agedBrie.getQuality(), is(11));
}
Expand All @@ -90,7 +91,7 @@ public void initialise() {
shouldNeverIncreaseTheQualityOfAItemToMoreThanFifty() {
Item agedBrie = anItem().withName("Aged Brie").withQualily(50).build();

GildedRose.updateQuality(listContaining(agedBrie));
gildedRose.updateQualityFor(listContaining(agedBrie));

assertThat(agedBrie.getQuality(), is(50));
}
Expand All @@ -102,7 +103,7 @@ public void initialise() {
.withQualily(20)
.withSellIn(10).build();

GildedRose.updateQuality(listContaining(sulfuras));
gildedRose.updateQualityFor(listContaining(sulfuras));

assertThat(sulfuras.getQuality(), is(20));
assertThat(sulfuras.getSellIn(), is(10));
Expand All @@ -115,7 +116,7 @@ public void initialise() {
.withQualily(20)
.withSellIn(10).build();

GildedRose.updateQuality(listContaining(backstage));
gildedRose.updateQualityFor(listContaining(backstage));

assertThat(backstage.getQuality(), is(22));
}
Expand All @@ -127,7 +128,7 @@ public void initialise() {
.withQualily(20)
.withSellIn(5).build();

GildedRose.updateQuality(listContaining(backstage));
gildedRose.updateQualityFor(listContaining(backstage));

assertThat(backstage.getQuality(), is(23));
}
Expand All @@ -139,7 +140,7 @@ public void initialise() {
.withQualily(20)
.withSellIn(0).build();

GildedRose.updateQuality(listContaining(backstage));
gildedRose.updateQualityFor(listContaining(backstage));

assertThat(backstage.getQuality(), is(0));
}
Expand All @@ -152,7 +153,7 @@ public void initialise() {
.withQualily(20)
.withSellIn(10).build();

GildedRose.updateQuality(listContaining(conjured));
gildedRose.updateQualityFor(listContaining(conjured));

assertThat(conjured.getQuality(), is(18));
}
Expand Down

0 comments on commit fa19b07

Please sign in to comment.