diff --git a/domain-model.md b/domain-model.md index 653f474..dd29bd1 100644 --- a/domain-model.md +++ b/domain-model.md @@ -1 +1,60 @@ -#Domain Models In Here \ No newline at end of file +#Domain Models In Here + +``` +As a supermarket shopper, +So that I can pay for products at checkout, +I'd like to be able to know the total cost of items in my basket. +``` + +```C# +struct Item + PROPERTIES: + public string itemName {get} + public float cost {get} + + METHODS: + public Item(string itemName, float cost) + +Class Basket + properties: + public List items {get} + + methods: + public Basket() // initialize items list + public void AddItem(string itemName, float cost) + public float TotalCost(List) return the total cost of the basket +``` + + +``` +As an organised individual, +So that I can evaluate my shopping habits, +I'd like to see an itemised receipt that includes the name and price of the products +I bought as well as the quantity, and a total cost of my basket. + +``` + +``` C# +struct Item + PROPERTIES: + public string itemName {get} + public float cost {get} + + METHODS: + public Item(string itemName, float cost) + +Class Basket + properties: + public List items {get} + + methods: + public Basket() // initialize items list + + public void AddItem(string itemName, float cost) + + public float TotalCost(List) return the total cost of the basket + + public List Receipt(List items) + returns a list with:| Name | Price | Quantity | + Last index of list | TotalCost() | +``` \ 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..fed4938 --- /dev/null +++ b/tdd-domain-modelling.CSharp.Main/Basket.cs @@ -0,0 +1,62 @@ +namespace tdd_domain_modelling.CSharp.Main +{ + public class Basket + { + public List items { get; } + + public Basket() + { + items = new List(); + } + + public void AddItem(string itemName, float price) + { + items.Add(new Item(itemName, price)); + } + + public float TotalCost() + { + float totalCost = 0; + foreach (var item in items) + { + totalCost += item.Cost; + } + return totalCost; + } + + public List Receipt() + { + List receipt = new List(); + Dictionary itemQuantity = new Dictionary(); + foreach (var item in items) + { + if (!itemQuantity.ContainsKey(item)) + { + itemQuantity.Add(item, 1); + } + else + { + itemQuantity[item]++; + } + } + + foreach (var item in itemQuantity) + { + receipt.Add($"|{item.Key.ItemName}|{item.Key.Cost}|x{item.Value}"); + } + receipt.Add($"Total cost: {TotalCost()}"); + return receipt; + } + } + + public struct Item + { + public string ItemName { get; } + public float Cost { get; } + public Item(string itemName, float cost) + { + ItemName = itemName; + Cost = cost; + } + } +} diff --git a/tdd-domain-modelling.CSharp.Test/BasketTest.cs b/tdd-domain-modelling.CSharp.Test/BasketTest.cs new file mode 100644 index 0000000..6043e70 --- /dev/null +++ b/tdd-domain-modelling.CSharp.Test/BasketTest.cs @@ -0,0 +1,73 @@ +using Microsoft.VisualStudio.TestPlatform.ObjectModel; +using NUnit.Framework; +using tdd_domain_modelling.CSharp.Main; + +namespace tdd_domain_modelling.CSharp.Test +{ + [TestFixture] + public class BasketTest + { + private Basket? basket; + [SetUp] + public void Init() + { + basket = new Basket(); + } + + [TestCase("", 123f)] + [TestCase("Test", 0f)] + public void CreateItem(string name, float cost) + { + Item item = new Item(name, cost); + Assert.That(item.ItemName, Is.EqualTo(name)); + Assert.That(item.Cost, Is.EqualTo(cost)); + } + + [TestCase("Test", 10f)] + public void AddItem(string name, float cost) + { + Basket basket = new Basket(); + basket.AddItem(name, cost); + Assert.That(basket.items.Count, Is.EqualTo(1)); + } + + [TestCase("Test", 10f)] + [TestCase("Test", 0f)] + [TestCase("Test", 1000f)] + public void TotalCost(string name, float cost) + { + Basket basket = new Basket(); + basket.AddItem(name, cost); + Assert.That(basket.TotalCost(), Is.EqualTo(cost)); + } + + [Test] + public void EmptyReceipt() + { + List expectedResult = new List() + { + { "Total cost: 0"} + }; + Basket basket = new Basket(); + Assert.That(basket.Receipt(), Is.EqualTo(expectedResult)); + } + + [Test] + public void ReceiptWithItems() + { + List expectedResult = new List() + { + {"|Milk|10|x5"}, + {"|Chips|20|x1"}, + { "Total cost: 70"} + }; + Basket basket = new Basket(); + for (int i = 0; i < 5; i++) + { + basket.AddItem("Milk", 10); + } + basket.AddItem("Chips", 20); + Assert.That(basket.Receipt(), Is.EqualTo(expectedResult)); + } + } +} \ No newline at end of file diff --git a/tdd-domain-modelling.CSharp.Test/tdd-domain-modelling.CSharp.Test.csproj b/tdd-domain-modelling.CSharp.Test/tdd-domain-modelling.CSharp.Test.csproj index 758370d..fee6fc1 100644 --- a/tdd-domain-modelling.CSharp.Test/tdd-domain-modelling.CSharp.Test.csproj +++ b/tdd-domain-modelling.CSharp.Test/tdd-domain-modelling.CSharp.Test.csproj @@ -7,9 +7,9 @@ - - - + + +