-
Notifications
You must be signed in to change notification settings - Fork 305
/
Cart.cs
40 lines (31 loc) · 1.22 KB
/
Cart.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System.Collections.Generic;
using System.Linq;
namespace SportsStore.Models {
public class Cart {
private List<CartLine> lineCollection = new List<CartLine>();
public virtual 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 virtual void RemoveLine(Product product) =>
lineCollection.RemoveAll(l => l.Product.ProductID == product.ProductID);
public virtual decimal ComputeTotalValue() =>
lineCollection.Sum(e => e.Product.Price * e.Quantity);
public virtual void Clear() => lineCollection.Clear();
public virtual IEnumerable<CartLine> Lines => lineCollection;
}
public class CartLine {
public int CartLineID { get; set; }
public Product Product { get; set; }
public int Quantity { get; set; }
}
}