private Dictionary <string, List <PromoModels.MarkedItem> > ApplyPromo(PromoModels.Promo promo, Dictionary <string, List <PromoModels.MarkedItem> > groupedItems) { do { var appliedBuys = ApplyBuys(groupedItems, promo); if (!appliedBuys.Applied) { break; } groupedItems = appliedBuys.GroupItems; groupedItems = ApplyGets(groupedItems, promo); } while (true); return(groupedItems); }
private IEnumerable <PromoModels.Promo> GetDefaultPromos() { var promo1 = new PromoModels.Promo { Id = "promo1", Description = "3 of A's for 130", Buys = new List <PromoModels.Buy> { new PromoModels.Buy() { Category = "A", Count = 3 } }, Gets = new List <PromoModels.Get> { new PromoModels.Get() { Category = "A", All = true, Off = new PromoModels.Off() { Discount = new PromoModels.Discount() { Percentage = 0 }, Fixed = new PromoModels.Fixed() { Price = 130 } } } } }; var promo2 = new PromoModels.Promo { Id = "promo1", Description = "B", Buys = new List <PromoModels.Buy> { new PromoModels.Buy() { Category = "B", Count = 2 } }, Gets = new List <PromoModels.Get> { new PromoModels.Get() { Category = "", All = true, Off = new PromoModels.Off() { Discount = new PromoModels.Discount() { Percentage = 0 }, Fixed = new PromoModels.Fixed() { Price = 45 } } } } }; var promo3 = new PromoModels.Promo { Id = "promo1", Description = "", Buys = new List <PromoModels.Buy> { new PromoModels.Buy() { Category = "C", Count = 1 }, new PromoModels.Buy() { Category = "D", Count = 1 } }, Gets = new List <PromoModels.Get> { new PromoModels.Get() { Category = "", All = true, Off = new PromoModels.Off() { Discount = new PromoModels.Discount() { Percentage = 0 }, Fixed = new PromoModels.Fixed() { Price = 30 } } } } }; return(new List <PromoModels.Promo> { promo1, promo2, promo3 }); }
private AppliedBuy ApplyBuy(PromoModels.Buy buy, Dictionary <string, List <PromoModels.MarkedItem> > groupedItems, PromoModels.Promo promo) { if (!groupedItems.TryGetValue(buy.Category, out List <PromoModels.MarkedItem> value)) { return(new AppliedBuy { Applied = false, GroupItems = groupedItems }); } var matchedItems = new List <PromoModels.MarkedItem>(); foreach (var item in groupedItems[buy.Category]) { var isMarkedBuy = item.MarkedBuys.TryGetValue(promo.Id, out bool val1); var isMarkedGet = item.MarkedGets.TryGetValue(promo.Id, out float val2); if (!isMarkedBuy && !isMarkedGet) { matchedItems.Add(item); } } if (matchedItems.Count < buy.Count) { return(new AppliedBuy { Applied = false, GroupItems = groupedItems }); } groupedItems = MarkBuyItems(groupedItems, buy, promo); return(new AppliedBuy { Applied = true, GroupItems = groupedItems }); }
private Dictionary <string, List <PromoModels.MarkedItem> > MarkBuyItems(Dictionary <string, List <PromoModels.MarkedItem> > groupedItems, PromoModels.Buy buy, PromoModels.Promo promo) { var markCount = 0; for (int i = 0; i < groupedItems[buy.Category].Count; i++) { var item = groupedItems[buy.Category][i]; if (markCount == buy.Count) { break; } var isMarkedBuy = item.MarkedBuys.TryGetValue(promo.Id, out bool val1); var isMarkedGet = item.MarkedGets.TryGetValue(promo.Id, out float val2); if (!isMarkedBuy && !isMarkedGet) { groupedItems[buy.Category][i].MarkedBuys[promo.Id] = true; markCount += 1; } } return(groupedItems); }
private Dictionary <string, List <PromoModels.MarkedItem> > ApplyGets(Dictionary <string, List <PromoModels.MarkedItem> > groupedItems, PromoModels.Promo promo) { foreach (var get in promo.Gets) { groupedItems = ApplyGet(get, groupedItems, promo); } return(groupedItems); }
private Dictionary <string, List <PromoModels.MarkedItem> > ApplyGet(PromoModels.Get get, Dictionary <string, List <PromoModels.MarkedItem> > groupedItems, PromoModels.Promo promo) { if (!groupedItems.TryGetValue(get.Category, out List <PromoModels.MarkedItem> val)) { return(groupedItems); } groupedItems = MarkGetItems(groupedItems, get, promo); return(groupedItems); }
private Dictionary <string, List <PromoModels.MarkedItem> > MarkGetItems(Dictionary <string, List <PromoModels.MarkedItem> > groupedItems, PromoModels.Get get, PromoModels.Promo promo) { var markCount = 0; for (int i = 0; i < groupedItems[get.Category].Count; i++) { var item = groupedItems[get.Category][i]; if (get.All == false && markCount == get.Count) { break; } var isMarkedBuy = item.MarkedBuys.TryGetValue(promo.Id, out bool val1); var isMarkedGet = item.MarkedGets.TryGetValue(promo.Id, out float val2); if (!isMarkedBuy && !isMarkedGet) { groupedItems[get.Category][i].MarkedGets[promo.Id] = ComputeOffPrice(item.Item.Price, get.Off); markCount += 1; } } return(groupedItems); }
private AppliedBuys ApplyBuys(Dictionary <string, List <PromoModels.MarkedItem> > groupedItems, PromoModels.Promo promo) { var applied = true; foreach (var buy in promo.Buys) { var appliedBuy = ApplyBuy(buy, groupedItems, promo); applied = applied && appliedBuy.Applied; } return(new AppliedBuys { Applied = applied, GroupItems = groupedItems }); }