From 84783a27e2ad82a51e74961b0231f438f391557a Mon Sep 17 00:00:00 2001 From: Colin DAMON <cdamon@ippon.fr> Date: Thu, 25 Feb 2021 08:21:59 +0100 Subject: [PATCH] Gilded rose without flow control --- .../.gitlab-ci.yml | 11 ++++ gilded-rose-without-flow-control/pom.xml | 29 +++++++++ gilded-rose-without-flow-control/readme.md | 9 +++ .../main/java/com/gildedrose/AgedBrie.java | 17 ++++++ .../java/com/gildedrose/BackstagePass.java | 19 ++++++ .../main/java/com/gildedrose/GildedRose.java | 19 ++++++ .../src/main/java/com/gildedrose/Item.java | 20 ++++++ .../main/java/com/gildedrose/NormalItem.java | 18 ++++++ .../main/java/com/gildedrose/Sulfuras.java | 13 ++++ .../java/com/gildedrose/WarehouseItem.java | 37 +++++++++++ .../java/com/gildedrose/WarehouseItems.java | 20 ++++++ .../java/com/gildedrose/GildedRoseGolden.java | 61 +++++++++++++++++++ .../java/com/gildedrose/GildedRoseTest.java | 27 ++++++++ .../java/com/gildedrose/ItemsProvider.java | 19 ++++++ 14 files changed, 319 insertions(+) create mode 100644 gilded-rose-without-flow-control/.gitlab-ci.yml create mode 100644 gilded-rose-without-flow-control/pom.xml create mode 100644 gilded-rose-without-flow-control/readme.md create mode 100644 gilded-rose-without-flow-control/src/main/java/com/gildedrose/AgedBrie.java create mode 100644 gilded-rose-without-flow-control/src/main/java/com/gildedrose/BackstagePass.java create mode 100644 gilded-rose-without-flow-control/src/main/java/com/gildedrose/GildedRose.java create mode 100644 gilded-rose-without-flow-control/src/main/java/com/gildedrose/Item.java create mode 100644 gilded-rose-without-flow-control/src/main/java/com/gildedrose/NormalItem.java create mode 100644 gilded-rose-without-flow-control/src/main/java/com/gildedrose/Sulfuras.java create mode 100644 gilded-rose-without-flow-control/src/main/java/com/gildedrose/WarehouseItem.java create mode 100644 gilded-rose-without-flow-control/src/main/java/com/gildedrose/WarehouseItems.java create mode 100644 gilded-rose-without-flow-control/src/test/java/com/gildedrose/GildedRoseGolden.java create mode 100644 gilded-rose-without-flow-control/src/test/java/com/gildedrose/GildedRoseTest.java create mode 100644 gilded-rose-without-flow-control/src/test/java/com/gildedrose/ItemsProvider.java 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 00000000..37e0a313 --- /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 00000000..1040b167 --- /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 00000000..4074d687 --- /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 00000000..15a33b4e --- /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 00000000..e3316a86 --- /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 00000000..cb63f4fc --- /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 00000000..8728dcf7 --- /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 00000000..539dbde8 --- /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 00000000..81894cc4 --- /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 00000000..af17938b --- /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 00000000..3e80361d --- /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 00000000..9ac08abc --- /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 00000000..2ac0b76a --- /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 00000000..cbe7eba6 --- /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; + } +} -- GitLab