From c3bd6d57e4ba0932edee443fd12a1558d82056d6 Mon Sep 17 00:00:00 2001 From: Stian Andreas Ytterstad Nordvik Date: Mon, 15 Jan 2024 09:56:33 +0100 Subject: [PATCH] Stian Andreas Ytterstad Nordvik C#3 --- domain-model.md | 51 ++++++++++++++++++- exercise2.md | 1 + tdd-domain-modelling.CSharp.Main/Basket.cs | 44 ++++++++++++++++ .../BasketTest.cs | 51 +++++++++++++++++++ .../CohortManagerTests.cs | 4 +- 5 files changed, 148 insertions(+), 3 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..1434a70 100644 --- a/domain-model.md +++ b/domain-model.md @@ -1 +1,50 @@ -#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. + +| Classes | Methods | Scenario | Outputs | +|-----------------|---------------------------------------------|------------------------|---------| +| ShopManager | totalCostItems(List basket) | Basketcontains items | int | +| | | Basket is empty | 0 | + +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 | +|-----------------|---------------------------------------------|---------------------------------|------------------| +| ShopManager | `getUserReceipt(List basket)` | Basket has items | List | +| | | Basket is empty | "Basket is empty"| + + + +As a user, +So I can find a specific cohort, +I want to search a list of all cohorts by a cohort name. + +| Classes | Methods | Scenario | Outputs | +|-----------------|---------------------------------------------|------------------------|---------| +| `CohortManager` | `search(List cohorts, string name)` | If name is in list | true | +| | | If name is not in list | false | + +As a supermarket shopper, +So that I can restock my cupboards, +I want to add products into my basket. + +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. + +| Classes | Members | Methods | Scenario | Outputs | +|----------|-----------------------------------------------------------------------|--------------------------------------|------------------------------------------------------------|---------| +| `Basket` | `Dictionary items` (key is product name, value is price) | `AddItem(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 | +| | | `TotalBasketCost()` | Total value of items in basket | int | + + + + diff --git a/exercise2.md b/exercise2.md index e8c8e0d..716fa79 100644 --- a/exercise2.md +++ b/exercise2.md @@ -65,3 +65,4 @@ I'd like to be able to know the total cost of items in my basket. | `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 | | | | | 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..9505081 --- /dev/null +++ b/tdd-domain-modelling.CSharp.Main/Basket.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace Main +{ + public class Basket +{ + + + + public Dictionary Items { get; set; } = new Dictionary(); + + + public bool AddItem(string name, int value) + { + if (Items.ContainsKey(name)) + { + return false; + } else + { + this.Items.Add(name, value); + return true; + } + + } + + public int TotalBasketCost() + { + + int totalcost = 0; + foreach (var item in Items) + { + totalcost += item.Value; + } + + return totalcost; + } + + } +} diff --git a/tdd-domain-modelling.CSharp.Test/BasketTest.cs b/tdd-domain-modelling.CSharp.Test/BasketTest.cs new file mode 100644 index 0000000..a7d1b02 --- /dev/null +++ b/tdd-domain-modelling.CSharp.Test/BasketTest.cs @@ -0,0 +1,51 @@ +using Main; +using NUnit.Framework; +using NUnit.Framework.Constraints; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Test +{ + [TestFixture] + public class BasketTest +{ + [TestCase("Banana", 2)] + public void AddItemTest(string product, int price) + { + //arrange + Basket basket = new Basket(); + + //act + basket.AddItem(product, price); + + //assert + Assert.AreEqual(basket.Items.Count, 1); + + + } + + [TestCase()] + public void TotalBasketCostTest() + { + //arrange + Basket basket = new Basket(); + + + basket.AddItem("Banana", 2); + basket.AddItem("Apple", 3); + basket.AddItem("Coca cola", 6); + + + //act + int totalcost = basket.TotalBasketCost(); + + //assert + Assert.IsTrue(totalcost == 11); + + } +} +} diff --git a/tdd-domain-modelling.CSharp.Test/CohortManagerTests.cs b/tdd-domain-modelling.CSharp.Test/CohortManagerTests.cs index 56f3873..7527af1 100644 --- a/tdd-domain-modelling.CSharp.Test/CohortManagerTests.cs +++ b/tdd-domain-modelling.CSharp.Test/CohortManagerTests.cs @@ -7,9 +7,9 @@ namespace tdd_domain_modelling.CSharp.Test public class CohortManagerTest { [Test] - public void FirstTest() + public void searchCohort() { - CohortManager core = new CohortManager(); + } } } \ No newline at end of file