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

@ -32,6 +32,15 @@ public class GildedRose
!IsBetterWithAgeItem(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) private void DailyItemUpdate(Item item)
@ -40,35 +49,23 @@ public class GildedRose
if (IsRegularItem(item)) if (IsRegularItem(item))
{ {
if (item.Quality > MinQuality) DecreaseQuality(item, 1);
{
item.Quality = item.Quality - 1;
}
} }
if(IsBetterWithAgeItem(item) || IsBackstagePassesItem(item)) if(IsBetterWithAgeItem(item) || IsBackstagePassesItem(item))
{ {
if (item.Quality < MaxQuality) IncreaseQuality(item, 1);
if (IsBackstagePassesItem(item))
{ {
item.Quality = item.Quality + 1; if (item.SellIn < 11)
if (IsBackstagePassesItem(item))
{ {
if (item.SellIn < 11) IncreaseQuality(item, 1);
{ }
if (item.Quality < MaxQuality)
{
item.Quality = item.Quality + 1;
}
}
if (item.SellIn < 6) if (item.SellIn < 6)
{ {
if (item.Quality < MaxQuality) IncreaseQuality(item, 1);
{
item.Quality = item.Quality + 1;
}
}
} }
} }
} }
@ -79,23 +76,17 @@ public class GildedRose
{ {
if (IsRegularItem(item)) if (IsRegularItem(item))
{ {
if (item.Quality > MinQuality) DecreaseQuality(item, 1);
{
item.Quality = item.Quality - 1;
}
} }
if(IsBackstagePassesItem(item)) if(IsBackstagePassesItem(item))
{ {
item.Quality = item.Quality - item.Quality; DecreaseQuality(item, item.Quality);
} }
if(IsBetterWithAgeItem(item)) if(IsBetterWithAgeItem(item))
{ {
if (item.Quality < MaxQuality) IncreaseQuality(item, 1);
{
item.Quality = item.Quality + 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"). 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. 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> 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 This is the state of the code now