internal static void SeedDb(BillsPaymentSystemContext currentDb) { using (currentDb) { User[] users = new User[] { new User() { FirstName = "Ivanov", LastName = "Petrov", Email = "*****@*****.**", Password = "******" }, new User() { FirstName = "Tanya", LastName = "Fikova", Email = "*****@*****.**", Password = "******" } }; CultureInfo provider = CultureInfo.InvariantCulture; CreditCard[] creditCards = { new CreditCard() { ExpirationDate = DateTime.ParseExact("21.06.2019", "dd.MM.yyyy", provider), Limit = 2000m, MoneyOwed = 50m, }, new CreditCard() { ExpirationDate = DateTime.ParseExact("22.07.2020", "dd.MM.yyyy", provider), Limit = 3000m, MoneyOwed = 500m, }, new CreditCard() { ExpirationDate = DateTime.ParseExact("23.08.2018", "dd.MM.yyyy", provider), Limit = 6000m, MoneyOwed = 3.1m, } }; BankAccount[] bankAccounts = { new BankAccount() { Balance = 1400m, BankName = "BankOfSvalbard", SWIFTCode = "SVLBRDMNY" }, new BankAccount() { Balance = 1565m, BankName = "BankOfWrangelIsland", SWIFTCode = "WRNGLGLD" } }; PaymentMethod[] paymentMethods = new PaymentMethod[] { new PaymentMethod { User = users[0], CreditCard = creditCards[0], Type = AccountType.CreditCard }, new PaymentMethod { User = users[0], CreditCard = creditCards[1], Type = AccountType.CreditCard }, new PaymentMethod { User = users[0], BankAccount = bankAccounts[0], Type = AccountType.BankAccount }, // This causes an exception, as it should //new PaymentMethod //{ // User = users[0], // BankAccount = bankAccounts[0], // CreditCard = creditCards[2], // Type = AccountType.BankAccount //}, }; currentDb.Users.AddRange(users); currentDb.CreditCards.AddRange(creditCards); currentDb.BankAccounts.AddRange(bankAccounts); currentDb.PaymentMethods.AddRange(paymentMethods); currentDb.SaveChanges(); } }
protected Command(BillsPaymentSystemContext context) { this.context = context; }
public UserInfoCommand(BillsPaymentSystemContext context) { this.context = context; }
public static void Seed(BillsPaymentSystemContext context) { SeedUsers(context); }
public static void Seed(BillsPaymentSystemContext context) { var users = new[] { new User { FirstName = "Pesho", LastName = "Ivanov", Email = "*****@*****.**", Password = "******" }, new User { FirstName = "Ivan", LastName = "Georgiev", Email = "*****@*****.**", Password = "******" }, new User { FirstName = "Maria", LastName = "Atanasova", Email = "*****@*****.**", Password = "******" } }; context.Users.AddRange(users); var bankAccounts = new[] { new BankAccount() { BankName = "Bulbank", SwiftCode = "UNCRBGSF" }, new BankAccount() { BankName = "Fibank", SwiftCode = "FINVBGSF" }, new BankAccount() { BankName = "Postbank", SwiftCode = "BPBIBGSF" } }; bankAccounts[0].Deposit(15000m); bankAccounts[1].Deposit(50000m); bankAccounts[2].Deposit(78000m); context.BankAccounts.AddRange(bankAccounts); var creditCards = new[] { new CreditCard() { Limit = 5000m, ExpirationDate = DateTime.Parse("15.12.2019") }, new CreditCard() { Limit = 2000m, ExpirationDate = DateTime.Parse("15.01.2020") }, new CreditCard() { Limit = 1000m, ExpirationDate = DateTime.Parse("31.12.2018") } }; creditCards[0].Deposit(1000m); creditCards[1].Deposit(500m); creditCards[2].Deposit(250m); context.CreditCards.AddRange(creditCards); var paymentMethods = new[] { new PaymentMethod() { UserId = users[0].UserId, Type = PaymentMethodType.BankAccount, BankAccountId = bankAccounts[0].BankAccountId }, new PaymentMethod() { UserId = users[0].UserId, Type = PaymentMethodType.BankAccount, BankAccountId = bankAccounts[2].BankAccountId }, new PaymentMethod() { UserId = users[1].UserId, Type = PaymentMethodType.CreditCard, CreditCardId = creditCards[1].CreditCardId }, new PaymentMethod() { UserId = users[2].UserId, Type = PaymentMethodType.BankAccount, BankAccountId = bankAccounts[1].BankAccountId }, new PaymentMethod() { UserId = users[2].UserId, Type = PaymentMethodType.CreditCard, CreditCardId = creditCards[2].CreditCardId }, new PaymentMethod() { UserId = users[2].UserId, Type = PaymentMethodType.CreditCard, CreditCardId = creditCards[0].CreditCardId } }; context.PaymentMethods.AddRange(paymentMethods); context.SaveChanges(); }
public Engine(IReader reader, IWriter writer, BillsPaymentSystemContext context) { this.reader = reader; this.writer = writer; this.context = context; }
public DbInitializer(BillsPaymentSystemContext context) { this.context = context; }
private static void SeedUsers(BillsPaymentSystemContext db, int count) { UserGenerator.InitialUserSeed(db, count); }
public void DbReset(BillsPaymentSystemContext db) { db.Database.EnsureDeleted(); db.Database.EnsureCreated(); }
private static void SeedPaymentMethods(BillsPaymentSystemContext db, int count) { PaymentMethodsGenerator.InitialPaymentMethodSeed(db, count); }
private static void SeedBankAccounts(BillsPaymentSystemContext db, int count) { BankAccountGenerator.InitialBankAccountSeed(db, count); }
private static void SeedCreditCards(BillsPaymentSystemContext db, int count) { CreditCardGenerator.InitialCreditCardSeed(db, count); }
private static void PayBills(int userId, decimal amount) { using (var db = new BillsPaymentSystemContext()) { var toPayFromAccounts = db.BankAccounts .Where(ba => ba.PaymentMethod.UserId == userId) .OrderBy(ba => ba.BankAccountId) .ToList(); var toPayFromCredit = db.CreditCards .Where(cc => cc.PaymentMethod.UserId == userId) .OrderBy(cc => cc.CreditCardId) .ToList(); var totalBalance = toPayFromAccounts .Sum(b => b.Balance) + toPayFromCredit .Sum(cc => cc.LimitLeft); if (totalBalance < amount) { throw new ArgumentOutOfRangeException("Insufficient funds!"); } bool wereBillsPaied = false; foreach (var bankAccount in toPayFromAccounts) { if (wereBillsPaied) { break; } if (bankAccount.Balance >= amount) { bankAccount.Withdraw(amount); amount = 0; wereBillsPaied = true; } else { amount -= bankAccount.Balance; bankAccount.Withdraw(bankAccount.Balance); } } foreach (var creditCard in toPayFromCredit) { if (wereBillsPaied) { break; } if (creditCard.LimitLeft >= amount) { creditCard.Withdraw(amount); amount = 0; wereBillsPaied = true; } else { amount -= creditCard.LimitLeft; creditCard.Withdraw(creditCard.LimitLeft); } } db.SaveChanges(); } }
private static void Seed(BillsPaymentSystemContext db) { using ( db) { var user = new User() { FirstName = "Pesho", LastName = "Stamatov", Email = "*****@*****.**", Password = "******" }; var credCards = new CreditCard[] { new CreditCard() { ExpirationDate = new DateTime(2018, 6, 28), Limit = 1000m, MoneyOwned = 5m }, new CreditCard() { ExpirationDate = new DateTime(2012, 4, 12), Limit = 400m, MoneyOwned = 200m } }; var bankAccount = new BankAccount() { Balance = 1230m, SwiftCode = "FINVBFSF", BankName = "Swiss Band" }; var paymentMethods = new PaymentMethod[] { new PaymentMethod() { User = user, CreditCard = credCards[0], Type = PaymentMethodType.CreditCard }, new PaymentMethod() { User = user, CreditCard = credCards[1], Type = PaymentMethodType.CreditCard }, new PaymentMethod() { User = user, BankAccount = bankAccount, Type = PaymentMethodType.BankAccount }, }; db.Users.Add(user); db.CreditCards.AddRange(credCards); db.BankAccounts.Add(bankAccount); db.PaymentMethods.AddRange(paymentMethods); db.SaveChanges(); } }
private static void PayBills(int userId, decimal amount, BillsPaymentSystemContext context) { var user = context.Users.Find(userId); if (user == null) { Console.WriteLine($"User with id {userId} not found!"); return; } decimal userMoney = 0m; var bankAccounts = context .BankAccounts.Join(context.PaymentMethods, (ba => ba.BankAccountId), (p => p.BankAccountId), (ba, p) => new { UserId = p.UserId, BankAccountId = ba.BankAccountId, Balance = ba.Balance }) .Where(ba => ba.UserId == userId) .ToList(); var creditCards = context .CreditCards.Join(context.PaymentMethods, (c => c.CreditCardId), (p => p.CreditCardId), (c, p) => new { UserId = p.UserId, CreditCardId = c.CreditCardId, LimitLeft = c.LimitLeft }) .Where(c => c.UserId == userId) .ToList(); userMoney += bankAccounts.Sum(b => b.Balance); userMoney += creditCards.Sum(c => c.LimitLeft); if (userMoney < amount) { throw new InvalidOperationException("Insufficient funds!"); } bool isBillsPayed = false; foreach (var bankAccount in bankAccounts.OrderBy(b => b.BankAccountId)) { var currentAccount = context.BankAccounts.Find(bankAccount.BankAccountId); if (amount <= currentAccount.Balance) { currentAccount.Withdraw(amount); isBillsPayed = true; } else { amount -= currentAccount.Balance; currentAccount.Withdraw(currentAccount.Balance); } if (isBillsPayed) { context.SaveChanges(); return; } } foreach (var creditCard in creditCards.OrderBy(c => c.CreditCardId)) { var currentCreditCard = context.CreditCards.Find(creditCard.CreditCardId); if (amount <= currentCreditCard.LimitLeft) { currentCreditCard.Withdraw(amount); isBillsPayed = true; } else { amount -= currentCreditCard.LimitLeft; currentCreditCard.Withdraw(currentCreditCard.LimitLeft); } if (isBillsPayed) { context.SaveChanges(); return; } } }
public ListPaymentMethodsCommand(BillsPaymentSystemContext context) : base(context) { }
public DepositCommand(BillsPaymentSystemContext context) : base(context) { }
public Engine(ICommandInterpreter commandInterpreter, BillsPaymentSystemContext context) { this.commandInterpreter = commandInterpreter; this.context = context; }
private static void SeedDatabase() { PaymentMethod[] paymentMethods = { new PaymentMethod { User = new User { FirstName = "Ivan", LastName = "Gogov", Email = "*****@*****.**", Password = "******" }, PaymentType = PaymentType.BankAccount, BankAccount = new BankAccount { Balance = 120, BankName = "Random Bank", SwiftCode = "RDBank123" }, CreditCard = null }, new PaymentMethod { User = new User { FirstName = "Tosho", LastName = "Toshev", Email = "*****@*****.**", Password = "******" }, PaymentType = PaymentType.BankAccount, BankAccount = new BankAccount { Balance = 111110, BankName = "Random Bank", SwiftCode = "RDBank124" }, CreditCard = null }, new PaymentMethod { User = new User { FirstName = "Petur", LastName = "Sevliev", Email = "*****@*****.**", Password = "******" }, PaymentType = PaymentType.BankAccount, BankAccount = new BankAccount { Balance = 12, BankName = "Shitty Bank", SwiftCode = "ShittyBank123" }, CreditCard = null }, new PaymentMethod { User = new User { FirstName = "Vasil", LastName = "Mendeleev", Email = "*****@*****.**", Password = "******" }, PaymentType = PaymentType.BankAccount, BankAccount = new BankAccount { Balance = 5, BankName = "Shitty Bank", SwiftCode = "ShittyBank124" }, CreditCard = null }, new PaymentMethod { User = new User { FirstName = "Misho", LastName = "Zvezdev", Email = "*****@*****.**", Password = "******" }, PaymentType = PaymentType.BankAccount, BankAccount = new BankAccount { Balance = 50, BankName = "Shitty Bank", SwiftCode = "ShittyBank125" }, CreditCard = null }, new PaymentMethod { User = new User { FirstName = "Pencho", LastName = "Penchev", Email = "*****@*****.**", Password = "******" }, PaymentType = PaymentType.CreditCard, BankAccount = null, CreditCard = new CreditCard { Limit = 100000, MoneyOwed = 100, ExpirationDate = new DateTime(2040, 11, 1) } }, new PaymentMethod { User = new User { FirstName = "Zdravko", LastName = "Jelev", Email = "*****@*****.**", Password = "******" }, PaymentType = PaymentType.CreditCard, BankAccount = null, CreditCard = new CreditCard { Limit = 1200, MoneyOwed = 200, ExpirationDate = new DateTime(2042, 4, 15) } }, new PaymentMethod { User = new User { FirstName = "Ivan", LastName = "Gogov", Email = "*****@*****.**", Password = "******" }, PaymentType = PaymentType.CreditCard, BankAccount = null, CreditCard = new CreditCard { Limit = 10, MoneyOwed = 2, ExpirationDate = new DateTime(2040, 12, 1) } }, new PaymentMethod { User = new User { FirstName = "Sasho", LastName = "Uchev", Email = "*****@*****.**", Password = "******" }, PaymentType = PaymentType.CreditCard, BankAccount = null, CreditCard = new CreditCard { Limit = 10000, MoneyOwed = 9990, ExpirationDate = new DateTime(2040, 2, 20) } } }; using (var context = new BillsPaymentSystemContext()) { context.PaymentMethods.AddRange(paymentMethods); context.SaveChanges(); } }
private static void PayBills(BillsPaymentSystemContext dbContext) { var userIdInput = int.Parse(Console.ReadLine()); var amount = decimal.Parse(Console.ReadLine()); var user = dbContext.Users .Where(u => u.UserId.Equals(userIdInput)) .Select(u => new { Name = u.FirstName + " " + u.LastName, BankAccounts = u.UserPayments .Where(up => up.PaymentType.Equals(PaymentType.BankAccount)) .Select(up => up.BankAccount) .OrderBy(up => up.BankAccountId), CreditCards = u.UserPayments .Where(up => up.PaymentType.Equals(PaymentType.CreditCard)) .Select(up => up.CreditCard) .OrderBy(up => up.CreditCardId) }) .FirstOrDefault(); var bankAccounts = user.BankAccounts.ToArray(); var creditCards = user.CreditCards.ToArray(); var totalMoney = bankAccounts.Sum(ba => ba.Balance) + creditCards.Sum(cc => cc.LimitLeft); if (amount > totalMoney) { Console.WriteLine("Insufficient funds!"); return; } foreach (var bankAccount in bankAccounts) { if (amount >= bankAccount.Balance) { amount -= bankAccount.Balance; bankAccount.Withdraw(bankAccount.Balance); } else { bankAccount.Withdraw(amount); amount = 0; break; } } foreach (var creditCard in creditCards) { if (amount >= creditCard.LimitLeft) { amount -= creditCard.LimitLeft; creditCard.Withdraw(creditCard.LimitLeft); } else { creditCard.Withdraw(amount); amount = 0; break; } } }
public void InitializeDB(BillsPaymentSystemContext context) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); Seed(context); }
public DepositCommand(BillsPaymentSystemContext context) { this.context = context; }
public static void Seed(BillsPaymentSystemContext db) { using (db) { var user = new User { FirstName = "Guy", LastName = "Gilbert", Email = "*****@*****.**", Password = "******" }; var card = new CreditCard { ExpirationDate = new DateTime(2020, 03, 1), Limit = 800m, MoneyOwed = 100m }; var bankAccounts = new[] { new BankAccount { Balance = 2000m, BankName = "Unicredit Bulbank", SwiftCode = "UNCRBGSF" }, new BankAccount { Balance = 1000m, BankName = "First Investment Bank", SwiftCode = "FINVBGSF" } } .ToList(); var paymentMethods = new[] { new PaymentMethod { User = user, BankAccount = bankAccounts[0], Type = PaymentMethodType.BankAccount }, new PaymentMethod { User = user, BankAccount = bankAccounts[1], Type = PaymentMethodType.BankAccount }, new PaymentMethod { User = user, CreditCard = card, Type = PaymentMethodType.CreditCard } } .ToList(); db.Users.Add(user); db.CreditCards.Add(card); db.BankAccounts.AddRange(bankAccounts); db.PaymentMethods.AddRange(paymentMethods); db.SaveChanges(); } }
public PayBillCommand(BillsPaymentSystemContext context) : base(context) { }
public PayBillsCommands(BillsPaymentSystemContext context) { this.context = context; }
public WithdrawCommand(BillsPaymentSystemContext context) { this.context = context; }
public CreditCardService(BillsPaymentSystemContext dbContext) { this.dbContext = dbContext; }
private static void Seed(BillsPaymentSystemContext db) { using (db) { var user = new User() { FirstName = "Ivan", LastName = "Petrov", 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("20.05.2020", "dd.MM.yyyy", null), Limit = 2500m, MoneyOwed = 300m } }; var bankAccount = new BankAccount() { Balance = 3000m, BankName = "Swiss Bank", SwiftCode = "SSWSBKJS" }; 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 PayBills(int userId, decimal amount, BillsPaymentSystemContext context) { if (!context.Users.Any(x => x.UserId == userId)) { throw new ArgumentException($"User with id {userId} not found!"); } var user = context.Users .Where(x => x.UserId == userId) .Select(x => new { Name = $"{x.FirstName} {x.LastName}", BankAccounts = x.PaymentMethods .OrderBy(i => i.BankAccountId) .Where(ba => ba.Type == PaymentMethodType.BankAccount) .Select(ba => ba.BankAccount).ToList(), CreditCards = x.PaymentMethods .OrderBy(i => i.CreditCardId) .Where(y => y.Type == PaymentMethodType.CreditCard) .Select(cc => cc.CreditCard).ToList() }).FirstOrDefault(); decimal bankAccBalance = 0.0m; foreach (var acc in user.BankAccounts) { decimal currBal = acc.Balance; bankAccBalance += currBal; } decimal cardBalance = 0.0m; foreach (var card in user.CreditCards) { decimal currLeftLimit = card.LimitLeft; cardBalance += currLeftLimit; } decimal totalBalance = bankAccBalance + cardBalance; if (totalBalance < amount) { throw new InvalidOperationException("Insufficient funds!"); } bool billsArePaid = false; foreach (var acc in user.BankAccounts) { var currAccount = context.BankAccounts.Find(acc.BankAccountId); if (amount <= currAccount.Balance) { currAccount.Withdraw(amount); billsArePaid = true; } else { amount -= currAccount.Balance; currAccount.Withdraw(currAccount.Balance); } if (billsArePaid) { context.SaveChanges(); return; } } foreach (var card in user.CreditCards) { var currCard = context.CreditCards.Find(card.CreditCardId); if (amount <= currCard.LimitLeft) { currCard.Withdraw(amount); billsArePaid = true; } else { amount -= currCard.LimitLeft; currCard.Withdraw(currCard.LimitLeft); } if (billsArePaid) { context.SaveChanges(); return; } } }
public static void SeedDb(BillsPaymentSystemContext db) { var firstAcc = new BankAccount { BankName = "First Investment Bank", SwiftCode = "FINVBGSF", }; firstAcc.Deposit(1000); var secondAcc = new BankAccount { BankName = "Unicredit Bulbank", SwiftCode = "UNCRBGSF" }; secondAcc.Deposit(1000); var firstCreditCard = new CreditCard { Limit = 1000, ExpirationDate = new DateTime(2020, 01, 01) }; firstCreditCard.Withdraw(200); var secondCreditCard = new CreditCard { Limit = 2000, ExpirationDate = new DateTime(2020, 01, 01) }; secondCreditCard.Withdraw(500); var firstUser = new User { FirstName = "Mickey", LastName = "Mouse", Email = "*****@*****.**", Password = "******" }; var secondUser = new User { FirstName = "Donald", LastName = "Duck", Email = "*****@*****.**", Password = "******" }; var firstPayment = new PaymentMethod { BankAccount = firstAcc, User = firstUser, Type = PaymentType.BankAccount }; var secondPayment = new PaymentMethod { BankAccount = secondAcc, User = firstUser, Type = PaymentType.BankAccount }; var thirdPayment = new PaymentMethod { CreditCard = firstCreditCard, User = firstUser, Type = PaymentType.CreditCard }; var fourthPayment = new PaymentMethod { CreditCard = secondCreditCard, User = firstUser, Type = PaymentType.CreditCard }; db.Users.AddRange(firstUser, secondUser); db.CreditCards.AddRange(firstCreditCard, secondCreditCard); db.BankAccounts.AddRange(firstAcc, secondAcc); db.PaymentMethods.AddRange(firstPayment, secondPayment, thirdPayment, fourthPayment); db.SaveChanges(); }