diff --git a/exercise.main/Basket.cs b/exercise.main/Basket.cs new file mode 100644 index 00000000..61f6d765 --- /dev/null +++ b/exercise.main/Basket.cs @@ -0,0 +1,155 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Principal; +using System.Text; + + +namespace Boolean.CSharp.Main +{ + public class Basket + { + + private Dictionary basket; + private int capacity; + private double totalPrice; + Inventory inventory; + + public Basket() + { + this.basket = new Dictionary(); + this.capacity = 10; + this.totalPrice = 0; + this.inventory = new Inventory(); + } + + public int GetNumItemsInBasket() + { + int sum = 0; + foreach (KeyValuePair item in basket) + { + sum += item.Value.Quantity; + } + return sum; + } + + public double CalculateTotal() + { + double total = 0; + foreach (KeyValuePair item in basket) + { + total += item.Value.Quantity * item.Value.GetPrice; + } + return total; + } + + public string AddItem(string item, int quantity) + { + if ((GetNumItemsInBasket() + quantity) > capacity) + { + return "There is no room in your basket for this order"; + } + + if (!inventory.GetMapTypeVariantToSKU.ContainsKey("item")) + { + return "Im sorry, that is not on the menu"; + } + + // Add + string sku = inventory.GetMapTypeVariantToSKU[item]; + if (basket.ContainsKey(sku)) + { + basket[sku].Quantity += quantity; + } + else + { + basket.Add(sku, new BasketItem( + inventory.GetInventory[sku].GetName, + inventory.GetInventory[sku].GetType, + inventory.GetInventory[sku].GetPrice, + quantity)); + } + // update price + TotalPrice = CalculateTotal(); + return $"{basket[sku].GetNametype} has been added to the basket"; + } + + public string AddBagelWithFillings(int quantity, List fillings) + { + if ((GetNumItemsInBasket() + quantity* (1 + fillings.Count)) > capacity) // each quantity is one bagle and num of fillings + { + return "There is no room in your basket for this order"; + } + + // Add plain bagle + if (basket.ContainsKey("BGLP")) + { + basket["BGLP"].Quantity += quantity; + } + else + { + basket.Add("BGLP", new BasketItem( + inventory.GetInventory["BGLP"].GetName, + inventory.GetInventory["BGLP"].GetType, + inventory.GetInventory["BGLP"].GetPrice, + quantity + )); + } + // Add Fillings + foreach (string item in fillings) + { + + + if (!inventory.GetMapTypeVariantToSKU.ContainsKey(item)) + { + continue; + } + string sku = inventory.GetMapTypeVariantToSKU[item]; + if (basket.ContainsKey(sku)) + { + basket[sku].Quantity += quantity; + } + else + { + basket.Add(sku, new BasketItem( + inventory.GetInventory[sku].GetName, + inventory.GetInventory[sku].GetType, + inventory.GetInventory[sku].GetPrice, + quantity)); + } + } + // update price + TotalPrice = CalculateTotal(); + return $"Plain Bagel and chosen Fillings has been added to the basket"; + } + + public string RemoveItem(string item) + { + + string sku = inventory.GetMapTypeVariantToSKU[item]; + if (!basket.ContainsKey(sku)) + { + return "Item was not found in your basket"; + } + // Remove + else + { + basket.Remove(sku); + } + // update price + TotalPrice = CalculateTotal(); + return $"{inventory.GetInventory[sku].GetNametype} has been removed from the basket"; + } + + + + public int Capacity { get { return capacity; } set { capacity = value; } } + public double TotalPrice { get { return totalPrice; } set { totalPrice = value; } } + public double PriceOfItem(string item) { return inventory.GetInventory[inventory.GetMapTypeVariantToSKU[item]].GetPrice; } + + public Dictionary Baskets { get { return basket; } } + public BasketItem GetBasketitem(string sku) { return basket[sku]; } + public Inventory GetInventory { get { return inventory; } } + + } +} \ No newline at end of file diff --git a/exercise.main/BasketItem.cs b/exercise.main/BasketItem.cs new file mode 100644 index 00000000..2bd0980a --- /dev/null +++ b/exercise.main/BasketItem.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Principal; +using System.Text; + + +namespace Boolean.CSharp.Main +{ + public class BasketItem : Item + { + public BasketItem(string name, string type, double price, int quantity) + : base(name, type, price, quantity) { } + + public BasketItem(string name, string type, double price, int quantity, List fillings) + : base(name, type, price, quantity, fillings) { } + } +} \ No newline at end of file diff --git a/exercise.main/Inventory.cs b/exercise.main/Inventory.cs new file mode 100644 index 00000000..a044dca3 --- /dev/null +++ b/exercise.main/Inventory.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Principal; +using System.Text; + + +namespace Boolean.CSharp.Main +{ + public class Inventory + { + private Dictionary inventory; + private Dictionary mapTypeVariantToSKU; + private List> itemsList = new List>{ + new List{"BGLO", "0,49", "Bagel", "Onion"}, + new List{"BGLP", "0,39", "Bagel", "Plain"}, + new List{ "BGLE", "0,49", "Bagel", "Everything"}, + new List{ "BGLS", "0,49", "Bagel", "Sesame"}, + + new List{ "COFB", "0,99", "Coffee", "Black"}, + new List{ "COFW", "1,19", "Coffee", "White"}, + new List{ "COFC", "1,29", "Coffee", "Capuccino"}, + new List{ "COFL", "1,29", "Coffee", "Latte"}, + + new List{ "FILB", "0,12", "Filling", "Bacon"}, + new List{ "FILE", "0,12", "Filling", "Egg"}, + new List{ "FILC", "0,12", "Filling", "Cheese"}, + new List{ "FILX", "0,12", "Filling", "Cream Cheese"}, + new List{ "FILS", "0,12", "Filling", "Smoked Salmon"}, + new List{ "FILH", "0,12", "Filling", "Ham"} + }; + + public Inventory() + { + inventory = new Dictionary(); + mapTypeVariantToSKU = new Dictionary(); + + foreach (var item in itemsList) + { + inventory.Add(item[0], new InventoryItem(item[2], item[3], double.Parse(item[1]), 0)); + mapTypeVariantToSKU.Add(inventory[item[0]].GetNametype, item[0]); + } + } + + public Dictionary GetInventory { get { return inventory; } } + public Dictionary GetMapTypeVariantToSKU { get { return mapTypeVariantToSKU; } } + + } +} \ No newline at end of file diff --git a/exercise.main/InventoryItem.cs b/exercise.main/InventoryItem.cs new file mode 100644 index 00000000..2bc03143 --- /dev/null +++ b/exercise.main/InventoryItem.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Principal; +using System.Text; + + +namespace Boolean.CSharp.Main +{ + public class InventoryItem : Item + { + public InventoryItem(string name, string type, double price, int quantity) + : base(name, type, price, quantity) { } + } +} \ No newline at end of file diff --git a/exercise.main/Item.cs b/exercise.main/Item.cs new file mode 100644 index 00000000..cc706465 --- /dev/null +++ b/exercise.main/Item.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Principal; +using System.Text; + + +namespace Boolean.CSharp.Main +{ + public class Item + { + private string name; + private string type; + private string nametype; + private double price; + private int quantity; + private List fillings; + + public Item(string name, string type, double price, int quantity) + { + this.name = name; + this.type = type; + this.nametype = name + " " + type; + this.price = price; + this.quantity = quantity; + this.fillings = new List(); + } + public Item(string name, string type, double price, int quantity, List fillings) + { + this.name = name; + this.type = type; + this.nametype = name + " " + type; + this.price = price; + this.quantity = quantity; + this.fillings = fillings; + } + + public string GetName { get { return name; } } + + public string GetType { get { return type; } } + + public string GetNametype { get { return nametype; } } + + public double GetPrice { get { return price; } } + + public int Quantity { get { return quantity; } set { quantity = value; } } + + public List GetFillings { get { return fillings; } } + + + + + + + + } +} \ No newline at end of file diff --git a/exercise.tests/Tests.cs b/exercise.tests/Tests.cs new file mode 100644 index 00000000..9ac673a4 --- /dev/null +++ b/exercise.tests/Tests.cs @@ -0,0 +1,172 @@ +namespace exercise.tests; +using Boolean.CSharp.Main; +using Newtonsoft.Json.Linq; + +public class Tests +{ + [SetUp] + public void Setup() + { + } + + [Test] + public void TestAdd() + { + Basket basket = new Basket(); + + string orderMessage = basket.AddItem("Bagel Onion", 1); + Assert.That(orderMessage, Is.EqualTo("Bagel Onion has been added to the basket")); + orderMessage = basket.AddItem("Bagel Everything", 5); + Assert.That(orderMessage, Is.EqualTo("Bagel Everything has been added to the basket")); + orderMessage = basket.AddItem("Coffee Latte", 1); + Assert.That(orderMessage, Is.EqualTo("Coffee Latte has been added to the basket")); + orderMessage = basket.AddItem("Coffee Black", 2); + Assert.That(orderMessage, Is.EqualTo("Coffee Black has been added to the basket")); + orderMessage = basket.AddItem("Filling Ham", 1); + Assert.That(orderMessage, Is.EqualTo("Filling Ham has been added to the basket")); + + Assert.That(basket.Baskets.Count, Is.EqualTo(5)); + Assert.That(basket.GetNumItemsInBasket(), Is.EqualTo(10)); + } + + [Test] + public void TestFullBasket() + { + Basket basket = new Basket(); + + string orderMessage = basket.AddItem("Bagel Onion", 11); + Assert.That(orderMessage, Is.EqualTo("There is no room in your basket for this order")); + Assert.That(basket.Baskets.Count, Is.EqualTo(0)); + + + orderMessage = basket.AddItem("Bagel Everything", 10); + Assert.That(orderMessage, Is.EqualTo("Bagel Everything has been added to the basket")); + + orderMessage = basket.AddItem("Bagel Onion", 1); + Assert.That(orderMessage, Is.EqualTo("There is no room in your basket for this order")); + + + Assert.That(basket.Baskets.Count, Is.EqualTo(1)); + Assert.That(basket.GetNumItemsInBasket(), Is.EqualTo(10)); + } + + [Test] + public void TestRemove() + { + Basket basket = new Basket(); + + string orderMessage = basket.AddItem("Bagel Everything", 10); + Assert.That(orderMessage, Is.EqualTo("Bagel Everything has been added to the basket")); + + orderMessage = basket.AddItem("Bagel Onion", 1); + Assert.That(orderMessage, Is.EqualTo("There is no room in your basket for this order")); + + orderMessage = basket.RemoveItem("Bagel Everything"); + Assert.That(orderMessage, Is.EqualTo("Bagel Everything has been removed from the basket")); + + + Assert.That(basket.Baskets.Count, Is.EqualTo(0)); + Assert.That(basket.GetNumItemsInBasket(), Is.EqualTo(0)); + } + + [Test] + public void TestRemoveNotInBasket() + { + Basket basket = new Basket(); + + string orderMessage = basket.AddItem("Bagel Everything", 5); + Assert.That(orderMessage, Is.EqualTo("Bagel Everything has been added to the basket")); + + orderMessage = basket.RemoveItem("Bagel Onion"); + Assert.That(orderMessage, Is.EqualTo("Item was not found in your basket")); + + + Assert.That(basket.Baskets.Count, Is.EqualTo(1)); + Assert.That(basket.GetNumItemsInBasket(), Is.EqualTo(5)); + } + + + [Test] + public void TestGetTotalPrice() + { + Basket basket = new Basket(); + + string orderMessage = basket.AddItem("Bagel Onion", 1); + Assert.That(orderMessage, Is.EqualTo("Bagel Onion has been added to the basket")); + orderMessage = basket.AddItem("Bagel Everything", 5); + Assert.That(orderMessage, Is.EqualTo("Bagel Everything has been added to the basket")); + orderMessage = basket.AddItem("Coffee Latte", 1); + Assert.That(orderMessage, Is.EqualTo("Coffee Latte has been added to the basket")); + orderMessage = basket.AddItem("Coffee Black", 2); + Assert.That(orderMessage, Is.EqualTo("Coffee Black has been added to the basket")); + orderMessage = basket.AddItem("Filling Ham", 1); + Assert.That(orderMessage, Is.EqualTo("Filling Ham has been added to the basket")); + + Assert.That(Math.Round(basket.TotalPrice, 2, MidpointRounding.AwayFromZero), Is.EqualTo(6.33)); + } + + + [Test] + public void TestGetPrices() + { + Basket basket = new Basket(); + + Assert.That(basket.GetInventory.GetInventory["BGLP"].GetPrice, Is.EqualTo(0.39)); + Assert.That(basket.GetInventory.GetInventory["COFW"].GetPrice, Is.EqualTo(1.19)); + Assert.That(basket.GetInventory.GetInventory["FILC"].GetPrice, Is.EqualTo(0.12)); + Assert.That(basket.GetInventory.GetInventory["COFB"].GetPrice, Is.EqualTo(0.99)); + } + + + [Test] + public void TestChangeBasketCapacity() + { + Basket basket = new Basket(); + + string orderMessage = basket.AddItem("Bagel Onion", 11); + Assert.That(orderMessage, Is.EqualTo("There is no room in your basket for this order")); + Assert.That(basket.Baskets.Count, Is.EqualTo(0)); + + basket.Capacity = 25; + + orderMessage = basket.AddItem("Bagel Onion", 15); + Assert.That(orderMessage, Is.EqualTo("Bagel Onion has been added to the basket")); + + + orderMessage = basket.AddItem("Bagel Everything", 5); + Assert.That(orderMessage, Is.EqualTo("Bagel Everything has been added to the basket")); + + Assert.That(basket.Baskets.Count, Is.EqualTo(2)); + Assert.That(basket.GetNumItemsInBasket(), Is.EqualTo(20)); + } + + + [Test] + public void TestOffMenuOrder() + { + Basket basket = new Basket(); + + string orderMessage = basket.AddItem("Hot Bagel", 1); + Assert.That(orderMessage, Is.EqualTo("Im sorry, that is not on the menu")); + + Assert.That(basket.Baskets.Count, Is.EqualTo(0)); + Assert.That(basket.GetNumItemsInBasket(), Is.EqualTo(0)); + } + + [Test] + public void TestBuildYourOwnBagel() + { + Basket basket = new Basket(); + + List fillings = new List{ "Filling Ham", "Filling Cheese", "Filling Egg" }; + + string orderMessage = basket.AddBagelWithFillings(2, fillings); + Assert.That(orderMessage, Is.EqualTo("Plain Bagel and chosen Fillings has been added to the basket")); + + Assert.That(basket.Baskets.Count, Is.EqualTo(4)); + Assert.That(basket.GetNumItemsInBasket(), Is.EqualTo(8)); + } + + + +} \ 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 9fed8e17..e629244c 100644 --- a/exercise.tests/exercise.tests.csproj +++ b/exercise.tests/exercise.tests.csproj @@ -17,4 +17,8 @@ + + + +