🔨 refactoring

This commit is contained in:
Velizar Todorov 2021-11-24 21:39:58 +01:00
parent 9de8dc4f40
commit 1531172f23
11 changed files with 402 additions and 6 deletions

View File

@ -1,9 +1,9 @@
# Types of items
| Item name | Quality | Description |
| Item name | Quality changes | Description |
|:------------|:-----------------|:------------- |
| Sulfura | Always 80, constant value, cannot change.| Legendary item|
| Aged Brie | **increases** in quality twice as fast, when sell-in date approaches | - |
| Backstage Pass | **increases** in quality as sell-in date approaches; Quality increases by 2 when there are 10 days or less and by 3 when there are days or less but Quality drops to 0 after the concert. |- |
| Normal | **decreases** in quality twice as fast, when sell-in date approaches |- |
| Conjured | **decreases twice as fast** in quality as normal items, when sell-in date approaches |- |
| Normal | **Quality decreases by 1;** <br> **Quality decreases by 2**, when sell-in date approaches | - |
| Aged Brie | **Quality increases by 1;** <br> **Quality increases by 2**, when sell-in date approaches | - |
| Conjured | <b>Quality decreases by 1;</b> <br> <b>Quality decreases by 4</b> when sell-in date approaches | - |
| Backstage Pass | <b>Quality increases by 1;</b><br> <b>Quality increases by 2</b> when there are 10 days or less;<br> <b>Quality increases by 3 </b> when there are 5 days or less;<br> <b>Quality drops to 0</b> after the concert. | - |
| Sulfura | **Always 80**, constant value, cannot change.| Legendary item |

View File

@ -0,0 +1,29 @@
package com.gildedrose.item_helpers;
import com.gildedrose.Item;
import com.gildedrose.items.AgedBrie;
import com.gildedrose.items.BackstagePass;
import com.gildedrose.items.Conjured;
import com.gildedrose.items.Normal;
import com.gildedrose.items.Sulfura;
import static com.gildedrose.item_helpers.ItemName.getItemName;
public class ItemFactory {
public ItemType getItemType(Item item) {
ItemName itemName = getItemName(item.name);
switch (itemName) {
case AGED_BRIE:
return new AgedBrie(item);
case SULFURA:
return new Sulfura(item);
case BACKSTAGE_PASS:
return new BackstagePass(item);
case CONJURED:
return new Conjured(item);
default:
return new Normal(item);
}
}
}

View File

@ -0,0 +1,30 @@
package com.gildedrose.item_helpers;
public enum ItemName {
SULFURA("Sulfura"),
NORMAL("Normal"),
AGED_BRIE("Aged Brie"),
BACKSTAGE_PASS("Backstage pass"),
CONJURED("Conjured");
private final String name;
ItemName(String input) {
this.name = input;
}
@Override
public String toString() {
return name;
}
public static ItemName getItemName(String input) {
for (ItemName itemName : ItemName.values()) {
String itemNameStr = itemName.toString().toUpperCase();
if (itemNameStr.contains(input.toUpperCase()))
return itemName;
}
return NORMAL;
}
}

View File

@ -0,0 +1,7 @@
package com.gildedrose.item_helpers;
public interface ItemType {
void updateQuality();
}

View File

@ -0,0 +1,48 @@
package com.gildedrose.items;
import com.gildedrose.Item;
import com.gildedrose.item_helpers.ItemType;
import static java.lang.Math.max;
public class AgedBrie implements ItemType {
private final Item item;
public AgedBrie(Item item) {
this.item = item;
}
@Override
public void updateQuality() {
decrementSellInDate();
if (qualityIsPositive()) {
if (sellInDatePasses()) {
incrementQualityByTwo();
} else {
incrementQuality();
}
}
}
private boolean qualityIsPositive() {
return item.quality > 0;
}
private void decrementSellInDate() {
this.item.sellIn--;
}
private boolean sellInDatePasses() {
return this.item.sellIn < 0;
}
private void incrementQualityByTwo() {
this.item.quality = max(this.item.quality + 2, 0);
}
private void incrementQuality() {
this.item.quality++;
}
}

View File

@ -0,0 +1,52 @@
package com.gildedrose.items;
import com.gildedrose.Item;
import com.gildedrose.item_helpers.ItemType;
public class BackstagePass implements ItemType {
private final Item item;
public BackstagePass(Item item) {
this.item = item;
}
@Override
public void updateQuality() {
decrementSellInDate();
determineQuality();
}
private void determineQuality() {
if (this.item.sellIn >= 10) {
incrementQuality();
} else if (this.item.sellIn >= 5) {
incrementQualityByTwo();
} else if (this.item.sellIn >= 0) {
incrementQualityByThree();
} else {
makeQualityZero();
}
}
private void decrementSellInDate() {
this.item.sellIn--;
}
private void incrementQuality() {
this.item.quality++;
}
private void makeQualityZero() {
this.item.quality = 0;
}
private void incrementQualityByTwo() {
this.item.quality = this.item.quality + 2;
}
private void incrementQualityByThree() {
this.item.quality = this.item.quality + 3;
}
}

View File

