Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using MbmStore.Models;
  6.  
  7. namespace MbmStore.Models
  8. {
  9. public class Cart
  10. {
  11. private List<CartLine> lines = new List<CartLine>();
  12. public decimal TotalPrice
  13. {
  14. // Linq syntax
  15. get { return lines.Sum(e => e.Product.Price * e.Quantity); }
  16. }
  17. public List<CartLine> Lines { get { return lines; } }
  18. public Cart() { }
  19. public void AddItem(Product product, int quantity)
  20. {
  21. CartLine item = lines.Where(p => p.Product.ProductId == product.ProductId).FirstOrDefault();
  22. if (item == null)
  23. {
  24. lines.Add(new CartLine { Product = product, Quantity = quantity });
  25. }
  26. else
  27. {
  28. item.Quantity += quantity;
  29. }
  30. }
  31. public void RemoveItem(Product product)
  32. {
  33. lines.RemoveAll(i => i.Product.ProductId == product.ProductId);
  34. }
  35. public void Clear()
  36. {
  37. lines.Clear();
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement