private static void Seed(BillsPaymentSystemContext context) { using (context) { var users = new User[] { new User { Email = "*****@*****.**", FirstName = "Ivan", LastName = "Ivanov", Password = "******", }, new User { FirstName = "Stiliyan", LastName = "Milanov", Email = "*****@*****.**", Password = "******" } }; var bankAccount1 = new BankAccount("Unicredit", "1231dsada"); bankAccount1.Deposit(250m); var bankAccount2 = new BankAccount("Sibank", "dasdg123"); bankAccount2.Deposit(1000m); var bankAccounts = new[] { bankAccount1, bankAccount2 }; var creditCard1 = new CreditCard(); creditCard1.ExpirationDate = DateTime.ParseExact("20.12.2018", "dd.MM.yyyy", null); creditCard1.Deposit(2000m); var creditCard2 = new CreditCard(); creditCard2.ExpirationDate = DateTime.ParseExact("20.12.2022", "dd.MM.yyyy", null); creditCard2.Deposit(1560m); var creditCards = new[] { creditCard1, creditCard2 //new CreditCard //{ // ExpirationDate = DateTime.ParseExact("20.12.2018","dd.MM.yyyy",null), // Limit = 1550m, // MoneyOwed = 250 //}, //new CreditCard //{ // ExpirationDate = DateTime.ParseExact("20.12.2022","dd.MM.yyyy",null), // Limit = 2200m, // MoneyOwed = 25m //} }; var paymentMethods = new[] { new PaymentMethod { Type = PaymentType.CreditCard, User = users[0], CreditCard = creditCards[0] }, new PaymentMethod { Type = PaymentType.CreditCard, User = users[0], CreditCard = creditCards[1] }, new PaymentMethod { Type = PaymentType.BankAccount, User = users[0], BankAccount = bankAccounts[1] }, new PaymentMethod { Type = PaymentType.BankAccount, User = users[1], BankAccount = bankAccounts[0] }, }; context.Users.AddRange(users); context.BankAccounts.AddRange(bankAccounts); context.CreditCards.AddRange(creditCards); context.PaymentMethods.AddRange(paymentMethods); context.SaveChanges(); } }
public static void PayBills(int userId, decimal amount) { using (var db = new BillsPaymentSystemContext()) { var user = db.Users.Find(userId); if (user == null) { throw new ArgumentException("There is no such user"); } var paymentMethods = db.PaymentMethods .Where(pm => pm.User == user); var bankAccounts = paymentMethods .Where(pm => pm.Type == PaymentType.BankAccount) .Select(e => e.BankAccount) .OrderBy(e => e.BankAccountId) .ToList(); var creditCards = paymentMethods .Where(pm => pm.Type == PaymentType.CreditCard) .Select(e => e.CreditCard) .OrderBy(e => e.CreditCardId) .ToList(); var totalBalance = bankAccounts.Sum(ba => ba.Balance); var totalLimitLeft = creditCards.Sum(cc => cc.LimitLeft); if (amount > totalBalance + totalLimitLeft) { Console.WriteLine("Insufficient funds!"); return; } for (int i = 0; i < bankAccounts.Count; i++) { if (amount == 0) { break; } var amountToWithdraw = 0m; if (bankAccounts[i].Balance - amount < 0) { amountToWithdraw = bankAccounts[i].Balance - amount; } else { amountToWithdraw = amount; } if (amountToWithdraw > 0) { bankAccounts[i].Withdraw(amountToWithdraw); } amount -= bankAccounts[i].Balance; } db.SaveChanges(); for (int i = 0; i < creditCards.Count; i++) { if (amount == 0) { break; } var amountToWithdraw = 0m; if (creditCards[0].LimitLeft - amount < 0) { amountToWithdraw = creditCards[0].LimitLeft - amount; } else { amountToWithdraw = amount; } if (amountToWithdraw > 0) { creditCards[i].Withdraw(amountToWithdraw); } amount -= amountToWithdraw; } db.SaveChanges(); } }
private static void PayBills(BillsPaymentSystemContext db, int userId) { decimal withdrawAmount = decimal.Parse(Console.ReadLine()); decimal userBankAccountBalance = db.PaymentMethods .Where(pm => pm.UserId == userId) .Select(x => x.BankAccount.Balance) .Sum(); decimal userCreditCardBalance = db.PaymentMethods .Where(pm => pm.UserId == userId) .Select(x => x.CreditCard.MoneyOwned) .Sum(); if (userBankAccountBalance + userCreditCardBalance < withdrawAmount) { Console.WriteLine("Insufficient funds!"); return; } var bankAccounts = db.PaymentMethods .Where(pm => pm.UserId == userId) .Select(x => x.BankAccount) .Where(x => x != null) .OrderBy(x => x.BankAccountId) .ToList(); foreach (var method in bankAccounts) { decimal accountBalance = method.Balance; method.Withdraw(withdrawAmount); withdrawAmount -= Math.Abs(accountBalance - method.Balance); if (withdrawAmount == 0) { break; } } if (withdrawAmount > 0) { var creditCards = db.PaymentMethods .Where(pm => pm.UserId == userId) .Select(x => x.CreditCard) .Where(x => x != null) .OrderBy(x => x.CreditCardId) .ToList(); foreach (var method in creditCards) { decimal credit = method.MoneyOwned; method.Withdraw(withdrawAmount); withdrawAmount -= Math.Abs(credit - method.MoneyOwned); if (withdrawAmount == 0) { break; } } db.SaveChanges(); } }
private static void Seed(BillsPaymentSystemContext context) { var users = new[] { new User { FirstName = "Pesho", LastName = "Ivanov", Email = "*****@*****.**", Password = "******" }, new User { FirstName = "Gosho", LastName = "Petrov", Email = "*****@*****.**", Password = "******" }, new User { FirstName = "Stamat", LastName = "Todorov", Email = "*****@*****.**", Password = "******" }, new User { FirstName = "Toshko", LastName = "Vladimirov", Email = "*****@*****.**", Password = "******" } }; context.Users.AddRange(users); var creditCards = new[] { new CreditCard(new DateTime(2018, 6, 20), 1500, 15000), new CreditCard(new DateTime(2018, 6, 25), 1800, 20000), new CreditCard(new DateTime(2019, 7, 4), 14000, 15000), new CreditCard(new DateTime(2019, 2, 5), 4500, 16000) }; context.CreditCards.AddRange(creditCards); var bankAccounts = new[] { new BankAccount("SG Expresbank", "TGBHJKL", 2455), new BankAccount("Investbank", "TBGINKFL", 12000), new BankAccount("DSK", "TBGDSK", 14000), new BankAccount("Raiffensen bank", "TBGFRF", 8500) }; context.BankAccounts.AddRange(bankAccounts); var paymentMethods = new[] { new PaymentMethod { User = users[0], Type = Data.Models.Type.BankAccount, BankAccount = bankAccounts[0] }, new PaymentMethod { User = users[0], Type = Data.Models.Type.BankAccount, BankAccount = bankAccounts[1] }, new PaymentMethod { User = users[0], Type = Data.Models.Type.CreditCard, CreditCard = creditCards[0] }, new PaymentMethod { User = users[1], Type = Data.Models.Type.CreditCard, CreditCard = creditCards[1] }, new PaymentMethod { User = users[2], Type = Data.Models.Type.BankAccount, BankAccount = bankAccounts[2] }, new PaymentMethod { User = users[2], Type = Data.Models.Type.CreditCard, CreditCard = creditCards[2] }, new PaymentMethod { User = users[2], Type = Data.Models.Type.CreditCard, CreditCard = creditCards[3] }, new PaymentMethod { User = users[3], Type = Data.Models.Type.BankAccount, BankAccount = bankAccounts[3] } }; context.PaymentMethods.AddRange(paymentMethods); context.SaveChanges(); }
private static void Seed(BillsPaymentSystemContext db) { using (db) { var user = new User() { FirstName = "Georgi", LastName = "Georgiev", Email = "*****@*****.**", Password = "******" }; var creditCards = new CreditCard[] { new CreditCard { ExpirationDate = DateTime.ParseExact("24/01/2018", "dd/MM/yyyy", CultureInfo.InvariantCulture), Limit = 2000m, MoneyOwed = 100m }, new CreditCard { ExpirationDate = DateTime.ParseExact("24/01/2018", "dd/MM/yyyy", CultureInfo.InvariantCulture), Limit = 1000m, MoneyOwed = 200m } }; var bankAccount = new BankAccount() { Balance = 1600m, BankName = "DSK", SwiftCode = "DSKBANK" }; var paymentMethods = new PaymentMethod[] { new PaymentMethod() { User = user, CreditCard = creditCards[0], Type = PaymentMethodType.CreditCard }, new PaymentMethod() { User = user, CreditCard = creditCards[1], Type = PaymentMethodType.CreditCard }, new PaymentMethod() { User = user, BankAccount = bankAccount, Type = PaymentMethodType.BankAccount } }; db.Users.Add(user); db.CreditCards.AddRange(creditCards); db.BankAccounts.Add(bankAccount); db.PaymentMethods.AddRange(paymentMethods); db.SaveChanges(); } }
private static void Seed(BillsPaymentSystemContext db) { using (db) { var user = new User() { FirstName = "Pesho", LastName = "Peshov", Email = "*****@*****.**", Password = "******" }; var creditCards = new CreditCard[] { new CreditCard() { ExpirationDate = DateTime.ParseExact("20.05.2020", "dd.MM.yyyy", null), Limit = 1000m, MoneyOwed = 5m }, new CreditCard() { ExpirationDate = DateTime.ParseExact("21.05.2020", "dd.MM.yyyy", null), Limit = 400m, MoneyOwed = 200m } }; var bankAccount = new BankAccount() { Balance = 1500m, BankName = "Swiss Bank", SwiftCode = "SWSSBANK" }; var paymentMethods = new PaymentMethod[] { new PaymentMethod() { User = user, CreditCard = creditCards[0], Type = PaymentMethodType.CreditCard }, new PaymentMethod() { User = user, CreditCard = creditCards[1], Type = PaymentMethodType.CreditCard }, new PaymentMethod() { User = user, BankAccount = bankAccount, Type = PaymentMethodType.BankAccount } }; db.Users.Add(user); db.CreditCards.AddRange(creditCards); db.BankAccounts.Add(bankAccount); db.PaymentMethods.AddRange(paymentMethods); db.SaveChanges(); } }
private static void Seed(BillsPaymentSystemContext context) { var users = new[] { new User { FirstName = "Anton", LastName = "Apostolov", Password = "******", Email = "*****@*****.**" }, new User { FirstName = "Zornitsa", LastName = "Orlova", Password = "******", Email = "*****@*****.**" }, new User { FirstName = "Kaloyan", LastName = "Minchev", Password = "******", Email = "*****@*****.**" } }; var creditCards = new[] { new CreditCard ( 600, 230.18m, new DateTime(2019, 11, 30) ), new CreditCard ( 900, 0, new DateTime(2018, 10, 31) ), new CreditCard ( 3200, 120.35m, new DateTime(2020, 05, 31) ), new CreditCard ( 50000, 0, new DateTime(2029, 01, 31) ) }; var bankAccounts = new[] { new BankAccount ( 301.18m, "First Investment Bank", "FINVBGSFXXX" ), new BankAccount ( 540329.85m, "Banque Cantonale De Geneve", "BCGECHGGXXX" ) }; var paymentMethods = new[] { new PaymentMethod { User = users[0], Type = PaymentMethodType.CreditCard, CreditCard = creditCards[0] }, new PaymentMethod { User = users[0], Type = PaymentMethodType.BankAccount, BankAccount = bankAccounts[0] }, new PaymentMethod { User = users[1], Type = PaymentMethodType.CreditCard, CreditCard = creditCards[1] }, new PaymentMethod { User = users[1], Type = PaymentMethodType.CreditCard, CreditCard = creditCards[2] }, new PaymentMethod { User = users[2], Type = PaymentMethodType.BankAccount, BankAccount = bankAccounts[1] }, new PaymentMethod() { User = users[2], Type = PaymentMethodType.CreditCard, CreditCard = creditCards[3] } }; context.PaymentMethods.AddRange(paymentMethods); context.SaveChanges(); }