bool IsCategoryOfferVoucherValid(CategoryOfferVoucher voucher) { bool isValid = false; if (IsVoucherThresholdMet(voucher)) { List <Product> qualifyingProducts = BasketItems.Where(w => w.Category == voucher.ProductCategory).ToList(); if (qualifyingProducts.Count > 0) { decimal qualifyingProductsTotalPrice = 0; decimal adjustedProductDiscount = 0; foreach (Product product in qualifyingProducts) { qualifyingProductsTotalPrice = qualifyingProductsTotalPrice + product.Price; } if (voucher.Discount > qualifyingProductsTotalPrice) { adjustedProductDiscount = adjustedProductDiscount + qualifyingProductsTotalPrice; } isValid = true; _discountTotal = _discountTotal + adjustedProductDiscount; } else { _errorMessage = $"There are no products in your basket applicable to voucher {voucher.VoucherCode}."; } } return(isValid); }
bool IsVoucherValid(GiftVoucher voucher) { bool isVoucherValid = false; if (voucher.GetType() == typeof(GiftVoucher)) { isVoucherValid = true; _discountTotal = _discountTotal + voucher.Discount; } else if (!IsOfferVoucherAlreadyApplied) { if (voucher.GetType() == typeof(OfferVoucher)) { OfferVoucher offerVoucher = (OfferVoucher)voucher; isVoucherValid = IsOfferVoucherValid(offerVoucher); } else if (voucher.GetType() == typeof(CategoryOfferVoucher)) { CategoryOfferVoucher offerVoucher = (CategoryOfferVoucher)voucher; isVoucherValid = IsCategoryOfferVoucherValid(offerVoucher); } } else { _errorMessage = "You can only use one offer voucher at a time."; } return(isVoucherValid); }