From 79ed5e60426a0cfa2a6ec80b6aab342175bc1c98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Skivenesv=C3=A5g=20Johannessen?= Date: Mon, 15 Jan 2024 11:15:10 +0100 Subject: [PATCH] Martin S. Johannessen C#3 --- domain-model.md | 37 ++++++++++++++- tdd-domain-modelling.CSharp.Main/Basket.cs | 40 ++++++++++++++++ .../BasketTests.cs | 47 +++++++++++++++++++ 3 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 tdd-domain-modelling.CSharp.Main/Basket.cs create mode 100644 tdd-domain-modelling.CSharp.Test/BasketTests.cs diff --git a/domain-model.md b/domain-model.md index 653f474..910050d 100644 --- a/domain-model.md +++ b/domain-model.md @@ -1 +1,36 @@ -#Domain Models In Here \ No newline at end of file +``` +As a user, +So I can find a specific cohort, +I want to search a list of all cohorts by a cohort name. +``` + + +Class: CohortManager +Method: FindCohort(string name) +Scenario: name is a substring of cohort name. Return: list of cohort names that name is a substing of +Scenario: name is not a substring of any cohort. Return: Empty list + +``` +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. +``` + +Class: ShoppingCart. Method: double CalculateTotal() +Class: Item. Method: double GetPrice() +Scenario: There are a number of items in the cartList. Out: CalculateTotal() + +``` +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: Checkout. Method: GetReceipt() +Class: Item. Method: GetName() +Class: Shopper. Method: AddReceit(receit). Method: GetReceits() +Scenario: The shopper buys something. Void: Shopper.AddReceit(Checkout.GetReceit()) +Scenario: The shopper wants to look at their reciets. Out: Shopper.GetReceits() + + diff --git a/tdd-domain-modelling.CSharp.Main/Basket.cs b/tdd-domain-modelling.CSharp.Main/Basket.cs new file mode 100644 index 0000000..9ed5f81 --- /dev/null +++ b/tdd-domain-modelling.CSharp.Main/Basket.cs @@ -0,0 +1,40 @@ +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 + { + private Dictionary _items; + + public Basket() + { + _items = new Dictionary(); + } + + public void Add(string product, double price) + { + if (_items.ContainsKey(product)) + { + _items[product] += price; + } + else + { + _items.Add(product, price); + } + } + + public bool Contains(string product) + { + return _items.ContainsKey(product); + } + + public double Sum() + { + return _items.Values.Sum(); + } + } +} diff --git a/tdd-domain-modelling.CSharp.Test/BasketTests.cs b/tdd-domain-modelling.CSharp.Test/BasketTests.cs new file mode 100644 index 0000000..0121e10 --- /dev/null +++ b/tdd-domain-modelling.CSharp.Test/BasketTests.cs @@ -0,0 +1,47 @@ +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Diagnostics; +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 BasketTests + { + [Test] + public void BasketAddItem() + { + //arrange + Basket basket = new Basket(); + string product = "Deodorant"; + int price = 100; + + //act + basket.Add(product, price); + + //assert + Assert.That(basket.Contains(product), Is.True); + + } + + [Test] + public void BasketSumItems() + { + //arrange + Basket basket = new Basket(); + basket.Add("Deodorant", 100); + basket.Add("Deodorant", 100); + basket.Add("Cat", 50.50); + + //act + double sum = basket.Sum(); + + //assert + Assert.That(sum, Is.EqualTo(250.50)); + } + } +}