diff --git a/csharpcore/GildedRose/DailyUpdater.cs b/csharpcore/GildedRose/DailyUpdater.cs index a9a4aec3..78c862fc 100644 --- a/csharpcore/GildedRose/DailyUpdater.cs +++ b/csharpcore/GildedRose/DailyUpdater.cs @@ -4,13 +4,17 @@ namespace GildedRoseKata; public abstract class DailyUpdater { - protected const int MinQuality = 0; - protected const int MaxQuality = 50; + private const int MinQuality = 0; + private const int MaxQuality = 50; public void DailyUpdate(Item item) { UpdateSellIn(item); UpdateQuality(item); + if (IsExpired(item)) + { + UpdateQuality(item); + } } public abstract void UpdateQuality(Item item); @@ -18,28 +22,22 @@ public abstract class DailyUpdater protected static bool IsExpired(Item item) => item.SellIn < 0; - protected static void IncreaseQuality(Item item, int byValue) + protected static void IncreaseQuality(Item item, int byValue = 1) { item.Quality = int.Min(item.Quality + byValue, MaxQuality); } - protected static void DecreaseQuality(Item item, int byValue) + protected static void DecreaseQuality(Item item, int byValue = 1) { item.Quality = int.Max(item.Quality - byValue, MinQuality); } - - } public class DailyUpdaterForRegularItems : DailyUpdater { public override void UpdateQuality(Item item) { - DecreaseQuality(item, 1); - if (IsExpired(item)) - { - DecreaseQuality(item, 1); - } + DecreaseQuality(item); } } @@ -47,11 +45,7 @@ public class DailyUpdaterForBetterWithAgeItems : DailyUpdater { public override void UpdateQuality(Item item) { - IncreaseQuality(item, 1); - if (IsExpired(item)) - { - IncreaseQuality(item, 1); - } + IncreaseQuality(item); } } @@ -62,7 +56,7 @@ public class DailyUpdaterForBackstagePassesItems : DailyUpdater { if (item.SellIn > 9) { - IncreaseQuality(item, 1); + IncreaseQuality(item); } else if (item.SellIn > 4) { diff --git a/csharpcore/GildedRose/DailyUpdaterFactory.cs b/csharpcore/GildedRose/DailyUpdaterFactory.cs new file mode 100644 index 00000000..75077d3b --- /dev/null +++ b/csharpcore/GildedRose/DailyUpdaterFactory.cs @@ -0,0 +1,31 @@ +namespace GildedRoseKata; + +public static class DailyUpdaterFactory +{ + public static DailyUpdater GetDailyUpdater(Item item) + { + if (IsLegendaryItem(item)) + { + return new DailyUpdaterForLegendaryItems(); + } + + if (IsBetterWithAgeItem(item)) + { + return new DailyUpdaterForBetterWithAgeItems(); + } + + if (IsBackstagePassesItem(item)) + { + return new DailyUpdaterForBackstagePassesItems(); + } + + return new DailyUpdaterForRegularItems(); + } + + private static bool IsLegendaryItem(Item item) => item.Name.ToLower().Contains("sulfuras"); + + private static bool IsBackstagePassesItem(Item item) => item.Name.ToLower().Contains("backstage passes"); + + private static bool IsBetterWithAgeItem(Item item) => item.Name.ToLower().Equals("aged brie"); + +} \ No newline at end of file diff --git a/csharpcore/GildedRose/GildedRose.cs b/csharpcore/GildedRose/GildedRose.cs index 7abeccb5..d89324b3 100644 --- a/csharpcore/GildedRose/GildedRose.cs +++ b/csharpcore/GildedRose/GildedRose.cs @@ -5,8 +5,6 @@ namespace GildedRoseKata; public class GildedRose { private readonly IList _items; - const int MinQuality = 0; - const int MaxQuality = 50; public GildedRose(IList items) { @@ -17,34 +15,9 @@ public class GildedRose { foreach (var item in _items) { - DailyUpdater dailyUpdater = getDailyUpdater(item); + DailyUpdater dailyUpdater = DailyUpdaterFactory.GetDailyUpdater(item); dailyUpdater.DailyUpdate(item); } } - - private static bool IsLegendaryItem(Item item) => item.Name.ToLower().Contains("sulfuras"); - - private static bool IsBackstagePassesItem(Item item) => item.Name.ToLower().Contains("backstage passes"); - - private static bool IsBetterWithAgeItem(Item item) => item.Name.ToLower().Equals("aged brie"); - private DailyUpdater getDailyUpdater(Item item) - { - if (IsLegendaryItem(item)) - { - return new DailyUpdaterForLegendaryItems(); - } - - if (IsBetterWithAgeItem(item)) - { - return new DailyUpdaterForBetterWithAgeItems(); - } - - if (IsBackstagePassesItem(item)) - { - return new DailyUpdaterForBackstagePassesItems(); - } - - return new DailyUpdaterForRegularItems(); - } } \ No newline at end of file diff --git a/csharpcore/GildedRose/RefactoringDiary.md b/csharpcore/GildedRose/RefactoringDiary.md index a70da823..750f821c 100644 --- a/csharpcore/GildedRose/RefactoringDiary.md +++ b/csharpcore/GildedRose/RefactoringDiary.md @@ -16,7 +16,9 @@ I decorated these tests with `[Ignore]` until I get to a stage in the refactorin 5. Used the `Template` design pattern to create DailyUpdater abstract class and derived classes to handle the DailyUpdate's steps for the different types of items.
The different derived classes will be moved to their own files in the next PR. Left them together for now to show the steps. 6. Created a `Simple Factory` method to return the appropriate DailyUpdater object for the item type.
-For this PR, I left this method in the GildedRose.cp to show the steps. But in the next PR I'll move it into its own class. +7. Moved DailyUpdaterFactory to its own class. +8. Moved the logic of "Once the sell by date has passed, Quality degrades twice as fast" into the DailyUpdater Template class since it's relevant to all types of items. +9. Moved Decrease/IncreaseItem byValue to be 1 by default. This is the state of the code now