GildedRose-Refactoring-Kata/csharp/GildedRose.cs
@arik 68ee4cc4cd
Update GildedRose.cs
In this refactored version, I've made the following changes:

Changed the name of the private member variable to use camelCase naming convention and added a "readonly" keyword to make it immutable.
Changed the loop to use a "foreach" loop instead of a "for" loop, which is more idiomatic in C#.
Extracted the code for updating an individual item's quality into a separate private method to improve readability and reduce duplication.
Removed unnecessary nested "if" statements by using "else if" and "else" statements.
Extracted the code for increasing or decreasing an item's quality into separate private methods to improve readability and reduce duplication.
Moved the logic for updating an item's sell-in value into a separate private method to improve readability and reduce duplication.
2023-04-24 02:00:56 +03:00

88 lines
2.1 KiB
C#

using System.Collections.Generic;
namespace csharp
{
public class GildedRose
{
private readonly IList<Item> _items;
public GildedRose(IList<Item> items)
{
_items = items;
}
public void UpdateQuality()
{
foreach (var item in _items)
{
UpdateItemQuality(item);
UpdateItemSellIn(item);
}
}
private void UpdateItemQuality(Item item)
{
if (item.Name == "Aged Brie")
{
IncreaseQuality(item);
if (item.SellIn < 0)
{
IncreaseQuality(item);
}
}
else if (item.Name == "Backstage passes to a TAFKAL80ETC concert")
{
IncreaseQuality(item);
if (item.SellIn < 11)
{
IncreaseQuality(item);
}
if (item.SellIn < 6)
{
IncreaseQuality(item);
}
if (item.SellIn < 0)
{
item.Quality = 0;
}
}
else if (item.Name == "Sulfuras, Hand of Ragnaros")
{
// Do nothing, the item's quality and sell-in never change.
}
else
{
DecreaseQuality(item);
if (item.SellIn < 0)
{
DecreaseQuality(item);
}
}
}
private void IncreaseQuality(Item item)
{
if (item.Quality < 50)
{
item.Quality++;
}
}
private void DecreaseQuality(Item item)
{
if (item.Quality > 0)
{
item.Quality--;
}
}
private void UpdateItemSellIn(Item item)
{
if (item.Name != "Sulfuras, Hand of Ragnaros")
{
item.SellIn--;
}
}
}
}