public Order AddOrder(string productId) { var product = storeProducts.Where(x => x.Id == productId)?.FirstOrDefault(); var p = userOrder.Products.Where(x => x.Product.Id == productId)?.FirstOrDefault(); if (p != null) { p.Quantity += 1; } else { userOrder.Products.Add(new ProductOrder() { Product = product, Quantity = 1 }); } var cal = new Q2Cal(); userOrder = cal.CalulateOrder(userOrder); return(userOrder); }
public void CalulateOrderSuccess() { Order inputOrder = new Order() { Products = new List <ProductOrder>() { new ProductOrder { Product = new Product { Id = "p01", Name = "iPhoneXSMax", Is3Free1 = true, Price = 2000, }, Quantity = 9 }, new ProductOrder { Product = new Product { Id = "p02", Name = "HUAWEI Mate 20 X", Is3Free1 = true, Price = 4000, }, Quantity = 7 }, new ProductOrder { Product = new Product { Id = "p03", Name = "iPhoneX", Is3Free1 = false, Price = 3500, }, Quantity = 8 }, }, CheckOutPrice = 0, Discount = 0, TotalPrice = 0, }; var cal = new Q2Cal(); var result = cal.CalulateOrder(inputOrder); Order expectedOrder = new Order() { Products = new List <ProductOrder>() { new ProductOrder { Product = new Product { Id = "p01", Name = "iPhoneXSMax", Is3Free1 = true, Price = 2000, }, Quantity = 9 }, new ProductOrder { Product = new Product { Id = "p02", Name = "HUAWEI Mate 20 X", Is3Free1 = true, Price = 4000, }, Quantity = 7 }, new ProductOrder { Product = new Product { Id = "p03", Name = "iPhoneX", Is3Free1 = false, Price = 3500, }, Quantity = 8 }, }, TotalPrice = 74000, Discount = 8000, CheckOutPrice = 66000, }; result.Should().BeEquivalentTo(expectedOrder); }