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.
This commit is contained in:
@arik 2023-04-24 02:00:56 +03:00 committed by GitHub
parent 5a4e92199b
commit 68ee4cc4cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,88 +1,86 @@
using System.Collections.Generic;
using System.Collections.Generic;
namespace csharp
{
public class GildedRose
{
IList<Item> Items;
public GildedRose(IList<Item> Items)
private readonly IList<Item> _items;
public GildedRose(IList<Item> items)
{
this.Items = Items;
_items = items;
}
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")
{
if (Items[i].Quality > 0)
{
if (Items[i].Name != "Sulfuras, Hand of Ragnaros")
{
Items[i].Quality = Items[i].Quality - 1;
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
{
if (Items[i].Quality < 50)
DecreaseQuality(item);
if (item.SellIn < 0)
{
Items[i].Quality = Items[i].Quality + 1;
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;
}
}
DecreaseQuality(item);
}
}
}
if (Items[i].Name != "Sulfuras, Hand of Ragnaros")
private void IncreaseQuality(Item item)
{
Items[i].SellIn = Items[i].SellIn - 1;
if (item.Quality < 50)
{
item.Quality++;
}
}
if (Items[i].SellIn < 0)
private void DecreaseQuality(Item item)
{
if (Items[i].Name != "Aged Brie")
if (item.Quality > 0)
{
if (Items[i].Name != "Backstage passes to a TAFKAL80ETC concert")
item.Quality--;
}
}
private void UpdateItemSellIn(Item item)
{
if (Items[i].Quality > 0)
if (item.Name != "Sulfuras, Hand of Ragnaros")
{
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
{
if (Items[i].Quality < 50)
{
Items[i].Quality = Items[i].Quality + 1;
}
}
}
item.SellIn--;
}
}
}