public void AddItem(Product product, int quantity) { CartLine line = lineCollection .FirstOrDefault(p => p.Product.ProductID == product.ProductID); if (line == null) { lineCollection.Add(new CartLine { Product = product, Quantity = quantity }); } else { line.Quantity += quantity; } }
public void Calculate_Cart_Total() { Product p1 = new Product { ProductID = 1, Name = "P1", Price = 100 }; Product p2 = new Product { ProductID = 2, Name = "P2", Price = 100 }; Product p3 = new Product { ProductID = 3, Name = "P3", Price = 100 }; Product p4 = new Product { ProductID = 4, Name = "P4", Price = 100 }; //创建一个购物车 Cart cart = new Cart(); cart.AddItem(p1, 10); cart.AddItem(p2, 2); Assert.AreEqual(cart.ComputeTotalValue(), 1200); }
public void Can_Add_New_Lines() { Product p1 = new Product { ProductID = 1, Name = "P1", Price = 100 }; Product p2 = new Product { ProductID = 2, Name = "P2", Price = 100 }; Product p3 = new Product { ProductID = 3, Name = "P3", Price = 100 }; Product p4 = new Product { ProductID = 4, Name = "P4", Price = 100 }; Cart cart = new Cart(); cart.AddItem(p1, 10); cart.AddItem(p2, 2); var result = cart.Lines.ToArray(); Assert.AreEqual(result.Length, 2); Assert.AreEqual(result[0].Product, p1); Assert.AreEqual(result[1].Product, p2); }
public void Can_Clear_Contents() { Product p1 = new Product { ProductID = 1, Name = "P1", Price = 100 }; Product p2 = new Product { ProductID = 2, Name = "P2", Price = 100 }; Product p3 = new Product { ProductID = 3, Name = "P3", Price = 100 }; Product p4 = new Product { ProductID = 4, Name = "P4", Price = 100 }; //创建一个购物车 Cart cart = new Cart(); cart.AddItem(p1, 10); cart.AddItem(p2, 2); cart.ClearItem(); Assert.AreEqual(cart.Lines.Count(), 0); }
public void Can_Remove_Line() { Product p1 = new Product { ProductID = 1, Name = "P1", Price = 100 }; Product p2 = new Product { ProductID = 2, Name = "P2", Price = 100 }; Product p3 = new Product { ProductID = 3, Name = "P3", Price = 100 }; Product p4 = new Product { ProductID = 4, Name = "P4", Price = 100 }; //创建一个购物车 Cart cart = new Cart(); cart.AddItem(p1, 10); cart.AddItem(p2, 2); cart.AddItem(p1, 3); cart.RemoveItem(p2); Assert.AreEqual(cart.Lines.Count(c => c.Product == p2), 0); Assert.AreEqual(cart.Lines.First(p => p.Product==p1).Quantity, 13); }
public int RemoveItem(Product product) { return lineCollection.RemoveAll(l => l.Product.ProductID == product.ProductID); }