public decimal PayWithBankAsMuchAsPossible(IEnumerable <BankAccount> bankAccounts, decimal amount, BillsPaymentDbContext db) { foreach (var account in bankAccounts) { db.Entry(account).State = EntityState.Unchanged; if (account.Balance >= amount) { account.Withdraw(amount); amount = 0; break; } amount -= account.Balance; account.Withdraw(account.Balance); } return(amount); }
public void PayWithCreditCards(decimal amount, IEnumerable <CreditCard> creditCards, BillsPaymentDbContext db) { if (creditCards.Select(c => c.LimitLeft).Sum() < amount) { throw new InvalidOperationException(AmountGreaterThanPossibilities); } foreach (var card in creditCards) { db.Entry(card).State = EntityState.Unchanged; if (card.LimitLeft >= amount) { card.Withdraw(amount); return; } amount -= card.LimitLeft; card.Withdraw(card.LimitLeft); } }