Commit 84783a27 authored by Colin DAMON's avatar Colin DAMON

Gilded rose without flow control

parent caac5fe1
package-gilded-rose-without-flow-control:
variables:
PROJECT_FOLDER: "gilded-rose-without-flow-control"
extends: .java
only:
refs:
- master
- merge_requests
changes:
- ".gitlab-common-ci.yml"
- "gilded-rose-without-flow-control/**/*"
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<version>1.0.0</version>
<groupId>fr.ippon.kata</groupId>
<artifactId>java-parent</artifactId>
<relativePath>../java-parent</relativePath>
</parent>
<version>1.0.0-SNAPSHOT</version>
<artifactId>gilded-rose-without-flow-control</artifactId>
<name>GildedRoseWithoutFlowControl</name>
<developers>
<developer>
<email>arey@ippon.fr</email>
<name>Anthony REY</name>
</developer>
<developer>
<email>cdamon@ippon.fr</email>
<name>Colin DAMON</name>
</developer>
</developers>
</project>
# Gilded Rose
Résolution du kata [GildedRose](https://github.com/emilybache/GildedRose-Refactoring-Kata) en supprimant tout les controles de flow
- **Auteur** : Anthony REY et Colin DAMON
- **Date** : 25/02/2021
- **Langage** : Java
- **Niveau** : Enervé
- **Replay** : TOOD
package com.gildedrose;
class AgedBrie extends WarehouseItem {
public AgedBrie(Item item) {
super(item);
}
@Override
public void update() {
decreaseSellIn();
increaseQuality();
increaseOver(0);
}
}
package com.gildedrose;
class BackstagePass extends WarehouseItem {
public BackstagePass(Item item) {
super(item);
}
@Override
public void update() {
decreaseSellIn();
increaseQuality();
increaseOver(10);
increaseOver(5);
qualityToZero();
}
}
package com.gildedrose;
import java.util.Arrays;
class GildedRose {
Item[] items;
public GildedRose(Item[] items) {
this.items = items;
}
public void updateQuality() {
Arrays.stream(items).forEach(this::updateItem);
}
private void updateItem(Item item) {
WarehouseItems.get(item).update();
}
}
package com.gildedrose;
public class Item {
public String name;
public int sellIn;
public int quality;
public Item(String name, int sellIn, int quality) {
this.name = name;
this.sellIn = sellIn;
this.quality = quality;
}
@Override
public String toString() {
return this.name + ", " + this.sellIn + ", " + this.quality;
}
}
package com.gildedrose;
import java.util.Optional;
public class NormalItem extends WarehouseItem {
public NormalItem(Item item) {
super(item);
}
@Override
void update() {
decreaseSellIn();
decreaseQuality();
Optional.of(getSellIn()).filter(sellIn -> sellIn < 0).ifPresent(sellIn -> decreaseQuality());
}
}
package com.gildedrose;
public class Sulfuras extends WarehouseItem {
public Sulfuras(Item item) {
super(item);
}
@Override
void update() {
// Sulfuras is LEGENDARY and tho doesn't need update
}
}
package com.gildedrose;
import java.util.Optional;
public abstract class WarehouseItem {
private final Item item;
public WarehouseItem(Item item) {
this.item = item;
}
protected void increaseQuality() {
item.quality = Math.min(item.quality + 1, 50);
}
protected int getSellIn() {
return item.sellIn;
}
protected void qualityToZero() {
Optional.of(getSellIn()).filter(sellIn -> sellIn < 0).ifPresent(value -> item.quality = 0);
}
protected void decreaseSellIn() {
item.sellIn -= 1;
}
protected void decreaseQuality() {
item.quality = Math.max(0, item.quality - 1);
}
protected void increaseOver(int sellIn) {
Optional.of(getSellIn()).filter(value -> value < sellIn).ifPresent(value -> increaseQuality());
}
abstract void update();
}
package com.gildedrose;
import java.util.Map;
import java.util.function.Function;
class WarehouseItems {
private static final String SULFURAS_NAME = "Sulfuras, Hand of Ragnaros";
private static final String BACKSTAGE_PASSES_NAME = "Backstage passes to a TAFKAL80ETC concert";
private static final String AGED_BRIE_NAME = "Aged Brie";
private static final Map<String, Function<Item, WarehouseItem>> ITEMS_FACTORIES = buildFactories();
public static WarehouseItem get(Item item) {
return ITEMS_FACTORIES.getOrDefault(item.name, NormalItem::new).apply(item);
}
private static Map<String, Function<Item, WarehouseItem>> buildFactories() {
return Map.of(SULFURAS_NAME, Sulfuras::new, AGED_BRIE_NAME, AgedBrie::new, BACKSTAGE_PASSES_NAME, BackstagePass::new);
}
}
package com.gildedrose;
class GildedRoseGolden {
Item[] items;
public GildedRoseGolden(Item[] items) {
this.items = 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;
}
}
}
}
}
}
package com.gildedrose;
import static org.assertj.core.api.Assertions.*;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
class GildedRoseTest {
@Test
void shouldBehaveLikeGoldenMaster() {
GildedRose rose = new GildedRose(ItemsProvider.get());
GildedRoseGolden golden = new GildedRoseGolden(ItemsProvider.get());
for (int day = 0; day < 100; day++) {
rose.updateQuality();
golden.updateQuality();
assertThat(rose.items)
.extracting(item -> item.sellIn)
.containsExactly(Arrays.stream(golden.items).map(item -> item.sellIn).toArray(Integer[]::new));
assertThat(rose.items)
.extracting(item -> item.quality)
.containsExactly(Arrays.stream(golden.items).map(item -> item.quality).toArray(Integer[]::new));
}
}
}
package com.gildedrose;
public final class ItemsProvider {
static Item[] get() {
Item[] items = new Item[] {
new Item("+5 Dexterity Vest", 10, 20),
new Item("Aged Brie", 2, 0),
new Item("Elixir of the Mongoose", 5, 7),
new Item("Sulfuras, Hand of Ragnaros", 0, 80),
new Item("Sulfuras, Hand of Ragnaros", -1, 80),
new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20),
new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49),
new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49)
};
return items;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment