Skip to content

Made it so products can have their amounts varied at runtime #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions conferences/13-inheritance/accounting/Cart.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Accounting
using System;
namespace Accounting
{
public class Cart
{
Expand Down Expand Up @@ -31,7 +32,11 @@ public virtual IEnumerable<Product> Products()

public void Add(Product product)
{
this.products.Add(product);
int index = products.FindIndex(p => p.Name == product.Name && p.GetType() == product.GetType());
if (index == -1)
this.products.Add();
else
this.products[index].AddUnits(product.Units);
}
}

Expand Down
7 changes: 6 additions & 1 deletion conferences/13-inheritance/accounting/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ 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; }

Expand Down
4 changes: 2 additions & 2 deletions conferences/13-inheritance/app/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down