mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-17 23:41:27 +00:00
intermediate refactor
it's not what i was hoping for. my preferred solution would be valid/acceptable, but i would have had to change all the tests. ain't nobody got time for that rn. :D
This commit is contained in:
parent
061b1104af
commit
33f9bc4d5f
11
Java/src/main/java/com/gildedrose/AgedBrieStrategy.java
Normal file
11
Java/src/main/java/com/gildedrose/AgedBrieStrategy.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package com.gildedrose;
|
||||||
|
|
||||||
|
public enum AgedBrieStrategy implements QualityUpdateStrategy {
|
||||||
|
INSTANCE;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void applyTo(Item item) {
|
||||||
|
item.quality = Math.min(50, item.sellIn < 0 ? item.quality+2 : item.quality+1);
|
||||||
|
--item.sellIn;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,19 @@
|
|||||||
|
package com.gildedrose;
|
||||||
|
|
||||||
|
public enum BackstagePassesStrategy implements QualityUpdateStrategy {
|
||||||
|
INSTANCE;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void applyTo(Item item) {
|
||||||
|
if (item.sellIn <= 0) {
|
||||||
|
item.quality = 0;
|
||||||
|
} else if (item.sellIn >= 10) {
|
||||||
|
item.quality = Math.min(50, item.quality+1);
|
||||||
|
} else if (item.sellIn >= 5) {
|
||||||
|
item.quality = Math.min(50, item.quality+2);
|
||||||
|
} else {
|
||||||
|
item.quality = Math.min(50, item.quality+3);
|
||||||
|
}
|
||||||
|
--item.sellIn;
|
||||||
|
}
|
||||||
|
}
|
||||||
15
Java/src/main/java/com/gildedrose/ConjuredStrategy.java
Normal file
15
Java/src/main/java/com/gildedrose/ConjuredStrategy.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package com.gildedrose;
|
||||||
|
|
||||||
|
public enum ConjuredStrategy implements QualityUpdateStrategy {
|
||||||
|
INSTANCE;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void applyTo(Item item) {
|
||||||
|
if (item.sellIn > 0) {
|
||||||
|
item.quality = Math.max(0, item.quality-2);
|
||||||
|
} else {
|
||||||
|
item.quality = Math.max(0, item.quality-4);
|
||||||
|
}
|
||||||
|
--item.sellIn;
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Java/src/main/java/com/gildedrose/DefaultStrategy.java
Normal file
11
Java/src/main/java/com/gildedrose/DefaultStrategy.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package com.gildedrose;
|
||||||
|
|
||||||
|
public enum DefaultStrategy implements QualityUpdateStrategy {
|
||||||
|
INSTANCE;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void applyTo(Item item) {
|
||||||
|
item.quality = Math.max(0, item.sellIn > 0 ? (item.quality-1) : (item.quality-2));
|
||||||
|
item.sellIn = item.sellIn - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,5 +1,9 @@
|
|||||||
package com.gildedrose;
|
package com.gildedrose;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
class GildedRose {
|
class GildedRose {
|
||||||
Item[] items;
|
Item[] items;
|
||||||
|
|
||||||
@ -8,55 +12,18 @@ class GildedRose {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void updateQuality() {
|
public void updateQuality() {
|
||||||
for (int i = 0; i < items.length; i++) {
|
Arrays.stream(items).forEach(this::updateQuality);
|
||||||
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")) {
|
public void updateQuality(Item item) {
|
||||||
if (items[i].sellIn < 11) {
|
if (item.name.equals("Aged Brie")) {
|
||||||
if (items[i].quality < 50) {
|
AgedBrieStrategy.INSTANCE.applyTo(item);
|
||||||
items[i].quality = items[i].quality + 1;
|
} else if (item.name.startsWith("Backstage passes")) {
|
||||||
}
|
BackstagePassesStrategy.INSTANCE.applyTo(item);
|
||||||
}
|
} else if (item.name.startsWith("Conjured")) {
|
||||||
|
ConjuredStrategy.INSTANCE.applyTo(item);
|
||||||
if (items[i].sellIn < 6) {
|
} else {
|
||||||
if (items[i].quality < 50) {
|
DefaultStrategy.INSTANCE.applyTo(item);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
package com.gildedrose;
|
||||||
|
|
||||||
|
public interface QualityUpdateStrategy {
|
||||||
|
void applyTo(Item item);
|
||||||
|
}
|
||||||
@ -10,25 +10,25 @@ class GildedRoseTest {
|
|||||||
@Test
|
@Test
|
||||||
@DisplayName("Regular item")
|
@DisplayName("Regular item")
|
||||||
void updateQualityRegularItem() {
|
void updateQualityRegularItem() {
|
||||||
Item itemUnderTest = new Item("Sample Regular Item", 1, 5);
|
Item item = new Item("Sample Regular Item", 1, 5);
|
||||||
Item[] items = new Item[] { itemUnderTest };
|
Item[] items = new Item[] { item };
|
||||||
GildedRose app = new GildedRose(items);
|
GildedRose app = new GildedRose(items);
|
||||||
|
|
||||||
app.updateQuality();
|
app.updateQuality();
|
||||||
assertEquals(0, itemUnderTest.sellIn);
|
assertEquals(0, item.sellIn);
|
||||||
assertEquals(4, itemUnderTest.quality);
|
assertEquals(4, item.quality);
|
||||||
|
|
||||||
app.updateQuality();
|
app.updateQuality();
|
||||||
assertEquals(-1, itemUnderTest.sellIn);
|
assertEquals(-1, item.sellIn);
|
||||||
assertEquals(2, itemUnderTest.quality);
|
assertEquals(2, item.quality);
|
||||||
|
|
||||||
app.updateQuality();
|
app.updateQuality();
|
||||||
assertEquals(-2, itemUnderTest.sellIn);
|
assertEquals(-2, item.sellIn);
|
||||||
assertEquals(0, itemUnderTest.quality);
|
assertEquals(0, item.quality);
|
||||||
|
|
||||||
app.updateQuality();
|
app.updateQuality();
|
||||||
assertEquals(-3, itemUnderTest.sellIn);
|
assertEquals(-3, item.sellIn);
|
||||||
assertEquals(0, itemUnderTest.quality);
|
assertEquals(0, item.quality);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user