refactoring second pass

This commit is contained in:
OKinane 2023-03-11 15:29:19 +01:00
parent d2bf4e2f7e
commit 44908d4a89

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
namespace GildedRoseKata namespace GildedRoseKata
{ {
@ -23,60 +24,57 @@ namespace GildedRoseKata
if (Items[i].Name == "Aged Brie") if (Items[i].Name == "Aged Brie")
{ {
IncreaseQualityByOne(i); SetQuality(i, quantity => quantity + 1, quantity => quantity + 2);
if (Items[i].SellIn < 0)
{
IncreaseQualityByOne(i);
}
} }
else if (Items[i].Name == "Backstage passes to a TAFKAL80ETC concert") else if (Items[i].Name == "Backstage passes to a TAFKAL80ETC concert")
{ {
IncreaseBackstageQuality(i); SetQuality(i, quantity => GetNewBackstageQuality(i), q => 0);
if (Items[i].SellIn < 0)
{
Items[i].Quality = 0;
}
} }
else else
{ {
DecreaseQualityByOne(i); SetQuality(i, quantity => quantity - 1, quantity => quantity - 2);
if (Items[i].SellIn < 0)
{
DecreaseQualityByOne(i);
}
} }
} }
} }
private void IncreaseBackstageQuality(int i) private void SetQuality(int itemIndex, Func<int, int> beforeSellBy, Func<int, int> afterSellBy)
{ {
IncreaseQualityByOne(i); bool wasAbove50 = Items[itemIndex].Quality > 50;
if (Items[i].SellIn < 10) if (Items[itemIndex].SellIn >= 0)
{ {
IncreaseQualityByOne(i); Items[itemIndex].Quality = beforeSellBy(Items[itemIndex].Quality);
}
else
{
Items[itemIndex].Quality = afterSellBy(Items[itemIndex].Quality);
} }
if (Items[i].SellIn < 5) if (!wasAbove50 && Items[itemIndex].Quality > 50)
{ {
IncreaseQualityByOne(i); Items[itemIndex].Quality = 50;
}
if (Items[itemIndex].Quality < 0)
{
Items[itemIndex].Quality = 0;
} }
} }
private void DecreaseQualityByOne(int i) private int GetNewBackstageQuality(int itemIndex)
{ {
if (Items[i].Quality > 0) int newQuantity = Items[itemIndex].Quality;
{ ++newQuantity;
Items[i].Quality = Items[i].Quality - 1;
}
}
private void IncreaseQualityByOne(int i) if (Items[itemIndex].SellIn < 10)
{
if (Items[i].Quality < 50)
{ {
Items[i].Quality = Items[i].Quality + 1; ++newQuantity;
} }
if (Items[itemIndex].SellIn < 5)
{
++newQuantity;
}
return newQuantity;
} }
} }
} }