mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-16 06:51:27 +00:00
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:
parent
5a4e92199b
commit
68ee4cc4cd
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Items[i].Quality < 50)
|
||||
{
|
||||
Items[i].Quality = Items[i].Quality + 1;
|
||||
UpdateItemQuality(item);
|
||||
UpdateItemSellIn(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Items[i].Name != "Sulfuras, Hand of Ragnaros")
|
||||
private void UpdateItemQuality(Item item)
|
||||
{
|
||||
if (item.Name == "Aged Brie")
|
||||
{
|
||||
IncreaseQuality(item);
|
||||
if (item.SellIn < 0)
|
||||
{
|
||||
Items[i].SellIn = Items[i].SellIn - 1;
|
||||
IncreaseQuality(item);
|
||||
}
|
||||
|
||||
if (Items[i].SellIn < 0)
|
||||
}
|
||||
else if (item.Name == "Backstage passes to a TAFKAL80ETC concert")
|
||||
{
|
||||
IncreaseQuality(item);
|
||||
if (item.SellIn < 11)
|
||||
{
|
||||
if (Items[i].Name != "Aged Brie")
|
||||
{
|
||||
if (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;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Items[i].Quality = Items[i].Quality - Items[i].Quality;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Items[i].Quality < 50)
|
||||
{
|
||||
Items[i].Quality = Items[i].Quality + 1;
|
||||
}
|
||||
}
|
||||
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--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user