diff --git a/gilded-rose-without-flow-control/.gitlab-ci.yml b/gilded-rose-without-flow-control/.gitlab-ci.yml new file mode 100644 index 0000000000000000000000000000000000000000..37e0a3134f8f9d3bc2c0f7d9d408c94e943d25f6 --- /dev/null +++ b/gilded-rose-without-flow-control/.gitlab-ci.yml @@ -0,0 +1,11 @@ +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/**/*" diff --git a/gilded-rose-without-flow-control/pom.xml b/gilded-rose-without-flow-control/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..1040b167ff867d0ba297fb9cd55dfa0e63b95437 --- /dev/null +++ b/gilded-rose-without-flow-control/pom.xml @@ -0,0 +1,29 @@ +<?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> diff --git a/gilded-rose-without-flow-control/readme.md b/gilded-rose-without-flow-control/readme.md new file mode 100644 index 0000000000000000000000000000000000000000..4074d6879a280996c6f98db356f053d7ce911f87 --- /dev/null +++ b/gilded-rose-without-flow-control/readme.md @@ -0,0 +1,9 @@ +# 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 diff --git a/gilded-rose-without-flow-control/src/main/java/com/gildedrose/AgedBrie.java b/gilded-rose-without-flow-control/src/main/java/com/gildedrose/AgedBrie.java new file mode 100644 index 0000000000000000000000000000000000000000..15a33b4ead0f6100c8faca6a19747e78c3e403b3 --- /dev/null +++ b/gilded-rose-without-flow-control/src/main/java/com/gildedrose/AgedBrie.java @@ -0,0 +1,17 @@ +package com.gildedrose; + +class AgedBrie extends WarehouseItem { + + public AgedBrie(Item item) { + super(item); + } + + @Override + public void update() { + decreaseSellIn(); + + increaseQuality(); + + increaseOver(0); + } +} diff --git a/gilded-rose-without-flow-control/src/main/java/com/gildedrose/BackstagePass.java b/gilded-rose-without-flow-control/src/main/java/com/gildedrose/BackstagePass.java new file mode 100644 index 0000000000000000000000000000000000000000..e3316a86d431cf101a48b2c62f36fa8943d7d23e --- /dev/null +++ b/gilded-rose-without-flow-control/src/main/java/com/gildedrose/BackstagePass.java @@ -0,0 +1,19 @@ +package com.gildedrose; + +class BackstagePass extends WarehouseItem { + + public BackstagePass(Item item) { + super(item); + } + + @Override + public void update() { + decreaseSellIn(); + + increaseQuality(); + + increaseOver(10); + increaseOver(5); + qualityToZero(); + } +} diff --git a/gilded-rose-without-flow-control/src/main/java/com/gildedrose/GildedRose.java b/gilded-rose-without-flow-control/src/main/java/com/gildedrose/GildedRose.java new file mode 100644 index 0000000000000000000000000000000000000000..cb63f4fc9fd4ef6d684ff1f980f0a08c4949c1ca --- /dev/null +++ b/gilded-rose-without-flow-control/src/main/java/com/gildedrose/GildedRose.java @@ -0,0 +1,19 @@ +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(); + } +} diff --git a/gilded-rose-without-flow-control/src/main/java/com/gildedrose/Item.java b/gilded-rose-without-flow-control/src/main/java/com/gildedrose/Item.java new file mode 100644 index 0000000000000000000000000000000000000000..8728dcf786c0720bb5f21f8d31e15732d94c56e8 --- /dev/null +++ b/gilded-rose-without-flow-control/src/main/java/com/gildedrose/Item.java @@ -0,0 +1,20 @@ +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; + } +} diff --git a/gilded-rose-without-flow-control/src/main/java/com/gildedrose/NormalItem.java b/gilded-rose-without-flow-control/src/main/java/com/gildedrose/NormalItem.java new file mode 100644 index 0000000000000000000000000000000000000000..539dbde848c2fe7348b060e94f87f467a3957279 --- /dev/null +++ b/gilded-rose-without-flow-control/src/main/java/com/gildedrose/NormalItem.java @@ -0,0 +1,18 @@ +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()); + } +} diff --git a/gilded-rose-without-flow-control/src/main/java/com/gildedrose/Sulfuras.java b/gilded-rose-without-flow-control/src/main/java/com/gildedrose/Sulfuras.java new file mode 100644 index 0000000000000000000000000000000000000000..81894cc48d7500e1b60696ce632450bd042d63dd --- /dev/null +++ b/gilded-rose-without-flow-control/src/main/java/com/gildedrose/Sulfuras.java @@ -0,0 +1,13 @@ +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 + } +} diff --git a/gilded-rose-without-flow-control/src/main/java/com/gildedrose/WarehouseItem.java b/gilded-rose-without-flow-control/src/main/java/com/gildedrose/WarehouseItem.java new file mode 100644 index 0000000000000000000000000000000000000000..af17938b156b2d4564c9e0b7274f2e9066b8e96e --- /dev/null +++ b/gilded-rose-without-flow-control/src/main/java/com/gildedrose/WarehouseItem.java @@ -0,0 +1,37 @@ +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(); +} diff --git a/gilded-rose-without-flow-control/src/main/java/com/gildedrose/WarehouseItems.java b/gilded-rose-without-flow-control/src/main/java/com/gildedrose/WarehouseItems.java new file mode 100644 index 0000000000000000000000000000000000000000..3e80361d168292e92e01539e593b6a71f5f62b2b --- /dev/null +++ b/gilded-rose-without-flow-control/src/main/java/com/gildedrose/WarehouseItems.java @@ -0,0 +1,20 @@ +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); + } +} diff --git a/gilded-rose-without-flow-control/src/test/java/com/gildedrose/GildedRoseGolden.java b/gilded-rose-without-flow-control/src/test/java/com/gildedrose/GildedRoseGolden.java new file mode 100644 index 0000000000000000000000000000000000000000..9ac08abce7552302f71befc53935f72667e3721f --- /dev/null +++ b/gilded-rose-without-flow-control/src/test/java/com/gildedrose/GildedRoseGolden.java @@ -0,0 +1,61 @@ +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; + } + } + } + } + } +} diff --git a/gilded-rose-without-flow-control/src/test/java/com/gildedrose/GildedRoseTest.java b/gilded-rose-without-flow-control/src/test/java/com/gildedrose/GildedRoseTest.java new file mode 100644 index 0000000000000000000000000000000000000000..2ac0b76a71830ec5ac371268f24382ff1d7f480c --- /dev/null +++ b/gilded-rose-without-flow-control/src/test/java/com/gildedrose/GildedRoseTest.java @@ -0,0 +1,27 @@ +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)); + } + } +} diff --git a/gilded-rose-without-flow-control/src/test/java/com/gildedrose/ItemsProvider.java b/gilded-rose-without-flow-control/src/test/java/com/gildedrose/ItemsProvider.java new file mode 100644 index 0000000000000000000000000000000000000000..cbe7eba60fb505111a9046afa66cad5005066c36 --- /dev/null +++ b/gilded-rose-without-flow-control/src/test/java/com/gildedrose/ItemsProvider.java @@ -0,0 +1,19 @@ +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; + } +}