public CashOutPageViewModel() { var bancnotes = from b in context.Banknotes select b; Sum500Variant = CashOutVariants.GetCashOutVariants(500, bancnotes); Sum1000Variant = CashOutVariants.GetCashOutVariants(1000, bancnotes); Sum2000Variant = CashOutVariants.GetCashOutVariants(2000, bancnotes); Sum3000Variant = CashOutVariants.GetCashOutVariants(3000, bancnotes); Sum4000Variant = CashOutVariants.GetCashOutVariants(4000, bancnotes); Sum5000Variant = CashOutVariants.GetCashOutVariants(5000, bancnotes); }
public static CashOutVariantsModel GetCashOutVariants(int sum, IQueryable <Banknote> banknotes) { CashOutVariantsModel cashOutVariantsModel = new CashOutVariantsModel(); cashOutVariantsModel.CashOutBancnotes = new Dictionary <int, int>(); cashOutVariantsModel.IsPossibly = false; cashOutVariantsModel.Remainder = 0; var banknotesSorted = (from b in banknotes orderby b.NominalValue descending select b).ToList(); int count = 0; foreach (var item in banknotesSorted) { if (item.Count > 0) { count = sum / item.NominalValue; if (count > 0) { if (item.Count >= count) { sum -= item.NominalValue * count; cashOutVariantsModel.CashOutBancnotes.Add(item.NominalValue, count); } else { count = item.Count; sum -= item.NominalValue * count; cashOutVariantsModel.CashOutBancnotes.Add(item.NominalValue, count); } } } Console.WriteLine(item.NominalValue + " " + item.Count); } if (sum == 0) { cashOutVariantsModel.IsPossibly = true; } cashOutVariantsModel.Remainder = sum; return(cashOutVariantsModel); }