mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 22:41:30 +00:00
Refactoring UpdateQuality function to make it easier to read and modify
This commit is contained in:
parent
137b503798
commit
3b309a199f
@ -2,8 +2,20 @@
|
|||||||
|
|
||||||
namespace csharpcore
|
namespace csharpcore
|
||||||
{
|
{
|
||||||
|
public class GildedRoseConstants
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
public class GildedRose
|
public class GildedRose
|
||||||
{
|
{
|
||||||
|
public const int MIN_QUALITY = 0;
|
||||||
|
public const int MAX_QUALITY = 50;
|
||||||
|
|
||||||
|
public const string SULFURA_ITEM = "sulfura";
|
||||||
|
public const string AGED_BRIE_ITEM = "aged brie";
|
||||||
|
public const string BACKSTAGE_PASS_ITEM = "backstage pass";
|
||||||
|
|
||||||
|
|
||||||
IList<Item> Items;
|
IList<Item> Items;
|
||||||
public GildedRose(IList<Item> Items)
|
public GildedRose(IList<Item> Items)
|
||||||
{
|
{
|
||||||
@ -12,78 +24,79 @@ namespace csharpcore
|
|||||||
|
|
||||||
public void UpdateQuality()
|
public void UpdateQuality()
|
||||||
{
|
{
|
||||||
for (var i = 0; i < Items.Count; i++)
|
foreach (var item in Items)
|
||||||
{
|
{
|
||||||
if (Items[i].Name != "Aged Brie" && Items[i].Name != "Backstage passes to a TAFKAL80ETC concert")
|
var lowerCaseItemName = item.Name.ToLower();
|
||||||
{
|
if (lowerCaseItemName.Contains(SULFURA_ITEM))
|
||||||
if (Items[i].Quality > 0)
|
HandleSulfuraItem(item);
|
||||||
{
|
|
||||||
if (Items[i].Name != "Sulfuras, Hand of Ragnaros")
|
|
||||||
{
|
|
||||||
Items[i].Quality = Items[i].Quality - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (Items[i].Quality < 50)
|
item.SellIn -= 1;
|
||||||
|
if (lowerCaseItemName.Contains(AGED_BRIE_ITEM))
|
||||||
{
|
{
|
||||||
Items[i].Quality = Items[i].Quality + 1;
|
HandleAgedBrieItem(item);
|
||||||
|
|
||||||
if (Items[i].Name == "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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
else if (lowerCaseItemName.Contains(BACKSTAGE_PASS_ITEM))
|
||||||
|
|
||||||
if (Items[i].Name != "Sulfuras, Hand of Ragnaros")
|
|
||||||
{
|
|
||||||
Items[i].SellIn = Items[i].SellIn - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Items[i].SellIn < 0)
|
|
||||||
{
|
|
||||||
if (Items[i].Name != "Aged Brie")
|
|
||||||
{
|
{
|
||||||
if (Items[i].Name != "Backstage passes to a TAFKAL80ETC concert")
|
HandleBackstagePassItem(item);
|
||||||
{
|
|
||||||
if (Items[i].Quality > 0)
|
|
||||||
{
|
|
||||||
if (Items[i].Name != "Sulfuras, Hand of Ragnaros")
|
|
||||||
{
|
|
||||||
Items[i].Quality = Items[i].Quality - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Items[i].Quality = Items[i].Quality - Items[i].Quality;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (Items[i].Quality < 50)
|
HandleMiscelaniousItem(item);
|
||||||
{
|
|
||||||
Items[i].Quality = Items[i].Quality + 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void HandleSulfuraItem(Item item)
|
||||||
|
{
|
||||||
|
// We do not alter anything on Sulfura items !
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HandleAgedBrieItem(Item item)
|
||||||
|
{
|
||||||
|
if (item.SellIn > 0)
|
||||||
|
UpdateItemQualityValue(item, 1);
|
||||||
|
|
||||||
|
else
|
||||||
|
UpdateItemQualityValue(item, 2); // Is it correct ? This is to reflect old code but seems to not be in the spec
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HandleBackstagePassItem(Item item)
|
||||||
|
{
|
||||||
|
if (item.SellIn >= 10)
|
||||||
|
UpdateItemQualityValue(item, 1);
|
||||||
|
|
||||||
|
else if (item.SellIn >= 5)
|
||||||
|
UpdateItemQualityValue(item, 2);
|
||||||
|
|
||||||
|
else if (item.SellIn > 0)
|
||||||
|
UpdateItemQualityValue(item, 3);
|
||||||
|
|
||||||
|
else
|
||||||
|
item.Quality = MIN_QUALITY;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HandleMiscelaniousItem(Item item)
|
||||||
|
{
|
||||||
|
if (item.SellIn > 0)
|
||||||
|
UpdateItemQualityValue(item, -1);
|
||||||
|
|
||||||
|
else
|
||||||
|
UpdateItemQualityValue(item, -2);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateItemQualityValue(Item item, int qualityStep)
|
||||||
|
{
|
||||||
|
item.Quality += qualityStep;
|
||||||
|
if (item.Quality < MIN_QUALITY)
|
||||||
|
item.Quality = MIN_QUALITY;
|
||||||
|
|
||||||
|
if (item.Quality > MAX_QUALITY)
|
||||||
|
item.Quality = MAX_QUALITY;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -72,7 +72,7 @@ namespace csharpcore
|
|||||||
app.UpdateQuality();
|
app.UpdateQuality();
|
||||||
|
|
||||||
Assert.Equal(afterQuality, items[0].Quality);
|
Assert.Equal(afterQuality, items[0].Quality);
|
||||||
Assert.Equal(beforeSellIn-1, items[0].SellIn);
|
Assert.Equal(beforeSellIn - 1, items[0].SellIn);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user