diff --git a/domain-model.md b/domain-model.md index 653f474..976c457 100644 --- a/domain-model.md +++ b/domain-model.md @@ -1 +1,28 @@ -#Domain Models In Here \ No newline at end of file +#Domain Models In Here + +## Exercise 1 + +User Story 1 + +``` +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. +``` + +User Story 2 + +``` +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. +``` + +| Classes | Methods | Scenario | Outputs | +|--------------|-------------------------------------------|-----------------------------------------------------------------|---------------------------------------| +| `Customer` | GetTotal(Basket basket) | If basket is not empty | Total cost of basket | +| | | If basket is empty | 0 | +| `Basket` | GetTotal(Dictionary>) | If basket is not empty | Total cost of basket | +| | | If basket is empty | 0 | +| `Product` | | | | diff --git a/exercise2.md b/exercise2.md index e8c8e0d..8799ca8 100644 --- a/exercise2.md +++ b/exercise2.md @@ -62,6 +62,6 @@ I'd like to be able to know the total cost of items in my basket. | Classes | Members | Methods | Scenario | Outputs | |----------|--------------------------------------------------------------------|----------------------------------|------------------------------------------------------------|---------| -| `Basket` | `HashMap items` (key is product name, value is price) | `add(String product, int price)` | Item with the provided name *is not* already in the basket | true | +| `Basket` | `Dictionary items` (key is product name, value is price) | `add(String product, int price)` | Item with the provided name *is not* already in the basket | true | | | | | Item with the provided name *is* already in the basket | false | | | | `total()` | | int | diff --git a/tdd-domain-modelling.CSharp.Main/Basket.cs b/tdd-domain-modelling.CSharp.Main/Basket.cs new file mode 100644 index 0000000..e84999c --- /dev/null +++ b/tdd-domain-modelling.CSharp.Main/Basket.cs @@ -0,0 +1,28 @@ + + +namespace tdd_domain_modelling.CSharp.Main +{ + public class Basket + { + private Dictionary _basket = new (); + public Dictionary basket { get { return _basket; } } + + public bool Add(string product, int price) + { + if (basket.ContainsKey(product)) return false; + _basket.Add(product, price); + return true; + } + + public int Total() + { + int count = 0; + foreach (var item in _basket) + { + count += item.Value; + } + + return count; + } + } +} \ No newline at end of file diff --git a/tdd-domain-modelling.CSharp.Test/BasketTests.cs b/tdd-domain-modelling.CSharp.Test/BasketTests.cs new file mode 100644 index 0000000..7c0d1af --- /dev/null +++ b/tdd-domain-modelling.CSharp.Test/BasketTests.cs @@ -0,0 +1,103 @@ +using NUnit.Framework; +using tdd_domain_modelling.CSharp.Main; + +namespace tdd_domain_modelling.CSharp.Test +{ + public class BasketTests + { + [Test] + public void AddTest() + { + // arrange + Basket basket = new(); + string product = "Pear"; + int price = 10; + // act + var result = basket.Add(product, price); + // assert + Assert.True(result); + } + [Test] + public void AddSeveralTest() + { + // arrange + Basket basket = new(); + string product1 = "Pear"; + int price1 = 10; + string product2 = "Coffee"; + int price2 = 55; + // act + var result1 = basket.Add(product1, price1); + var result2 = basket.Add(product2, price2); + + // assert + Assert.True(result1); + Assert.True(result2); + + } + + [Test] + public void AddConflictingTest() + { + // arrange + Basket basket = new(); + string product = "Pear"; + int price = 10; + // act + var result1 = basket.Add(product, price); + var result2 = basket.Add(product, price); + + // assert + Assert.True(result1); + Assert.False(result2); + } + + [Test] + public void TotalZeroItemsTest() + { + Basket basket = new(); + + int result = basket.Total(); + int expectedResult = 0; + + Assert.AreEqual(expectedResult, result); + + } + + [Test] + public void TotalOneItemTest() + { + // arrange + Basket basket = new(); + string product = "Pear"; + int price = 10; + // act + basket.Add(product, price); + var result = basket.Total(); + int expectedResult = price; + + Assert.AreEqual(expectedResult, result); + + } + + [Test] + public void TotalSeveralItemsTest() + { + // arrange + Basket basket = new(); + string product1 = "Pear"; + int price1 = 10; + string product2 = "Coffee"; + int price2 = 55; + // act + basket.Add(product1, price1); + basket.Add(product2, price2); + + var result = basket.Total(); + int expectedResult = price1 + price2; + + Assert.AreEqual(expectedResult, result); + + } + } +}