Skip to content
Open
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
29 changes: 28 additions & 1 deletion domain-model.md
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
#Domain Models In Here
#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<string, Tuple<int, int>>) | If basket is not empty | Total cost of basket |
| | | If basket is empty | 0 |
| `Product` | | | |
2 changes: 1 addition & 1 deletion exercise2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, int> 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<String, int> 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 |
28 changes: 28 additions & 0 deletions tdd-domain-modelling.CSharp.Main/Basket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@


namespace tdd_domain_modelling.CSharp.Main
{
public class Basket
{
private Dictionary<string, int> _basket = new ();
public Dictionary<string, int> 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;
}
}
}
103 changes: 103 additions & 0 deletions tdd-domain-modelling.CSharp.Test/BasketTests.cs
Original file line number Diff line number Diff line change
@@ -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);

}
}
}