From dc831d926a1810ecdef2cd50f4c753faae62e3a9 Mon Sep 17 00:00:00 2001 From: Kristian Date: Fri, 12 Jan 2024 13:41:48 +0100 Subject: [PATCH] Solution --- domain-model.md | 6 +- tdd-domain-modelling.CSharp.Main/Basket.cs | 35 ++++++++ .../BasketTest.cs | 88 +++++++++++++++++++ 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 tdd-domain-modelling.CSharp.Main/Basket.cs create mode 100644 tdd-domain-modelling.CSharp.Test/BasketTest.cs diff --git a/domain-model.md b/domain-model.md index 653f474..81b9b93 100644 --- a/domain-model.md +++ b/domain-model.md @@ -1 +1,5 @@ -#Domain Models In Here \ No newline at end of file +#Domain Models In Here +| Classes | Methods | Scenario | Outputs | +|-----------------|-----------------------|----------------------------|----------| +| `Cart` | `total()` | Return total value of cart | double | +| | `receipt()` | Return detailed receipt | string | \ No newline at end of file diff --git a/tdd-domain-modelling.CSharp.Main/Basket.cs b/tdd-domain-modelling.CSharp.Main/Basket.cs new file mode 100644 index 0000000..6d40cf7 --- /dev/null +++ b/tdd-domain-modelling.CSharp.Main/Basket.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace tdd_domain_modelling.CSharp.Main +{ + public class Basket + { + private Dictionary _prices = new Dictionary(); + private List _items = new List(); + + public List Items { get { return _items; } } + public Boolean Add(string product, int price) + { + if (!_items.Contains(product)) + { + _items.Add(product); + _prices.Add(product, price); + return true; + } + else + { + { _items.Add(product); return false; } + } + + } + + public int Total() + { + return _items.Select(p => _prices[p]).ToArray().Sum(); + } + } +} diff --git a/tdd-domain-modelling.CSharp.Test/BasketTest.cs b/tdd-domain-modelling.CSharp.Test/BasketTest.cs new file mode 100644 index 0000000..e83a2e1 --- /dev/null +++ b/tdd-domain-modelling.CSharp.Test/BasketTest.cs @@ -0,0 +1,88 @@ +using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using NUnit.Framework; +using tdd_domain_modelling.CSharp.Main; +using System.Xml.Linq; + +namespace tdd_domain_modelling.CSharp.Test +{ + public class BasketTest + { + [Test] + public void ShouldNotBeEmpty() + { + // 1. Set up + Basket basket = new Basket(); + + // 2. Execute + basket.Add("Milk", 5); + + // 3. Verify + Assert.That(basket.Items.Count > 0); + } + + [Test] + public void ShouldReturnTrue() + { + // 1. Set up + Basket basket = new Basket(); + + // 2. Execute + Boolean added = basket.Add("Milk", 5); + + // 3. Verify + Assert.That(added); + } + + [Test] + public void ShouldReturnFalse() + { + // 1. Set up + Basket basket = new Basket(); + + // 2. Execute + basket.Add("Milk", 5); + Boolean added = basket.Add("Milk", 5); + + // 3. Verify + Assert.That(!added); + } + + [Test] + public void ShouldReturnTotal() + { + // 1. Set up + Basket basket = new Basket(); + + // 2. Execute + basket.Add("Milk", 5); + basket.Add("Sugar", 7); + basket.Add("Egg", 4); + + // 3. Verify + Assert.That(basket.Total() == 16); + } + [Test] + public void ShouldReturnTotalDuplicate() + { + // 1. Set up + Basket basket = new Basket(); + + // 2. + basket.Add("Sugar", 7); + basket.Add("Milk", 5); + basket.Add("Milk", 5); + basket.Add("Sugar", 7); + basket.Add("Egg", 4); + + // 3. Verify + Assert.That(basket.Total() == 28); + } + + } +}