@ -0,0 +1,47 @@
package com.gildedrose.items;
import com.gildedrose.Item;
import com.gildedrose.item_helpers.ItemType;
import static java.lang.Math.max;
public class Conjured implements ItemType {
private final Item item;
public Conjured(Item item) {
this.item = item;
}
@Override
public void updateQuality() {
decrementSellInDate();
if (qualityIsGreaterThanZero()) {
if (sellInDatePasses()) {
decrementQualityByFour();
} else {
decrementQuality();
}
}
}
private boolean qualityIsGreaterThanZero() {
return item.quality > 0;
}
private void decrementSellInDate() {
this.item.sellIn--;
}
private void decrementQualityByFour() {
this.item.quality = max(this.item.quality - 4, 0);
}
private boolean sellInDatePasses() {
return this.item.sellIn < 0;
}
private void decrementQuality() {
this.item.quality--;
}
}

View File

@ -0,0 +1,47 @@
package com.gildedrose.items;
import com.gildedrose.Item;
import com.gildedrose.item_helpers.ItemType;
import static java.lang.Math.max;
public class Normal implements ItemType {
private final Item item;
public Normal(Item item) {
this.item = item;
}
@Override
public void updateQuality() {
decrementSellInDate();
if (qualityIsPositive()) {
if (sellInDatePasses()) {
decrementQualityByTwo();
} else {
decrementQuality();
}
}
}
private boolean qualityIsPositive() {
return item.quality > 0;
}
private void decrementSellInDate() {
this.item.sellIn--;
}
private void decrementQualityByTwo() {
this.item.quality = max(this.item.quality - 2, 0);
}
private boolean sellInDatePasses() {
return this.item.sellIn < 0;
}
private void decrementQuality() {
this.item.quality--;
}
}

View File

@ -0,0 +1,30 @@
package com.gildedrose.items;
import com.gildedrose.Item;
import com.gildedrose.item_helpers.ItemType;
public class Sulfura implements ItemType {
private static final int QUALITY = 80;
private final Item item;
public Sulfura(Item item) {
this.item = item;
}
@Override
public void updateQuality() {
decrementSellInDate();
setQualityTo80();
}
private void setQualityTo80() {
if (this.item.quality != QUALITY) {
this.item.quality = QUALITY;
}
}
private void decrementSellInDate() {
this.item.sellIn--;
}
}

View File

@ -1,7 +1,10 @@
package com.gildedrose;
import com.gildedrose.item_helpers.ItemFactory;
import com.gildedrose.item_helpers.ItemType;
import org.junit.jupiter.api.Test;
import static com.gildedrose.item_helpers.ItemName.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
class GildedRoseTest {
@ -14,4 +17,70 @@ class GildedRoseTest {
assertEquals("foo", app.items[0].name);
}
@Test
void testNormalItem() {
ItemFactory itemFactory = new ItemFactory();
int days = 20;
Item normalItem = new Item(NORMAL.toString(), 10, 20);
for (int i = 0; i < days; i++) {
ItemType itemType = itemFactory.getItemType(normalItem);
itemType.updateQuality();
System.out.println("name, sell-in, quality");
System.out.println(normalItem);
}
}
@Test
void testConjuredItem() {
ItemFactory itemFactory = new ItemFactory();
int days = 20;
Item normalItem = new Item(CONJURED.toString(), 10, 40);
for (int i = 0; i < days; i++) {
ItemType itemType = itemFactory.getItemType(normalItem);
itemType.updateQuality();
System.out.println("name, sell-in, quality");
System.out.println(normalItem);
}
}
@Test
void testSulfuraItem() {
ItemFactory itemFactory = new ItemFactory();
int days = 20;
Item normalItem = new Item(SULFURA.toString(), 10, 40);
for (int i = 0; i < days; i++) {
ItemType itemType = itemFactory.getItemType(normalItem);
itemType.updateQuality();
System.out.println("name, sell-in, quality");
System.out.println(normalItem);
}
}
@Test
void testAgedBrieItem() {
ItemFactory itemFactory = new ItemFactory();
int days = 20;
Item normalItem = new Item(AGED_BRIE.toString(), 10, 40);
for (int i = 0; i < days; i++) {
ItemType itemType = itemFactory.getItemType(normalItem);
itemType.updateQuality();
System.out.println("name, sell-in, quality");
System.out.println(normalItem);
}
}
@Test
void testBackstagePassItem() {
ItemFactory itemFactory = new ItemFactory();
int days = 20;
Item normalItem = new Item(BACKSTAGE_PASS.toString(), 15, 40);
for (int i = 0; i < days; i++) {
ItemType itemType = itemFactory.getItemType(normalItem);
itemType.updateQuality();
System.out.println("name, sell-in, quality");
System.out.println(normalItem);
}
}
}

View File

@ -0,0 +1,37 @@
package com.gildedrose;
public class TexttestFixtureTemp {
public static void main(String[] args) {
System.out.println("OMGHAI!");
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),
// this conjured item does not work properly yet
new Item("Conjured Mana Cake", 3, 6) };
GildedRose app = new GildedRose(items);
int days = 20;
if (args.length > 0) {
days = Integer.parseInt(args[0]) + 1;
}
for (int i = 0; i < days; i++) {
System.out.println("-------- day " + i + " --------");
System.out.println("name, sellIn, quality");
for (Item item : items) {
System.out.println(item);
}
System.out.println();
app.updateQuality();
}
}
}