From 9aa91618e823327c0f5679dd71b9fa57f58518e9 Mon Sep 17 00:00:00 2001 From: Pankaj Gupta Date: Thu, 7 Mar 2024 15:27:17 -0600 Subject: [PATCH] 1. Added Baked items in the UpdateQuality and added unit tests 2. Added notes for increasing the code readability 3. Added notes for adding two patterns AbstractFactory and builder so that code can refactored and maintained long term and it can handle more item types --- csharpcore60/ShiMart/ShiMart.cs | 27 ++++++++++++++++++++++++--- csharpcore60/ShiMartTest/UnitTest1.cs | 24 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/csharpcore60/ShiMart/ShiMart.cs b/csharpcore60/ShiMart/ShiMart.cs index 353655d..b2821cc 100644 --- a/csharpcore60/ShiMart/ShiMart.cs +++ b/csharpcore60/ShiMart/ShiMart.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; + +using System.Collections.Generic; namespace ShiMartKata { @@ -10,6 +11,17 @@ public ShiMart(IList Items) this.Items = Items; } + // TODO:Increase code readbility + // 1. Reduce if conditions + // 2. Refacor code in more methods + // 3. Refactor SellIn logic in a new method : UpdateSellIn. Note: QualityCalulation is dependent on SellIn Value + + + // Make the program item independent + // 1. Item.cs in a class ItemWithType.cs, inherits Item.cs + // 2. Use Builder pattern for BuildingItem + // 3. It requires creation of centeraliztion of multiple type of items and handle related busisness logic, + // I would prefer, use Abstarct Factory Pattern for different type of items public void UpdateQuality() { for (var i = 0; i < Items.Count; i++) @@ -30,10 +42,15 @@ public void UpdateQuality() { if (Items[i].Quality > 0) { - if (Items[i].Name != "Canned Beans") + if (Items[i].Name != "Canned Beans" && !Items[i].Name.Contains("Baked")) { Items[i].Quality = Items[i].Quality - 1; } + + if (Items[i].Name.Contains("Baked")) + { + Items[i].Quality = Items[i].Quality - 2; + } } } @@ -50,10 +67,14 @@ public void UpdateQuality() { if (Items[i].Quality > 0) { - if (Items[i].Name != "Canned Beans") + if (Items[i].Name != "Canned Beans" && !Items[i].Name.Contains("Baked")) { Items[i].Quality = Items[i].Quality - 1; } + if (Items[i].Name.Contains("Baked")) + { + Items[i].Quality = Items[i].Quality - 2; + } } } } diff --git a/csharpcore60/ShiMartTest/UnitTest1.cs b/csharpcore60/ShiMartTest/UnitTest1.cs index 703c1ec..3b15e29 100644 --- a/csharpcore60/ShiMartTest/UnitTest1.cs +++ b/csharpcore60/ShiMartTest/UnitTest1.cs @@ -18,5 +18,29 @@ public void Test1() Assert.Equal(9, items.First().SellIn); Assert.Equal(19, items.First().Quality); } + + [Fact] + public void VerifyBakedItem() + { + var items = new List { new Item { Name = "Baked Sourdough Bread", SellIn = 10, Quality = 20 } }; + var app = new ShiMart(items); + + app.UpdateQuality(); + + Assert.Equal(9, items.First().SellIn); + Assert.Equal(18, items.First().Quality); + } + + [Fact] + public void VerifyBakedItemAfterExpiry() + { + var items = new List { new Item { Name = "Baked Sourdough Bread", SellIn = -1, Quality = 20 } }; + var app = new ShiMart(items); + + app.UpdateQuality(); + + Assert.Equal(-2, items.First().SellIn); + Assert.Equal(16, items.First().Quality); + } } }