public double Buy(params string[] basketByNames) { double price = 0; if (StoreStock.Catalog != null && basketByNames.Any()) { ShoppingCart.ShoppingCart shoppingCart = new ShoppingCart.ShoppingCart(); foreach (string item in basketByNames) { Book book = StoreStock.Catalog.FirstOrDefault(x => x.Name == item); if (book == null) { continue; } Category category = StoreStock.Category.FirstOrDefault(x => x.Name == book.Category); shoppingCart.Add(new Product() { Category = category, Book = book }); } price = shoppingCart.GetTotalPrice(); } return(price); }
public CheckoutService(UserManager <User> userManager, FurryPalDbContext dbContext, ShoppingCart.ShoppingCart cart) { this.userManager = userManager; this.dbContext = dbContext; this.cart = cart; }
public IDiscount DiscountFor(ShoppingCart.ShoppingCart shoppingCart) { return(_discounts .Where(discount => discount.IsApplicableTo(shoppingCart)) .OrderByDescending(discount => discount.Percentage) .DefaultIfEmpty(new NoDiscount()) .First()); }
public List <BillDiscount> ApplyOffer(ShoppingCart.ShoppingCart shoppingCart) { var discountsResult = new List <BillDiscount>(); _firstHandler?.ApplyOffer(shoppingCart, discountsResult); return(discountsResult); }
public void print(string writePath, Bill bill, ShoppingCart.ShoppingCart cart, PriceList priceList) { var header = "\tSHOPPING BILL"; var line = "--------------------------------"; var discountsLine = "DISCOUNTS:"; var totalLine = "TOTAL:"; var subtotal = $"{"Subtotal:",15} {Math.Round(bill.Subtotal, 2),5}$"; var total = $"{"Total:",15} {Math.Round(bill.Total, 2),5}$"; var items = cart.GetItems(); var discountItems = bill.BillDiscounts; var s = $"{"[Item]",5} {"[Qty]",8} {"[Rate]",7} {"[Amount]",7}\n"; foreach (var t in items) { s += $"{t,6} {cart.GetQuantity(t),6} {Math.Round(priceList.GetPriceOf(t), 2),8}$ {Math.Round(priceList.GetPriceOf(t) * cart.GetQuantity(t), 2),5}$\n"; } string str = null; str = discountItems.Count > 0 ? discountItems.Aggregate(str, (current, t) => current + $"{t.GetDiscountName()} - {t.GetDiscountPercent(),3}% off:{t.GetDiscountValue().ToString(),4}$\n") : "No Offers"; Directory.CreateDirectory(writePath.Split(new char[] { '\\' })[0]); try { using (var sw = new StreamWriter(writePath, false, Encoding.Default)) { sw.WriteLine(line); sw.WriteLine(header); sw.WriteLine(line); sw.WriteLine(s); sw.WriteLine(line); sw.WriteLine(discountsLine); sw.WriteLine(line); sw.WriteLine(str); sw.WriteLine(line); sw.WriteLine(totalLine); sw.WriteLine(line); sw.WriteLine(subtotal); sw.WriteLine(total); sw.WriteLine(line); } Console.WriteLine("Bill has been printed."); } catch (Exception e) { Console.WriteLine(e.Message); } }
public Bill GenerateBill(ShoppingCart.ShoppingCart shoppingCart) { double subtotal = CalculateSubTotal(shoppingCart); List <BillDiscount> billDiscounts = _mainOfferHandler.ApplyOffer(shoppingCart); double discount = totalDiscount(billDiscounts); double total = subtotal - discount; return(new Bill(subtotal, total, billDiscounts)); }
private static ShoppingCartItem CreateCustomerShoppingCartItem(ProductTypeEnum productType, ShoppingCart.ShoppingCart cart) { var shoppingCartItem = new ShoppingCartItem(); shoppingCartItem.ProductId = (int)productType; cart.Items.Add(shoppingCartItem); return(shoppingCartItem); }
private double CalculateSubTotal(ShoppingCart.ShoppingCart shoppingCart) { double subtotal = 0; foreach (var item in shoppingCart.GetItems()) { subtotal += (_priceList.GetPriceOf(item) * shoppingCart.GetQuantity(item)); } return(subtotal); }
private ShoppingCart.ShoppingCart GetCustomerShoppingCart(string customerName) { ShoppingCart.ShoppingCart cart = null; for (var i = 0; i < _carts.Count; ++i) { if (_carts[i].CustomerName == customerName) { cart = _carts[i]; break; } } return(cart); }
private static ShoppingCartItem GetCustomerShoppingCartItem(ProductTypeEnum productType, ShoppingCart.ShoppingCart cart) { ShoppingCartItem shoppingCartItem = null; for (var i = 0; i < cart.Items.Count; ++i) { var item = cart.Items[i]; if (item.ProductId == (int)productType) { shoppingCartItem = item; break; } } return(shoppingCartItem); }
public override void ApplyOffer(ShoppingCart.ShoppingCart cart, List <BillDiscount> billDiscountResult) { var itemQuantity = cart.GetQuantity(_offerItem.name); var targetQuantity = cart.GetQuantity(_offerItem.discountItem.name); if (itemQuantity > 0 && targetQuantity > 0 && itemQuantity >= _offerItem.quantity) { BillDiscount billDiscount = ProcessDiscount((long)Math.Truncate((double)itemQuantity / _offerItem.quantity)); billDiscountResult.Add(billDiscount); } if (IsHasNextHandler()) { nextHandler.ApplyOffer(cart, billDiscountResult); } }
public bool InventoryAndPriceCheck(ShoppingCart.ShoppingCart shoppingCart) { if (shoppingCart == null) { throw new ArgumentNullException("shoppingCart"); } bool hasInventoryPriceErrors = false; foreach (var shoppingCartItem in shoppingCart.ShoppingCartItems) { var inventoryProduct = this.inventoryProductRepository.GetInventoryProduct(shoppingCartItem.ProductId); if (inventoryProduct == null) { shoppingCartItem.CheckoutErrorMessage += string.Format(CultureInfo.CurrentCulture, Strings.DiscontinuedProduct) + Environment.NewLine; hasInventoryPriceErrors = true; } else if (!string.IsNullOrWhiteSpace(shoppingCartItem.CheckoutErrorMessage)) { shoppingCartItem.CheckoutErrorMessage = string.Empty; } if (shoppingCartItem.ProductPrice != inventoryProduct.ListPrice) { shoppingCartItem.CheckoutErrorMessage += string.Format( CultureInfo.CurrentCulture, Strings.ProductPriceHasChanged, shoppingCartItem.ProductPrice, inventoryProduct.ListPrice); shoppingCartItem.CheckoutErrorMessage += Environment.NewLine; shoppingCartItem.ProductPrice = inventoryProduct.ListPrice; hasInventoryPriceErrors = true; } else if (!string.IsNullOrWhiteSpace(shoppingCartItem.CheckoutErrorMessage)) { shoppingCartItem.CheckoutErrorMessage = string.Empty; } // There would be many other checks to validate invetory quanity vs. the quanity in the shopping cart // as well as locking the quanity during the checkout process, but this is beyond the scope of this // guidance and depends on the needs of the business and domain. } return(hasInventoryPriceErrors); }
public ShoppingCart.ShoppingCart CheckoutShoppingCart(string customerName, bool detialed) { var cart = GetCustomerShoppingCart(customerName); var weight = 0M; var fiveTentsDiscount = false; var shoppingCart = new ShoppingCart.ShoppingCart { Items = cart.Items }; foreach (var item in shoppingCart.Items) { fiveTentsDiscount = fiveTentsDiscount || item.ProductId == (int)ProductTypeEnum.Tent && item.Count >= 5; var threeTentsDiscount = item.ProductId == (int)ProductTypeEnum.Tent && item.Count >= 3 && item.Count < 5; shoppingCart.SubTotal += (decimal)(threeTentsDiscount ? (double)item.Cost * ThreeTentsDiscountPercentage : (double)item.Cost); shoppingCart.Discounts += threeTentsDiscount ? item.Cost * (decimal)0.15 : 0; // calculate weight var product = _catalog.GetProductByType((ProductTypeEnum)item.ProductId); weight += item.Count * product.Weight; } // apply 5 tent discount, applies to all items shoppingCart.Discounts += shoppingCart.SubTotal * (decimal)(fiveTentsDiscount ? 1 - FiveTentsDiscountAmount : 0D); shoppingCart.SubTotal *= (decimal)(fiveTentsDiscount ? FiveTentsDiscountAmount : 1.0D); //$20 shipping charge is < $200 shoppingCart.ShippingFees += shoppingCart.SubTotal < ShippingChargeThreshHold ? DefaultShippingCharge : 0; //10kg $25 overweight shipping charge shoppingCart.ShippingFees += weight > OverWeightThreadHold ? OverWeightShippingCharge : 0; return(shoppingCart); }
public bool IsApplicableTo(ShoppingCart.ShoppingCart shoppingCart) { return(shoppingCart.Any(p => p.Product is Book) && shoppingCart.Any(p => p.Product is Music)); }
public ShopperAccountInfo(String id) { this.shoppperID = id; this.myCart = new ShoppingCart.ShoppingCart(); }
public bool IsApplicableTo(ShoppingCart.ShoppingCart shoppingCart) => true;
public bool IsApplicableTo(ShoppingCart.ShoppingCart shoppingCart) { return(shoppingCart.Count(p => p.Product is Book) >= 3); }
public abstract void ApplyOffer(ShoppingCart.ShoppingCart cart, List <BillDiscount> billDiscountResult);