From d8e575a45687c285f30557ee7e6faee167f84b9b Mon Sep 17 00:00:00 2001 From: Joaquin Lindkvist Date: Wed, 14 Aug 2024 12:50:46 +0200 Subject: [PATCH] Completed the test exercises according to the tasks in #readme1 and #readme2 --- domain-model.md | 17 ++++- tdd-domain-modelling.CSharp.Main/Basket.cs | 34 ++++++++++ .../SupermarketTests.cs | 66 +++++++++++++++++++ 3 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 tdd-domain-modelling.CSharp.Main/Basket.cs create mode 100644 tdd-domain-modelling.CSharp.Test/SupermarketTests.cs diff --git a/domain-model.md b/domain-model.md index 653f474..d01a385 100644 --- a/domain-model.md +++ b/domain-model.md @@ -1 +1,16 @@ -#Domain Models In Here \ No newline at end of file +#Domain Models In Here + +| Classes | Methods | Scenario | Outputs | +|-----------------|---------------------------------------------|------------------------------------------------------|---------------------------------------------------------| +| `Basket` | `Add(string name, int price)` | If item doesn't exist, add to the list of items | add item to Tuple | +| | | If item does exist, update the quantity of item item | update the quantity of similar item in Tuple | +| | `Total()` | If basket is empty, return 0 | 0 | +| | | If basket has items, calculate the cost of all items | int sum | +| `CashRegister` | `TotalCost(Basket basket)` | Calculate the summed up cost of all items in basket | int sum | + + +| Classes | Methods | Scenario | Outputs | +|-----------------|--------------------------------|------------------------------------------------------------------------------------------------------------|----------------| +| `Basket` | `ListItems()` | If basket is empty, return empty string | string.Empty() | +| `Basket` | | If basket has items, return string listing every item's name, price, and quantity in a receipt format | string | +| `CashRegister` | `PrintReceipt(Basket basket)` | Calls ListItems() and Total() from basket to create a string for the full 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..b075cff --- /dev/null +++ b/tdd-domain-modelling.CSharp.Main/Basket.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace tdd_domain_modelling.CSharp.Main +{ + public class Basket + { + Dictionary items = new Dictionary(); + + public bool Add(string name, int price) + { + if (items.ContainsKey(name)) + { + return false; + } + items.Add(name, price); + return true; + } + + public int Total() + { + int sum = 0; + foreach (var item in items) + { + sum += item.Value; + } + return sum; + } + } +} diff --git a/tdd-domain-modelling.CSharp.Test/SupermarketTests.cs b/tdd-domain-modelling.CSharp.Test/SupermarketTests.cs new file mode 100644 index 0000000..9aa1e55 --- /dev/null +++ b/tdd-domain-modelling.CSharp.Test/SupermarketTests.cs @@ -0,0 +1,66 @@ +using NUnit.Framework; +using System.Diagnostics.CodeAnalysis; +using tdd_domain_modelling.CSharp.Main; + +namespace tdd_domain_modelling.CSharp.Test +{ + [TestFixture] + public class SupermarketTests + { + [Test] + public void AddCupboardsTests() + { + //arrange + Basket basket = new Basket(); + string name = "Cupboard"; + int price = 12; + bool expected = true; + + //act + bool result = basket.Add(name, price); + + //assert + Assert.That(result, Is.EqualTo(expected)); + } + + [Test] + public void AddDuplicateCupboardsTests() + { + //arrange + Basket basket = new Basket(); + string name = "Cupboard"; + int price = 12; + bool expected = false; + + //act + basket.Add(name, price); + bool result = basket.Add(name, price); + + //assert + Assert.That(result, Is.EqualTo((bool)expected)); + } + + [Test] + public void TotalCostTest() + { + //arrange + Basket basket = new Basket(); + string name1 = "Coffee Mug"; + int price1 = 8; + string name2 = "Cupboard"; + int price2 = 12; + string name3 = "Cat Food"; + int price3 = 20; + int expected = 40; + + //act + basket.Add(name1, price1); + basket.Add(name2, price2); + basket.Add(name3, price3); + int result = basket.Total(); + + //assert + Assert.That(result, Is.EqualTo(expected)); + } + } +} \ No newline at end of file