From 56122e920ce56b7388fe0f375d1c9eb3d91fa36b Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 8 Feb 2024 11:53:43 +0100 Subject: [PATCH 1/5] Wrote test for adding items to basket --- domain-model.md | 27 ++++++++++++++++++- tdd-domain-modelling.CSharp.Main/Basket.cs | 22 +++++++++++++++ .../CohortManager.cs | 8 ++++++ tdd-domain-modelling.CSharp.Main/Program.cs | 6 ++++- .../BasketTest.cs | 27 +++++++++++++++++++ .../CohortManagerTests.cs | 11 ++++++-- 6 files changed, 97 insertions(+), 4 deletions(-) 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..4e361a9 100644 --- a/domain-model.md +++ b/domain-model.md @@ -1 +1,26 @@ -#Domain Models In Here \ No newline at end of file +#Domain Models In Here +EXERCISE 1: +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. + +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. + +Class Shopper + +PROPERTIES: +double totalCost +double itemCost +string itemName + +Dictionary items + +METHODS: +double addCosts(totalCost, itemCost) +returns: +totalCost + +List createReceipt (Dictionary items, double totalCost) +return: +receipt (First index contains 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..8bd7c9a --- /dev/null +++ b/tdd-domain-modelling.CSharp.Main/Basket.cs @@ -0,0 +1,22 @@ +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 + { + int totalCost; + public Dictionary items = new Dictionary(); + public bool addItem(string name, int price) + { + + } + public int total() + { + + } + } +} diff --git a/tdd-domain-modelling.CSharp.Main/CohortManager.cs b/tdd-domain-modelling.CSharp.Main/CohortManager.cs index 6a5669c..4b4b529 100644 --- a/tdd-domain-modelling.CSharp.Main/CohortManager.cs +++ b/tdd-domain-modelling.CSharp.Main/CohortManager.cs @@ -8,5 +8,13 @@ namespace tdd_domain_modelling.CSharp.Main { public class CohortManager { + public bool search(List cohorts, String name) + { + foreach(var cohort in cohorts) + { + if(cohort == name) return true; + } + return false; + } } } diff --git a/tdd-domain-modelling.CSharp.Main/Program.cs b/tdd-domain-modelling.CSharp.Main/Program.cs index 3751555..ecbb05a 100644 --- a/tdd-domain-modelling.CSharp.Main/Program.cs +++ b/tdd-domain-modelling.CSharp.Main/Program.cs @@ -1,2 +1,6 @@ // See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +//Console.WriteLine("Hello, World!"); + +using tdd_domain_modelling.CSharp.Main; +// "Cheese" 60, "Milk" 10, "Eggs" 30, "Potatoes" 15, "Onions" 20 +Basket basket = new Basket(); \ No newline at end of file diff --git a/tdd-domain-modelling.CSharp.Test/BasketTest.cs b/tdd-domain-modelling.CSharp.Test/BasketTest.cs new file mode 100644 index 0000000..95dd8b2 --- /dev/null +++ b/tdd-domain-modelling.CSharp.Test/BasketTest.cs @@ -0,0 +1,27 @@ +using NUnit.Framework; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using tdd_domain_modelling.CSharp.Main; + +namespace tdd_domain_modelling.CSharp.Test +{ + [TestFixture] + public class BasketTest + { + [TestCase("Cheese", 60)] + [TestCase("Milk", 10)] + [TestCase("Eggs", 30)] + [TestCase("Potatoes", 15)] + [TestCase("Onions", 20)] + public void testAddToBasket(string item, int price) + { + Basket basket = new Basket(); + basket.addItem(item, price); + Assert.That(basket.items[item].Equals(item)); + } + } +} diff --git a/tdd-domain-modelling.CSharp.Test/CohortManagerTests.cs b/tdd-domain-modelling.CSharp.Test/CohortManagerTests.cs index 56f3873..3cc66e1 100644 --- a/tdd-domain-modelling.CSharp.Test/CohortManagerTests.cs +++ b/tdd-domain-modelling.CSharp.Test/CohortManagerTests.cs @@ -6,10 +6,17 @@ namespace tdd_domain_modelling.CSharp.Test [TestFixture] public class CohortManagerTest { - [Test] - public void FirstTest() + [TestCase("Name1", true)] + [TestCase("Name4", false)] + public void FirstTest(string name, bool expectedResult) { + List cohorts = new List(); + cohorts.Add("Name1"); + cohorts.Add("Name2"); + cohorts.Add("Name3"); CohortManager core = new CohortManager(); + bool result = core.search(cohorts, name); + Assert.That(result, Is.EqualTo(expectedResult)); } } } \ No newline at end of file From 5e0504821964946cc75b0086e78f4d7674eb8e08 Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 8 Feb 2024 12:00:17 +0100 Subject: [PATCH 2/5] Updated test --- tdd-domain-modelling.CSharp.Main/Basket.cs | 4 ++-- tdd-domain-modelling.CSharp.Test/BasketTest.cs | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/tdd-domain-modelling.CSharp.Main/Basket.cs b/tdd-domain-modelling.CSharp.Main/Basket.cs index 8bd7c9a..e0abb4f 100644 --- a/tdd-domain-modelling.CSharp.Main/Basket.cs +++ b/tdd-domain-modelling.CSharp.Main/Basket.cs @@ -12,11 +12,11 @@ public class Basket public Dictionary items = new Dictionary(); public bool addItem(string name, int price) { - + throw new NotImplementedException(); } public int total() { - + throw new NotImplementedException(); } } } diff --git a/tdd-domain-modelling.CSharp.Test/BasketTest.cs b/tdd-domain-modelling.CSharp.Test/BasketTest.cs index 95dd8b2..e89006b 100644 --- a/tdd-domain-modelling.CSharp.Test/BasketTest.cs +++ b/tdd-domain-modelling.CSharp.Test/BasketTest.cs @@ -19,9 +19,12 @@ public class BasketTest [TestCase("Onions", 20)] public void testAddToBasket(string item, int price) { + //Setup Basket basket = new Basket(); + //Execute basket.addItem(item, price); - Assert.That(basket.items[item].Equals(item)); + //Verify, if the item in the dictionary with the provided key has the same price as the given item it has been properly added. + Assert.That(basket.items[item].Equals(price)); } } } From 66e017cea825026a68531a22668bb59cb0485bc2 Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 8 Feb 2024 12:30:40 +0100 Subject: [PATCH 3/5] Implemented solution for addItem, now test passes. --- tdd-domain-modelling.CSharp.Main/Basket.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tdd-domain-modelling.CSharp.Main/Basket.cs b/tdd-domain-modelling.CSharp.Main/Basket.cs index e0abb4f..fcfc8ca 100644 --- a/tdd-domain-modelling.CSharp.Main/Basket.cs +++ b/tdd-domain-modelling.CSharp.Main/Basket.cs @@ -12,7 +12,13 @@ public class Basket public Dictionary items = new Dictionary(); public bool addItem(string name, int price) { - throw new NotImplementedException(); + if (items.ContainsKey(name)) + { + return false; + } + items[name] = price; + totalCost += price; + return true; } public int total() { From c957e6bc9ee3b45d4d7d707544adff32dfb2b600 Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 8 Feb 2024 12:36:34 +0100 Subject: [PATCH 4/5] Wrote test for getting the total cost of your basket. --- tdd-domain-modelling.CSharp.Test/BasketTest.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tdd-domain-modelling.CSharp.Test/BasketTest.cs b/tdd-domain-modelling.CSharp.Test/BasketTest.cs index e89006b..e58b564 100644 --- a/tdd-domain-modelling.CSharp.Test/BasketTest.cs +++ b/tdd-domain-modelling.CSharp.Test/BasketTest.cs @@ -26,5 +26,18 @@ public void testAddToBasket(string item, int price) //Verify, if the item in the dictionary with the provided key has the same price as the given item it has been properly added. Assert.That(basket.items[item].Equals(price)); } + [Test] + public void testGetTotal() + { + //Setup + Basket basket = new Basket(); + basket.addItem("Milk", 10); + basket.addItem("Potatoes", 15); + basket.addItem("Onions", 20); + //Execute + int total= basket.total(); + //Verify + Assert.That(total.Equals(45)); + } } } From a3ba19e1b018768edc6ecd6a3ec92e9ed7fd1340 Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 8 Feb 2024 12:37:02 +0100 Subject: [PATCH 5/5] Implemented total() --- tdd-domain-modelling.CSharp.Main/Basket.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tdd-domain-modelling.CSharp.Main/Basket.cs b/tdd-domain-modelling.CSharp.Main/Basket.cs index fcfc8ca..2a8a9e5 100644 --- a/tdd-domain-modelling.CSharp.Main/Basket.cs +++ b/tdd-domain-modelling.CSharp.Main/Basket.cs @@ -22,7 +22,7 @@ public bool addItem(string name, int price) } public int total() { - throw new NotImplementedException(); + return totalCost; } } }