public string BestCharge(List <string> inputs) { List <Item> allItems = itemRepository.FindAll(); List <SalesPromotion> allPromotions = salesPromotionRepository.FindAll(); string purchaseMsg = "============= Order details =============\n"; string promotionMsg = "-----------------------------------\n" + "Promotion used:\n"; double savedMoney = 0; double costMoney = 0; double finalCost = 0; string promotionItems = ""; string output = ""; foreach (string a in inputs) { var record = a.Split('x'); var id = record[0].Trim(); string name = ""; double number = double.Parse(record[1]); double price = 0; bool isOnSale = IsOnSale(id); foreach (Item b in allItems) { if (id == b.Id) { name = b.Name; price = b.Price; if (isOnSale) { promotionItems += name; savedMoney += b.Price * number * 0.5; } else { promotionMsg = ""; } } } var recordPrice = price * number; costMoney += recordPrice; purchaseMsg += name + " x " + number + " = " + recordPrice + "\n"; } finalCost = costMoney - savedMoney; promotionMsg += promotionMsg + "(" + promotionItems + "), saving" + savedMoney + " yuan\n"; output = purchaseMsg + promotionMsg; output += "Total: " + finalCost + "yuan\n" + "==================================="; return(output); Console.WriteLine(output); }
private List <FeasiblePromotion> GetPromotionScenarios(List <ItemWithCount> selectedItems) { List <SalesPromotion> promotionDataBase = salesPromotionRepository.FindAll(); List <FeasiblePromotion> possiblePromotions = new List <FeasiblePromotion>(); List <string> inputItems = new List <string>(); foreach (var item in selectedItems) { inputItems.Add(item.Id); } foreach (var promotion in promotionDataBase) { if (promotion.RelatedItems.Intersect(inputItems).Count() != 0) { FeasiblePromotion temp = new FeasiblePromotion(); temp.DiscountedIds = promotion.RelatedItems.Intersect(inputItems); var discountItems = new List <ItemWithCount>(); foreach (var id in temp.DiscountedIds) { foreach (var j in selectedItems) { if (id == j.Id) { discountItems.Add(j); } } } temp.DiscountedItems = discountItems; temp.DisplayName = promotion.DisplayName; temp.Discount = Convert.ToDouble(promotion.Type.Substring(0, 2)) / 100; possiblePromotions.Add(temp); } } return(possiblePromotions); }
public string BestCharge(List <string> inputs) { //var output = new StringBuilder(); var output = ""; output += "============= Order details =============\n"; var allItems = itemRepository.FindAll(); var allItemsDict = BuildItemFactoryDict(allItems); var promoRules = salesPromotionRepository.FindAll(); var itemsDict = BuildItemsDict(inputs); double totalPrice = CalculateTotalPrice(allItemsDict, itemsDict, ref output); double totalPromoPrice = 0; var promo = FindMostPromotedRule(promoRules, allItemsDict, itemsDict); if (promo != null) { output += "-----------------------------------\n"; output += "Promotion used:\n"; var boughtPromoItems = promo.RelatedItems.Where(id => itemsDict.ContainsKey(id)); var promoInfo = $"{promo.DisplayName} ({boughtPromoItems.Select(id => allItemsDict[id].Name).Aggregate((a, b) => a + ", " + b)})"; var discount = ReadDiscountFromPromoType(promo.Type); var promoPrice = boughtPromoItems.Select(id => allItemsDict[id].Price * itemsDict[id] * (1 - discount)).Sum(); totalPromoPrice += promoPrice; output += $"{promoInfo}, saving {promoPrice} yuan\n"; } output += "-----------------------------------\n"; output += $"Total:{totalPrice - totalPromoPrice} yuan\n"; output += "==================================="; return(output.ToString()); }
public string BestCharge(List <string> inputs) { //TODO: write code here List <Item> foodList = itemRepository.FindAll(); List <SalesPromotion> promotionList = salesPromotionRepository.FindAll(); List <string> usePromotionList = new List <string>(); bool haveSalesPromotion = false; List <FoodOrder> foodOrders = GenerateFoodOrder(foodList, inputs, promotionList, usePromotionList, ref haveSalesPromotion); double originalTotalPrice = 0; double totalPrice = 0; string output = "============= Order details =============\n"; foreach (FoodOrder singleFoodOrder in foodOrders) { originalTotalPrice += singleFoodOrder.OriginalSutTotal; totalPrice += singleFoodOrder.SutTotal; output += singleFoodOrder.Name + " x " + singleFoodOrder.Count + " = " + singleFoodOrder.OriginalSutTotal + " yuan\n"; } output += "-----------------------------------\n"; if (haveSalesPromotion) { output += "Promotion used:\n" + "Half price for certain dishes ("; foreach (string useName in usePromotionList) { output += useName + ", "; } output = output.Substring(0, output.Length - 2); output += ")"; double savingMoney = originalTotalPrice - totalPrice; output += ", saving " + savingMoney + " yuan\n"; output += "-----------------------------------\n"; } output += "Total:" + totalPrice + " yuan\n" + "==================================="; return(output); }
private bool ReductionExists(List <string> itemIdList, List <string> reductedItems) { List <SalesPromotion> promotionList = salesPromotionRepository.FindAll(); List <string> promotionItemList = promotionList[0].RelatedItems; foreach (var element in itemIdList) { if (promotionItemList.Contains(element)) { reductedItems.Add(element); } } return(reductedItems.Count != 0); }
public string BestCharge(List <string> inputs) { List <Item> items = itemRepository.FindAll(); List <SalesPromotion> salesPromotions = salesPromotionRepository.FindAll(); var inputsFull = inputs.Select(item => item.Split(' ')).Join(items, input => input[0], item => item.Id, (input, item) => new { id = input[0], name = item.Name, count = int.Parse(input[2]), price = item.Price }).ToList(); var originPrice = inputsFull.Select(item => item.price * item.count).Aggregate((x, y) => x + y); var originPriceDescription = inputsFull.Select(item => $"{ item.name} x {item.count} = {item.price * item.count} yuan\n").Aggregate($"============= Order details =============\n", (x, y) => x + y); string savedPriceDescription = null; double savedPrice = 0; foreach (var salesPromotion in salesPromotions) { var savedItems = inputsFull.Join(salesPromotion.RelatedItems, input => input.id, related => related, (input, related) => input).ToList(); if (savedItems.Count == 0) { continue; } var promotionSavedPriceDescription = savedItems.Select(item => $"{item.name}, ").Aggregate((x, y) => x + y).TrimEnd(new char[] { ' ', ',' }); var promotionSavedPrice = savedItems.Select(item => item.price * item.count * 0.5).Aggregate((x, y) => x + y); if (promotionSavedPrice > savedPrice) { savedPrice = promotionSavedPrice; savedPriceDescription = promotionSavedPriceDescription; } } var savingDescription = savedPriceDescription == null ? $"-----------------------------------\n" + $"Total:{originPrice} yuan\n" + $"===================================" : "-----------------------------------\n" + "Promotion used:\n" + $"{salesPromotions[0].DisplayName} ({savedPriceDescription}), saving {savedPrice} yuan\n" + $"-----------------------------------\n" + $"Total:{originPrice - savedPrice} yuan\n" + $"==================================="; return($"{originPriceDescription}{savingDescription}"); }
//to do: imporve performance public string BestCharge(List <string> inputs) { var avaliableItems = itemRepository.FindAll(); var avaliablePromotions = salesPromotionRepository.FindAll(); var itemInShoppingCart = new List <(Item item, double itemPrice)>(); var receipt = "============= Order details =============\n"; double totalPrice = 0; foreach (var itemString in inputs) { var itemInfo = DeserializeInputStringToIdAndQuantity(itemString); if (itemInfo == null) { continue; } var item = avaliableItems.Where(i => i.Id == itemInfo.Value.id).FirstOrDefault(); var itemPrice = item.Price * itemInfo.Value.quantity; itemInShoppingCart.Add((item, itemPrice)); totalPrice += itemPrice; receipt = receipt + $"{item.Name} x {itemInfo.Value.quantity} = {itemPrice} yuan\n"; } var validPromotion = GetValidPromotion(itemInShoppingCart, avaliablePromotions); if (validPromotion.Count != 0) { receipt = receipt + "-----------------------------------\n"; var bestDiscount = GetBestDiscount(itemInShoppingCart, avaliablePromotions); receipt = receipt + $"Promotion used:\n{bestDiscount.promotionName} ({bestDiscount.discountItems}), saving {bestDiscount.totalDiscount} yuan\n"; totalPrice = totalPrice - bestDiscount.totalDiscount; } receipt = receipt + "-----------------------------------\n" + $"Total:{totalPrice} yuan\n" + "==================================="; return(receipt); }
public string BestCharge(List <string> inputs) { string ret = "============= Order details =============\n"; var inputDict = new Dictionary <string, int>(); var savingList = new List <string>(); bool isUsingPromo = false; double total = 0.0; var itemDict = itemRepository.FindAll().ToDictionary(x => x.Id, x => x); var promoList = salesPromotionRepository.FindAll(); var totalRelatedItems = promoList.SelectMany(x => x.RelatedItems).ToList(); foreach (var item in inputs) { var parsedStrings = item.Split(" "); inputDict.Add(parsedStrings[0], Convert.ToInt32(parsedStrings[2])); double subtotal = itemDict[parsedStrings[0]].Price * Convert.ToInt32(parsedStrings[2]); total += subtotal; ret += $"{itemDict[parsedStrings[0]].Name} x {parsedStrings[2]} = {(int)subtotal} yuan\n"; if (totalRelatedItems.Contains(parsedStrings[0])) { isUsingPromo = true; } } ret += "-----------------------------------\n"; if (isUsingPromo) { ret += "Promotion used:\n"; double saving = 0.0; string savingString = ""; SalesPromotion selectedCoupon = promoList[0]; foreach (var coupon in promoList) { var discount = 0.0; var discountList = coupon.RelatedItems.Where(x => inputDict.ContainsKey(x)); if (!discountList.Any()) { continue; } var currentSaving = discountList.Select(x => itemDict[x].Price * inputDict[x] * 0.5).Sum(); if (currentSaving > saving) { saving = currentSaving; selectedCoupon = coupon; } } savingList = selectedCoupon.RelatedItems.Where(x => inputDict.ContainsKey(x)).ToList(); foreach (var id in savingList) { if (savingList.IndexOf(id) == 0) { savingString += itemDict[id].Name; } else { savingString += ", " + itemDict[id].Name; } } total -= saving; ret += $"{promoList[0].DisplayName} ({savingString}), saving {(int)saving} yuan\n"; ret += "-----------------------------------\n"; } ret += $"Total:{(int)total} yuan\n"; ret += "==================================="; return(ret); }
public string BestCharge(List <string> inputs) { var sale = salesPromotionRepository.FindAll(); var saleItems = sale[0].RelatedItems; var AllItems = itemRepository.FindAll(); List <string[]> itemList = new List <string[]>(); List <string[]> saledItemList = new List <string[]>(); double totalPrice = 0; double savaPrice = 0; string result = "============= Order details =============\n"; foreach (string orderString in inputs) { var itemId = orderString.Substring(0, 8); var itemNumber = orderString.Substring(11, 1); foreach (Item item in AllItems) { if (item.Id == itemId) { var information = new string[4] { itemId, item.Name, itemNumber, item.Price.ToString() }; itemList.Add(information); result += item.Name + " x " + itemNumber + " = " + (item.Price * int.Parse(itemNumber)).ToString() + " yuan\n"; totalPrice += item.Price * int.Parse(itemNumber); } } } result += "-----------------------------------\n"; foreach (string[] orderedItem in itemList) { foreach (string saledItem in saleItems) { var itemSaledId = saledItem.Substring(0, 8); if (itemSaledId == orderedItem[0]) { saledItemList.Add(orderedItem); savaPrice += int.Parse(orderedItem[2]) * int.Parse(orderedItem[3]) * 0.5; } } } if (saledItemList.Count > 0) { result += "Promotion used:\nHalf price for certain dishes ("; for (int k = 0; k < saledItemList.Count; k++) { if (k != 0) { result = result + ", " + saledItemList[k][1]; } else { result += saledItemList[k][1]; } } result += "), saving "; result += savaPrice + " yuan\n-----------------------------------\n"; } totalPrice -= savaPrice; result += "Total:" + totalPrice + " yuan\n==================================="; return(result); }
public string BestCharge(List <string> inputs) { //TODO: write code here List <Item> items = itemRepository.FindAll(); List <SalesPromotion> salesItemsList = salesPromotionRepository.FindAll(); List <string> salesItems = salesItemsList[0].RelatedItems; string firstPartResult = "============= Order details =============\n"; string secondPartResult = "-----------------------------------\n" + "Promotion used:\n" + "Half price for certain dishes ("; double totalPrice = 0; double totalSaving = 0; int saleCount = 0; foreach (string s in inputs) { string[] stringArray = s.Split(" x "); string barcode = stringArray[0]; int number = Convert.ToInt32(stringArray[1]); bool isSale = false; for (int i = 0; i < salesItems.Count; i++) { if (barcode == salesItems[i]) { isSale = true; saleCount += 1; } } double price = 0; string name = ""; for (int j = 0; j < items.Count; j++) { if (barcode == items[j].Id) { price = items[j].Price; name = items[j].Name; } } if (isSale) { totalPrice += (number * price) / 2; totalSaving += (number * price) / 2; if (saleCount == 1) { secondPartResult += name; } else { secondPartResult += ", " + name; } } else { totalPrice += number * price; } firstPartResult += name + " x " + number + " = " + number * price + " yuan\n"; } secondPartResult += "), saving " + totalSaving + " yuan\n"; string thirdPartResult = "-----------------------------------\n" + "Total:" + totalPrice + " yuan\n" + "==================================="; if (saleCount > 0) { return(firstPartResult + secondPartResult + thirdPartResult); } else { return(firstPartResult + thirdPartResult); } }