static void Main(string[] args) { ShoppingCart shoppingBucket = new ShoppingCart(); //Weekly Offer WeeklyOffer offer = new WeeklyOffer(); offer.Offer.Add(Products.Apple, new Dictionary <long, double> { { 2, 45 } }); offer.Offer.Add(Products.Banana, new Dictionary <long, double> { { 3, 130 } }); //Product Price PricePerItem pricePerItem = new PricePerItem(); pricePerItem.PricePerProduct.Add(Products.Apple, 30); pricePerItem.PricePerProduct.Add(Products.Banana, 50); pricePerItem.PricePerProduct.Add(Products.Peach, 60); Console.WriteLine("\n\n"); Console.WriteLine("====================================="); Console.WriteLine("Shopping Cart Kata"); Console.WriteLine("====================================="); Console.WriteLine("\n\n"); double value = 0; string inputMessage = CheckoutKataHelper.GenerateInputMessage(); do { Console.Write(inputMessage); value = CheckoutKataHelper.ValidateInput(); if (value != 0) { shoppingBucket = CheckoutKataHelper.AddItemInShoppingCart(value, shoppingBucket); } } while (value != 0); //Add in the bill Bill custBill = CheckoutKataHelper.GenerateBill(shoppingBucket.cart, offer, pricePerItem); //Print bill. CheckoutKataHelper.PrintBill(custBill, offer, pricePerItem); }
public void Setup() { shoppingCart = new ShoppingCart(); //Weekly Offer offer = new WeeklyOffer(); offer.Offer.Add(Products.Apple, new Dictionary <long, double> { { 2, 45 } }); offer.Offer.Add(Products.Banana, new Dictionary <long, double> { { 3, 130 } }); //Product Price pricePerItem = new PricePerItem(); pricePerItem.PricePerProduct.Add(Products.Apple, 30); pricePerItem.PricePerProduct.Add(Products.Banana, 50); pricePerItem.PricePerProduct.Add(Products.Peach, 60); }
private void SizeListBox_SelectedIndexChanged(object sender, EventArgs e) { try { SalesSummaryGroupBox.Enabled = true; BigBagRadioButton.Checked = false; SmallBagRadioButton.Checked = false; double PricePerItem; PricePerItem = GetNetweightPrice(itemPriceSelected); NoOfItemLabel.Text = PricePerItem.ToString(); double totalprice = double.Parse(QuantityTextBox.Text) * double.Parse(NoOfItemLabel.Text); TotalLabel.Text = totalprice.ToString(); QuantityTextBox.Text = "1"; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// Print Generate Bill /// </summary> /// <param name="customerBill"></param> public static void PrintBill(Bill customerBill, WeeklyOffer offer, PricePerItem ppp) { Console.WriteLine("\n\n"); Console.WriteLine("\n\n"); Console.WriteLine("=========================="); Console.WriteLine("Generate System Invoice"); Console.WriteLine("=========================="); Console.WriteLine("Prodcut name --- Offer Applied on --- Normal rate --- Total"); Console.WriteLine("-----------------------------------------------------------"); customerBill.Products.ForEach(item => { Dictionary <long, double> offeredPriceForItem = offer.Offer.FirstOrDefault(kvp => kvp.Key == item.Product).Value; StringBuilder strBuild = new StringBuilder(); strBuild.Append(item.Product); strBuild.Append(" --- "); //check if offered prices are applied on the items if (item.ItemsWithOfferedPrice != null && item.ItemsWithOfferedPrice.Keys.Count > 0) { offeredPriceForItem?.Keys?.OrderByDescending(v => v).ToList().ForEach(key => { item.ItemsWithOfferedPrice.Keys.ToList().ForEach(offeredItem => { if (offeredItem % key == 0) { strBuild.Append(key * (offeredItem / key) + " ( " + offeredItem / key + " X " + offeredPriceForItem[key] + " )"); strBuild.Append("---"); } }); }); } else { strBuild.Append("0 X 0"); strBuild.Append("---"); } //check if normal prices are applied on the items if (item.ItemsWithPrice != null && item.ItemsWithPrice.Count > 0) { strBuild.Append(String.Join(',', item.ItemsWithPrice.Keys) + " X " + ppp.PricePerProduct[item.Product].ToString()); } else { strBuild.Append("0 X " + ppp.PricePerProduct[item.Product].ToString()); } strBuild.Append("---"); strBuild.Append(String.Join(',', item.TotalPrice())); Console.WriteLine(strBuild.ToString()); }); Console.WriteLine("------------------------------------------------"); Console.WriteLine("Payable Amount ==> " + customerBill.PayableAmount()); Console.WriteLine("------------------------------------------------"); }
/// <summary> /// Generate bill with applying Weekly offers to added items in the shopping cart /// </summary> /// <param name="ShoppingCart"></param> /// <param name="offer"></param> public static Bill GenerateBill(Dictionary <Products, long> ShoppingCart, WeeklyOffer offer, PricePerItem productPrices) { // Generate bill object Bill custBill = new Bill(); List <BilledItem> billedItems = new List <BilledItem>(); foreach (Products item in ShoppingCart.Keys) { BilledItem billItem = new BilledItem(); billItem.Product = item; Dictionary <long, double> offeredPriceForItem = offer.Offer.FirstOrDefault(kvp => kvp.Key == item).Value; long productCountInBucket = ShoppingCart[item]; // apply Offer long howManyOnOfferedPrice = 0; offeredPriceForItem?.Keys?.OrderByDescending(v => v).ToList().ForEach(key => { if (offeredPriceForItem != null && productCountInBucket >= key) { howManyOnOfferedPrice = productCountInBucket - (productCountInBucket % key); billItem.ItemsWithOfferedPrice.Add(howManyOnOfferedPrice, ((howManyOnOfferedPrice / key) * offeredPriceForItem[key])); } }); // check without offer items and apply rate. long howManyWithoutOfferedPrice = productCountInBucket - billItem.ItemsWithOfferedPrice.Keys.Sum(); if (howManyWithoutOfferedPrice > 0) { billItem.ItemsWithPrice.Add(howManyWithoutOfferedPrice, howManyWithoutOfferedPrice * productPrices.PricePerProduct[item]); } // add billed items in the bill array billedItems.Add(billItem); } custBill.Products = billedItems; return(custBill); }