public CheckoutDto CalculateTotals(Cart cart, Coupon coupon = null) { if (cart.ShippingAddress == null) { throw new MissingDataException("Cannot calculate total cost - missing Shipping method"); } var itemCost = cart.Items.Sum(item => item.Price * item.Quantity); var shippingCost = _shippingCalculator.CalculateShippingCost(cart); var customerDiscount = 0.0; if (cart.CustomerType == CustomerType.Premium) { customerDiscount = 10.0; } var customerDiscountPercent = (100.0 - customerDiscount) / 100.0; var total = (itemCost + shippingCost) * customerDiscountPercent; var shoppingCartDto = _mapper.Map <ShoppingCartDto>(cart); var checkoutDto = new CheckoutDto(shoppingCartDto, shippingCost, customerDiscount, total); if (coupon != null) { return(checkoutDto with { TotalAfterCoupon = _couponEngine.CalculateDiscount(checkoutDto, coupon) }); } else { return(checkoutDto); } }
public void Process(Order order) { if (order.IsShipped) { throw new InvalidOperationException("The order is already is processed..."); } order.Shipment = new Shipment { Cost = _shippingCalculator.CalculateShippingCost(order), ShippingDate = DateTime.Today.AddDays(1) }; var totalCost = order.TotalPrice + order.Shipment.Cost; Console.WriteLine($"Total price + shipping : {totalCost}"); }
public CheckoutDto CalculateTotals(Cart cart) { var itemCost = cart.Items.Sum(item => item.Price * item.Quantity); var shippingCost = _shippingCalculator.CalculateShippingCost(cart); var customerDiscount = 0.0; if (cart.CustomerType == CustomerType.Premium) { customerDiscount = 10.0; } var customerDiscountPercent = (100.0 - customerDiscount) / 100.0; var total = (itemCost + shippingCost) * customerDiscountPercent; var shoppingCartDto = _mapper.Map <ShoppingCartDto>(cart); return(new CheckoutDto(shoppingCartDto, shippingCost, customerDiscount, total)); }
public CheckoutDto CalculateTotals(Cart cart) { if (cart.ShippingAddress == null) { throw new MissingDataException("Cannot calculate total cost - missing Shipping method"); } var itemCost = cart.Items.Sum(item => item.Price * item.Quantity); var shippingCost = _shippingCalculator.CalculateShippingCost(cart); var customerDiscount = GetCustomerDiscountPercent(cart, out var customerDiscountPercent); var total = (itemCost + shippingCost) * customerDiscountPercent; var shoppingCartDto = _mapper.Map <ShoppingCartDto>(cart); return(new CheckoutDto(shoppingCartDto, shippingCost, customerDiscount, total)); }