diff --git a/SportsStore/SportsStore.Domain/Entities/Cart.cs b/SportsStore/SportsStore.Domain/Entities/Cart.cs new file mode 100644 index 0000000..9e8dea5 --- /dev/null +++ b/SportsStore/SportsStore.Domain/Entities/Cart.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SportsStore.Domain.Entities +{ + class Cart + { + private List lineCollection = new List(); + + public void AddItem(Product product, int quantity) + { + CartLine line = lineCollection + .Where(p => p.Product.ProductID == product.ProductID) + .FirstOrDefault(); + + if (line == null) + { + lineCollection.Add(new CartLine + { + Product = product, + Quantity = quantity + }); + } + else + { + line.Quantity += quantity; + } + } + + public void RemoveLine(Product product) + { + lineCollection.RemoveAll(l => l.Product.ProductID == product.ProductID); + } + + public void Clear() + { + lineCollection.Clear(); + } + + public IEnumerable Lines + { + get { return lineCollection; } + } + } + + public class CartLine + { + public Product Product { get; set; } + public int Quantity { get; set; } + } +} diff --git a/SportsStore/SportsStore.Domain/SportsStore.Domain.csproj b/SportsStore/SportsStore.Domain/SportsStore.Domain.csproj index be1143d..3b7d79a 100644 --- a/SportsStore/SportsStore.Domain/SportsStore.Domain.csproj +++ b/SportsStore/SportsStore.Domain/SportsStore.Domain.csproj @@ -75,6 +75,7 @@ +