replace items by specific objects in tests

This commit is contained in:
Sallah Kokaina 2019-11-01 19:13:56 +01:00
parent 871cc40b41
commit fa1749ec17
5 changed files with 86 additions and 41 deletions

View File

@ -23,12 +23,21 @@
## Technical Issues, with a balanced priority
-[x] item names are hardcoded
-[ ] items are identified by the name in a hardcoded way
-[x] items are identified by the name in a hardcoded way
-[ ] nested logic
-[x] long method
-[ ] plain logic
-[ ] complex operations
-[ ] Item properties are public
-[x] multiple access by index
## Refactoring actions
-[x] extract hardcoded variables
-[x] create polymorphism for items
-[x] move item names as item members
-[x] extract methods
-[x] extract boolean methods
-[x] encapsulate methods
-[ ] isolate specific logics
-[ ] move specific logic to backstage
-[x] replace items by specific objects in tests

View File

@ -15,54 +15,54 @@ class GildedRose {
public void updateQuality() {
for (int i = 0; i < items.length; i++) {
if (!items[i].name.equals(Brie.BRIE)
&& !items[i].name.equals(Backstage.BACKSTAGE)) {
if (items[i].quality > 0) {
if (!items[i].name.equals(Sulfuras.SULFURAS)) {
items[i].quality = items[i].quality - 1;
}
final Item item = items[i];
if (!item.name.equals(Brie.BRIE) && !item.name.equals(Backstage.BACKSTAGE)) {
if (item.quality > 0 && !item.name.equals(Sulfuras.SULFURAS)) {
decreaseQualityByOne(item);
}
} else {
if (items[i].quality < 50) {
items[i].quality = items[i].quality + 1;
if (items[i].name.equals(Backstage.BACKSTAGE)) {
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;
}
}
}
}
else {
if (item.quality < 50) {
increaseQuality(item);
}
}
if (!items[i].name.equals(Sulfuras.SULFURAS)) {
items[i].sellIn = items[i].sellIn - 1;
}
updateSellIn(item);
if (items[i].sellIn < 0) {
if (!items[i].name.equals(Brie.BRIE)) {
if (!items[i].name.equals(Backstage.BACKSTAGE)) {
if (items[i].quality > 0) {
if (!items[i].name.equals(Sulfuras.SULFURAS)) {
items[i].quality = items[i].quality - 1;
}
}
} else {
items[i].quality = items[i].quality - items[i].quality;
if (item.sellIn < 0) {
if (item.name.equals(Brie.BRIE)) {
if (item.quality < 50) {
item.increaseQuality();
}
} else {
if (items[i].quality < 50) {
items[i].quality = items[i].quality + 1;
if (!item.name.equals(Backstage.BACKSTAGE)) {
if (!item.name.equals(Sulfuras.SULFURAS) && item.quality > 0) {
decreaseQualityByOne(item);
}
} else {
item.quality = 0;
}
}
}
}
}
private void decreaseQualityByOne(Item item) {
item.decreaseQuality();
}
private void increaseQuality(Item item) {
item.increaseQuality();
if (item.name.equals(Backstage.BACKSTAGE)) {
item.increaseBackstageQuality();
}
}
private void updateSellIn(Item item) {
if (!item.name.equals(Sulfuras.SULFURAS)) {
item.sellIn = item.sellIn - 1;
}
}
}

View File

@ -18,4 +18,22 @@ public class Item {
public String toString() {
return this.name + ", " + this.sellIn + ", " + this.quality;
}
public void decreaseQuality() {
this.quality = this.quality - 1;
}
public void increaseQuality() {
this.quality = this.quality + 1;
}
public void increaseBackstageQuality() {
if (this.sellIn < 11 && this.quality < 50) {
this.increaseQuality();
}
if (this.sellIn < 6 && this.quality < 50) {
this.increaseQuality();
}
}
}

View File

@ -7,4 +7,10 @@ public class Sulfuras extends Item {
public Sulfuras(int sellIn, int quality) {
super(SULFURAS, sellIn, quality);
}
/*@Override
public void increaseQuality() {
super.increaseQuality();
}*/
}

View File

@ -1,10 +1,22 @@
package com.gildedrose;
import com.gildedrose.item.Backstage;
import com.gildedrose.item.Brie;
import com.gildedrose.item.Item;
import com.gildedrose.item.Sulfuras;
public class TestHelper {
static Item getItem(String name, Integer sellIn, Integer quality){
return new Item(name, sellIn, quality);
switch (name){
case "Backstage passes to a TAFKAL80ETC concert":
return new Backstage(sellIn, quality);
case "Aged Brie":
return new Brie(sellIn, quality);
case "Sulfuras, Hand of Ragnaros":
return new Sulfuras(sellIn, quality);
default:
return new Item(name, sellIn, quality);
}
}
}