From 46f18432f1a381b0268f03e527e7b5e268e2154d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Wullum?= Date: Fri, 10 Jan 2025 15:51:12 +0100 Subject: [PATCH 1/5] Half of User stories completed, with adhering tests. --- Domain model.md | 14 +++++ exercise.main/Bagel.cs | 46 ++++++++++++++++ exercise.main/Basket.cs | 45 ++++++++++++++++ exercise.main/Item.cs | 31 +++++++++++ exercise.sln | 1 + exercise.tests/UnitTest1.cs | 78 ++++++++++++++++++++++++++-- exercise.tests/exercise.tests.csproj | 4 ++ 7 files changed, 215 insertions(+), 4 deletions(-) create mode 100644 Domain model.md create mode 100644 exercise.main/Bagel.cs create mode 100644 exercise.main/Basket.cs create mode 100644 exercise.main/Item.cs diff --git a/Domain model.md b/Domain model.md new file mode 100644 index 00000000..dde97b87 --- /dev/null +++ b/Domain model.md @@ -0,0 +1,14 @@ +omain Model + +| Classes | Methods/Properties | Scenario | Outputs | +|-----------|-----------------------------------|---------------------------------------------|-------------------------| +| | AddToBasket(string Id) | Adds an order to basket | | +| | RemoveFromBasket(string Id) | Removes an order form basket | | +| Basket.cs | CheckFull() | Checks if basket is at max capacity | "Basket is full" | +| | SetBasketSize(int size) | Sets a new max capacity to baskets | | +| | RemoveFromBasket(string Id) | Tries to remove nonexsistent item | "No such item" | +| | GetTotal() | See the total cost of items in basket | Total cost | +| | Property to expose price of bagel | See price of bagel before adding to basket | Bagel cost | +| | AddFillig(string Id) | Adds filling to bagel | | +| | Property to expose price filling | See price of filling before adding to bagel | Filling cost | +| | AddToBasket(string Id) | Tries to add nonexsistent item | "No such ware in stock" | \ No newline at end of file diff --git a/exercise.main/Bagel.cs b/exercise.main/Bagel.cs new file mode 100644 index 00000000..0c789a77 --- /dev/null +++ b/exercise.main/Bagel.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Metadata.Ecma335; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main +{ + public class Bagel : Item + { + private string variant; + + public struct Fillings + { + + public string Id { get; } + public string Variant { get; } + public double Price { get { return Price; } set { Price = value; } } + + public Fillings(string id, string variant, double price) + { + Id = id; + Variant = variant; + Price = price; + } + } + + public Bagel(string id, string variant, double price) : base(id, price) + { + this.variant = variant; + } + + + + public override string getVariant() + { + return variant; + } + + //public double getPrice(){ + // return base.getPrice() + this.Fillings.Price; + //} + + } +} diff --git a/exercise.main/Basket.cs b/exercise.main/Basket.cs new file mode 100644 index 00000000..b4b59cf3 --- /dev/null +++ b/exercise.main/Basket.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.Design; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main +{ + public class Basket + { + private static int maxSize; + private List items; + + public int MaxSize { get { return maxSize; } set { maxSize = value; } } + + public List Items { get { return items; } set { items = value; } } + + public void addToBasket(Item order) + { + if (!checkFull()){ items.Add(order); } + else { throw new Exception("Basket is full"); } + } + + public void removeFromBasket(Item order) + { + if (items.Contains(order)) { items.Remove(order); } + else { throw new Exception("No such item in basket"); } + } + + public bool checkFull() { return items.Count >= maxSize; } + + public void setBasketSize(bool manager, int newSize) + { + if (!manager) { throw new Exception("No persmission to alter size"); } + else { maxSize = newSize; } + } + + public double getTotalCost() + { + return 42.2; + } + + } +} diff --git a/exercise.main/Item.cs b/exercise.main/Item.cs new file mode 100644 index 00000000..4eeaa5d7 --- /dev/null +++ b/exercise.main/Item.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main +{ + public abstract class Item + { + private string id; + private double price; + + public string Id { get { return id; } } + public double Price { get { return price; } } + + public Item(string id, double price) + { + this.id = id; + this.price = price; + } + + public abstract string getVariant(); + + public double getPrice() { return price; } + + } + + + +} diff --git a/exercise.sln b/exercise.sln index 0efb5453..71950fe4 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 + Domain model.md = Domain model.md extension1.md = extension1.md extension2.md = extension2.md extension3.md = extension3.md diff --git a/exercise.tests/UnitTest1.cs b/exercise.tests/UnitTest1.cs index 7bdb8968..4770171b 100644 --- a/exercise.tests/UnitTest1.cs +++ b/exercise.tests/UnitTest1.cs @@ -1,15 +1,85 @@ +using exercise.main; + namespace exercise.tests; public class Tests { - [SetUp] - public void Setup() + //Basket basket = new Basket(); + Bagel bagelO = new Bagel("BGLO", "Bagel", 0.49); + Bagel bagelP = new Bagel("BGLP", "Bagel", 0.39); + + + [Test] + public void TestAddToBasket() + { + + Basket expBasket = new Basket { Items = new List() { bagelO, bagelP } }; + Basket basket = new Basket + { + Items = new List() { bagelO }, + MaxSize = 2 + }; + + + basket.addToBasket(bagelP); + + Assert.That(basket.Items.Contains(bagelP)); + Assert.That(basket.Items, Is.EqualTo(expBasket.Items)); + + try + { + basket.addToBasket(bagelO); + Assert.Fail(); + } + catch (Exception e) { Console.WriteLine(e); } + } + + [Test] + public void TestRemoveBasket() { + Basket basket = new Basket { Items = new List() { bagelO, bagelP } }; + Basket expBasket = new Basket { Items = new List() { bagelO } }; + + basket.removeFromBasket(bagelP); + + Assert.That(basket.Items, Does.Not.Contain(bagelP)); + Assert.That(basket.Items, Is.EqualTo(expBasket.Items)); + + try + { + basket.removeFromBasket(bagelP); + Assert.Fail(); + } + catch (Exception e) { Console.WriteLine(e); } } [Test] - public void Test1() + public void TestSetBasketSize() + { + Basket basket = new Basket + { + Items = new List() { bagelO }, + MaxSize = 2 + }; + + basket.setBasketSize(true, 1); + + Assert.That(basket.MaxSize, Is.EqualTo(1)); + + try + { + basket.setBasketSize(false, 3); + Assert.Fail(); + } + catch (Exception e) { Console.WriteLine(e); } + + } + + [Test] + public void TestGetTotalCost() { Assert.Pass(); } -} \ No newline at end of file +} + + diff --git a/exercise.tests/exercise.tests.csproj b/exercise.tests/exercise.tests.csproj index 9fed8e17..a3a97d4f 100644 --- a/exercise.tests/exercise.tests.csproj +++ b/exercise.tests/exercise.tests.csproj @@ -17,4 +17,8 @@ + + + + From c2d64329a59ea44e42efc1b4840d2f441e830b95 Mon Sep 17 00:00:00 2001 From: Hafaykon Date: Sun, 12 Jan 2025 22:09:26 +0100 Subject: [PATCH 2/5] Added methods and tests for total cost --- exercise.main/Bagel.cs | 22 ++++++++++++++++------ exercise.main/Basket.cs | 11 ++++++++++- exercise.main/Item.cs | 2 +- exercise.tests/UnitTest1.cs | 24 ++++++++++++++++++++---- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/exercise.main/Bagel.cs b/exercise.main/Bagel.cs index 0c789a77..cf61ad30 100644 --- a/exercise.main/Bagel.cs +++ b/exercise.main/Bagel.cs @@ -11,12 +11,15 @@ public class Bagel : Item { private string variant; + private List fillings_list; + + public List Fillings_list { get { return fillings_list; } set { fillings_list = value; } } + public struct Fillings { - public string Id { get; } public string Variant { get; } - public double Price { get { return Price; } set { Price = value; } } + public double Price { get; } public Fillings(string id, string variant, double price) { @@ -32,15 +35,22 @@ public Bagel(string id, string variant, double price) : base(id, price) } - public override string getVariant() { return variant; } - //public double getPrice(){ - // return base.getPrice() + this.Fillings.Price; - //} + public override double getPrice() + { + if (fillings_list != null && fillings_list.Count > 0) + { + double total_filling = 0; + foreach (Fillings filling in fillings_list) { total_filling += filling.Price; } + + return base.Price + total_filling; + } + return base.Price; + } } } diff --git a/exercise.main/Basket.cs b/exercise.main/Basket.cs index b4b59cf3..a03fb5e3 100644 --- a/exercise.main/Basket.cs +++ b/exercise.main/Basket.cs @@ -38,7 +38,16 @@ public void setBasketSize(bool manager, int newSize) public double getTotalCost() { - return 42.2; + if(items != null && items.Count > 0) + { + double totalCost = 0; + foreach (Item item in items) + { + totalCost += item.getPrice(); + } + return totalCost; + } + return 0; } } diff --git a/exercise.main/Item.cs b/exercise.main/Item.cs index 4eeaa5d7..5ce613c5 100644 --- a/exercise.main/Item.cs +++ b/exercise.main/Item.cs @@ -22,7 +22,7 @@ public Item(string id, double price) public abstract string getVariant(); - public double getPrice() { return price; } + public abstract double getPrice(); } diff --git a/exercise.tests/UnitTest1.cs b/exercise.tests/UnitTest1.cs index 4770171b..25675b4e 100644 --- a/exercise.tests/UnitTest1.cs +++ b/exercise.tests/UnitTest1.cs @@ -1,10 +1,10 @@ using exercise.main; +using static exercise.main.Bagel; namespace exercise.tests; public class Tests { - //Basket basket = new Basket(); Bagel bagelO = new Bagel("BGLO", "Bagel", 0.49); Bagel bagelP = new Bagel("BGLP", "Bagel", 0.39); @@ -12,7 +12,6 @@ public class Tests [Test] public void TestAddToBasket() { - Basket expBasket = new Basket { Items = new List() { bagelO, bagelP } }; Basket basket = new Basket { @@ -20,7 +19,6 @@ public void TestAddToBasket() MaxSize = 2 }; - basket.addToBasket(bagelP); Assert.That(basket.Items.Contains(bagelP)); @@ -34,6 +32,7 @@ public void TestAddToBasket() catch (Exception e) { Console.WriteLine(e); } } + [Test] public void TestRemoveBasket() { @@ -53,6 +52,7 @@ public void TestRemoveBasket() catch (Exception e) { Console.WriteLine(e); } } + [Test] public void TestSetBasketSize() { @@ -75,10 +75,26 @@ public void TestSetBasketSize() } + [Test] public void TestGetTotalCost() { - Assert.Pass(); + Basket basket = new Basket { Items = new List() }; + + Assert.That(basket.getTotalCost(), Is.EqualTo(0)); + + Fillings fillings = new Fillings("FILE", "Filling", 0.12); + bagelO.Fillings_list = new List() { fillings }; + double onionWFilling = 0.49 + 0.12; + + Assert.That(bagelP.getPrice(), Is.EqualTo(0.39)); + Assert.That(bagelO.getPrice(), Is.EqualTo(onionWFilling)); + + basket.Items = new List() { bagelO, bagelP }; + double totalCost = 0.39 + 0.49 + 0.12; + + Assert.That(basket.getTotalCost(), Is.EqualTo(totalCost)); + } } From 8aa212f7288a0005aeccffe4d4373c57d7e13809 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Wullum?= Date: Mon, 13 Jan 2025 15:57:19 +0100 Subject: [PATCH 3/5] Added inventory++ --- exercise.main/Bagel.cs | 20 ++++++++++++++------ exercise.main/Basket.cs | 27 +++++++++++++++++++++++---- exercise.main/Coffee.cs | 19 +++++++++++++++++++ exercise.main/Item.cs | 18 ++++++++---------- exercise.tests/UnitTest1.cs | 33 +++++++++++++++++++++++++++------ 5 files changed, 91 insertions(+), 26 deletions(-) create mode 100644 exercise.main/Coffee.cs diff --git a/exercise.main/Bagel.cs b/exercise.main/Bagel.cs index cf61ad30..806ea00c 100644 --- a/exercise.main/Bagel.cs +++ b/exercise.main/Bagel.cs @@ -15,6 +15,16 @@ public class Bagel : Item public List Fillings_list { get { return fillings_list; } set { fillings_list = value; } } + public Dictionary StockItems = new Dictionary() + { + {"FILB", new Fillings("Filling", "Bacon", 0.12)}, + {"FILE", new Fillings("Filling", "Egg", 0.12)}, + {"FILC", new Fillings("Filling", "Cheese", 0.12)}, + {"FILX", new Fillings("Filling", "Cream Cheese", 0.12)}, + {"FILS", new Fillings("Filling", "Smoked Salmon", 0.12)}, + {"FILH", new Fillings("Filling", "Ham", 0.12)}, + }; + public struct Fillings { public string Id { get; } @@ -29,15 +39,13 @@ public Fillings(string id, string variant, double price) } } - public Bagel(string id, string variant, double price) : base(id, price) - { - this.variant = variant; - } + public Bagel(string name, string variant, double price) : base(name, variant, price) { } - public override string getVariant() + public void addFillingByID(string id) { - return variant; + if (StockItems.ContainsKey(id)) { fillings_list.Add(StockItems[id]); } + else { throw new Exception("Order not in stock"); } } public override double getPrice() diff --git a/exercise.main/Basket.cs b/exercise.main/Basket.cs index a03fb5e3..ddbd9c43 100644 --- a/exercise.main/Basket.cs +++ b/exercise.main/Basket.cs @@ -10,32 +10,51 @@ namespace exercise.main public class Basket { private static int maxSize; - private List items; + private List items; // Should be dict?: private Dictionary items; + + public Dictionary StockItems = new Dictionary() + { + {"BGLO", new Bagel("Bagel", "Onion", 0.49)}, + {"BGLP", new Bagel("Bagel", "Plain", 0.39)}, + {"BGLE", new Bagel("Bagel", "Everything", 0.49)}, + {"BGLS", new Bagel("Bagel", "Sesame", 0.49)}, + {"COFB", new Coffee("Coffee", "Black", 0.99)}, + {"COFW", new Coffee("Coffee", "White", 1.19)}, + {"COFC", new Coffee("Coffee", "Cappucino", 1.29)}, + {"COFL", new Coffee("Coffee", "Latte", 1.29)} + }; + public int MaxSize { get { return maxSize; } set { maxSize = value; } } public List Items { get { return items; } set { items = value; } } - public void addToBasket(Item order) + + public void addToBasketByID(string order) { - if (!checkFull()){ items.Add(order); } - else { throw new Exception("Basket is full"); } + if (checkFull()){ throw new Exception("Basket is full"); } + else if (!StockItems.ContainsKey(order)) { throw new Exception("Order not in stock"); } + else { items.Add(StockItems[order]); } } + public void removeFromBasket(Item order) { if (items.Contains(order)) { items.Remove(order); } else { throw new Exception("No such item in basket"); } } + public bool checkFull() { return items.Count >= maxSize; } + public void setBasketSize(bool manager, int newSize) { if (!manager) { throw new Exception("No persmission to alter size"); } else { maxSize = newSize; } } + public double getTotalCost() { if(items != null && items.Count > 0) diff --git a/exercise.main/Coffee.cs b/exercise.main/Coffee.cs new file mode 100644 index 00000000..20824cbb --- /dev/null +++ b/exercise.main/Coffee.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main +{ + public class Coffee : Item + { + public Coffee(string name, string variant, double price) : base(name,variant, price) { } + + + public override double getPrice() + { + return base.Price; + } + } +} diff --git a/exercise.main/Item.cs b/exercise.main/Item.cs index 5ce613c5..f65425e8 100644 --- a/exercise.main/Item.cs +++ b/exercise.main/Item.cs @@ -8,19 +8,17 @@ namespace exercise.main { public abstract class Item { - private string id; - private double price; + public string Name { get; set; } + public double Price { get; set; } + public string Variant { get; set; } - public string Id { get { return id; } } - public double Price { get { return price; } } - - public Item(string id, double price) + public Item(string name, string variant, double price) { - this.id = id; - this.price = price; - } + Name = name; + Price = price; + Variant = variant; - public abstract string getVariant(); + } public abstract double getPrice(); diff --git a/exercise.tests/UnitTest1.cs b/exercise.tests/UnitTest1.cs index 25675b4e..6b5062cd 100644 --- a/exercise.tests/UnitTest1.cs +++ b/exercise.tests/UnitTest1.cs @@ -1,3 +1,5 @@ +using System; +using System.Linq; using exercise.main; using static exercise.main.Bagel; @@ -5,8 +7,22 @@ namespace exercise.tests; public class Tests { - Bagel bagelO = new Bagel("BGLO", "Bagel", 0.49); - Bagel bagelP = new Bagel("BGLP", "Bagel", 0.39); + Bagel bagelO = new Bagel("Bagel", "Onion", 0.49); + Bagel bagelP = new Bagel("Bagel", "Plain", 0.39); + + Coffee coffee = new Coffee("COFB", "Coffee", 0.99); + + //public Dictionary Inventory = new Dictionary() + // { + // {"BGLO", new Bagel("Bagel", "Onion", 0.49)}, + // {"BGLP", new Bagel("Bagel", "Plain", 0.39)}, + // {"BGLE", new Bagel("Bagel", "Everything", 0.49)}, + // {"BGLS", new Bagel("Bagel", "Sesame", 0.49)}, + // {"COFB", new Coffee("Coffee", "Black", 0.99)}, + // {"COFW", new Coffee("Coffee", "White", 1.19)}, + // {"COFC", new Coffee("Coffee", "Cappucino", 1.29)}, + // {"COFL", new Coffee("Coffee", "Latte", 1.29)} + // }; [Test] @@ -19,14 +35,18 @@ public void TestAddToBasket() MaxSize = 2 }; - basket.addToBasket(bagelP); + Assert.That(basket.Items.Count, Is.EqualTo(1)); - Assert.That(basket.Items.Contains(bagelP)); - Assert.That(basket.Items, Is.EqualTo(expBasket.Items)); + basket.addToBasketByID("BGLP"); + + Assert.That(basket.Items.Count, Is.EqualTo(2)); + Assert.That(basket.Items.Contains(basket.StockItems["BGLP"])); + Assert.That(basket.Items[1].Variant, Is.EqualTo(expBasket.Items[1].Variant)); + Assert.That(basket.Items[1].Variant.ToString(), Is.EqualTo("Plain")); try { - basket.addToBasket(bagelO); + basket.addToBasketByID("BGLO"); Assert.Fail(); } catch (Exception e) { Console.WriteLine(e); } @@ -87,6 +107,7 @@ public void TestGetTotalCost() bagelO.Fillings_list = new List() { fillings }; double onionWFilling = 0.49 + 0.12; + Assert.That(bagelO.Fillings_list[0].Price, Is.EqualTo(0.12)); Assert.That(bagelP.getPrice(), Is.EqualTo(0.39)); Assert.That(bagelO.getPrice(), Is.EqualTo(onionWFilling)); From a0d67885bcf0cc3a41ec9936612d86a10210e145 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Wullum?= Date: Tue, 14 Jan 2025 10:28:37 +0100 Subject: [PATCH 4/5] Finshed func for all user stories --- exercise.main/Bagel.cs | 47 ++++++++++++-------- exercise.main/Basket.cs | 33 ++++++-------- exercise.main/Coffee.cs | 3 +- exercise.main/Item.cs | 6 ++- exercise.tests/UnitTest1.cs | 85 +++++++++++++++++++++++-------------- 5 files changed, 102 insertions(+), 72 deletions(-) diff --git a/exercise.main/Bagel.cs b/exercise.main/Bagel.cs index 806ea00c..eb3e1fbf 100644 --- a/exercise.main/Bagel.cs +++ b/exercise.main/Bagel.cs @@ -11,51 +11,62 @@ public class Bagel : Item { private string variant; - private List fillings_list; + private List fillings_list; - public List Fillings_list { get { return fillings_list; } set { fillings_list = value; } } + public List Fillings_list { get { return fillings_list; } set { fillings_list = value; } } - public Dictionary StockItems = new Dictionary() + public static List StockItems = new List() { - {"FILB", new Fillings("Filling", "Bacon", 0.12)}, - {"FILE", new Fillings("Filling", "Egg", 0.12)}, - {"FILC", new Fillings("Filling", "Cheese", 0.12)}, - {"FILX", new Fillings("Filling", "Cream Cheese", 0.12)}, - {"FILS", new Fillings("Filling", "Smoked Salmon", 0.12)}, - {"FILH", new Fillings("Filling", "Ham", 0.12)}, + new Filling("FILB", "Filling", "Bacon", 0.12), + new Filling("FILE", "Filling", "Egg", 0.12), + new Filling("FILC", "Filling", "Cheese", 0.12), + new Filling("FILX", "Filling", "Cream Cheese", 0.12), + new Filling("FILS", "Filling", "Smoked Salmon", 0.12), + new Filling("FILH", "Filling", "Ham", 0.12) }; - public struct Fillings + public struct Filling { public string Id { get; } public string Variant { get; } + public string Name { get; } public double Price { get; } - public Fillings(string id, string variant, double price) + public Filling(string id, string variant, string name, double price) { Id = id; Variant = variant; + Name = name; Price = price; } } - public Bagel(string name, string variant, double price) : base(name, variant, price) { } + public Bagel(string id, string name, string variant, double price) : base(id, name, variant, price) { } + + public Bagel(string id, string name, string variant, double price, List fillings) : base(id, name, variant, price) + { + fillings_list = fillings; + } + - public void addFillingByID(string id) + public void addFilling(Filling filling) { - if (StockItems.ContainsKey(id)) { fillings_list.Add(StockItems[id]); } + if (StockItems.Contains(filling)) { fillings_list.Add(filling); } else { throw new Exception("Order not in stock"); } } + public void removeFilling(Filling filling) + { + if (fillings_list.Contains(filling)){ fillings_list.Remove(filling); } + else { throw new Exception("No such item in basket"); } + } + public override double getPrice() { if (fillings_list != null && fillings_list.Count > 0) { - double total_filling = 0; - foreach (Fillings filling in fillings_list) { total_filling += filling.Price; } - - return base.Price + total_filling; + return base.Price + fillings_list.Sum(f => f.Price); } return base.Price; } diff --git a/exercise.main/Basket.cs b/exercise.main/Basket.cs index ddbd9c43..e5d76453 100644 --- a/exercise.main/Basket.cs +++ b/exercise.main/Basket.cs @@ -10,18 +10,18 @@ namespace exercise.main public class Basket { private static int maxSize; - private List items; // Should be dict?: private Dictionary items; + private List items; - public Dictionary StockItems = new Dictionary() + public static List StockItems = new List() { - {"BGLO", new Bagel("Bagel", "Onion", 0.49)}, - {"BGLP", new Bagel("Bagel", "Plain", 0.39)}, - {"BGLE", new Bagel("Bagel", "Everything", 0.49)}, - {"BGLS", new Bagel("Bagel", "Sesame", 0.49)}, - {"COFB", new Coffee("Coffee", "Black", 0.99)}, - {"COFW", new Coffee("Coffee", "White", 1.19)}, - {"COFC", new Coffee("Coffee", "Cappucino", 1.29)}, - {"COFL", new Coffee("Coffee", "Latte", 1.29)} + new Bagel("BGLO", "Bagel", "Onion", 0.49), + new Bagel("BGLP", "Bagel", "Plain", 0.39), + new Bagel("BGLE", "Bagel", "Everything", 0.49), + new Bagel("BGLS", "Bagel", "Sesame", 0.49), + new Coffee("COFB", "Coffee", "Black", 0.99), + new Coffee("COFW", "Coffee", "White", 1.19), + new Coffee("COFC", "Coffee", "Cappucino", 1.29), + new Coffee("COFL", "Coffee", "Latte", 1.29) }; @@ -30,11 +30,11 @@ public class Basket public List Items { get { return items; } set { items = value; } } - public void addToBasketByID(string order) + public void addToBasket(Item order) { if (checkFull()){ throw new Exception("Basket is full"); } - else if (!StockItems.ContainsKey(order)) { throw new Exception("Order not in stock"); } - else { items.Add(StockItems[order]); } + else if (!StockItems.Contains(order)) { throw new Exception("Order not in stock"); } + else { items.Add(order); } } @@ -59,12 +59,7 @@ public double getTotalCost() { if(items != null && items.Count > 0) { - double totalCost = 0; - foreach (Item item in items) - { - totalCost += item.getPrice(); - } - return totalCost; + return items.Sum(i => i.getPrice()); } return 0; } diff --git a/exercise.main/Coffee.cs b/exercise.main/Coffee.cs index 20824cbb..d0c9a46f 100644 --- a/exercise.main/Coffee.cs +++ b/exercise.main/Coffee.cs @@ -8,8 +8,7 @@ namespace exercise.main { public class Coffee : Item { - public Coffee(string name, string variant, double price) : base(name,variant, price) { } - + public Coffee(string id, string name, string variant, double price) : base(id, name, variant, price) { } public override double getPrice() { diff --git a/exercise.main/Item.cs b/exercise.main/Item.cs index f65425e8..682c7736 100644 --- a/exercise.main/Item.cs +++ b/exercise.main/Item.cs @@ -8,15 +8,17 @@ namespace exercise.main { public abstract class Item { + public string Id { get; set; } public string Name { get; set; } public double Price { get; set; } public string Variant { get; set; } - public Item(string name, string variant, double price) + public Item(string id, string name, string variant, double price) { + Id = id; Name = name; - Price = price; Variant = variant; + Price = price; } diff --git a/exercise.tests/UnitTest1.cs b/exercise.tests/UnitTest1.cs index 6b5062cd..686dd0a1 100644 --- a/exercise.tests/UnitTest1.cs +++ b/exercise.tests/UnitTest1.cs @@ -7,46 +7,30 @@ namespace exercise.tests; public class Tests { - Bagel bagelO = new Bagel("Bagel", "Onion", 0.49); - Bagel bagelP = new Bagel("Bagel", "Plain", 0.39); + Bagel bagelO = (Bagel)Basket.StockItems[0]; + Bagel bagelP = (Bagel)Basket.StockItems[1]; - Coffee coffee = new Coffee("COFB", "Coffee", 0.99); + Coffee coffee = (Coffee)Basket.StockItems[4]; - //public Dictionary Inventory = new Dictionary() - // { - // {"BGLO", new Bagel("Bagel", "Onion", 0.49)}, - // {"BGLP", new Bagel("Bagel", "Plain", 0.39)}, - // {"BGLE", new Bagel("Bagel", "Everything", 0.49)}, - // {"BGLS", new Bagel("Bagel", "Sesame", 0.49)}, - // {"COFB", new Coffee("Coffee", "Black", 0.99)}, - // {"COFW", new Coffee("Coffee", "White", 1.19)}, - // {"COFC", new Coffee("Coffee", "Cappucino", 1.29)}, - // {"COFL", new Coffee("Coffee", "Latte", 1.29)} - // }; + Filling egg = Bagel.StockItems[1]; [Test] public void TestAddToBasket() { - Basket expBasket = new Basket { Items = new List() { bagelO, bagelP } }; - Basket basket = new Basket - { - Items = new List() { bagelO }, - MaxSize = 2 + Basket basket = new Basket + { + Items = new List() { bagelO }, + MaxSize = 2 }; + Basket expBasket = new Basket { Items = new List() { bagelO, bagelP } }; - Assert.That(basket.Items.Count, Is.EqualTo(1)); - - basket.addToBasketByID("BGLP"); - - Assert.That(basket.Items.Count, Is.EqualTo(2)); - Assert.That(basket.Items.Contains(basket.StockItems["BGLP"])); - Assert.That(basket.Items[1].Variant, Is.EqualTo(expBasket.Items[1].Variant)); - Assert.That(basket.Items[1].Variant.ToString(), Is.EqualTo("Plain")); - + basket.addToBasket(bagelP); + Assert.That(basket.Items.Contains(bagelP)); + Assert.That(basket.Items, Is.EqualTo(expBasket.Items)); try { - basket.addToBasketByID("BGLO"); + basket.addToBasket(bagelO); Assert.Fail(); } catch (Exception e) { Console.WriteLine(e); } @@ -103,8 +87,8 @@ public void TestGetTotalCost() Assert.That(basket.getTotalCost(), Is.EqualTo(0)); - Fillings fillings = new Fillings("FILE", "Filling", 0.12); - bagelO.Fillings_list = new List() { fillings }; + Filling fillings = new Filling("FILE", "Filling", "Egg", 0.12); + bagelO.Fillings_list = new List() { fillings }; double onionWFilling = 0.49 + 0.12; Assert.That(bagelO.Fillings_list[0].Price, Is.EqualTo(0.12)); @@ -117,6 +101,45 @@ public void TestGetTotalCost() Assert.That(basket.getTotalCost(), Is.EqualTo(totalCost)); } + + + [Test] + public void TestAddFilling() + { + bagelO.Fillings_list = new List(); + + Assert.That(bagelO.Fillings_list, Does.Not.Contain(egg)); + + bagelO.addFilling(egg); + + Assert.That(bagelO.Fillings_list, Does.Contain(egg)); + + Filling notInStock = new Filling("FILP", "Filling", "Pepperoni", 0.12); + + try + { + bagelO.addFilling(notInStock); + Assert.Fail(); + } + catch (Exception e) { Console.WriteLine(e); } + } + + [Test] + public void TestRemoveFilling() + { + bagelO.Fillings_list = new List() { egg }; + + bagelO.removeFilling(egg); + + Assert.That(bagelO.Fillings_list, Does.Not.Contain(egg)); + + try + { + bagelO.removeFilling(egg); + Assert.Fail(); + } + catch (Exception e) { Console.WriteLine(e); } + } } From 04106abe850c12cc8e3ecdd5c69d7e4102edb4b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Wullum?= Date: Tue, 14 Jan 2025 15:58:51 +0100 Subject: [PATCH 5/5] Finsihed Core --- exercise.main/Bagel.cs | 29 +++++-- exercise.main/Basket.cs | 34 +++++---- exercise.main/Coffee.cs | 15 ++++ exercise.main/Item.cs | 8 ++ exercise.main/Program.cs | 148 +++++++++++++++++++++++++++++++++++- exercise.tests/UnitTest1.cs | 16 ++-- 6 files changed, 220 insertions(+), 30 deletions(-) diff --git a/exercise.main/Bagel.cs b/exercise.main/Bagel.cs index eb3e1fbf..c7ac6206 100644 --- a/exercise.main/Bagel.cs +++ b/exercise.main/Bagel.cs @@ -9,7 +9,6 @@ namespace exercise.main { public class Bagel : Item { - private string variant; private List fillings_list; @@ -39,6 +38,11 @@ public Filling(string id, string variant, string name, double price) Name = name; Price = price; } + + public string getFillingToString() + { + return $"{Id}, {Name}, {Variant}, {Price}"; + } } public Bagel(string id, string name, string variant, double price) : base(id, name, variant, price) { } @@ -49,19 +53,29 @@ public Bagel(string id, string name, string variant, double price, List } + public override List getFillings() + { + return fillings_list; + } - public void addFilling(Filling filling) + public override void addFilling(string order) { - if (StockItems.Contains(filling)) { fillings_list.Add(filling); } - else { throw new Exception("Order not in stock"); } + var fillingOrder = StockItems.FirstOrDefault(x => x.Id == order); + + if (fillingOrder.Id == null) { throw new Exception("Order not in stock"); } + else { fillings_list.Add(fillingOrder); } } - public void removeFilling(Filling filling) + + public override void removeFilling(string order) { - if (fillings_list.Contains(filling)){ fillings_list.Remove(filling); } - else { throw new Exception("No such item in basket"); } + var fillingOrder = fillings_list.FirstOrDefault(x => x.Id == order); + + if (fillingOrder.Id == null) { throw new Exception("No such item in basket"); } + else { fillings_list.Remove(fillingOrder); } } + public override double getPrice() { if (fillings_list != null && fillings_list.Count > 0) @@ -71,5 +85,6 @@ public override double getPrice() return base.Price; } + } } diff --git a/exercise.main/Basket.cs b/exercise.main/Basket.cs index e5d76453..d9a72947 100644 --- a/exercise.main/Basket.cs +++ b/exercise.main/Basket.cs @@ -14,7 +14,7 @@ public class Basket public static List StockItems = new List() { - new Bagel("BGLO", "Bagel", "Onion", 0.49), + new Bagel("BGLO", "Bagel", "Onion", 0.49, new List()), new Bagel("BGLP", "Bagel", "Plain", 0.39), new Bagel("BGLE", "Bagel", "Everything", 0.49), new Bagel("BGLS", "Bagel", "Sesame", 0.49), @@ -30,23 +30,30 @@ public class Basket public List Items { get { return items; } set { items = value; } } - public void addToBasket(Item order) + //public void addToBasket(Item order) + //{ + // if (checkFull()){ throw new Exception("Basket is full"); } + // else if (!StockItems.Contains(order)) { throw new Exception("Order not in stock"); } + // else { items.Add(order); } + //} + + public void addToBasket(string order) { - if (checkFull()){ throw new Exception("Basket is full"); } - else if (!StockItems.Contains(order)) { throw new Exception("Order not in stock"); } - else { items.Add(order); } + var item = StockItems.FirstOrDefault(i => i.Id == order); + + if (items.Count >= maxSize) { throw new Exception("Basket is full"); } + else if (item == null) { throw new Exception("Order not in stock"); } + else { items.Add(item); } } - public void removeFromBasket(Item order) + public void removeFromBasket(string order) { - if (items.Contains(order)) { items.Remove(order); } - else { throw new Exception("No such item in basket"); } - } - - - public bool checkFull() { return items.Count >= maxSize; } + var item = items.FirstOrDefault(i => i.Id == order); + if (item == null) { throw new Exception("No such item in basket"); } + else { items.Remove(item); } + } public void setBasketSize(bool manager, int newSize) { @@ -54,12 +61,11 @@ public void setBasketSize(bool manager, int newSize) else { maxSize = newSize; } } - public double getTotalCost() { if(items != null && items.Count > 0) { - return items.Sum(i => i.getPrice()); + return Math.Round(items.Sum(i => i.getPrice()), 2); } return 0; } diff --git a/exercise.main/Coffee.cs b/exercise.main/Coffee.cs index d0c9a46f..164a2096 100644 --- a/exercise.main/Coffee.cs +++ b/exercise.main/Coffee.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using static exercise.main.Bagel; namespace exercise.main { @@ -14,5 +15,19 @@ public override double getPrice() { return base.Price; } + + public override void addFilling(string order) + { + throw new Exception("Currently no filling for coffee"); + } + public override void removeFilling(string order) + { + throw new Exception("Currently no filling for coffee"); + } + + public override List getFillings() + { + throw new Exception("Currently no filling for coffee"); + } } } diff --git a/exercise.main/Item.cs b/exercise.main/Item.cs index 682c7736..7cb26ebe 100644 --- a/exercise.main/Item.cs +++ b/exercise.main/Item.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using static exercise.main.Bagel; namespace exercise.main { @@ -23,7 +24,14 @@ public Item(string id, string name, string variant, double price) } public abstract double getPrice(); + public abstract void addFilling(string order); + public abstract void removeFilling(string order); + public abstract List getFillings(); + public string getItemToString() + { + return $"{Id}, {Name}, {Variant}, {Price}"; + } } diff --git a/exercise.main/Program.cs b/exercise.main/Program.cs index 3751555c..bcca080c 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -1,2 +1,148 @@ // See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +using System.ComponentModel; +using exercise.main; + +Console.WriteLine("- - - Welcome to Bob's Bagels! - - -"); +Console.WriteLine("Actions:\n v: View basket \n m: See menu\n a: Add [ID] to basket\n r: Remove from basket\n f: Add filling\n rf: remove filling\n ms: Set new basket size\n e: Exit\n"); + +Basket basket = new Basket() { Items = new List(), MaxSize = 10 }; + +string action = Console.ReadLine(); + +while (action != "e") +{ + Console.WriteLine("Actions:\n v: View basket \n m: See menu\n a: Add [ID] to basket\n r: Remove from basket\n f: Add filling\n rf: remove filling\n ms: Set new basket size\n e: Exit\n"); + + switch (action) + { + case "m": + Console.WriteLine("\nID NAME VARIANT PRICE"); + Basket.StockItems.ForEach(i => Console.WriteLine(i.getItemToString())); + break; + + case "v": + Console.WriteLine("\nID NAME VARIANT PRICE"); + basket.Items.ForEach(i => Console.WriteLine(i.getItemToString())); + Console.WriteLine("Fillings: "); + try { basket.Items.ForEach(i => i.getFillings().ForEach(j => Console.WriteLine(j.getFillingToString()))); } + catch(Exception ex) { } + + Console.WriteLine($"_______________________\nTotal cost: £{basket.getTotalCost()}"); + break; + + case "a": + Console.WriteLine("What [ID] would you like to add to basket? (eg. BGLO)\n b: Back\n"); + string addItemId = Console.ReadLine(); + while (addItemId != "b") + { + try + { + basket.addToBasket(addItemId); + Console.WriteLine("Added to basket!\n"); + } + catch (Exception ex) { Console.WriteLine(ex.Message); } + + addItemId = Console.ReadLine(); + } + break; + + case "r": + Console.WriteLine("What [ID] would you like to remove from basket? (eg. BGLO)\n b: Back\n"); + string removeItemId = Console.ReadLine(); + while (removeItemId != "b") + { + try + { + basket.removeFromBasket(removeItemId); + Console.WriteLine("Removed from basket!\n"); + } + catch (Exception ex) { Console.WriteLine(ex.Message); } + + removeItemId = Console.ReadLine(); + } + break; + + case "f": + if (!basket.Items.Any(i => i.Name == "Bagel")) + { + Console.WriteLine("No bagel in basket"); + break; + } + Console.WriteLine("What bagel [ORDER] would you like yo add filling to? (eg. 1)\n"); + Console.WriteLine("\nORDER ID NAME VARIANT PRICE"); + basket.Items.ForEach(i => Console.WriteLine($"{basket.Items.IndexOf(i)}: {i.getItemToString()}")); + + string order = Console.ReadLine(); + int orderNr = 0; + if (Int32.TryParse(order, out orderNr) && (basket.Items.Count >= orderNr || orderNr >= 0)) + { + Console.WriteLine("What filling would you like to add? (eg. FILE)\n b: Back\n"); + string fill = Console.ReadLine(); + while (fill != "b") + { + try + { + basket.Items[orderNr].addFilling(fill); + Console.WriteLine("Added to filling to bagel!\n"); + } + catch (Exception ex) { Console.WriteLine(ex.Message); } + + fill = Console.ReadLine(); + } + break; + } + else { Console.WriteLine("Invalid order. ") ; } + break; + + + case "rf": + if (!basket.Items.Any(i => i.getFillings().Count > 0)) + { + Console.WriteLine("No fillings to remove"); + break; + } + Console.WriteLine("What bagel [ORDER] would you like remove filling from? (eg. 1)\n"); + Console.WriteLine("\nORDER ID NAME VARIANT PRICE"); + basket.Items.ForEach(i => Console.WriteLine($"{basket.Items.IndexOf(i)}: {i.getItemToString()}")); + + string orderF = Console.ReadLine(); + int orderNrF = 0; + if (Int32.TryParse(orderF, out orderNrF) && (basket.Items.Count >= orderNrF || orderNrF >= 0)) + { + Console.WriteLine("What filling would you like to remove? (eg. FILE)\n b: Back\n"); + string fill = Console.ReadLine(); + while (fill != "b") + { + try + { + basket.Items[orderNrF].removeFilling(fill); + Console.WriteLine("Removed filling from bagel!\n"); + } + catch (Exception ex) { Console.WriteLine(ex.Message); } + + fill = Console.ReadLine(); + } + break; + } + else { Console.WriteLine("Invalid order."); } + break; + + case "ms": + Console.WriteLine("Manager mode activated\n Set new size of basket: "); + string newStr = Console.ReadLine(); + + int newSize = 0; + if (Int32.TryParse(newStr, out newSize)) + { + basket.MaxSize = newSize; + Console.WriteLine($"New basket size set to {newStr}"); + } + else { Console.WriteLine("Invalid size."); } + break; + + default: + break; + } + action = Console.ReadLine(); +} +Console.WriteLine("Goodbye!"); diff --git a/exercise.tests/UnitTest1.cs b/exercise.tests/UnitTest1.cs index 686dd0a1..936b478b 100644 --- a/exercise.tests/UnitTest1.cs +++ b/exercise.tests/UnitTest1.cs @@ -25,12 +25,12 @@ public void TestAddToBasket() }; Basket expBasket = new Basket { Items = new List() { bagelO, bagelP } }; - basket.addToBasket(bagelP); + basket.addToBasket("BGLP"); Assert.That(basket.Items.Contains(bagelP)); Assert.That(basket.Items, Is.EqualTo(expBasket.Items)); try { - basket.addToBasket(bagelO); + basket.addToBasket("BGLO"); Assert.Fail(); } catch (Exception e) { Console.WriteLine(e); } @@ -43,14 +43,14 @@ public void TestRemoveBasket() Basket basket = new Basket { Items = new List() { bagelO, bagelP } }; Basket expBasket = new Basket { Items = new List() { bagelO } }; - basket.removeFromBasket(bagelP); + basket.removeFromBasket("BGLP"); Assert.That(basket.Items, Does.Not.Contain(bagelP)); Assert.That(basket.Items, Is.EqualTo(expBasket.Items)); try { - basket.removeFromBasket(bagelP); + basket.removeFromBasket("BGLP"); Assert.Fail(); } catch (Exception e) { Console.WriteLine(e); } @@ -110,7 +110,7 @@ public void TestAddFilling() Assert.That(bagelO.Fillings_list, Does.Not.Contain(egg)); - bagelO.addFilling(egg); + bagelO.addFilling("FILE"); Assert.That(bagelO.Fillings_list, Does.Contain(egg)); @@ -118,7 +118,7 @@ public void TestAddFilling() try { - bagelO.addFilling(notInStock); + bagelO.addFilling("FILP"); Assert.Fail(); } catch (Exception e) { Console.WriteLine(e); } @@ -129,13 +129,13 @@ public void TestRemoveFilling() { bagelO.Fillings_list = new List() { egg }; - bagelO.removeFilling(egg); + bagelO.removeFilling("FILE"); Assert.That(bagelO.Fillings_list, Does.Not.Contain(egg)); try { - bagelO.removeFilling(egg); + bagelO.removeFilling("FILE"); Assert.Fail(); } catch (Exception e) { Console.WriteLine(e); }