From 0b10ce5f989635577c9ed6c1e3ff4215ed0fcc21 Mon Sep 17 00:00:00 2001 From: kenny6789 <30763481+kenny6789@users.noreply.github.com> Date: Tue, 20 Oct 2020 20:25:17 +1300 Subject: [PATCH 1/2] Update Till.cs I have rewrote the code of addB to DiscountItems and updated AddItemC. Also, in Total() function, I have added a variable 'noOfItemsC' to keep track the maximum number of ItemC. --- Checkout/Till.cs | 76 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 19 deletions(-) diff --git a/Checkout/Till.cs b/Checkout/Till.cs index 984c179..784c4eb 100644 --- a/Checkout/Till.cs +++ b/Checkout/Till.cs @@ -16,19 +16,17 @@ public class Till public double Total() { double total = 0; + double noOfItemsC = 0; foreach(var item in _items) { - if(item.Key.Equals('A')) + if(item.Key.Equals('A') || item.Key.Equals('B')) { - total += 50 * item.Value; + //total += 50 * item.Value; + total += DiscountItems(item.Value.ToString(), item) } - else if(item.Key.Equals('B')) - { - total += AddB(item.Value.ToString()); - } else if(item.Key.Equals('C')) { - total = AddItemC(total, item); + total = AddItemC(total, item, noOfItemsC); } else total = AddItemD(total, item); } @@ -45,30 +43,70 @@ static double AddItemD(double total, KeyValuePair item) return total; } - - private static double AddItemC(double total, KeyValuePair item) + private static double AddItemC(double total, KeyValuePair item, double noOfItemsC) { - if (item.Key.Equals('C')) + if(noOfItemsC <= 6) { total += 15 * item.Value; } - return total; } - public double AddB(string numberItems) + + public double DiscountItems(string numberItems, KeyValuePair nameItems) { double items = Double.Parse(numberItems); - + var cost = 0 if(items == 0) return 0; - var cost = items * 30; - var numberOfPairs = items / 2; + if(nameItems.Key.Equals(‘A’)) + { + cost = items * 50; + var itemADiscount = items / 3; + + //discount is 20 on every three items + var discount = itemADiscount * 20 + cost -= discount + } + + else if(nameItems.Key.Equals(‘B’)) + { + cost = items * 30; + var numberOfPairs = items / 2; + + //discount is 15 on each pair + var discount = numberOfPairs * 15 + cost -= discount + } + + return cost - // discount is 15 on each pair - var discount = numberOfPairs * 15; - return cost - discount; } + //This is your original code +// private static double AddItemC(double total, KeyValuePair item) +// { +// if (item.Key.Equals('C')) +// { +// total += 15 * item.Value; +// } + +// return total; +// } + +// public double AddB(string numberItems) +// { +// double items = Double.Parse(numberItems); + +// if(items == 0) return 0; + +// var cost = items * 30; +// var numberOfPairs = items / 2; + +// // discount is 15 on each pair +// var discount = numberOfPairs * 15; +// return cost - discount; +// } + public void Scan(string items) { @@ -78,4 +116,4 @@ public void Scan(string items) } } } -} \ No newline at end of file +} From 92ec0c26fcde274060ae8e40f91118b3599b20c5 Mon Sep 17 00:00:00 2001 From: kenny6789 <30763481+kenny6789@users.noreply.github.com> Date: Tue, 20 Oct 2020 20:40:13 +1300 Subject: [PATCH 2/2] Update Till.cs --- Checkout/Till.cs | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/Checkout/Till.cs b/Checkout/Till.cs index 784c4eb..1e7624f 100644 --- a/Checkout/Till.cs +++ b/Checkout/Till.cs @@ -80,33 +80,7 @@ public double DiscountItems(string numberItems, KeyValuePair nameItem } return cost - } - //This is your original code -// private static double AddItemC(double total, KeyValuePair item) -// { -// if (item.Key.Equals('C')) -// { -// total += 15 * item.Value; -// } - -// return total; -// } - -// public double AddB(string numberItems) -// { -// double items = Double.Parse(numberItems); - -// if(items == 0) return 0; - -// var cost = items * 30; -// var numberOfPairs = items / 2; - -// // discount is 15 on each pair -// var discount = numberOfPairs * 15; -// return cost - discount; -// } - public void Scan(string items) {