public double Calculate(DiscountCalculationModel model)
        {
            // Discount applied only on first purchase by the customer
            var isFirstPurchase = !_db.Purchases.Any(p => p.CustomerId == model.CustomerId);

            return(isFirstPurchase ? model.Sum * 0.7 : model.Sum);
        }
Beispiel #2
0
        public double Calculate(DiscountCalculationModel model)
        {
            // Discount applied if the overall value of previous purchases exceeds
            var    customerPurchases     = _db.Purchases.Where(p => p.CustomerId == model.CustomerId).ToList();
            double customerProvidedValue = 0;

            foreach (var purchase in customerPurchases)
            {
                customerProvidedValue += purchase.Units * purchase.Product.Price;
            }

            return(customerProvidedValue > 800 ? model.Sum * 0.9 : model.Sum);
        }
Beispiel #3
0
        public ActionResult Index(CalculatorViewModel model)
        {
            if (ModelState.IsValid)
            {
                var pickedDiscount           = model.DiscountTypes.FirstOrDefault(d => d.Id == model.PickedDiscountTypeId);
                var discountCalculationModel = new DiscountCalculationModel();
                Mapper.Map(model, discountCalculationModel);

                // The calculcator factory gets the necessary Calculator service implementation based on the provided discount type name
                model.DiscountedSum = _calculatorFactory.Get(pickedDiscount.Name).Calculate(discountCalculationModel);
                ModelState.Clear();
            }

            return(View(model));
        }
 public double Calculate(DiscountCalculationModel model)
 {
     // Discount applied only when the unit count exceeds 1000
     return(model.Units > 1000 ? model.Sum * 0.8 : model.Sum);
 }