Skip to content
Open

Done #55

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion domain-model.md
Original file line number Diff line number Diff line change
@@ -1 +1,60 @@
#Domain Models In Here
#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<item> items {get}

methods:
public Basket() // initialize items list
public void AddItem(string itemName, float cost)
public float TotalCost(List<Item>) 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<item> items {get}

methods:
public Basket() // initialize items list

public void AddItem(string itemName, float cost)

public float TotalCost(List<Item>) return the total cost of the basket

public List<string> Receipt(List<item> items)
returns a list with:| Name | Price | Quantity |
Last index of list | TotalCost() |
```
62 changes: 62 additions & 0 deletions tdd-domain-modelling.CSharp.Main/Basket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
namespace tdd_domain_modelling.CSharp.Main
{
public class Basket
{
public List<Item> items { get; }

public Basket()
{
items = new List<Item>();
}

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<string> Receipt()
{
List<string> receipt = new List<string>();
Dictionary<Item, float> itemQuantity = new Dictionary<Item, float>();
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;
}
}
}
73 changes: 73 additions & 0 deletions tdd-domain-modelling.CSharp.Test/BasketTest.cs
Original file line number Diff line number Diff line change
@@ -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<string> expectedResult = new List<string>()
{
{ "Total cost: 0"}
};
Basket basket = new Basket();
Assert.That(basket.Receipt(), Is.EqualTo(expectedResult));
}

[Test]
public void ReceiptWithItems()
{
List<string> expectedResult = new List<string>()
{
{"|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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="microsoft.net.test.sdk" Version="17.5.0" />
<PackageReference Include="nunit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="microsoft.net.test.sdk" Version="17.8.0" />
<PackageReference Include="nunit" Version="4.0.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>

<ItemGroup>
Expand Down