DailyItemUpdate: extract increase/decrease quality into a separate method

This commit is contained in:
Sarah Ashri 2024-03-13 14:04:40 +10:00
parent c85ce91a08
commit 3d28a742b5
2 changed files with 29 additions and 34 deletions

View File

@ -30,45 +30,42 @@ public class GildedRose
private static bool IsRegularItem(Item item) => (!IsLegendaryItem(item) &&
!IsBackstagePassesItem(item) &&
!IsBetterWithAgeItem(item));
private static void IncreaseQuality(Item item, int byValue)
{
item.Quality = int.Min(item.Quality + byValue, MaxQuality);
}
private static void DecreaseQuality(Item item, int byValue)
{
item.Quality = int.Max(item.Quality - byValue, MinQuality);
}
private void DailyItemUpdate(Item item)
{
if (IsLegendaryItem(item)) return;
if (IsRegularItem(item))
{
if (item.Quality > MinQuality)
{
item.Quality = item.Quality - 1;
}
DecreaseQuality(item, 1);
}
if(IsBetterWithAgeItem(item) || IsBackstagePassesItem(item))
{
if (item.Quality < MaxQuality)
IncreaseQuality(item, 1);
if (IsBackstagePassesItem(item))
{
item.Quality = item.Quality + 1;
if (IsBackstagePassesItem(item))
if (item.SellIn < 11)
{
if (item.SellIn < 11)
{
if (item.Quality < MaxQuality)
{
item.Quality = item.Quality + 1;
}
}
IncreaseQuality(item, 1);
}
if (item.SellIn < 6)
{
if (item.Quality < MaxQuality)
{
item.Quality = item.Quality + 1;
}
}
if (item.SellIn < 6)
{
IncreaseQuality(item, 1);
}
}
}
@ -79,23 +76,17 @@ public class GildedRose
{
if (IsRegularItem(item))
{
if (item.Quality > MinQuality)
{
item.Quality = item.Quality - 1;
}
DecreaseQuality(item, 1);
}
if(IsBackstagePassesItem(item))
{
item.Quality = item.Quality - item.Quality;
DecreaseQuality(item, item.Quality);
}
if(IsBetterWithAgeItem(item))
{
if (item.Quality < MaxQuality)
{
item.Quality = item.Quality + 1;
}
IncreaseQuality(item, 1);
}
}
}

View File

@ -11,6 +11,10 @@ NOTES:
Some of the tests failed due to bugs I discovered in the code/existing tests (e.g. relying on exact name instead of the mentioned "code word").
I decorated these tests with `[Ignore]` until I get to a stage in the refactoring where I can easily fix the issues and re-introduce the tests.
2. small refactorings of the UpdateQuality() method to make it more readable and separate the different (unrelated) handling of the different types.<br>
3. Extract Increasing/Decreasing Quality into a method.<br>
It feels like Quality should have been a TinyType that manages its own limits. However, since we're not allowed to change Item, I'm just extracting a method.
This is the state of the code now