private static void PayWithCreditCards(decimal amount, CreditCard[] creditCards, BillsPaymentSystemContext context) { if (creditCards.Select(cc => cc.LimitLeft).Sum() < amount) { throw new ArgumentException("Amount is greater than the cards possibilities"); } foreach (var card in creditCards) { /* * Due to the Anonymous Object produced by the select in PayBills(...), all instances are * in a Detached state from the change tracker. */ // context.Entry(card).State = EntityState.Modified; // Will send aquery updating all the Entity's fields context.Entry(card).State = EntityState.Unchanged; if (card.LimitLeft >= amount) { card.Withdraw(amount); return; } amount -= card.LimitLeft; card.Withdraw(card.LimitLeft); } }
private static decimal PayWithBankAsMuchAsPossuble(BankAccount[] bankAccounts, decimal amount, BillsPaymentSystemContext context) { foreach (var account in bankAccounts) { /* * Due to the Anonymous Object produced by the select in PayBills(...), all instances are * in a Detached state from the change tracker. */ // context.Entry(account).State = EntityState.Modified; // Will send aquery updating all the Entity's fields context.Entry(account).State = EntityState.Unchanged; if (account.Balance >= amount) { account.Withdraw(amount); amount = 0; break; } amount -= account.Balance; account.Withdraw(account.Balance); } return(amount); }
private static void PayBillsWithCreditCards(CreditCard[] creditCards, decimal amount, BillsPaymentSystemContext db) { foreach (var creditCard in creditCards) { db.Entry(creditCard).State = EntityState.Modified; //Change all modified fields in CreditCard. db.Entry(creditCard).State = EntityState.Unchanged; if (creditCard.LimitLeft >= amount) { creditCard.Withdraw(amount); return; } amount -= creditCard.LimitLeft; creditCard.Withdraw(creditCard.LimitLeft); } }
private static void PayBills(int id, decimal amount, BillsPaymentSystemContext db) { var user = db.Users .Where(u => u.UserId == id) .Select(u => new { Name = $"{u.FirstName} {u.LastName}", CreditCards = u.PaymentMethods.Where(pm => pm.Type == PaymentType.Card).Select(pm => pm.CreditCard).ToList(), BankAccounts = u.PaymentMethods.Where(pm => pm.Type == PaymentType.Bank).Select(pm => pm.BankAccount).ToList(), }) .FirstOrDefault(); if (amount > 0) { foreach (var b in user.BankAccounts) { amount = b.Withdrawal(amount); } } if (amount > 0) { foreach (var c in user.CreditCards) { amount = c.Withdrawal(amount); } } if (amount == 0) { foreach (var b in user.BankAccounts) { db.Entry(b).State = EntityState.Modified; } foreach (var c in user.CreditCards) { db.Entry(c).State = EntityState.Modified; } db.SaveChanges(); } else { Console.WriteLine("Insufficient funds!"); } }
private static decimal PayBillsFromBankAccounts(BankAccount[] bankAccounts, decimal amount, BillsPaymentSystemContext db) { foreach (var account in bankAccounts) { db.Entry(account).State = EntityState.Modified; //change all modified fields in BankAccount. 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 static void Main(string[] args) { using (BillsPaymentSystemContext db = new BillsPaymentSystemContext()) { var pm = db.PaymentMethods.FirstOrDefault(); db.Entry(pm).Reference(p => p.User).Load(); ; } //InitializeSeedData(); //ICommandInterpreter commandInterpreter = new CommandInterpreter(); //IEngine engine = new Engine(commandInterpreter); //engine.Run(); }
public string Execute(string[] args) { int userId = int.Parse(args[0]); User user = this.context .Users .FirstOrDefault(u => u.UserId == userId); context.Entry(user).Collection(u => u.PaymentMethods); if (user == null) { throw new ArgumentNullException($"User with id {userId} not found!"); } sb.AppendLine($"User: {user.FullName}"); List <BankAccount> bankAccounts = context .PaymentMethods .Include(ba => ba.BankAccount) .Where(pm => pm.UserId == userId) .Select(u => u.BankAccount) .ToList(); List <CreditCard> creditCards = context .PaymentMethods .Include(cc => cc.CreditCard) .Where(pm => pm.UserId == userId) .Select(u => u.CreditCard) .ToList(); CheckBankAccounts(bankAccounts); CheckCreditCards(creditCards); return(this.sb.ToString()); }