From c2e3708be0a8b629de332e0046f8f8f15630b3b8 Mon Sep 17 00:00:00 2001 From: leoamaro01 Date: Tue, 26 Apr 2022 15:26:23 -0400 Subject: [PATCH 1/3] Made it so products can have their amounts varied at runtime --- conferences/13-inheritance/accounting/Cart.cs | 6 +++++- conferences/13-inheritance/accounting/Product.cs | 2 +- conferences/13-inheritance/app/Program.cs | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/conferences/13-inheritance/accounting/Cart.cs b/conferences/13-inheritance/accounting/Cart.cs index ea03444..3a615f6 100644 --- a/conferences/13-inheritance/accounting/Cart.cs +++ b/conferences/13-inheritance/accounting/Cart.cs @@ -31,7 +31,11 @@ public virtual IEnumerable Products() public void Add(Product product) { - this.products.Add(product); + int index = products.FindIndex(p=>p.Name == product.Name); + if(index == -1) + this.products.Add(product); + else + this.products[index].Units += product.Units; } } diff --git a/conferences/13-inheritance/accounting/Product.cs b/conferences/13-inheritance/accounting/Product.cs index 3c0e0d4..b57e22d 100644 --- a/conferences/13-inheritance/accounting/Product.cs +++ b/conferences/13-inheritance/accounting/Product.cs @@ -11,7 +11,7 @@ public Product(string name, int price, int units) public string Name { get; private set; } public int Price { get; private set; } - public int Units { get; private set; } + public int Units { get; set; } public virtual int TotalCost() { diff --git a/conferences/13-inheritance/app/Program.cs b/conferences/13-inheritance/app/Program.cs index e89f056..ee6de10 100644 --- a/conferences/13-inheritance/app/Program.cs +++ b/conferences/13-inheritance/app/Program.cs @@ -8,8 +8,8 @@ class Program new Product("🐟 Pescado (kg)", 100, 10), new Product("🐔 Pollo (kg)", 150, 1), new Product("🐔 Pollo (kg)", 150, 5), - new Product("🥚 Huevo", 10, 1), - new Product("🥚 Huevos (caja)", 10, 30), + new Product("🥚 Huevos (unidades)", 10, 1), + new Product("🥚 Huevos (unidades)", 10, 30), }; static void Main() From 6930b0946cab503aea0a2c4e22600278ce32af14 Mon Sep 17 00:00:00 2001 From: leoamaro01 Date: Tue, 26 Apr 2022 16:06:25 -0400 Subject: [PATCH 2/3] Reformat Changed Units property's "set" accessor back to private. Added AddUnits(int) method for this purpose. Reformatted Add method in Cart to reflect this change. --- conferences/13-inheritance/accounting/Cart.cs | 6 +++--- conferences/13-inheritance/accounting/Product.cs | 9 +++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/conferences/13-inheritance/accounting/Cart.cs b/conferences/13-inheritance/accounting/Cart.cs index 3a615f6..a7b0c09 100644 --- a/conferences/13-inheritance/accounting/Cart.cs +++ b/conferences/13-inheritance/accounting/Cart.cs @@ -31,11 +31,11 @@ public virtual IEnumerable Products() public void Add(Product product) { - int index = products.FindIndex(p=>p.Name == product.Name); - if(index == -1) + int index = products.FindIndex(p => p.Name == product.Name); + if (index == -1) this.products.Add(product); else - this.products[index].Units += product.Units; + this.products[index].AddUnits(product.Units); } } diff --git a/conferences/13-inheritance/accounting/Product.cs b/conferences/13-inheritance/accounting/Product.cs index b57e22d..9791a96 100644 --- a/conferences/13-inheritance/accounting/Product.cs +++ b/conferences/13-inheritance/accounting/Product.cs @@ -11,20 +11,25 @@ public Product(string name, int price, int units) public string Name { get; private set; } public int Price { get; private set; } - public int Units { get; set; } + public int Units { get; private set; } public virtual int TotalCost() { return this.Price * this.Units; } + public virtual void AddUnits(int units) + { + this.Units += units; + } + public override string ToString() { return $"{this.Name} - ${this.Price} (x{this.Units}) = ${this.TotalCost()}"; } } - public class DiscountProduct: Product + public class DiscountProduct : Product { public double Discount { get; private set; } From 7e52cebbeece9b7e127261bcefbfe4fb40dca028 Mon Sep 17 00:00:00 2001 From: leoamaro01 Date: Tue, 26 Apr 2022 16:41:00 -0400 Subject: [PATCH 3/3] Added support for multiple types of products. --- conferences/13-inheritance/accounting/Cart.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/conferences/13-inheritance/accounting/Cart.cs b/conferences/13-inheritance/accounting/Cart.cs index a7b0c09..fb9a17d 100644 --- a/conferences/13-inheritance/accounting/Cart.cs +++ b/conferences/13-inheritance/accounting/Cart.cs @@ -1,4 +1,5 @@ -namespace Accounting +using System; +namespace Accounting { public class Cart { @@ -31,9 +32,9 @@ public virtual IEnumerable Products() public void Add(Product product) { - int index = products.FindIndex(p => p.Name == product.Name); + int index = products.FindIndex(p => p.Name == product.Name && p.GetType() == product.GetType()); if (index == -1) - this.products.Add(product); + this.products.Add(); else this.products[index].AddUnits(product.Units); }