From a4eefe5a3582800faf84da5771dfe4abca6e94de Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 16 Aug 2024 11:05:46 +0200 Subject: [PATCH 01/29] create-project-structure-and-domain-model --- DomainModel.ms | 29 +++++++++++++++++++++++++++++ exercise.main/Basket.cs | 24 ++++++++++++++++++++++++ exercise.main/BobsBagelStore.cs | 18 ++++++++++++++++++ exercise.main/Item.cs | 29 +++++++++++++++++++++++++++++ exercise.main/User.cs | 18 ++++++++++++++++++ 5 files changed, 118 insertions(+) create mode 100644 DomainModel.ms create mode 100644 exercise.main/Basket.cs create mode 100644 exercise.main/BobsBagelStore.cs create mode 100644 exercise.main/Item.cs create mode 100644 exercise.main/User.cs diff --git a/DomainModel.ms b/DomainModel.ms new file mode 100644 index 00000000..9983d003 --- /dev/null +++ b/DomainModel.ms @@ -0,0 +1,29 @@ +# Domain Model + +- As a member of the public, So I can order a bagel before work, I'd like to add a specific type of bagel to my basket. +- As a member of the public, So I can change my order, I'd like to remove a bagel from my basket. +- As a member of the public, So that I can not overfill my small bagel basket I'd like to know when my basket is full when I try adding an item beyond my basket capacity. +- As a Bob's Bagels manager, So that I can expand my business, I’d like to change the capacity of baskets. +- As a member of the public So that I can maintain my sanity I'd like to know if I try to remove an item that doesn't exist in my basket. +- As a customer, So I know how much money I need, I'd like to know the total cost of items in my basket. +- As a customer, So I know what the damage will be, I'd like to know the cost of a bagel before I add it to my basket. +- As a customer, So I can shake things up a bit, I'd like to be able to choose fillings for my bagel. +- As a customer, So I don't over-spend, I'd like to know the cost of each filling before I add it to my bagel order. +- As the manager, So we don't get any weird requests, I want customers to only be able to order things that we stock in our inventory. + + +| **Classes** | **Members** | **Methods** | **Scenario** | **Outputs** | +|:--:|:--:|:--:|:--:|:--:| +| `Basket` | `Dictionary items (value is num of same item in basket)` | `AddItemToBasket(Item item)` | Add item to basket | `true` | +| `Basket` | `Dictionary items` | `RemoveItemFromBasket(Item item)` | Remove item from basket | `true` | +| `Basket` | `Dictionary items` | `RemoveItemFromBasket(Item item)` | Remove item from basket that doesn't exist in basket | `false` | +| `Basket` | `Dictionary items, int basketCapacity` | `AddItemToBasket(Item item)` | Add item to basket when basket is full | `false` | +| `Basket, User` | `Dictionary items, int basketCapacity` | `ChangeCapacity(int capacity)` | As manager(admin) change basket capacity | `true` | +| `Basket` | `Dictionary items` | `Sum()` | Get sum of total cost of items in basket | `float` | +| `Item` | float price | | Get price of item | `float` | +| `BobsBagelStore` | `Dictionary inventory` | `ViewInventory()` | View products in store to choose wanted name and variant | `Console + bools` | +| `BobsBagelStore` | `Dictionary inventory` | `ViewInventory()` | View products with prices | `Console + bools` | +| `BobsBagelStore` | `Dictionary inventory` | `AddItemToBasket(Item item, Basket basket)` | Get item from inventory in stock | `true` | +| `BobsBagelStore` | `Dictionary inventory` | `AddItemToBasket(Item item, Basket basket)` | Get item from inventory that is not in stock | `true` | + + diff --git a/exercise.main/Basket.cs b/exercise.main/Basket.cs new file mode 100644 index 00000000..d6bc9a24 --- /dev/null +++ b/exercise.main/Basket.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main +{ + public class Basket + { + private int _basketID; + private int _basketCapacity = 0; + private Dictionary _items = new Dictionary(); + + public Basket(int basketID, int basketCapacity) + { + _basketID = basketID; + _basketCapacity = basketCapacity; + } + + public int BasketID { get { return _basketID; } } + public int BasketCapacity { get { return _basketCapacity; } } + } +} diff --git a/exercise.main/BobsBagelStore.cs b/exercise.main/BobsBagelStore.cs new file mode 100644 index 00000000..917f9c92 --- /dev/null +++ b/exercise.main/BobsBagelStore.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main +{ + public class BobsBagelStore + { + private Dictionary _inventory = new Dictionary(); + private List _baskets = new List(); + public BobsBagelStore() + { + + } + } +} diff --git a/exercise.main/Item.cs b/exercise.main/Item.cs new file mode 100644 index 00000000..2715340c --- /dev/null +++ b/exercise.main/Item.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main +{ + public class Item + { + private string _sku = string.Empty; + private float _price; + private string _name = string.Empty; + private string _variant = string.Empty; + + public Item(string sku, float price, string name, string variant) + { + _sku = sku; + _price = price; + _name = name; + _variant = variant; + } + + public string SKU { get; } + public string Price { get; } + public string Name { get; } + public string Variant { get; } + } +} diff --git a/exercise.main/User.cs b/exercise.main/User.cs new file mode 100644 index 00000000..8ef0eaaa --- /dev/null +++ b/exercise.main/User.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main +{ + public class User + { + private bool _isManager = false; + public User(bool isManager = false) + { + _isManager = isManager; + } + public bool IsManager { get { return _isManager; } } + } +} From 2fcce9573327d1bce5a376489230b9265483ab2d Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 16 Aug 2024 11:50:09 +0200 Subject: [PATCH 02/29] setup-class-structures --- DomainModel.md | 29 +++++++++++++++++++++++++++++ exercise.main/Basket.cs | 2 +- exercise.main/BobsBagelStore.cs | 19 +++++++++++++++++-- exercise.main/Enums.cs | 14 ++++++++++++++ exercise.main/User.cs | 10 ++++++---- exercise.sln | 4 ++++ 6 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 DomainModel.md create mode 100644 exercise.main/Enums.cs diff --git a/DomainModel.md b/DomainModel.md new file mode 100644 index 00000000..9983d003 --- /dev/null +++ b/DomainModel.md @@ -0,0 +1,29 @@ +# Domain Model + +- As a member of the public, So I can order a bagel before work, I'd like to add a specific type of bagel to my basket. +- As a member of the public, So I can change my order, I'd like to remove a bagel from my basket. +- As a member of the public, So that I can not overfill my small bagel basket I'd like to know when my basket is full when I try adding an item beyond my basket capacity. +- As a Bob's Bagels manager, So that I can expand my business, I’d like to change the capacity of baskets. +- As a member of the public So that I can maintain my sanity I'd like to know if I try to remove an item that doesn't exist in my basket. +- As a customer, So I know how much money I need, I'd like to know the total cost of items in my basket. +- As a customer, So I know what the damage will be, I'd like to know the cost of a bagel before I add it to my basket. +- As a customer, So I can shake things up a bit, I'd like to be able to choose fillings for my bagel. +- As a customer, So I don't over-spend, I'd like to know the cost of each filling before I add it to my bagel order. +- As the manager, So we don't get any weird requests, I want customers to only be able to order things that we stock in our inventory. + + +| **Classes** | **Members** | **Methods** | **Scenario** | **Outputs** | +|:--:|:--:|:--:|:--:|:--:| +| `Basket` | `Dictionary items (value is num of same item in basket)` | `AddItemToBasket(Item item)` | Add item to basket | `true` | +| `Basket` | `Dictionary items` | `RemoveItemFromBasket(Item item)` | Remove item from basket | `true` | +| `Basket` | `Dictionary items` | `RemoveItemFromBasket(Item item)` | Remove item from basket that doesn't exist in basket | `false` | +| `Basket` | `Dictionary items, int basketCapacity` | `AddItemToBasket(Item item)` | Add item to basket when basket is full | `false` | +| `Basket, User` | `Dictionary items, int basketCapacity` | `ChangeCapacity(int capacity)` | As manager(admin) change basket capacity | `true` | +| `Basket` | `Dictionary items` | `Sum()` | Get sum of total cost of items in basket | `float` | +| `Item` | float price | | Get price of item | `float` | +| `BobsBagelStore` | `Dictionary inventory` | `ViewInventory()` | View products in store to choose wanted name and variant | `Console + bools` | +| `BobsBagelStore` | `Dictionary inventory` | `ViewInventory()` | View products with prices | `Console + bools` | +| `BobsBagelStore` | `Dictionary inventory` | `AddItemToBasket(Item item, Basket basket)` | Get item from inventory in stock | `true` | +| `BobsBagelStore` | `Dictionary inventory` | `AddItemToBasket(Item item, Basket basket)` | Get item from inventory that is not in stock | `true` | + + diff --git a/exercise.main/Basket.cs b/exercise.main/Basket.cs index d6bc9a24..8b6adcbc 100644 --- a/exercise.main/Basket.cs +++ b/exercise.main/Basket.cs @@ -9,7 +9,7 @@ namespace exercise.main public class Basket { private int _basketID; - private int _basketCapacity = 0; + private int _basketCapacity = 3; private Dictionary _items = new Dictionary(); public Basket(int basketID, int basketCapacity) diff --git a/exercise.main/BobsBagelStore.cs b/exercise.main/BobsBagelStore.cs index 917f9c92..cb697f6a 100644 --- a/exercise.main/BobsBagelStore.cs +++ b/exercise.main/BobsBagelStore.cs @@ -1,18 +1,33 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; namespace exercise.main { public class BobsBagelStore { - private Dictionary _inventory = new Dictionary(); + private List _inventory = new List(); private List _baskets = new List(); public BobsBagelStore() { - + _inventory.Add(new Item("BGLO", 0.49f, "Bagel", "Onion")); + _inventory.Add(new Item("BGLP", 0.39f, "Bagel", "Plain")); + _inventory.Add(new Item("BGLE", 0.49f, "Bagel", "Everything")); + _inventory.Add(new Item("BGLS", 0.49f, "Bagel", "Sesame")); + _inventory.Add(new Item("COFB", 0.99f, "Coffee", "Black")); + _inventory.Add(new Item("COFW", 1.19f, "Coffee", "White")); + _inventory.Add(new Item("COFC", 1.29f, "Coffee", "Capuccino")); + _inventory.Add(new Item("COFL", 1.29f, "Coffee", "Latte")); + _inventory.Add(new Item("FILB", 0.12f, "Filling", "Bacon")); + _inventory.Add(new Item("FILE", 0.12f, "Filling", "Egg")); + _inventory.Add(new Item("FILC", 0.12f, "Filling", "Cheese")); + _inventory.Add(new Item("FILX", 0.12f, "Filling", "Cream Cheese")); + _inventory.Add(new Item("FILS", 0.12f, "Filling", "Smoked Salmon")); + _inventory.Add(new Item("FILH", 0.12f, "Filling", "Ham")); } } } diff --git a/exercise.main/Enums.cs b/exercise.main/Enums.cs new file mode 100644 index 00000000..18d1e290 --- /dev/null +++ b/exercise.main/Enums.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main +{ + public enum Role + { + Costumer, + Manager + } +} diff --git a/exercise.main/User.cs b/exercise.main/User.cs index 8ef0eaaa..fcd83788 100644 --- a/exercise.main/User.cs +++ b/exercise.main/User.cs @@ -8,11 +8,13 @@ namespace exercise.main { public class User { - private bool _isManager = false; - public User(bool isManager = false) + private Role _role; + private Basket _assignedBasket; + public User(Role role) { - _isManager = isManager; + _role = role; } - public bool IsManager { get { return _isManager; } } + public Role Role { get; } + public Basket AssignedBasket { get; } } } diff --git a/exercise.sln b/exercise.sln index 0efb5453..a09b2bad 100644 --- a/exercise.sln +++ b/exercise.sln @@ -9,6 +9,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "exercise.tests", "exercise. EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{825CCFE7-4F2E-4770-8393-FEB732F66EE4}" ProjectSection(SolutionItems) = preProject + DomainModel.ms = DomainModel.ms extension1.md = extension1.md extension2.md = extension2.md extension3.md = extension3.md @@ -34,4 +35,7 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {FB58CD55-C2BF-4D09-AFD1-1301366AD930} + EndGlobalSection EndGlobal From db1856fd1e7580c905a3b2b44135661e3671b938 Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 16 Aug 2024 11:51:49 +0200 Subject: [PATCH 03/29] removed-domainmodel-ms-name --- DomainModel.ms | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 DomainModel.ms diff --git a/DomainModel.ms b/DomainModel.ms deleted file mode 100644 index 9983d003..00000000 --- a/DomainModel.ms +++ /dev/null @@ -1,29 +0,0 @@ -# Domain Model - -- As a member of the public, So I can order a bagel before work, I'd like to add a specific type of bagel to my basket. -- As a member of the public, So I can change my order, I'd like to remove a bagel from my basket. -- As a member of the public, So that I can not overfill my small bagel basket I'd like to know when my basket is full when I try adding an item beyond my basket capacity. -- As a Bob's Bagels manager, So that I can expand my business, I’d like to change the capacity of baskets. -- As a member of the public So that I can maintain my sanity I'd like to know if I try to remove an item that doesn't exist in my basket. -- As a customer, So I know how much money I need, I'd like to know the total cost of items in my basket. -- As a customer, So I know what the damage will be, I'd like to know the cost of a bagel before I add it to my basket. -- As a customer, So I can shake things up a bit, I'd like to be able to choose fillings for my bagel. -- As a customer, So I don't over-spend, I'd like to know the cost of each filling before I add it to my bagel order. -- As the manager, So we don't get any weird requests, I want customers to only be able to order things that we stock in our inventory. - - -| **Classes** | **Members** | **Methods** | **Scenario** | **Outputs** | -|:--:|:--:|:--:|:--:|:--:| -| `Basket` | `Dictionary items (value is num of same item in basket)` | `AddItemToBasket(Item item)` | Add item to basket | `true` | -| `Basket` | `Dictionary items` | `RemoveItemFromBasket(Item item)` | Remove item from basket | `true` | -| `Basket` | `Dictionary items` | `RemoveItemFromBasket(Item item)` | Remove item from basket that doesn't exist in basket | `false` | -| `Basket` | `Dictionary items, int basketCapacity` | `AddItemToBasket(Item item)` | Add item to basket when basket is full | `false` | -| `Basket, User` | `Dictionary items, int basketCapacity` | `ChangeCapacity(int capacity)` | As manager(admin) change basket capacity | `true` | -| `Basket` | `Dictionary items` | `Sum()` | Get sum of total cost of items in basket | `float` | -| `Item` | float price | | Get price of item | `float` | -| `BobsBagelStore` | `Dictionary inventory` | `ViewInventory()` | View products in store to choose wanted name and variant | `Console + bools` | -| `BobsBagelStore` | `Dictionary inventory` | `ViewInventory()` | View products with prices | `Console + bools` | -| `BobsBagelStore` | `Dictionary inventory` | `AddItemToBasket(Item item, Basket basket)` | Get item from inventory in stock | `true` | -| `BobsBagelStore` | `Dictionary inventory` | `AddItemToBasket(Item item, Basket basket)` | Get item from inventory that is not in stock | `true` | - - From 135e982b732a4ebef5007d625538c3016fc2c010 Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 16 Aug 2024 13:51:28 +0200 Subject: [PATCH 04/29] Fixups-and-add-item-to-basket-test --- DomainModel.md | 21 +++++++++----------- exercise.main/Basket.cs | 12 +++++++----- exercise.main/BobsBagelStore.cs | 16 ++++++++++++++- exercise.main/Enums.cs | 2 +- exercise.main/Item.cs | 8 ++++---- exercise.main/Program.cs | 6 ++++++ exercise.main/User.cs | 5 ++--- exercise.sln | 2 +- exercise.tests/BobsBagelTests.cs | 29 ++++++++++++++++++++++++++++ exercise.tests/UnitTest1.cs | 15 -------------- exercise.tests/exercise.tests.csproj | 4 ++++ 11 files changed, 78 insertions(+), 42 deletions(-) create mode 100644 exercise.tests/BobsBagelTests.cs delete mode 100644 exercise.tests/UnitTest1.cs diff --git a/DomainModel.md b/DomainModel.md index 9983d003..c64b557a 100644 --- a/DomainModel.md +++ b/DomainModel.md @@ -14,16 +14,13 @@ | **Classes** | **Members** | **Methods** | **Scenario** | **Outputs** | |:--:|:--:|:--:|:--:|:--:| -| `Basket` | `Dictionary items (value is num of same item in basket)` | `AddItemToBasket(Item item)` | Add item to basket | `true` | -| `Basket` | `Dictionary items` | `RemoveItemFromBasket(Item item)` | Remove item from basket | `true` | -| `Basket` | `Dictionary items` | `RemoveItemFromBasket(Item item)` | Remove item from basket that doesn't exist in basket | `false` | -| `Basket` | `Dictionary items, int basketCapacity` | `AddItemToBasket(Item item)` | Add item to basket when basket is full | `false` | -| `Basket, User` | `Dictionary items, int basketCapacity` | `ChangeCapacity(int capacity)` | As manager(admin) change basket capacity | `true` | -| `Basket` | `Dictionary items` | `Sum()` | Get sum of total cost of items in basket | `float` | +| `Basket` | `Dictionary _items (value is num of same item in basket)` | `AddItem(Item item)` | Add item to basket | `true` | +| `Basket` | `Dictionary _items, int basketCapacity` | `AddItem(Item item)` | Add item to basket when basket is full | `false` | +| `Basket` | `Dictionary _items` | `RemoveItem(Item item)` | Remove item from basket | `true` | +| `Basket` | `Dictionary _items` | `RemoveItem(Item item)` | Remove item from basket that doesn't exist in basket | `false` | +| `Basket` | `int basketCapacity` | `ChangeCapacity(int capacity, User user)` | As manager(admin) change basket capacity | `true` | +| `Basket` | `Dictionary _items` | `Sum()` | Get sum of total cost of items in basket | `float` | +| `Basket` | `List _inventory` | `GetItem(Item item)` | Get item from inventory | `true` | +| `BobsBagelStore` | `List _inventory` | `ViewInventory()` | View products in store to choose wanted name and variant | `Console + bool` | +| `BobsBagelStore` | `List _inventory` | `ViewInventory()` | View products with prices | `Console + bool` | | `Item` | float price | | Get price of item | `float` | -| `BobsBagelStore` | `Dictionary inventory` | `ViewInventory()` | View products in store to choose wanted name and variant | `Console + bools` | -| `BobsBagelStore` | `Dictionary inventory` | `ViewInventory()` | View products with prices | `Console + bools` | -| `BobsBagelStore` | `Dictionary inventory` | `AddItemToBasket(Item item, Basket basket)` | Get item from inventory in stock | `true` | -| `BobsBagelStore` | `Dictionary inventory` | `AddItemToBasket(Item item, Basket basket)` | Get item from inventory that is not in stock | `true` | - - diff --git a/exercise.main/Basket.cs b/exercise.main/Basket.cs index 8b6adcbc..2598e809 100644 --- a/exercise.main/Basket.cs +++ b/exercise.main/Basket.cs @@ -8,17 +8,19 @@ namespace exercise.main { public class Basket { - private int _basketID; - private int _basketCapacity = 3; + private int _basketCapacity; private Dictionary _items = new Dictionary(); - public Basket(int basketID, int basketCapacity) + public Basket(int basketCapacity = 3) { - _basketID = basketID; _basketCapacity = basketCapacity; } - public int BasketID { get { return _basketID; } } + public bool AddItem(Item item) + { + return false; + } + public int BasketCapacity { get { return _basketCapacity; } } } } diff --git a/exercise.main/BobsBagelStore.cs b/exercise.main/BobsBagelStore.cs index cb697f6a..c918fb42 100644 --- a/exercise.main/BobsBagelStore.cs +++ b/exercise.main/BobsBagelStore.cs @@ -12,8 +12,18 @@ public class BobsBagelStore { private List _inventory = new List(); private List _baskets = new List(); - public BobsBagelStore() + + public bool AddBasket(Basket basket) + { + if (basket == null) return false; + if (_baskets.Contains(basket)) return false; + _baskets.Add(basket); + return true; + } + + public void StockUpInventory() { + _inventory = new List(); _inventory.Add(new Item("BGLO", 0.49f, "Bagel", "Onion")); _inventory.Add(new Item("BGLP", 0.39f, "Bagel", "Plain")); _inventory.Add(new Item("BGLE", 0.49f, "Bagel", "Everything")); @@ -29,5 +39,9 @@ public BobsBagelStore() _inventory.Add(new Item("FILS", 0.12f, "Filling", "Smoked Salmon")); _inventory.Add(new Item("FILH", 0.12f, "Filling", "Ham")); } + + public List Inventory { get { return _inventory; } } + + public List Baskets { get { return _baskets; } } } } diff --git a/exercise.main/Enums.cs b/exercise.main/Enums.cs index 18d1e290..b32727a1 100644 --- a/exercise.main/Enums.cs +++ b/exercise.main/Enums.cs @@ -11,4 +11,4 @@ public enum Role Costumer, Manager } -} +} \ No newline at end of file diff --git a/exercise.main/Item.cs b/exercise.main/Item.cs index 2715340c..a2839d57 100644 --- a/exercise.main/Item.cs +++ b/exercise.main/Item.cs @@ -21,9 +21,9 @@ public Item(string sku, float price, string name, string variant) _variant = variant; } - public string SKU { get; } - public string Price { get; } - public string Name { get; } - public string Variant { get; } + public string SKU { get { return _sku; } } + public float Price { get { return _price; } } + public string Name { get { return _name; } } + public string Variant { get { return _variant; } } } } diff --git a/exercise.main/Program.cs b/exercise.main/Program.cs index 3751555c..9775095b 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -1,2 +1,8 @@ // See https://aka.ms/new-console-template for more information +using exercise.main; + Console.WriteLine("Hello, World!"); + + + + diff --git a/exercise.main/User.cs b/exercise.main/User.cs index fcd83788..bcc88349 100644 --- a/exercise.main/User.cs +++ b/exercise.main/User.cs @@ -9,12 +9,11 @@ namespace exercise.main public class User { private Role _role; - private Basket _assignedBasket; public User(Role role) { _role = role; } - public Role Role { get; } - public Basket AssignedBasket { get; } + public Role Role { get { return _role; } } + public Basket AssignedBasket { get; set; } } } diff --git a/exercise.sln b/exercise.sln index a09b2bad..8da905ca 100644 --- a/exercise.sln +++ b/exercise.sln @@ -9,8 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "exercise.tests", "exercise. EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{825CCFE7-4F2E-4770-8393-FEB732F66EE4}" ProjectSection(SolutionItems) = preProject - DomainModel.ms = DomainModel.ms extension1.md = extension1.md + DomainModel.md = DomainModel.md extension2.md = extension2.md extension3.md = extension3.md extension4.md = extension4.md diff --git a/exercise.tests/BobsBagelTests.cs b/exercise.tests/BobsBagelTests.cs new file mode 100644 index 00000000..5a7c95e9 --- /dev/null +++ b/exercise.tests/BobsBagelTests.cs @@ -0,0 +1,29 @@ +using exercise.main; + +namespace exercise.tests; + +public class BobsBagelTests +{ + [SetUp] + public void Setup() + { + } + + [TestCase(3, true)] + [TestCase(2, false)] + public void TestAddItemToBasken(int basketCapacity, bool expectedResult) + { + BobsBagelStore store = new BobsBagelStore(); + Basket basket = new Basket(); + store.AddBasket(basket); + Item item1 = new Item("BGLO", 0.49f, "Bagel", "Onion"); + Item item2 = new Item("BGLP", 0.39f, "Bagel", "Plain"); + Item item3 = new Item("BGLE", 0.49f, "Bagel", "Everything"); + basket.AddItem(item1); + basket.AddItem(item2); + + bool actualResult = basket.AddItem(item3); + + Assert.That(actualResult, Is.EqualTo(expectedResult)); + } +} \ No newline at end of file diff --git a/exercise.tests/UnitTest1.cs b/exercise.tests/UnitTest1.cs deleted file mode 100644 index 7bdb8968..00000000 --- a/exercise.tests/UnitTest1.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace exercise.tests; - -public class Tests -{ - [SetUp] - public void Setup() - { - } - - [Test] - public void Test1() - { - Assert.Pass(); - } -} \ No newline at end of file diff --git a/exercise.tests/exercise.tests.csproj b/exercise.tests/exercise.tests.csproj index 0072a6d1..93317f6f 100644 --- a/exercise.tests/exercise.tests.csproj +++ b/exercise.tests/exercise.tests.csproj @@ -17,4 +17,8 @@ + + + + From ac46ec27e689d6da0d2cd07077a7348f9139334a Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 16 Aug 2024 14:53:22 +0200 Subject: [PATCH 05/29] Making-add-item-to-basket-test-pass --- exercise.main/Basket.cs | 26 ++++++++++++++++++++++++-- exercise.main/Program.cs | 10 ++++++++++ exercise.tests/BobsBagelTests.cs | 2 +- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/exercise.main/Basket.cs b/exercise.main/Basket.cs index 2598e809..49367fd7 100644 --- a/exercise.main/Basket.cs +++ b/exercise.main/Basket.cs @@ -15,10 +15,32 @@ public Basket(int basketCapacity = 3) { _basketCapacity = basketCapacity; } + + public bool AddItem(Item newItem) + { + if (_basketCapacity <= _items.Sum(item => item.Value)) + { + return false; + } + + Item? itemExists = FindItem(newItem.SKU); + if (itemExists == null) + { + _items.Add(newItem, 1); + return true; + } + else + { + _items[itemExists]++; + return true; + } + } - public bool AddItem(Item item) + private Item? FindItem(string sku) { - return false; + List itemsFound = _items.Where(item => item.Key.SKU == sku).Select(item => item.Key).ToList(); + if (itemsFound.Count == 0) return null; + return itemsFound[0]; } public int BasketCapacity { get { return _basketCapacity; } } diff --git a/exercise.main/Program.cs b/exercise.main/Program.cs index 9775095b..8d9e52c2 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -2,7 +2,17 @@ using exercise.main; Console.WriteLine("Hello, World!"); +int[] numbers = { }; +int first = numbers.First(); + +Console.WriteLine(first); + +/* + This code produces the following output: + + 9 +*/ diff --git a/exercise.tests/BobsBagelTests.cs b/exercise.tests/BobsBagelTests.cs index 5a7c95e9..c331e447 100644 --- a/exercise.tests/BobsBagelTests.cs +++ b/exercise.tests/BobsBagelTests.cs @@ -14,7 +14,7 @@ public void Setup() public void TestAddItemToBasken(int basketCapacity, bool expectedResult) { BobsBagelStore store = new BobsBagelStore(); - Basket basket = new Basket(); + Basket basket = new Basket(basketCapacity); store.AddBasket(basket); Item item1 = new Item("BGLO", 0.49f, "Bagel", "Onion"); Item item2 = new Item("BGLP", 0.39f, "Bagel", "Plain"); From 59004633b58fb4be7625d7d9f219a41238b3312b Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 16 Aug 2024 15:01:26 +0200 Subject: [PATCH 06/29] Add-RemoveItemFromBasket-Test --- exercise.main/Basket.cs | 5 +++++ exercise.main/Program.cs | 18 +----------------- exercise.tests/BobsBagelTests.cs | 18 ++++++++++++++++-- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/exercise.main/Basket.cs b/exercise.main/Basket.cs index 49367fd7..dc96a651 100644 --- a/exercise.main/Basket.cs +++ b/exercise.main/Basket.cs @@ -43,6 +43,11 @@ public bool AddItem(Item newItem) return itemsFound[0]; } + public bool RemoveItem(string sKU) + { + return false; + } + public int BasketCapacity { get { return _basketCapacity; } } } } diff --git a/exercise.main/Program.cs b/exercise.main/Program.cs index 8d9e52c2..8cba6217 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -1,18 +1,2 @@ -// See https://aka.ms/new-console-template for more information -using exercise.main; - -Console.WriteLine("Hello, World!"); -int[] numbers = { }; - -int first = numbers.First(); - -Console.WriteLine(first); - -/* - This code produces the following output: - - 9 -*/ - - +Console.WriteLine("Hello World"); diff --git a/exercise.tests/BobsBagelTests.cs b/exercise.tests/BobsBagelTests.cs index c331e447..12daefee 100644 --- a/exercise.tests/BobsBagelTests.cs +++ b/exercise.tests/BobsBagelTests.cs @@ -13,9 +13,7 @@ public void Setup() [TestCase(2, false)] public void TestAddItemToBasken(int basketCapacity, bool expectedResult) { - BobsBagelStore store = new BobsBagelStore(); Basket basket = new Basket(basketCapacity); - store.AddBasket(basket); Item item1 = new Item("BGLO", 0.49f, "Bagel", "Onion"); Item item2 = new Item("BGLP", 0.39f, "Bagel", "Plain"); Item item3 = new Item("BGLE", 0.49f, "Bagel", "Everything"); @@ -26,4 +24,20 @@ public void TestAddItemToBasken(int basketCapacity, bool expectedResult) Assert.That(actualResult, Is.EqualTo(expectedResult)); } + + [TestCase(true)] + [TestCase(false)] + public void TestRemoveItemFromBasket(bool expectedResult) + { + Basket basket = new Basket(); + Item item1 = new Item("BGLO", 0.49f, "Bagel", "Onion"); + Item item2 = new Item("BGLP", 0.39f, "Bagel", "Plain"); + basket.AddItem(item1); + if (expectedResult) + basket.AddItem(item2); + + bool actualResult = basket.RemoveItem(item2.SKU); + + Assert.That(actualResult, Is.EqualTo(expectedResult)); + } } \ No newline at end of file From ea155a3e0bee917ec8e49ba19c3c64702bc81af5 Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 16 Aug 2024 15:07:15 +0200 Subject: [PATCH 07/29] Make-TestRemoveItemFromBasket-pass --- exercise.main/Basket.cs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/exercise.main/Basket.cs b/exercise.main/Basket.cs index dc96a651..08866896 100644 --- a/exercise.main/Basket.cs +++ b/exercise.main/Basket.cs @@ -23,15 +23,15 @@ public bool AddItem(Item newItem) return false; } - Item? itemExists = FindItem(newItem.SKU); - if (itemExists == null) + Item? itemFound = FindItem(newItem.SKU); + if (itemFound == null) { _items.Add(newItem, 1); return true; } else { - _items[itemExists]++; + _items[itemFound]++; return true; } } @@ -43,9 +43,23 @@ public bool AddItem(Item newItem) return itemsFound[0]; } - public bool RemoveItem(string sKU) + public bool RemoveItem(string sku) { - return false; + Item? itemFound = FindItem(sku); + if (itemFound == null) + { + return false; + } + + if (_items[itemFound] > 1) + { + _items[itemFound]--; + } + else + { + _items.Remove(itemFound); + } + return true; } public int BasketCapacity { get { return _basketCapacity; } } From 1b746069214886986d1196ae4a94f5fb05ceb69b Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 16 Aug 2024 15:12:45 +0200 Subject: [PATCH 08/29] add-sumtestforbasket --- DomainModel.md | 2 +- exercise.main/Basket.cs | 5 ++++- exercise.tests/BobsBagelTests.cs | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/DomainModel.md b/DomainModel.md index c64b557a..82c9c6ff 100644 --- a/DomainModel.md +++ b/DomainModel.md @@ -19,7 +19,7 @@ | `Basket` | `Dictionary _items` | `RemoveItem(Item item)` | Remove item from basket | `true` | | `Basket` | `Dictionary _items` | `RemoveItem(Item item)` | Remove item from basket that doesn't exist in basket | `false` | | `Basket` | `int basketCapacity` | `ChangeCapacity(int capacity, User user)` | As manager(admin) change basket capacity | `true` | -| `Basket` | `Dictionary _items` | `Sum()` | Get sum of total cost of items in basket | `float` | +| `Basket` | `Dictionary _items` | `SumOfItems()` | Get sum of total cost of items in basket | `float` | | `Basket` | `List _inventory` | `GetItem(Item item)` | Get item from inventory | `true` | | `BobsBagelStore` | `List _inventory` | `ViewInventory()` | View products in store to choose wanted name and variant | `Console + bool` | | `BobsBagelStore` | `List _inventory` | `ViewInventory()` | View products with prices | `Console + bool` | diff --git a/exercise.main/Basket.cs b/exercise.main/Basket.cs index 08866896..f35e2eab 100644 --- a/exercise.main/Basket.cs +++ b/exercise.main/Basket.cs @@ -62,6 +62,9 @@ public bool RemoveItem(string sku) return true; } - public int BasketCapacity { get { return _basketCapacity; } } + public int SumOfItems() + { + return 0; + } } } diff --git a/exercise.tests/BobsBagelTests.cs b/exercise.tests/BobsBagelTests.cs index 12daefee..937e87b2 100644 --- a/exercise.tests/BobsBagelTests.cs +++ b/exercise.tests/BobsBagelTests.cs @@ -40,4 +40,21 @@ public void TestRemoveItemFromBasket(bool expectedResult) Assert.That(actualResult, Is.EqualTo(expectedResult)); } + + [Test] + public void TestSumOfItemsInBasket() + { + Basket basket = new Basket(); + Item item1 = new Item("BGLO", 0.49f, "Bagel", "Onion"); + Item item2 = new Item("BGLP", 0.39f, "Bagel", "Plain"); + Item item3 = new Item("BGLP", 0.39f, "Bagel", "Plain"); + basket.AddItem(item1); + basket.AddItem(item2); + basket.AddItem(item3); + int expectedResult = 3; + + int actualResult = basket.SumOfItems(); + + Assert.That(actualResult, Is.EqualTo(expectedResult)); + } } \ No newline at end of file From a9d06636963a0ecb833adb7a44d76476f320aa37 Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 16 Aug 2024 15:13:16 +0200 Subject: [PATCH 09/29] add-sumtestforbasket-pass --- exercise.main/Basket.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercise.main/Basket.cs b/exercise.main/Basket.cs index f35e2eab..20fcd110 100644 --- a/exercise.main/Basket.cs +++ b/exercise.main/Basket.cs @@ -18,7 +18,7 @@ public Basket(int basketCapacity = 3) public bool AddItem(Item newItem) { - if (_basketCapacity <= _items.Sum(item => item.Value)) + if (_basketCapacity <= SumOfItems()) { return false; } @@ -64,7 +64,7 @@ public bool RemoveItem(string sku) public int SumOfItems() { - return 0; + return _items.Sum(item => item.Value); } } } From 7cd66ac670b763e69ac71d2160030f49f0a1af60 Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 16 Aug 2024 15:14:53 +0200 Subject: [PATCH 10/29] small-refactor --- DomainModel.md | 2 +- exercise.main/Basket.cs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/DomainModel.md b/DomainModel.md index 82c9c6ff..3add6cdc 100644 --- a/DomainModel.md +++ b/DomainModel.md @@ -20,7 +20,7 @@ | `Basket` | `Dictionary _items` | `RemoveItem(Item item)` | Remove item from basket that doesn't exist in basket | `false` | | `Basket` | `int basketCapacity` | `ChangeCapacity(int capacity, User user)` | As manager(admin) change basket capacity | `true` | | `Basket` | `Dictionary _items` | `SumOfItems()` | Get sum of total cost of items in basket | `float` | -| `Basket` | `List _inventory` | `GetItem(Item item)` | Get item from inventory | `true` | +| `BobsBagelStore` | `List _inventory` | `GetItem(Item item)` | Get item from inventory | `true` | | `BobsBagelStore` | `List _inventory` | `ViewInventory()` | View products in store to choose wanted name and variant | `Console + bool` | | `BobsBagelStore` | `List _inventory` | `ViewInventory()` | View products with prices | `Console + bool` | | `Item` | float price | | Get price of item | `float` | diff --git a/exercise.main/Basket.cs b/exercise.main/Basket.cs index 20fcd110..2e60a896 100644 --- a/exercise.main/Basket.cs +++ b/exercise.main/Basket.cs @@ -23,7 +23,7 @@ public bool AddItem(Item newItem) return false; } - Item? itemFound = FindItem(newItem.SKU); + Item? itemFound = GetItem(newItem.SKU); if (itemFound == null) { _items.Add(newItem, 1); @@ -36,7 +36,7 @@ public bool AddItem(Item newItem) } } - private Item? FindItem(string sku) + private Item? GetItem(string sku) { List itemsFound = _items.Where(item => item.Key.SKU == sku).Select(item => item.Key).ToList(); if (itemsFound.Count == 0) return null; @@ -45,7 +45,7 @@ public bool AddItem(Item newItem) public bool RemoveItem(string sku) { - Item? itemFound = FindItem(sku); + Item? itemFound = GetItem(sku); if (itemFound == null) { return false; From f4f3e8280d830ced02893caeea6197aa70574739 Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 16 Aug 2024 15:20:12 +0200 Subject: [PATCH 11/29] add-changebasketcapacity-test --- exercise.main/Basket.cs | 5 +++++ exercise.tests/BobsBagelTests.cs | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/exercise.main/Basket.cs b/exercise.main/Basket.cs index 2e60a896..a6e9be40 100644 --- a/exercise.main/Basket.cs +++ b/exercise.main/Basket.cs @@ -66,5 +66,10 @@ public int SumOfItems() { return _items.Sum(item => item.Value); } + + public bool ChangeCapacity(int basketCapacity, User customer) + { + return false; + } } } diff --git a/exercise.tests/BobsBagelTests.cs b/exercise.tests/BobsBagelTests.cs index 937e87b2..8a1c8b60 100644 --- a/exercise.tests/BobsBagelTests.cs +++ b/exercise.tests/BobsBagelTests.cs @@ -1,4 +1,5 @@ using exercise.main; +using NUnit.Framework.Interfaces; namespace exercise.tests; @@ -57,4 +58,20 @@ public void TestSumOfItemsInBasket() Assert.That(actualResult, Is.EqualTo(expectedResult)); } + + [TestCase(2, true)] + [TestCase(1, false)] + public void TestChangeBasketCapacity(int basketCapacity, bool expectedResult) + { + Basket basket = new Basket(3); + User customer = new User(Role.Costumer); + Item item1 = new Item("BGLO", 0.49f, "Bagel", "Onion"); + Item item2 = new Item("BGLP", 0.39f, "Bagel", "Plain"); + basket.AddItem(item1); + basket.AddItem(item2); + + bool actualResult = basket.ChangeCapacity(basketCapacity, customer); + + Assert.That(actualResult, Is.EqualTo(expectedResult)); + } } \ No newline at end of file From 41f6d220f364194ce29fc17503ac6f9ba221fd66 Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 16 Aug 2024 15:32:07 +0200 Subject: [PATCH 12/29] add-changebasketcapacity-test-pass --- DomainModel.md | 4 ++-- exercise.main/Basket.cs | 26 +++++++++++++++++++++----- exercise.main/Enums.cs | 2 +- exercise.tests/BobsBagelTests.cs | 15 ++++++++------- 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/DomainModel.md b/DomainModel.md index 3add6cdc..4ea1a0d3 100644 --- a/DomainModel.md +++ b/DomainModel.md @@ -18,8 +18,8 @@ | `Basket` | `Dictionary _items, int basketCapacity` | `AddItem(Item item)` | Add item to basket when basket is full | `false` | | `Basket` | `Dictionary _items` | `RemoveItem(Item item)` | Remove item from basket | `true` | | `Basket` | `Dictionary _items` | `RemoveItem(Item item)` | Remove item from basket that doesn't exist in basket | `false` | -| `Basket` | `int basketCapacity` | `ChangeCapacity(int capacity, User user)` | As manager(admin) change basket capacity | `true` | -| `Basket` | `Dictionary _items` | `SumOfItems()` | Get sum of total cost of items in basket | `float` | +| `Basket` | `int basketCapacity` | `ChangeCapacity(int capacity, User user)` | As manager change basket capacity | `true` | +| `Basket` | `Dictionary _items` | `SumOfItemCosts()` | Get sum of total cost of items in basket | `float` | | `BobsBagelStore` | `List _inventory` | `GetItem(Item item)` | Get item from inventory | `true` | | `BobsBagelStore` | `List _inventory` | `ViewInventory()` | View products in store to choose wanted name and variant | `Console + bool` | | `BobsBagelStore` | `List _inventory` | `ViewInventory()` | View products with prices | `Console + bool` | diff --git a/exercise.main/Basket.cs b/exercise.main/Basket.cs index a6e9be40..491b7af0 100644 --- a/exercise.main/Basket.cs +++ b/exercise.main/Basket.cs @@ -18,8 +18,9 @@ public Basket(int basketCapacity = 3) public bool AddItem(Item newItem) { - if (_basketCapacity <= SumOfItems()) + if (_basketCapacity <= _items.Sum(item => item.Value)) { + Console.WriteLine("Basket capacity reached, can't add more items!"); return false; } @@ -48,6 +49,7 @@ public bool RemoveItem(string sku) Item? itemFound = GetItem(sku); if (itemFound == null) { + Console.WriteLine("Item not found!"); return false; } @@ -62,14 +64,28 @@ public bool RemoveItem(string sku) return true; } - public int SumOfItems() + public float SumOfItemCosts() { - return _items.Sum(item => item.Value); + float sum = 0f; + foreach (var item in _items) + { + sum += item.Key.Price * item.Value; + } + return sum; } - public bool ChangeCapacity(int basketCapacity, User customer) + public bool ChangeCapacity(int basketCapacity, User user) { - return false; + if (user.Role == Role.Customer) + { + return false; + } + if (basketCapacity < _items.Sum(item => item.Value)) + { + return false; + } + _basketCapacity = basketCapacity; + return true; } } } diff --git a/exercise.main/Enums.cs b/exercise.main/Enums.cs index b32727a1..5a162101 100644 --- a/exercise.main/Enums.cs +++ b/exercise.main/Enums.cs @@ -8,7 +8,7 @@ namespace exercise.main { public enum Role { - Costumer, + Customer, Manager } } \ No newline at end of file diff --git a/exercise.tests/BobsBagelTests.cs b/exercise.tests/BobsBagelTests.cs index 8a1c8b60..0f8f59dd 100644 --- a/exercise.tests/BobsBagelTests.cs +++ b/exercise.tests/BobsBagelTests.cs @@ -43,7 +43,7 @@ public void TestRemoveItemFromBasket(bool expectedResult) } [Test] - public void TestSumOfItemsInBasket() + public void TestSumOfItemsCostsInBasket() { Basket basket = new Basket(); Item item1 = new Item("BGLO", 0.49f, "Bagel", "Onion"); @@ -52,19 +52,20 @@ public void TestSumOfItemsInBasket() basket.AddItem(item1); basket.AddItem(item2); basket.AddItem(item3); - int expectedResult = 3; + float expectedResult = 1.27f; - int actualResult = basket.SumOfItems(); + float actualResult = basket.SumOfItemCosts(); Assert.That(actualResult, Is.EqualTo(expectedResult)); } - [TestCase(2, true)] - [TestCase(1, false)] - public void TestChangeBasketCapacity(int basketCapacity, bool expectedResult) + [TestCase(Role.Manager, 2, true)] + [TestCase(Role.Manager, 1, false)] + [TestCase(Role.Customer, 2, false)] + public void TestChangeBasketCapacity(Role userRole, int basketCapacity, bool expectedResult) { Basket basket = new Basket(3); - User customer = new User(Role.Costumer); + User customer = new User(userRole); Item item1 = new Item("BGLO", 0.49f, "Bagel", "Onion"); Item item2 = new Item("BGLP", 0.39f, "Bagel", "Plain"); basket.AddItem(item1); From cb8e48e77b31a6d2b6fb9a6b3af2e1b0131c8f6d Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 16 Aug 2024 15:41:15 +0200 Subject: [PATCH 13/29] add-test-for-getitemfrominventory --- DomainModel.md | 2 +- exercise.main/BobsBagelStore.cs | 9 +++++---- exercise.tests/BobsBagelTests.cs | 12 ++++++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/DomainModel.md b/DomainModel.md index 4ea1a0d3..c5a2cccd 100644 --- a/DomainModel.md +++ b/DomainModel.md @@ -20,7 +20,7 @@ | `Basket` | `Dictionary _items` | `RemoveItem(Item item)` | Remove item from basket that doesn't exist in basket | `false` | | `Basket` | `int basketCapacity` | `ChangeCapacity(int capacity, User user)` | As manager change basket capacity | `true` | | `Basket` | `Dictionary _items` | `SumOfItemCosts()` | Get sum of total cost of items in basket | `float` | -| `BobsBagelStore` | `List _inventory` | `GetItem(Item item)` | Get item from inventory | `true` | +| `BobsBagelStore` | `List _inventory` | `GetItem(string sku)` | Get item from inventory | `true` | | `BobsBagelStore` | `List _inventory` | `ViewInventory()` | View products in store to choose wanted name and variant | `Console + bool` | | `BobsBagelStore` | `List _inventory` | `ViewInventory()` | View products with prices | `Console + bool` | | `Item` | float price | | Get price of item | `float` | diff --git a/exercise.main/BobsBagelStore.cs b/exercise.main/BobsBagelStore.cs index c918fb42..d09485ce 100644 --- a/exercise.main/BobsBagelStore.cs +++ b/exercise.main/BobsBagelStore.cs @@ -21,6 +21,11 @@ public bool AddBasket(Basket basket) return true; } + public bool GetItem(string sku) + { + return false; + } + public void StockUpInventory() { _inventory = new List(); @@ -39,9 +44,5 @@ public void StockUpInventory() _inventory.Add(new Item("FILS", 0.12f, "Filling", "Smoked Salmon")); _inventory.Add(new Item("FILH", 0.12f, "Filling", "Ham")); } - - public List Inventory { get { return _inventory; } } - - public List Baskets { get { return _baskets; } } } } diff --git a/exercise.tests/BobsBagelTests.cs b/exercise.tests/BobsBagelTests.cs index 0f8f59dd..b2f95361 100644 --- a/exercise.tests/BobsBagelTests.cs +++ b/exercise.tests/BobsBagelTests.cs @@ -75,4 +75,16 @@ public void TestChangeBasketCapacity(Role userRole, int basketCapacity, bool exp Assert.That(actualResult, Is.EqualTo(expectedResult)); } + + [TestCase("BLGO", false)] + [TestCase("BGLO", true)] + public void TestGetItemFromInventory(string sku, bool expectedResult) + { + BobsBagelStore store = new BobsBagelStore(); + store.StockUpInventory(); + + bool actualResult = store.GetItem(sku); + + Assert.That(actualResult, Is.EqualTo(expectedResult)); + } } \ No newline at end of file From fde12ef131c685100da6b24d7cd120e80a77a1c5 Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 16 Aug 2024 17:11:35 +0200 Subject: [PATCH 14/29] work-in-progress --- exercise.main/BobsBagelStore.cs | 4 ++-- exercise.tests/BobsBagelTests.cs | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/exercise.main/BobsBagelStore.cs b/exercise.main/BobsBagelStore.cs index d09485ce..4e494802 100644 --- a/exercise.main/BobsBagelStore.cs +++ b/exercise.main/BobsBagelStore.cs @@ -21,9 +21,9 @@ public bool AddBasket(Basket basket) return true; } - public bool GetItem(string sku) + public Item? GetItem(string sku) { - return false; + return _inventory.Find(item => item.SKU == sku); } public void StockUpInventory() diff --git a/exercise.tests/BobsBagelTests.cs b/exercise.tests/BobsBagelTests.cs index b2f95361..0e08c118 100644 --- a/exercise.tests/BobsBagelTests.cs +++ b/exercise.tests/BobsBagelTests.cs @@ -76,15 +76,16 @@ public void TestChangeBasketCapacity(Role userRole, int basketCapacity, bool exp Assert.That(actualResult, Is.EqualTo(expectedResult)); } - [TestCase("BLGO", false)] - [TestCase("BGLO", true)] - public void TestGetItemFromInventory(string sku, bool expectedResult) + [TestCase("BLGO", "", null)] + [TestCase("BGLO", "BGLO", Item)] + public void TestGetItemFromInventory(string sku, string expectedStringResult, Item? expectedBoolResult) { BobsBagelStore store = new BobsBagelStore(); store.StockUpInventory(); - bool actualResult = store.GetItem(sku); - - Assert.That(actualResult, Is.EqualTo(expectedResult)); + var actualResult = store.GetItem(sku); + + Assert.That(actualResult, Is.InstanceOf()); + Assert.That(actualResult.SKU, Is.EqualTo(expectedStringResult)); } } \ No newline at end of file From b766964fffc9b4a7fc6d807326e0bf14d7b045aa Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 16 Aug 2024 17:24:12 +0200 Subject: [PATCH 15/29] Make-TestGetItemFromInventory-pass --- exercise.tests/BobsBagelTests.cs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/exercise.tests/BobsBagelTests.cs b/exercise.tests/BobsBagelTests.cs index 0e08c118..14513f42 100644 --- a/exercise.tests/BobsBagelTests.cs +++ b/exercise.tests/BobsBagelTests.cs @@ -76,16 +76,20 @@ public void TestChangeBasketCapacity(Role userRole, int basketCapacity, bool exp Assert.That(actualResult, Is.EqualTo(expectedResult)); } - [TestCase("BLGO", "", null)] - [TestCase("BGLO", "BGLO", Item)] - public void TestGetItemFromInventory(string sku, string expectedStringResult, Item? expectedBoolResult) + [TestCase("BGLO", "BGLO", true)] + [TestCase("BLGO", "", false)] + public void TestGetItemFromInventory(string sku, string expectedStringResult, bool expectedBoolResult) { BobsBagelStore store = new BobsBagelStore(); store.StockUpInventory(); - var actualResult = store.GetItem(sku); + Item? actualResult = store.GetItem(sku); - Assert.That(actualResult, Is.InstanceOf()); - Assert.That(actualResult.SKU, Is.EqualTo(expectedStringResult)); + // (actualResult == null) == expectedBoolResult + Assert.That((actualResult != null) == expectedBoolResult); + if (actualResult != null) + { + Assert.That(actualResult.SKU, Is.EqualTo(expectedStringResult)); + } } } \ No newline at end of file From af341eb41919b84ce3acf529b1268d1aa7e8bd5b Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 16 Aug 2024 18:02:31 +0200 Subject: [PATCH 16/29] add-view-inventory --- exercise.main/BobsBagelStore.cs | 21 +++++++++++++++++++++ exercise.main/Program.cs | 7 ++++++- exercise.tests/BobsBagelTests.cs | 13 ++++++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/exercise.main/BobsBagelStore.cs b/exercise.main/BobsBagelStore.cs index 4e494802..1823e8f5 100644 --- a/exercise.main/BobsBagelStore.cs +++ b/exercise.main/BobsBagelStore.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Globalization; using System.Linq; +using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; @@ -44,5 +46,24 @@ public void StockUpInventory() _inventory.Add(new Item("FILS", 0.12f, "Filling", "Smoked Salmon")); _inventory.Add(new Item("FILH", 0.12f, "Filling", "Ham")); } + + public bool ViewInventory() + { + Console.WriteLine("| SKU | Price | Name | Variant |"); + Console.WriteLine("|------|-------|---------|-------------|"); + foreach (Item item in _inventory) + { + Console.Write($"| {item.SKU} "); + Console.Write("| {0:C2} ", item.Price.ToString("C", new CultureInfo("en-GB"))); + Console.Write("| {0} ", item.Name.Length > 8 ? item.Name.Substring(0, 8) : item.Name); + int spacesAmount = 7 - item.Name.Length; + for (int i = 0; i < spacesAmount; i++) { Console.Write(" "); } + Console.Write("| {0} ", item.Variant.Length > 11 ? item.Variant.Substring(0, 11) : item.Variant); + spacesAmount = 11 - item.Variant.Length; + for (int i = 0; i < spacesAmount; i++) { Console.Write(" "); } + Console.Write("|\n"); + } + return true; + } } } diff --git a/exercise.main/Program.cs b/exercise.main/Program.cs index 8cba6217..3946802d 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -1,2 +1,7 @@ -Console.WriteLine("Hello World"); +using exercise.main; + +BobsBagelStore store = new BobsBagelStore(); +store.StockUpInventory(); + +store.ViewInventory(); diff --git a/exercise.tests/BobsBagelTests.cs b/exercise.tests/BobsBagelTests.cs index 14513f42..c78d46a7 100644 --- a/exercise.tests/BobsBagelTests.cs +++ b/exercise.tests/BobsBagelTests.cs @@ -85,11 +85,22 @@ public void TestGetItemFromInventory(string sku, string expectedStringResult, bo Item? actualResult = store.GetItem(sku); - // (actualResult == null) == expectedBoolResult Assert.That((actualResult != null) == expectedBoolResult); if (actualResult != null) { Assert.That(actualResult.SKU, Is.EqualTo(expectedStringResult)); } } + + [Test] + public void TestViewInventory() + { + BobsBagelStore store = new BobsBagelStore(); + store.StockUpInventory(); + bool expectedResult = true; + + bool acutalResult = store.ViewInventory(); + + Assert.That(acutalResult, Is.EqualTo(expectedResult)); + } } \ No newline at end of file From 35ffe7ea1fcd2149cbdf870f6860dbf774ecba3a Mon Sep 17 00:00:00 2001 From: Jonas Date: Mon, 19 Aug 2024 08:26:00 +0200 Subject: [PATCH 17/29] Exercise-core-complete --- DomainModel.md | 2 +- exercise.main/BobsBagelStore.cs | 3 +++ exercise.tests/BobsBagelTests.cs | 12 ++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/DomainModel.md b/DomainModel.md index c5a2cccd..b3ba888c 100644 --- a/DomainModel.md +++ b/DomainModel.md @@ -23,4 +23,4 @@ | `BobsBagelStore` | `List _inventory` | `GetItem(string sku)` | Get item from inventory | `true` | | `BobsBagelStore` | `List _inventory` | `ViewInventory()` | View products in store to choose wanted name and variant | `Console + bool` | | `BobsBagelStore` | `List _inventory` | `ViewInventory()` | View products with prices | `Console + bool` | -| `Item` | float price | | Get price of item | `float` | +| `Item` | float Price | | Get price of item | `float` | diff --git a/exercise.main/BobsBagelStore.cs b/exercise.main/BobsBagelStore.cs index 1823e8f5..831cbb07 100644 --- a/exercise.main/BobsBagelStore.cs +++ b/exercise.main/BobsBagelStore.cs @@ -55,12 +55,15 @@ public bool ViewInventory() { Console.Write($"| {item.SKU} "); Console.Write("| {0:C2} ", item.Price.ToString("C", new CultureInfo("en-GB"))); + Console.Write("| {0} ", item.Name.Length > 8 ? item.Name.Substring(0, 8) : item.Name); int spacesAmount = 7 - item.Name.Length; for (int i = 0; i < spacesAmount; i++) { Console.Write(" "); } + Console.Write("| {0} ", item.Variant.Length > 11 ? item.Variant.Substring(0, 11) : item.Variant); spacesAmount = 11 - item.Variant.Length; for (int i = 0; i < spacesAmount; i++) { Console.Write(" "); } + Console.Write("|\n"); } return true; diff --git a/exercise.tests/BobsBagelTests.cs b/exercise.tests/BobsBagelTests.cs index c78d46a7..ba9e453c 100644 --- a/exercise.tests/BobsBagelTests.cs +++ b/exercise.tests/BobsBagelTests.cs @@ -103,4 +103,16 @@ public void TestViewInventory() Assert.That(acutalResult, Is.EqualTo(expectedResult)); } + + [Test] + public void TestGetPriceFromItem() + { + Item item = new Item("BGLP", 0.39f, "Bagel", "Plain"); + + float expectedResult = 0.39f; + + float acutalResult = item.Price; + + Assert.That(acutalResult, Is.EqualTo(expectedResult)); + } } \ No newline at end of file From 04cdef02078a94700ed15e429aee0de85924bc19 Mon Sep 17 00:00:00 2001 From: Jonas Date: Mon, 19 Aug 2024 10:06:26 +0200 Subject: [PATCH 18/29] Exercise-extension2-add-domain-model-and-user-stories --- DomainModel.md | 14 +++++++++++++- exercise.main/BobsBagelStore.cs | 9 +++++---- exercise.main/Item.cs | 6 +++--- exercise.tests/BobsBagelTests.cs | 2 +- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/DomainModel.md b/DomainModel.md index b3ba888c..d93e36bc 100644 --- a/DomainModel.md +++ b/DomainModel.md @@ -1,4 +1,4 @@ -# Domain Model +# Domain Model - Core - As a member of the public, So I can order a bagel before work, I'd like to add a specific type of bagel to my basket. - As a member of the public, So I can change my order, I'd like to remove a bagel from my basket. @@ -24,3 +24,15 @@ | `BobsBagelStore` | `List _inventory` | `ViewInventory()` | View products in store to choose wanted name and variant | `Console + bool` | | `BobsBagelStore` | `List _inventory` | `ViewInventory()` | View products with prices | `Console + bool` | | `Item` | float Price | | Get price of item | `float` | + + +# Domain Model - Extension 2: Receipts +- As a customer, So i know which items im paying for, I'd like to get a receipt when checking out with the items in my basket. +- As a customer, So i know how much money i need to pay, I'd like get a total sum of the cost of all the items in my receipt. +- As a manager, So i know how the business of the store is going, I'd like to view total profit from customers today. + +| **Classes** | **Members** | **Methods** | **Scenario** | **Outputs** | +|:--:|:--:|:--:|:--:|:--:| +| `Receipt` | `List> _items (string=nameOfItem, int=amountOfItems, float=costOfItem` | `AddItem(Item item)` | Add item to receipt | `true` | +| `Receipt` | `List> _items (string=nameOfItem, int=amountOfItems, float=costOfItem` | `PrintReceipt(Item item)` | Print out receipt to terminal | `Console + bool` | +| `BobsBagelStore` | `List _receipts` | `ViewProfits()` | View total profits today | `Console + bool` | diff --git a/exercise.main/BobsBagelStore.cs b/exercise.main/BobsBagelStore.cs index 831cbb07..17d7dab0 100644 --- a/exercise.main/BobsBagelStore.cs +++ b/exercise.main/BobsBagelStore.cs @@ -49,8 +49,9 @@ public void StockUpInventory() public bool ViewInventory() { - Console.WriteLine("| SKU | Price | Name | Variant |"); - Console.WriteLine("|------|-------|---------|-------------|"); + Console.WriteLine("| SKU | Price | Name | Variant |"); + Console.WriteLine("|------|-------|---------|---------------|"); + foreach (Item item in _inventory) { Console.Write($"| {item.SKU} "); @@ -60,8 +61,8 @@ public bool ViewInventory() int spacesAmount = 7 - item.Name.Length; for (int i = 0; i < spacesAmount; i++) { Console.Write(" "); } - Console.Write("| {0} ", item.Variant.Length > 11 ? item.Variant.Substring(0, 11) : item.Variant); - spacesAmount = 11 - item.Variant.Length; + Console.Write("| {0} ", item.Variant.Length > 13 ? item.Variant.Substring(0, 13) : item.Variant); + spacesAmount = 13 - item.Variant.Length; for (int i = 0; i < spacesAmount; i++) { Console.Write(" "); } Console.Write("|\n"); diff --git a/exercise.main/Item.cs b/exercise.main/Item.cs index a2839d57..8bcc0187 100644 --- a/exercise.main/Item.cs +++ b/exercise.main/Item.cs @@ -8,10 +8,10 @@ namespace exercise.main { public class Item { - private string _sku = string.Empty; + private string _sku; private float _price; - private string _name = string.Empty; - private string _variant = string.Empty; + private string _name; + private string _variant; public Item(string sku, float price, string name, string variant) { diff --git a/exercise.tests/BobsBagelTests.cs b/exercise.tests/BobsBagelTests.cs index ba9e453c..9d782c38 100644 --- a/exercise.tests/BobsBagelTests.cs +++ b/exercise.tests/BobsBagelTests.cs @@ -84,7 +84,7 @@ public void TestGetItemFromInventory(string sku, string expectedStringResult, bo store.StockUpInventory(); Item? actualResult = store.GetItem(sku); - + Assert.That((actualResult != null) == expectedBoolResult); if (actualResult != null) { From fa56eb10056698da72b781518022ee8940e23b7c Mon Sep 17 00:00:00 2001 From: Jonas Date: Mon, 19 Aug 2024 10:19:14 +0200 Subject: [PATCH 19/29] Extension2-add-TestAddItemToReceipt --- exercise.main/Receipt.cs | 16 ++++++++++++++++ exercise.tests/BobsBagelTests.cs | 14 ++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 exercise.main/Receipt.cs diff --git a/exercise.main/Receipt.cs b/exercise.main/Receipt.cs new file mode 100644 index 00000000..4677bac9 --- /dev/null +++ b/exercise.main/Receipt.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main +{ + public class Receipt + { + public bool AddItem(Item item) + { + return false; + } + } +} diff --git a/exercise.tests/BobsBagelTests.cs b/exercise.tests/BobsBagelTests.cs index 9d782c38..408a1a9b 100644 --- a/exercise.tests/BobsBagelTests.cs +++ b/exercise.tests/BobsBagelTests.cs @@ -115,4 +115,18 @@ public void TestGetPriceFromItem() Assert.That(acutalResult, Is.EqualTo(expectedResult)); } + + [Test] + public void TestAddItemToReceipt() + { + Receipt receipt = new Receipt(); + Item item = new Item("BGLP", 0.39f, "Bagel", "Plain"); + + + bool expectedResult = true; + + bool acutalResult = receipt.AddItem(item); + + Assert.That(acutalResult, Is.EqualTo(expectedResult)); + } } \ No newline at end of file From 1572eee21bf681156cc8738dc10ce9851965b62a Mon Sep 17 00:00:00 2001 From: Jonas Date: Mon, 19 Aug 2024 11:02:11 +0200 Subject: [PATCH 20/29] Extension2-generate-receipt --- DomainModel.md | 4 ++-- exercise.main/Basket.cs | 2 ++ exercise.main/BobsBagelStore.cs | 32 +++++++++++++++++++++++++++++++- exercise.main/Receipt.cs | 10 ++++++++-- exercise.tests/BobsBagelTests.cs | 22 +++++++++++++++------- 5 files changed, 58 insertions(+), 12 deletions(-) diff --git a/DomainModel.md b/DomainModel.md index d93e36bc..ee9e2c54 100644 --- a/DomainModel.md +++ b/DomainModel.md @@ -33,6 +33,6 @@ | **Classes** | **Members** | **Methods** | **Scenario** | **Outputs** | |:--:|:--:|:--:|:--:|:--:| -| `Receipt` | `List> _items (string=nameOfItem, int=amountOfItems, float=costOfItem` | `AddItem(Item item)` | Add item to receipt | `true` | -| `Receipt` | `List> _items (string=nameOfItem, int=amountOfItems, float=costOfItem` | `PrintReceipt(Item item)` | Print out receipt to terminal | `Console + bool` | +| `BobsBagelStore` | `List _receipts` | `GenerateReceipt(Basket basket)` | Generate receipt from items in basket | `Receipt?` | +| `Receipt` | `Basket _relatedBasket` | `PrintReceipt(Item item)` | Print out receipt to terminal | `Console + bool` | | `BobsBagelStore` | `List _receipts` | `ViewProfits()` | View total profits today | `Console + bool` | diff --git a/exercise.main/Basket.cs b/exercise.main/Basket.cs index 491b7af0..95aa954f 100644 --- a/exercise.main/Basket.cs +++ b/exercise.main/Basket.cs @@ -87,5 +87,7 @@ public bool ChangeCapacity(int basketCapacity, User user) _basketCapacity = basketCapacity; return true; } + + public Dictionary Items { get { return _items; } } } } diff --git a/exercise.main/BobsBagelStore.cs b/exercise.main/BobsBagelStore.cs index 17d7dab0..f3027661 100644 --- a/exercise.main/BobsBagelStore.cs +++ b/exercise.main/BobsBagelStore.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Globalization; using System.Linq; +using System.Reflection.Metadata.Ecma335; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; @@ -12,8 +13,33 @@ namespace exercise.main { public class BobsBagelStore { + private int _receiptIDs = 0; private List _inventory = new List(); private List _baskets = new List(); + private List _receipts = new List(); + + public Receipt? GenerateReceipt(Basket basket) + { + if (basket == null) + { + return null; + } + + if (_receipts.Where(x => x.RelatedBasket == basket).Count() > 0) + { + return null; + } + _receiptIDs++; + + if (_baskets.Where(x => x == basket).Count() == 0) + { + AddBasket(basket); + } + + Receipt newReceipt = new Receipt(basket, _receiptIDs); + _receipts.Add(newReceipt); + return newReceipt; + } public bool AddBasket(Basket basket) { @@ -60,7 +86,7 @@ public bool ViewInventory() Console.Write("| {0} ", item.Name.Length > 8 ? item.Name.Substring(0, 8) : item.Name); int spacesAmount = 7 - item.Name.Length; for (int i = 0; i < spacesAmount; i++) { Console.Write(" "); } - + Console.Write("| {0} ", item.Variant.Length > 13 ? item.Variant.Substring(0, 13) : item.Variant); spacesAmount = 13 - item.Variant.Length; for (int i = 0; i < spacesAmount; i++) { Console.Write(" "); } @@ -69,5 +95,9 @@ public bool ViewInventory() } return true; } + + public List Baskets { get { return _baskets; } } + public List Inventory { get { return _inventory; } } + public List Receipts { get { return _receipts; } } } } diff --git a/exercise.main/Receipt.cs b/exercise.main/Receipt.cs index 4677bac9..ac85d1d0 100644 --- a/exercise.main/Receipt.cs +++ b/exercise.main/Receipt.cs @@ -8,9 +8,15 @@ namespace exercise.main { public class Receipt { - public bool AddItem(Item item) + private int _id; + private Basket _relatedBasket; + public Basket RelatedBasket { get { return _relatedBasket; } } + public int ID { get { return _id; } } + + public Receipt(Basket basket, int id) { - return false; + _relatedBasket = basket; + _id = id; } } } diff --git a/exercise.tests/BobsBagelTests.cs b/exercise.tests/BobsBagelTests.cs index 408a1a9b..3d51ed98 100644 --- a/exercise.tests/BobsBagelTests.cs +++ b/exercise.tests/BobsBagelTests.cs @@ -119,14 +119,22 @@ public void TestGetPriceFromItem() [Test] public void TestAddItemToReceipt() { - Receipt receipt = new Receipt(); - Item item = new Item("BGLP", 0.39f, "Bagel", "Plain"); - - - bool expectedResult = true; + BobsBagelStore store = new BobsBagelStore(); + Basket basket = new Basket(3); + Item item1 = new Item("BGLO", 0.49f, "Bagel", "Onion"); + Item item2 = new Item("BGLP", 0.39f, "Bagel", "Plain"); + basket.AddItem(item1); + basket.AddItem(item2); + bool expectedBoolResult = true; + Basket expectedBasketResult = basket; - bool acutalResult = receipt.AddItem(item); + Receipt? actualResult = store.GenerateReceipt(basket); - Assert.That(acutalResult, Is.EqualTo(expectedResult)); + Assert.That((actualResult != null) == expectedBoolResult); + if (actualResult != null) + { + Assert.That(actualResult.RelatedBasket, Is.EqualTo(expectedBasketResult)); + Assert.That(store.Baskets[0], Is.EqualTo(expectedBasketResult)); + } } } \ No newline at end of file From d94425b0185fae77e43ec4c340ec3540bd8d6531 Mon Sep 17 00:00:00 2001 From: Jonas Date: Mon, 19 Aug 2024 11:10:18 +0200 Subject: [PATCH 21/29] Extension2-add-view-profits --- exercise.main/BobsBagelStore.cs | 16 +++++++++++++++- exercise.main/Receipt.cs | 2 ++ exercise.tests/BobsBagelTests.cs | 17 +++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/exercise.main/BobsBagelStore.cs b/exercise.main/BobsBagelStore.cs index f3027661..94949b84 100644 --- a/exercise.main/BobsBagelStore.cs +++ b/exercise.main/BobsBagelStore.cs @@ -81,7 +81,7 @@ public bool ViewInventory() foreach (Item item in _inventory) { Console.Write($"| {item.SKU} "); - Console.Write("| {0:C2} ", item.Price.ToString("C", new CultureInfo("en-GB"))); + Console.Write("| {0:C2}£ ", item.Price.ToString("C", new CultureInfo("en-GB"))); Console.Write("| {0} ", item.Name.Length > 8 ? item.Name.Substring(0, 8) : item.Name); int spacesAmount = 7 - item.Name.Length; @@ -96,6 +96,20 @@ public bool ViewInventory() return true; } + public float ViewProfits() + { + float profits = 0; + foreach (Receipt receipt in _receipts) + { + foreach (Item item in receipt.RelatedBasket.Items.Keys) + { + profits += item.Price * receipt.RelatedBasket.Items[item]; + } + } + Console.WriteLine($"Todays profits came to a total of {profits}£"); + return profits; + } + public List Baskets { get { return _baskets; } } public List Inventory { get { return _inventory; } } public List Receipts { get { return _receipts; } } diff --git a/exercise.main/Receipt.cs b/exercise.main/Receipt.cs index ac85d1d0..06b37921 100644 --- a/exercise.main/Receipt.cs +++ b/exercise.main/Receipt.cs @@ -18,5 +18,7 @@ public Receipt(Basket basket, int id) _relatedBasket = basket; _id = id; } + + } } diff --git a/exercise.tests/BobsBagelTests.cs b/exercise.tests/BobsBagelTests.cs index 3d51ed98..c506f7ca 100644 --- a/exercise.tests/BobsBagelTests.cs +++ b/exercise.tests/BobsBagelTests.cs @@ -137,4 +137,21 @@ public void TestAddItemToReceipt() Assert.That(store.Baskets[0], Is.EqualTo(expectedBasketResult)); } } + + [Test] + public void TestViewTodaysStoreProfits() + { + BobsBagelStore store = new BobsBagelStore(); + Basket basket = new Basket(3); + Item item1 = new Item("BGLO", 0.49f, "Bagel", "Onion"); + Item item2 = new Item("BGLP", 0.39f, "Bagel", "Plain"); + basket.AddItem(item1); + basket.AddItem(item2); + store.GenerateReceipt(basket); + float expectedResult = 0.88f; + + float actualResult = store.ViewProfits(); + + Assert.That(actualResult, Is.EqualTo(expectedResult)); + } } \ No newline at end of file From f642f7dde165799a727e1d10f4f5f49f52c10685 Mon Sep 17 00:00:00 2001 From: Jonas Date: Mon, 19 Aug 2024 11:28:14 +0200 Subject: [PATCH 22/29] Extension2-complete --- DomainModel.md | 4 ++-- exercise.main/Program.cs | 16 ++++++++++++++-- exercise.main/Receipt.cs | 24 +++++++++++++++++++++++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/DomainModel.md b/DomainModel.md index ee9e2c54..e4d8dfb3 100644 --- a/DomainModel.md +++ b/DomainModel.md @@ -34,5 +34,5 @@ | **Classes** | **Members** | **Methods** | **Scenario** | **Outputs** | |:--:|:--:|:--:|:--:|:--:| | `BobsBagelStore` | `List _receipts` | `GenerateReceipt(Basket basket)` | Generate receipt from items in basket | `Receipt?` | -| `Receipt` | `Basket _relatedBasket` | `PrintReceipt(Item item)` | Print out receipt to terminal | `Console + bool` | -| `BobsBagelStore` | `List _receipts` | `ViewProfits()` | View total profits today | `Console + bool` | +| `Receipt` | `Basket _relatedBasket` | `PrintReceipt(Item item)` | Print out receipt to terminal | `Console` | +| `BobsBagelStore` | `List _receipts` | `ViewProfits()` | View total profits today | `Console + float` | diff --git a/exercise.main/Program.cs b/exercise.main/Program.cs index 3946802d..4f1b9b1f 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -1,7 +1,19 @@ using exercise.main; BobsBagelStore store = new BobsBagelStore(); -store.StockUpInventory(); +Basket basket = new Basket(5); +Item item1 = new Item("BGLE", 0.39f, "Bagel", "Everything"); +Item item2 = new Item("BGLS", 0.49f, "Bagel", "Sesame"); +Item item3 = new Item("COFB", 0.99f, "Coffee", "Black"); +Item item4 = new Item("FILX", 0.12f, "Filling", "Cream Cheese"); -store.ViewInventory(); +basket.AddItem(item1); +basket.AddItem(item2); +basket.AddItem(item3); +basket.AddItem(item3); +basket.AddItem(item4); + +Receipt receipt = store.GenerateReceipt(basket); + +receipt.PrintReceipt(); diff --git a/exercise.main/Receipt.cs b/exercise.main/Receipt.cs index 06b37921..a6c22555 100644 --- a/exercise.main/Receipt.cs +++ b/exercise.main/Receipt.cs @@ -19,6 +19,28 @@ public Receipt(Basket basket, int id) _id = id; } - + public void PrintReceipt() + { + float total = 0; + float itemTotal = 0; + + Console.WriteLine(" ~~~ Bob's Bagels ~~~\n"); + Console.WriteLine($" {DateTime.Now}\n"); + Console.WriteLine("----------------------------\n"); + + foreach (Item item in _relatedBasket.Items.Keys) + { + Console.Write($"{item.Name}\t\t"); + Console.Write($"{_relatedBasket.Items[item]}".PadRight(5)); + + itemTotal = item.Price * _relatedBasket.Items[item]; + Console.Write($"£{itemTotal}\n"); + total += itemTotal; + } + Console.WriteLine("\n----------------------------"); + Console.Write($"Total\t\t".PadRight(12)); + Console.WriteLine($"£{total}\n"); + Console.WriteLine("\t Thank you\n\tfor you order!"); + } } } From 8c8225dc6454988f780ba9d4ec95c7365a8971fe Mon Sep 17 00:00:00 2001 From: Jonas Date: Tue, 20 Aug 2024 08:22:17 +0200 Subject: [PATCH 23/29] Extension2-complete --- exercise.main/Receipt.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/exercise.main/Receipt.cs b/exercise.main/Receipt.cs index a6c22555..8ca4d8ca 100644 --- a/exercise.main/Receipt.cs +++ b/exercise.main/Receipt.cs @@ -9,13 +9,13 @@ namespace exercise.main public class Receipt { private int _id; - private Basket _relatedBasket; - public Basket RelatedBasket { get { return _relatedBasket; } } + private Dictionary _items = new Dictionary(); + public int ID { get { return _id; } } public Receipt(Basket basket, int id) { - _relatedBasket = basket; + _items = basket.Items; _id = id; } @@ -28,15 +28,17 @@ public void PrintReceipt() Console.WriteLine($" {DateTime.Now}\n"); Console.WriteLine("----------------------------\n"); - foreach (Item item in _relatedBasket.Items.Keys) + foreach (Item item in _items.Keys) { Console.Write($"{item.Name}\t\t"); - Console.Write($"{_relatedBasket.Items[item]}".PadRight(5)); + Console.Write($"{_items[item]}".PadRight(5)); - itemTotal = item.Price * _relatedBasket.Items[item]; + itemTotal = item.Price * _items[item]; Console.Write($"£{itemTotal}\n"); + total += itemTotal; } + Console.WriteLine("\n----------------------------"); Console.Write($"Total\t\t".PadRight(12)); Console.WriteLine($"£{total}\n"); From fd7cde50de67501a0990c0e5d72b0c0cfbc149d7 Mon Sep 17 00:00:00 2001 From: Jonas Date: Tue, 20 Aug 2024 15:57:42 +0200 Subject: [PATCH 24/29] Extension1&3-work-in-progress --- DomainModel.md | 10 ++++++ exercise.main/Basket.cs | 5 +-- exercise.main/BobsBagelStore.cs | 63 ++++++++++++++++++++------------- exercise.main/ClassDiagram1.cd | 18 ++++++++++ exercise.main/Discount.cs | 56 +++++++++++++++++++++++++++++ exercise.main/Program.cs | 5 ++- exercise.main/Receipt.cs | 57 +++++++++++++++++++---------- 7 files changed, 168 insertions(+), 46 deletions(-) create mode 100644 exercise.main/ClassDiagram1.cd create mode 100644 exercise.main/Discount.cs diff --git a/DomainModel.md b/DomainModel.md index e4d8dfb3..e1f19e38 100644 --- a/DomainModel.md +++ b/DomainModel.md @@ -36,3 +36,13 @@ | `BobsBagelStore` | `List _receipts` | `GenerateReceipt(Basket basket)` | Generate receipt from items in basket | `Receipt?` | | `Receipt` | `Basket _relatedBasket` | `PrintReceipt(Item item)` | Print out receipt to terminal | `Console` | | `BobsBagelStore` | `List _receipts` | `ViewProfits()` | View total profits today | `Console + float` | + + +# Domain Model - Extension 1: Discounts +- As a customer, So im able to save money, I'd like to take advantage of special offers available in the store. + +| **Classes** | **Members** | **Methods** | **Scenario** | **Outputs** | +|:--:|:--:|:--:|:--:|:--:| +| `Item` | `List _receipts` | `GenerateReceipt(Basket basket)` | Generate receipt from items in basket | `Receipt?` | +| `Receipt` | `Basket _relatedBasket` | `PrintReceipt(Item item)` | Print out receipt to terminal | `Console` | +| `BobsBagelStore` | `List _receipts` | `ViewProfits()` | View total profits today | `Console + float` | \ No newline at end of file diff --git a/exercise.main/Basket.cs b/exercise.main/Basket.cs index 95aa954f..16a4aead 100644 --- a/exercise.main/Basket.cs +++ b/exercise.main/Basket.cs @@ -9,10 +9,11 @@ namespace exercise.main public class Basket { private int _basketCapacity; - private Dictionary _items = new Dictionary(); + private Dictionary _items; public Basket(int basketCapacity = 3) { + _items = new Dictionary(); _basketCapacity = basketCapacity; } @@ -37,7 +38,7 @@ public bool AddItem(Item newItem) } } - private Item? GetItem(string sku) + public Item? GetItem(string sku) { List itemsFound = _items.Where(item => item.Key.SKU == sku).Select(item => item.Key).ToList(); if (itemsFound.Count == 0) return null; diff --git a/exercise.main/BobsBagelStore.cs b/exercise.main/BobsBagelStore.cs index 94949b84..72329c32 100644 --- a/exercise.main/BobsBagelStore.cs +++ b/exercise.main/BobsBagelStore.cs @@ -14,21 +14,24 @@ namespace exercise.main public class BobsBagelStore { private int _receiptIDs = 0; - private List _inventory = new List(); - private List _baskets = new List(); - private List _receipts = new List(); + private List _inventory; + private List _baskets; + private List _receipts; - public Receipt? GenerateReceipt(Basket basket) + public BobsBagelStore() + { + _inventory = new List(); + _baskets = new List(); + _receipts = new List(); + } + + public bool AddReceipt(Basket basket, Receipt newReceipt) { if (basket == null) { - return null; + return false; } - if (_receipts.Where(x => x.RelatedBasket == basket).Count() > 0) - { - return null; - } _receiptIDs++; if (_baskets.Where(x => x == basket).Count() == 0) @@ -36,9 +39,10 @@ public class BobsBagelStore AddBasket(basket); } - Receipt newReceipt = new Receipt(basket, _receiptIDs); + newReceipt.ID = _receiptIDs; + _receipts.Add(newReceipt); - return newReceipt; + return true; } public bool AddBasket(Basket basket) @@ -73,27 +77,38 @@ public void StockUpInventory() _inventory.Add(new Item("FILH", 0.12f, "Filling", "Ham")); } - public bool ViewInventory() + public string ViewInventory() { - Console.WriteLine("| SKU | Price | Name | Variant |"); - Console.WriteLine("|------|-------|---------|---------------|"); + StringBuilder message = new StringBuilder(); + + if (_inventory.Count == 0) + { + message.Append("No items in inventory!"); + Console.Write(message.ToString()); + return message.ToString(); + } + + message.Append("| SKU | Price | Name | Variant |\n"); + message.Append("|------|-------|---------|---------------|\n"); foreach (Item item in _inventory) { - Console.Write($"| {item.SKU} "); - Console.Write("| {0:C2}£ ", item.Price.ToString("C", new CultureInfo("en-GB"))); + message.Append($"| {item.SKU} "); + message.Append(string.Format("| {0:C2} ", item.Price.ToString("C", new CultureInfo("en-GB")))); - Console.Write("| {0} ", item.Name.Length > 8 ? item.Name.Substring(0, 8) : item.Name); + message.Append(string.Format("| {0} ", item.Name.Length > 8 ? item.Name.Substring(0, 8) : item.Name)); int spacesAmount = 7 - item.Name.Length; - for (int i = 0; i < spacesAmount; i++) { Console.Write(" "); } + for (int i = 0; i < spacesAmount; i++) { message.Append(" "); } - Console.Write("| {0} ", item.Variant.Length > 13 ? item.Variant.Substring(0, 13) : item.Variant); + message.Append(string.Format("| {0} ", item.Variant.Length > 13 ? item.Variant.Substring(0, 13) : item.Variant)); spacesAmount = 13 - item.Variant.Length; - for (int i = 0; i < spacesAmount; i++) { Console.Write(" "); } + for (int i = 0; i < spacesAmount; i++) { message.Append(" "); } - Console.Write("|\n"); + message.Append("|\n"); } - return true; + + Console.Write(message.ToString()); + return message.ToString(); } public float ViewProfits() @@ -101,9 +116,9 @@ public float ViewProfits() float profits = 0; foreach (Receipt receipt in _receipts) { - foreach (Item item in receipt.RelatedBasket.Items.Keys) + foreach (Item item in receipt.Items.Keys) { - profits += item.Price * receipt.RelatedBasket.Items[item]; + profits += item.Price * receipt.Items[item]; } } Console.WriteLine($"Todays profits came to a total of {profits}£"); diff --git a/exercise.main/ClassDiagram1.cd b/exercise.main/ClassDiagram1.cd new file mode 100644 index 00000000..225eaa44 --- /dev/null +++ b/exercise.main/ClassDiagram1.cd @@ -0,0 +1,18 @@ + + + + + + AAAABAAAAAgACEAAAAAAAAAAgBAAAAAEAAAAAAAACAA= + Basket.cs + + + + + + AAUAAAAQAACAAEAAAAAAAAAAQBAAAACAAAAACkABCAA= + BobsBagelStore.cs + + + + \ No newline at end of file diff --git a/exercise.main/Discount.cs b/exercise.main/Discount.cs new file mode 100644 index 00000000..75e110e7 --- /dev/null +++ b/exercise.main/Discount.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main +{ + public class Discount + { + public string DiscountItemSKU { get; set; } + public int ItemsHit { get; set; } + public float DiscountPrice { get; set; } + public int NumberOfDiscountsHit { get; set; } + + public float discountPrice { get; set; } + public int NumberOfRequiredItems { get; set; } + public Discount(string itemSKU, int numberOfRequiredItems, float discountPrice) + { + this.DiscountItemSKU = itemSKU; + this.NumberOfRequiredItems = numberOfRequiredItems; + this.DiscountPrice = discountPrice; + this.NumberOfDiscountsHit = 0; + this.ItemsHit = 0; + } + + public bool CalculateDiscount(Dictionary items) + { + if (items.Keys.Where(x => x.SKU == this.DiscountItemSKU).ToList().Count() == 0) + { + return false; + } + + Item foundItem = items.Keys.Where(x => x.SKU == this.DiscountItemSKU).ToList()[0]; + + if (items[foundItem] < NumberOfRequiredItems) + { + return false; + } + + while (items[foundItem] >= (ItemsHit + NumberOfRequiredItems)) + { + NumberOfDiscountsHit++; + ItemsHit += NumberOfRequiredItems; + } + + return true; + } + + public void ResetDiscount() + { + this.NumberOfDiscountsHit = 0; + this.ItemsHit = 0; + } + } +} \ No newline at end of file diff --git a/exercise.main/Program.cs b/exercise.main/Program.cs index 4f1b9b1f..78afc3d4 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -13,7 +13,10 @@ basket.AddItem(item3); basket.AddItem(item4); -Receipt receipt = store.GenerateReceipt(basket); +Receipt receipt = new Receipt(basket); + +store.AddReceipt(basket, receipt); receipt.PrintReceipt(); + diff --git a/exercise.main/Receipt.cs b/exercise.main/Receipt.cs index 8ca4d8ca..1a5c377d 100644 --- a/exercise.main/Receipt.cs +++ b/exercise.main/Receipt.cs @@ -8,41 +8,60 @@ namespace exercise.main { public class Receipt { - private int _id; - private Dictionary _items = new Dictionary(); + private Dictionary _items; + private List _discounts; - public int ID { get { return _id; } } + public int ID { get; set; } - public Receipt(Basket basket, int id) + public Receipt(Basket basket) { _items = basket.Items; - _id = id; + _discounts.Add(new Discount("BGLO", 6, 2.49f)); + _discounts.Add(new Discount("BGLP", 12, 3.99f)); + _discounts.Add(new Discount("BGLE", 6, 2.49f)); + _discounts.Add(new Discount("COFB", 1, 1.25f)); } - public void PrintReceipt() + public string PrintReceipt() { float total = 0; float itemTotal = 0; + StringBuilder message = new StringBuilder(); - Console.WriteLine(" ~~~ Bob's Bagels ~~~\n"); - Console.WriteLine($" {DateTime.Now}\n"); - Console.WriteLine("----------------------------\n"); + if (_items.Count() == 0) + { + message.Append("No items in basket!"); + Console.Write(message.ToString()); + return message.ToString(); + } + + message.Append(" ~~~ Bob's Bagels ~~~\n\n"); + message.Append($" {DateTime.Now}\n\n"); + message.Append("----------------------------\n\n"); foreach (Item item in _items.Keys) { - Console.Write($"{item.Name}\t\t"); - Console.Write($"{_items[item]}".PadRight(5)); + if (_items[item] > 0) + { + message.Append($"{item.Name}\t\t"); + message.Append($"{_items[item]}".PadRight(5)); + itemTotal = item.Price * _items[item]; + message.Append($"£{itemTotal}\n"); + total += itemTotal; + } + } - itemTotal = item.Price * _items[item]; - Console.Write($"£{itemTotal}\n"); + message.Append("\n----------------------------\n"); + message.Append($"Total\t\t".PadRight(12)); + message.Append($"£{total}\n\n"); + message.Append("\t Thank you\n\tfor you order!\n"); - total += itemTotal; - } + Console.Write(message.ToString()); - Console.WriteLine("\n----------------------------"); - Console.Write($"Total\t\t".PadRight(12)); - Console.WriteLine($"£{total}\n"); - Console.WriteLine("\t Thank you\n\tfor you order!"); + return message.ToString(); } + + public Dictionary Items { get { return _items; } } + public List Discounts { get { return _discounts; } } } } From 5c4b13f2a83d65b70a1042e1e96de968f0f7d891 Mon Sep 17 00:00:00 2001 From: Jonas Date: Wed, 21 Aug 2024 11:05:38 +0200 Subject: [PATCH 25/29] Exercise1&3-work-in-progress --- exercise.main/Program.cs | 16 +++++-- exercise.main/Receipt.cs | 100 ++++++++++++++++++++++++++++++++++----- 2 files changed, 101 insertions(+), 15 deletions(-) diff --git a/exercise.main/Program.cs b/exercise.main/Program.cs index 78afc3d4..9faea031 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -1,14 +1,22 @@ using exercise.main; BobsBagelStore store = new BobsBagelStore(); -Basket basket = new Basket(5); +Basket basket = new Basket(30); Item item1 = new Item("BGLE", 0.39f, "Bagel", "Everything"); -Item item2 = new Item("BGLS", 0.49f, "Bagel", "Sesame"); +Item item2 = new Item("BGLP", 0.39f, "Bagel", "Plain"); Item item3 = new Item("COFB", 0.99f, "Coffee", "Black"); Item item4 = new Item("FILX", 0.12f, "Filling", "Cream Cheese"); -basket.AddItem(item1); -basket.AddItem(item2); +for (int i = 0; i < 7; i++) +{ + basket.AddItem(item1); +} + +for (int i = 0; i < 15; i++) +{ + basket.AddItem(item2); +} + basket.AddItem(item3); basket.AddItem(item3); basket.AddItem(item4); diff --git a/exercise.main/Receipt.cs b/exercise.main/Receipt.cs index 1a5c377d..ce8e56ec 100644 --- a/exercise.main/Receipt.cs +++ b/exercise.main/Receipt.cs @@ -16,14 +16,19 @@ public class Receipt public Receipt(Basket basket) { _items = basket.Items; - _discounts.Add(new Discount("BGLO", 6, 2.49f)); - _discounts.Add(new Discount("BGLP", 12, 3.99f)); - _discounts.Add(new Discount("BGLE", 6, 2.49f)); - _discounts.Add(new Discount("COFB", 1, 1.25f)); + _discounts = new List() + { + new Discount("BGLO", 6, 2.49f), + new Discount("BGLP", 12, 3.99f), + new Discount("BGLE", 6, 2.49f), + new Discount("COFB", 1, 1.25f) + }; } public string PrintReceipt() { + Dictionary discountedItems = CalculateDiscounts(new Dictionary(_items)); + float total = 0; float itemTotal = 0; StringBuilder message = new StringBuilder(); @@ -35,15 +40,15 @@ public string PrintReceipt() return message.ToString(); } - message.Append(" ~~~ Bob's Bagels ~~~\n\n"); - message.Append($" {DateTime.Now}\n\n"); - message.Append("----------------------------\n\n"); + message.Append(" ~~~ Bob's Bagels ~~~\n\n"); + message.Append($" {DateTime.Now}\n\n"); + message.Append("---------------------------------\n\n"); foreach (Item item in _items.Keys) { if (_items[item] > 0) { - message.Append($"{item.Name}\t\t"); + message.Append($"{item.Variant} {item.Name}".PadRight(23)); message.Append($"{_items[item]}".PadRight(5)); itemTotal = item.Price * _items[item]; message.Append($"£{itemTotal}\n"); @@ -51,16 +56,89 @@ public string PrintReceipt() } } - message.Append("\n----------------------------\n"); - message.Append($"Total\t\t".PadRight(12)); + foreach (Item discountedItem in discountedItems.Keys) + { + if (discountedItems[discountedItem].Item1 > 0) + { + message.Append($"{discountedItem.Variant} {discountedItem.Name}".PadRight(23)); + message.Append($"{discountedItems[discountedItem].Item1}".PadRight(5)); + itemTotal = discountedItems[discountedItem].Item2; + message.Append($"£{itemTotal}\n"); + total += itemTotal; + } + } + message.Append("\n---------------------------------\n"); + message.Append($"Total\t\t".PadRight(18)); message.Append($"£{total}\n\n"); - message.Append("\t Thank you\n\tfor you order!\n"); + message.Append("\t Thank you\n\t for you order!\n"); Console.Write(message.ToString()); return message.ToString(); } + public Dictionary CalculateDiscounts(Dictionary items) + { + Dictionary discountedItems = new Dictionary(); + foreach (Discount discount in _discounts) + { + if (items.Keys.Where(x => x.SKU == discount.DiscountItemSKU).ToList().Count() > 0) + { + Item foundItem = items.Keys.Where(x => x.SKU == discount.DiscountItemSKU).ToList()[0]; + + if (foundItem.SKU == "COFB") + { + if (items.Keys.Where(x => x.Name == "Bagel").ToList().Count() > 0) + { + Item foundBagel = items.Keys.Where(x => x.Name == "Bagel").ToList()[0]; + + while (items[foundItem] >= (discount.ItemsHit + discount.NumberOfRequiredItems) && items[foundBagel] >= (discount.ItemsHit + discount.NumberOfRequiredItems)) + { + discount.NumberOfDiscountsHit++; + discount.ItemsHit += discount.NumberOfRequiredItems; + items[foundItem] -= discount.NumberOfRequiredItems; + items[foundBagel] -= discount.NumberOfRequiredItems; + } + + if (discount.NumberOfDiscountsHit > 0) + { + if (discountedItems.ContainsKey(foundItem)) + { + discountedItems[foundItem] = (discount.ItemsHit, discount.DiscountPrice * discount.NumberOfDiscountsHit); + } + else + { + discountedItems.Add(foundItem, (discount.ItemsHit, discount.DiscountPrice * discount.NumberOfDiscountsHit)); + } + } + } + } + else + { + while (items[foundItem] >= (discount.ItemsHit + discount.NumberOfRequiredItems)) + { + discount.NumberOfDiscountsHit++; + discount.ItemsHit += discount.NumberOfRequiredItems; + items[foundItem] -= discount.NumberOfRequiredItems; + } + + if (discount.NumberOfDiscountsHit > 0) + { + if (discountedItems.ContainsKey(foundItem)) + { + discountedItems[foundItem] = (discount.ItemsHit, discount.DiscountPrice * discount.NumberOfDiscountsHit); + } + else + { + discountedItems.Add(foundItem, (discount.ItemsHit, discount.DiscountPrice * discount.NumberOfDiscountsHit)); + } + } + } + } + } + return discountedItems; + } + public Dictionary Items { get { return _items; } } public List Discounts { get { return _discounts; } } } From df5935bc5c3523251529727937a8fb57e53fc620 Mon Sep 17 00:00:00 2001 From: Jonas Date: Wed, 21 Aug 2024 11:53:51 +0200 Subject: [PATCH 26/29] Extension1&3-complete --- DomainModel.md | 2 +- exercise.main/BobsBagelStore.cs | 2 +- exercise.main/Discount.cs | 24 ------------- exercise.main/Program.cs | 21 +++++++----- exercise.main/Receipt.cs | 58 ++++++++++++++++++++++---------- exercise.tests/BobsBagelTests.cs | 56 +++++++++++++++++++++++------- 6 files changed, 98 insertions(+), 65 deletions(-) diff --git a/DomainModel.md b/DomainModel.md index e1f19e38..da9cb050 100644 --- a/DomainModel.md +++ b/DomainModel.md @@ -38,7 +38,7 @@ | `BobsBagelStore` | `List _receipts` | `ViewProfits()` | View total profits today | `Console + float` | -# Domain Model - Extension 1: Discounts +# Domain Model - Extension 1 & 3: Discounts on receipt - As a customer, So im able to save money, I'd like to take advantage of special offers available in the store. | **Classes** | **Members** | **Methods** | **Scenario** | **Outputs** | diff --git a/exercise.main/BobsBagelStore.cs b/exercise.main/BobsBagelStore.cs index 72329c32..9709afae 100644 --- a/exercise.main/BobsBagelStore.cs +++ b/exercise.main/BobsBagelStore.cs @@ -106,7 +106,7 @@ public string ViewInventory() message.Append("|\n"); } - + message.Append("\n\n\n"); Console.Write(message.ToString()); return message.ToString(); } diff --git a/exercise.main/Discount.cs b/exercise.main/Discount.cs index 75e110e7..21d7314e 100644 --- a/exercise.main/Discount.cs +++ b/exercise.main/Discount.cs @@ -23,30 +23,6 @@ public Discount(string itemSKU, int numberOfRequiredItems, float discountPrice) this.NumberOfDiscountsHit = 0; this.ItemsHit = 0; } - - public bool CalculateDiscount(Dictionary items) - { - if (items.Keys.Where(x => x.SKU == this.DiscountItemSKU).ToList().Count() == 0) - { - return false; - } - - Item foundItem = items.Keys.Where(x => x.SKU == this.DiscountItemSKU).ToList()[0]; - - if (items[foundItem] < NumberOfRequiredItems) - { - return false; - } - - while (items[foundItem] >= (ItemsHit + NumberOfRequiredItems)) - { - NumberOfDiscountsHit++; - ItemsHit += NumberOfRequiredItems; - } - - return true; - } - public void ResetDiscount() { this.NumberOfDiscountsHit = 0; diff --git a/exercise.main/Program.cs b/exercise.main/Program.cs index 9faea031..566e9bbf 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -1,25 +1,30 @@ using exercise.main; BobsBagelStore store = new BobsBagelStore(); +store.StockUpInventory(); +store.ViewInventory(); Basket basket = new Basket(30); -Item item1 = new Item("BGLE", 0.39f, "Bagel", "Everything"); +Item item1 = new Item("BGLO", 0.49f, "Bagel", "Onion"); Item item2 = new Item("BGLP", 0.39f, "Bagel", "Plain"); Item item3 = new Item("COFB", 0.99f, "Coffee", "Black"); -Item item4 = new Item("FILX", 0.12f, "Filling", "Cream Cheese"); +Item item4 = new Item("BGLE", 0.49f, "Bagel", "Everything"); -for (int i = 0; i < 7; i++) -{ - basket.AddItem(item1); -} +basket.AddItem(item1); +basket.AddItem(item1); -for (int i = 0; i < 15; i++) +for (int i = 0; i < 12; i++) { basket.AddItem(item2); } +for (int i = 0; i < 6; i++) +{ + basket.AddItem(item4); +} +basket.AddItem(item3); basket.AddItem(item3); basket.AddItem(item3); -basket.AddItem(item4); + Receipt receipt = new Receipt(basket); diff --git a/exercise.main/Receipt.cs b/exercise.main/Receipt.cs index ce8e56ec..5512f760 100644 --- a/exercise.main/Receipt.cs +++ b/exercise.main/Receipt.cs @@ -29,8 +29,10 @@ public string PrintReceipt() { Dictionary discountedItems = CalculateDiscounts(new Dictionary(_items)); - float total = 0; - float itemTotal = 0; + float total = 0f; + float totalDiscount = 0f; + float itemDiscount = 0f; + float itemTotal = 0f; StringBuilder message = new StringBuilder(); if (_items.Count() == 0) @@ -46,32 +48,51 @@ public string PrintReceipt() foreach (Item item in _items.Keys) { - if (_items[item] > 0) + itemDiscount = 0f; + itemTotal = 0f; + if (discountedItems.ContainsKey(item)) { - message.Append($"{item.Variant} {item.Name}".PadRight(23)); + message.Append($"{item.Variant} {item.Name}".PadRight(22)); message.Append($"{_items[item]}".PadRight(5)); - itemTotal = item.Price * _items[item]; + + if (_items[item] > 0) + { + itemTotal += item.Price * (_items[item] - discountedItems[item].Item1); + } + if (discountedItems[item].Item1 > 0) + { + itemTotal += discountedItems[item].Item2; + } message.Append($"£{itemTotal}\n"); - total += itemTotal; + itemDiscount = item.Price * _items[item]; + itemDiscount = itemDiscount - discountedItems[item].Item2; + totalDiscount += itemDiscount; + message.Append($"\t\t\t(-£{float.Round(itemDiscount, 2)})\n\n"); } - } - - foreach (Item discountedItem in discountedItems.Keys) - { - if (discountedItems[discountedItem].Item1 > 0) + else { - message.Append($"{discountedItem.Variant} {discountedItem.Name}".PadRight(23)); - message.Append($"{discountedItems[discountedItem].Item1}".PadRight(5)); - itemTotal = discountedItems[discountedItem].Item2; - message.Append($"£{itemTotal}\n"); - total += itemTotal; + if (_items[item] > 0) + { + message.Append($"{item.Variant} {item.Name}".PadRight(22)); + message.Append($"{_items[item]}".PadRight(5)); + itemTotal = item.Price * _items[item]; + message.Append($"£{itemTotal}\n"); + } } + total += itemTotal; } + message.Append("\n---------------------------------\n"); message.Append($"Total\t\t".PadRight(18)); - message.Append($"£{total}\n\n"); + message.Append($"£{float.Round(total, 2)}\n\n"); + if (totalDiscount > 0f) + { + message.Append($"\n You saved a total of £{totalDiscount}\n\ton this shop\n\n"); + } message.Append("\t Thank you\n\t for you order!\n"); + message.Append("\n\n\n"); + Console.Write(message.ToString()); return message.ToString(); @@ -82,6 +103,7 @@ public string PrintReceipt() Dictionary discountedItems = new Dictionary(); foreach (Discount discount in _discounts) { + discount.ResetDiscount(); if (items.Keys.Where(x => x.SKU == discount.DiscountItemSKU).ToList().Count() > 0) { Item foundItem = items.Keys.Where(x => x.SKU == discount.DiscountItemSKU).ToList()[0]; @@ -142,4 +164,4 @@ public string PrintReceipt() public Dictionary Items { get { return _items; } } public List Discounts { get { return _discounts; } } } -} +} \ No newline at end of file diff --git a/exercise.tests/BobsBagelTests.cs b/exercise.tests/BobsBagelTests.cs index c506f7ca..61f5fa32 100644 --- a/exercise.tests/BobsBagelTests.cs +++ b/exercise.tests/BobsBagelTests.cs @@ -97,11 +97,10 @@ public void TestViewInventory() { BobsBagelStore store = new BobsBagelStore(); store.StockUpInventory(); - bool expectedResult = true; - bool acutalResult = store.ViewInventory(); + string result = store.ViewInventory(); - Assert.That(acutalResult, Is.EqualTo(expectedResult)); + Assert.That(result, Is.Not.Empty); } [Test] @@ -125,17 +124,13 @@ public void TestAddItemToReceipt() Item item2 = new Item("BGLP", 0.39f, "Bagel", "Plain"); basket.AddItem(item1); basket.AddItem(item2); - bool expectedBoolResult = true; - Basket expectedBasketResult = basket; + Receipt receipt = new Receipt(basket); - Receipt? actualResult = store.GenerateReceipt(basket); + bool expectedResult = true; + + bool actualResult = store.AddReceipt(basket, receipt); - Assert.That((actualResult != null) == expectedBoolResult); - if (actualResult != null) - { - Assert.That(actualResult.RelatedBasket, Is.EqualTo(expectedBasketResult)); - Assert.That(store.Baskets[0], Is.EqualTo(expectedBasketResult)); - } + Assert.That(actualResult, Is.EqualTo(expectedResult)); } [Test] @@ -147,11 +142,46 @@ public void TestViewTodaysStoreProfits() Item item2 = new Item("BGLP", 0.39f, "Bagel", "Plain"); basket.AddItem(item1); basket.AddItem(item2); - store.GenerateReceipt(basket); + Receipt receipt = new Receipt(basket); + float expectedResult = 0.88f; + store.AddReceipt(basket, receipt); + float actualResult = store.ViewProfits(); Assert.That(actualResult, Is.EqualTo(expectedResult)); } + + [Test] + public void TestPrintReceipt() + { + Basket basket = new Basket(30); + Item item1 = new Item("BGLO", 0.49f, "Bagel", "Onion"); + Item item2 = new Item("BGLP", 0.39f, "Bagel", "Plain"); + Item item3 = new Item("COFB", 0.99f, "Coffee", "Black"); + Item item4 = new Item("BGLE", 0.49f, "Bagel", "Everything"); + + basket.AddItem(item1); + basket.AddItem(item1); + + for (int i = 0; i < 12; i++) + { + basket.AddItem(item2); + } + + for (int i = 0; i < 6; i++) + { + basket.AddItem(item4); + } + basket.AddItem(item3); + basket.AddItem(item3); + basket.AddItem(item3); + + Receipt receipt = new Receipt(basket); + + string result = receipt.PrintReceipt(); + + Assert.That(result, Is.Not.Empty); + } } \ No newline at end of file From 2044c57bbbb2ff9ebcb3fb02d1d10612491c2285 Mon Sep 17 00:00:00 2001 From: Jonas Date: Thu, 22 Aug 2024 11:28:05 +0200 Subject: [PATCH 27/29] Exercise4-complete --- exercise.main/BobsBagelStore.cs | 52 ++++++++++++ exercise.main/Order.cs | 30 +++++++ exercise.main/Program.cs | 85 +++++++++++++++----- exercise.main/Receipt.cs | 122 +++++++++++++++-------------- exercise.main/SmsController.cs | 55 +++++++++++++ exercise.main/exercise.main.csproj | 4 + exercise.tests/BobsBagelTests.cs | 6 +- 7 files changed, 271 insertions(+), 83 deletions(-) create mode 100644 exercise.main/Order.cs create mode 100644 exercise.main/SmsController.cs diff --git a/exercise.main/BobsBagelStore.cs b/exercise.main/BobsBagelStore.cs index 9709afae..897cdd60 100644 --- a/exercise.main/BobsBagelStore.cs +++ b/exercise.main/BobsBagelStore.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading.Tasks; using System.Xml.Linq; +using Twilio.TwiML.Messaging; namespace exercise.main { @@ -17,12 +18,14 @@ public class BobsBagelStore private List _inventory; private List _baskets; private List _receipts; + private List _orders; public BobsBagelStore() { _inventory = new List(); _baskets = new List(); _receipts = new List(); + _orders = new List(); } public bool AddReceipt(Basket basket, Receipt newReceipt) @@ -125,8 +128,57 @@ public float ViewProfits() return profits; } + public string MakeOrder(Receipt receipt) + { + Order order = new Order(receipt); + _orders.Add(order); + StringBuilder orderMessage = new StringBuilder(); + + orderMessage.Append(" ~~~ Order Created ~~~\n\n"); + orderMessage.Append($"Items Ordered at {order.OrderDate}\n".PadRight(5)); + orderMessage.Append($"Estimated delivery time is {order.DeliveryTime} minutes\n"); + orderMessage.Append($"at {order.DeliveryDate.Hour}:{order.DeliveryDate.Minute}\n".PadLeft(5)); + orderMessage.Append($"\nOrder receipt is\n"); + + Console.WriteLine(orderMessage.ToString()); + + orderMessage.Insert(0, receipt.PrintReceipt()); + + return orderMessage.ToString(); + } + + public string PrintOrderHistory() + { + int counter = 0; + StringBuilder orderHistory = new StringBuilder(); + orderHistory.Append(" ~~~ Order History ~~~\n\n"); + foreach (Order order in _orders) + { + counter++; + orderHistory.Append($"Order {counter} from {order.OrderDate}\n"); + orderHistory.Append($"Delivered at {order.DeliveryDate}\n\n"); + orderHistory.Append($"With items:\n"); + orderHistory.Append(new string('-', 33) + '\n'); + orderHistory.Append($"{order.BelongingReceipt.CalculateItems()}"); + orderHistory.Append(new string('-', 33) + '\n'); + orderHistory.Append($"Total\t\t".PadRight(18)); + orderHistory.Append($"£{float.Round(order.BelongingReceipt.Total, 2)}\n"); + if (order.BelongingReceipt.TotalDiscount > 0f) + { + orderHistory.Append($"Total saved\t\t".PadRight(16)); + orderHistory.Append($"£{float.Round(order.BelongingReceipt.TotalDiscount, 2)}\n"); + } + orderHistory.Append(new string('~', 33) + "\n\n\n"); + } + + Console.WriteLine(orderHistory); + + return orderHistory.ToString(); + } + public List Baskets { get { return _baskets; } } public List Inventory { get { return _inventory; } } public List Receipts { get { return _receipts; } } + public List Orders { get { return _orders; } } } } diff --git a/exercise.main/Order.cs b/exercise.main/Order.cs new file mode 100644 index 00000000..01fb42b8 --- /dev/null +++ b/exercise.main/Order.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main +{ + public class Order + { + private DateTime _orderDate; + private DateTime _deliveryDate; + private int _deliveryTime; + private Receipt _belongingReceipt; + + public Order(Receipt receipt) + { + _orderDate = DateTime.Now; + _deliveryTime = new Random().Next(10, 30); + _deliveryDate = _orderDate.AddMinutes(_deliveryTime); + _belongingReceipt = receipt; + } + + public DateTime OrderDate { get { return _orderDate; } } + public DateTime DeliveryDate { get { return _deliveryDate; } } + public int DeliveryTime { get { return _deliveryTime; } } + public Receipt BelongingReceipt { get { return _belongingReceipt; } } + } +} diff --git a/exercise.main/Program.cs b/exercise.main/Program.cs index 566e9bbf..d0c2d9bd 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -1,35 +1,80 @@ using exercise.main; +string yourTwilioAccountSID = ""; +string yourTwilioAuthToken = ""; +string yourPhoneNumber = ""; BobsBagelStore store = new BobsBagelStore(); +string orderMessage = string.Empty; + store.StockUpInventory(); store.ViewInventory(); -Basket basket = new Basket(30); -Item item1 = new Item("BGLO", 0.49f, "Bagel", "Onion"); -Item item2 = new Item("BGLP", 0.39f, "Bagel", "Plain"); -Item item3 = new Item("COFB", 0.99f, "Coffee", "Black"); -Item item4 = new Item("BGLE", 0.49f, "Bagel", "Everything"); +Console.ReadLine(); -basket.AddItem(item1); -basket.AddItem(item1); +store.MakeOrder(CreateOrderExample1()); +Console.ReadLine(); + +store.MakeOrder(CreateOrderExample2()); +Console.ReadLine(); + +orderMessage += store.PrintOrderHistory(); + +SmsController sms = new SmsController(yourTwilioAccountSID, yourTwilioAuthToken); +sms.SendMessage(orderMessage, yourPhoneNumber); -for (int i = 0; i < 12; i++) -{ - basket.AddItem(item2); -} -for (int i = 0; i < 6; i++) +Receipt CreateOrderExample1() { - basket.AddItem(item4); -} -basket.AddItem(item3); -basket.AddItem(item3); -basket.AddItem(item3); + Basket basket1 = new Basket(30); + Item item11 = new Item("BGLO", 0.49f, "Bagel", "Onion"); + Item item12 = new Item("BGLP", 0.39f, "Bagel", "Plain"); + Item item13 = new Item("COFB", 0.99f, "Coffee", "Black"); + Item item14 = new Item("BGLE", 0.49f, "Bagel", "Everything"); + + basket1.AddItem(item11); + basket1.AddItem(item11); + + for (int i = 0; i < 12; i++) + { + basket1.AddItem(item12); + } + for (int i = 0; i < 6; i++) + { + basket1.AddItem(item14); + } + basket1.AddItem(item13); + basket1.AddItem(item13); + basket1.AddItem(item13); -Receipt receipt = new Receipt(basket); + Receipt receipt1 = new Receipt(basket1); + store.AddReceipt(basket1, receipt1); + return receipt1; +} + +Receipt CreateOrderExample2() +{ + Basket basket2 = new Basket(30); + Item item21 = new Item("BGLO", 0.49f, "Bagel", "Onion"); + Item item22 = new Item("BGLP", 0.39f, "Bagel", "Plain"); + Item item23 = new Item("COFB", 0.99f, "Coffee", "Black"); + Item item24 = new Item("BGLE", 0.49f, "Bagel", "Everything"); -store.AddReceipt(basket, receipt); + basket2.AddItem(item21); + basket2.AddItem(item21); -receipt.PrintReceipt(); + for (int i = 0; i < 4; i++) + { + basket2.AddItem(item22); + } + for (int i = 0; i < 10; i++) + { + basket2.AddItem(item24); + } + basket2.AddItem(item23); + basket2.AddItem(item23); + Receipt receipt2 = new Receipt(basket2); + store.AddReceipt(basket2, receipt2); + return receipt2; +} \ No newline at end of file diff --git a/exercise.main/Receipt.cs b/exercise.main/Receipt.cs index 5512f760..5597bc5d 100644 --- a/exercise.main/Receipt.cs +++ b/exercise.main/Receipt.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using Twilio.TwiML.Messaging; namespace exercise.main { @@ -10,11 +11,15 @@ public class Receipt { private Dictionary _items; private List _discounts; + private float _total = 0f; + private float _totalDiscount = 0f; public int ID { get; set; } public Receipt(Basket basket) { + _total = 0f; + _totalDiscount = 0f; _items = basket.Items; _discounts = new List() { @@ -27,12 +32,6 @@ public Receipt(Basket basket) public string PrintReceipt() { - Dictionary discountedItems = CalculateDiscounts(new Dictionary(_items)); - - float total = 0f; - float totalDiscount = 0f; - float itemDiscount = 0f; - float itemTotal = 0f; StringBuilder message = new StringBuilder(); if (_items.Count() == 0) @@ -42,9 +41,36 @@ public string PrintReceipt() return message.ToString(); } - message.Append(" ~~~ Bob's Bagels ~~~\n\n"); + message.Append(" ~~~ Bob's Bagels ~~~\n\n"); message.Append($" {DateTime.Now}\n\n"); - message.Append("---------------------------------\n\n"); + message.Append(new string('-', 33) + "\n\n"); + + message.Append(CalculateItems()); + + message.Append(new string('-', 33) + '\n'); + message.Append($"Total\t\t".PadRight(18)); + message.Append($"£{float.Round(_total, 2)}\n\n"); + if (_totalDiscount > 0f) + { + message.Append($"\n You saved a total of £{_totalDiscount}\n\ton this shop\n\n"); + } + message.Append("\t Thank you\n\t for you order!\n"); + + message.Append("\n\n\n"); + + Console.Write(message.ToString()); + + return message.ToString(); + } + + public string CalculateItems() + { + Dictionary discountedItems = CalculateDiscounts(new Dictionary(_items)); + StringBuilder message = new StringBuilder(); + float itemDiscount = 0f; + float itemTotal = 0f; + _total = 0f; + _totalDiscount = 0f; foreach (Item item in _items.Keys) { @@ -66,7 +92,7 @@ public string PrintReceipt() message.Append($"£{itemTotal}\n"); itemDiscount = item.Price * _items[item]; itemDiscount = itemDiscount - discountedItems[item].Item2; - totalDiscount += itemDiscount; + _totalDiscount += itemDiscount; message.Append($"\t\t\t(-£{float.Round(itemDiscount, 2)})\n\n"); } else @@ -79,22 +105,8 @@ public string PrintReceipt() message.Append($"£{itemTotal}\n"); } } - total += itemTotal; - } - - message.Append("\n---------------------------------\n"); - message.Append($"Total\t\t".PadRight(18)); - message.Append($"£{float.Round(total, 2)}\n\n"); - if (totalDiscount > 0f) - { - message.Append($"\n You saved a total of £{totalDiscount}\n\ton this shop\n\n"); + _total += itemTotal; } - message.Append("\t Thank you\n\t for you order!\n"); - - message.Append("\n\n\n"); - - Console.Write(message.ToString()); - return message.ToString(); } @@ -108,32 +120,19 @@ public string PrintReceipt() { Item foundItem = items.Keys.Where(x => x.SKU == discount.DiscountItemSKU).ToList()[0]; - if (foundItem.SKU == "COFB") + if ((foundItem.SKU == "COFB") && (items.Keys.Where(x => x.Name == "Bagel").ToList().Count() > 0)) { - if (items.Keys.Where(x => x.Name == "Bagel").ToList().Count() > 0) + Item foundBagel = items.Keys.Where(x => x.Name == "Bagel").ToList()[0]; + + while (items[foundItem] >= (discount.ItemsHit + discount.NumberOfRequiredItems) && items[foundBagel] >= (discount.ItemsHit + discount.NumberOfRequiredItems)) { - Item foundBagel = items.Keys.Where(x => x.Name == "Bagel").ToList()[0]; - - while (items[foundItem] >= (discount.ItemsHit + discount.NumberOfRequiredItems) && items[foundBagel] >= (discount.ItemsHit + discount.NumberOfRequiredItems)) - { - discount.NumberOfDiscountsHit++; - discount.ItemsHit += discount.NumberOfRequiredItems; - items[foundItem] -= discount.NumberOfRequiredItems; - items[foundBagel] -= discount.NumberOfRequiredItems; - } - - if (discount.NumberOfDiscountsHit > 0) - { - if (discountedItems.ContainsKey(foundItem)) - { - discountedItems[foundItem] = (discount.ItemsHit, discount.DiscountPrice * discount.NumberOfDiscountsHit); - } - else - { - discountedItems.Add(foundItem, (discount.ItemsHit, discount.DiscountPrice * discount.NumberOfDiscountsHit)); - } - } + discount.NumberOfDiscountsHit++; + discount.ItemsHit += discount.NumberOfRequiredItems; + items[foundItem] -= discount.NumberOfRequiredItems; + items[foundBagel] -= discount.NumberOfRequiredItems; } + + AddItemToDiscountedItems(discountedItems, foundItem, discount); } else { @@ -144,24 +143,31 @@ public string PrintReceipt() items[foundItem] -= discount.NumberOfRequiredItems; } - if (discount.NumberOfDiscountsHit > 0) - { - if (discountedItems.ContainsKey(foundItem)) - { - discountedItems[foundItem] = (discount.ItemsHit, discount.DiscountPrice * discount.NumberOfDiscountsHit); - } - else - { - discountedItems.Add(foundItem, (discount.ItemsHit, discount.DiscountPrice * discount.NumberOfDiscountsHit)); - } - } + AddItemToDiscountedItems(discountedItems, foundItem, discount); } } } return discountedItems; } + private void AddItemToDiscountedItems(Dictionary discountedItems, Item foundItem, Discount discount) + { + if (discount.NumberOfDiscountsHit > 0) + { + if (discountedItems.ContainsKey(foundItem)) + { + discountedItems[foundItem] = (discount.ItemsHit, discount.DiscountPrice * discount.NumberOfDiscountsHit); + } + else + { + discountedItems.Add(foundItem, (discount.ItemsHit, discount.DiscountPrice * discount.NumberOfDiscountsHit)); + } + } + } + public Dictionary Items { get { return _items; } } public List Discounts { get { return _discounts; } } + public float Total { get { return _total; } } + public float TotalDiscount { get { return _totalDiscount; } } } } \ No newline at end of file diff --git a/exercise.main/SmsController.cs b/exercise.main/SmsController.cs new file mode 100644 index 00000000..b46f3f43 --- /dev/null +++ b/exercise.main/SmsController.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Twilio.Types; +using Twilio; +using Twilio.Rest.Api.V2010.Account; +using Twilio.TwiML; + +namespace exercise.main +{ + public class SmsController + { + private string _accountSid = ""; + private string _authToken = ""; + public SmsController(string accountSid, string authToken) + { + _accountSid = accountSid; + _authToken = authToken; + } + + public bool SendMessage(string sms, string phoneNumber) + { + if (string.IsNullOrEmpty(sms) || string.IsNullOrEmpty(_accountSid) || string.IsNullOrEmpty(_authToken)) + { + return false; + } + + string phoneNumberFrom = "+12566702553"; + + TwilioClient.Init(_accountSid, _authToken); + + var messageOptions = new CreateMessageOptions( + new PhoneNumber(phoneNumber)); + messageOptions.From = new PhoneNumber(phoneNumberFrom); + messageOptions.Body = sms; + + + var message = MessageResource.Create(messageOptions); + Console.WriteLine(message.Body); + + return true; + } + + /* + [HttpPost] + public TwiMLResult ReceiveMessage() + { + var messagingResponse = new MessagingResponse(); + + return TwiML(messagingResponse); + }*/ + } +} diff --git a/exercise.main/exercise.main.csproj b/exercise.main/exercise.main.csproj index 2150e379..fdd1a66a 100644 --- a/exercise.main/exercise.main.csproj +++ b/exercise.main/exercise.main.csproj @@ -7,4 +7,8 @@ enable + + + + diff --git a/exercise.tests/BobsBagelTests.cs b/exercise.tests/BobsBagelTests.cs index 61f5fa32..b23aacdc 100644 --- a/exercise.tests/BobsBagelTests.cs +++ b/exercise.tests/BobsBagelTests.cs @@ -1,15 +1,11 @@ using exercise.main; using NUnit.Framework.Interfaces; +using System.Security.Cryptography.X509Certificates; namespace exercise.tests; public class BobsBagelTests { - [SetUp] - public void Setup() - { - } - [TestCase(3, true)] [TestCase(2, false)] public void TestAddItemToBasken(int basketCapacity, bool expectedResult) From de8cb2a79ebef32593d58ff5ab85fc1e9b29bdc0 Mon Sep 17 00:00:00 2001 From: Jonas Date: Thu, 22 Aug 2024 13:41:25 +0200 Subject: [PATCH 28/29] Updated-Class-Diagram --- exercise.main/ClassDiagram.cd | 67 ++++++++++++++++++++++++++++++++++ exercise.main/ClassDiagram1.cd | 18 --------- 2 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 exercise.main/ClassDiagram.cd delete mode 100644 exercise.main/ClassDiagram1.cd diff --git a/exercise.main/ClassDiagram.cd b/exercise.main/ClassDiagram.cd new file mode 100644 index 00000000..158e58e1 --- /dev/null +++ b/exercise.main/ClassDiagram.cd @@ -0,0 +1,67 @@ + + + + + + AAAABAAAAAgACEAAAAAAAAAAgBAAAAAEAAAAAAAACAA= + Basket.cs + + + + + + AAUAAAAQAAGAAEAAAAAAAAABQBAAAACAEIAACkABAAA= + BobsBagelStore.cs + + + + + + AABAAAAAAABAAAAAAAACAAACAgEAAAAAAAAAAAAAAgA= + Discount.cs + + + + + + AhAAIAAAAQAIAAAAAAAAAAQAAACAAAAAAAAAEAAAAAA= + Item.cs + + + + + + iAAAACQAAIAAAAAABAAAIAAAAgAAAAAAAAAAAAAAAAA= + Order.cs + + + + + + AABAQAAAAAABiAAAAAAAAICAABAAQAAAAggAACQAAAA= + Receipt.cs + + + + + + AAAAAAAAAAAAAAAAACAAAABAAAgAAAAAAAAAAAAAAAA= + SmsController.cs + + + + + + QAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAABAAAAAAA= + User.cs + + + + + + AAEAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAA= + Enums.cs + + + + \ No newline at end of file diff --git a/exercise.main/ClassDiagram1.cd b/exercise.main/ClassDiagram1.cd deleted file mode 100644 index 225eaa44..00000000 --- a/exercise.main/ClassDiagram1.cd +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - AAAABAAAAAgACEAAAAAAAAAAgBAAAAAEAAAAAAAACAA= - Basket.cs - - - - - - AAUAAAAQAACAAEAAAAAAAAAAQBAAAACAAAAACkABCAA= - BobsBagelStore.cs - - - - \ No newline at end of file From 22b0112272080c09521af37dfccbe99d3ba3d569 Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 23 Aug 2024 11:42:20 +0200 Subject: [PATCH 29/29] removed-phonenumber-from-smscontroller --- exercise.main/Program.cs | 4 +++- exercise.main/SmsController.cs | 4 +--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/exercise.main/Program.cs b/exercise.main/Program.cs index d0c2d9bd..69ff3f3b 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -3,6 +3,8 @@ string yourTwilioAccountSID = ""; string yourTwilioAuthToken = ""; string yourPhoneNumber = ""; +string yourTwilioPhoneNumberFrom = ""; + BobsBagelStore store = new BobsBagelStore(); string orderMessage = string.Empty; @@ -19,7 +21,7 @@ orderMessage += store.PrintOrderHistory(); SmsController sms = new SmsController(yourTwilioAccountSID, yourTwilioAuthToken); -sms.SendMessage(orderMessage, yourPhoneNumber); +sms.SendMessage(orderMessage, yourPhoneNumber, yourTwilioPhoneNumberFrom); Receipt CreateOrderExample1() diff --git a/exercise.main/SmsController.cs b/exercise.main/SmsController.cs index b46f3f43..9ec0d345 100644 --- a/exercise.main/SmsController.cs +++ b/exercise.main/SmsController.cs @@ -20,15 +20,13 @@ public SmsController(string accountSid, string authToken) _authToken = authToken; } - public bool SendMessage(string sms, string phoneNumber) + public bool SendMessage(string sms, string phoneNumber, string phoneNumberFrom) { if (string.IsNullOrEmpty(sms) || string.IsNullOrEmpty(_accountSid) || string.IsNullOrEmpty(_authToken)) { return false; } - string phoneNumberFrom = "+12566702553"; - TwilioClient.Init(_accountSid, _authToken); var messageOptions = new CreateMessageOptions(