Exemplo n.º 1
0
        private static void PayBills(BillsPaymantSystemContext context)
        {
            Console.Write("Enter userId: ");
            var userId = int.Parse(Console.ReadLine());

            Console.Write("Enter amount for bill pays: ");
            var amount = decimal.Parse(Console.ReadLine());

            try
            {
                var findUser = context.Users.Find(userId);

                if (findUser == null)
                {
                    Console.WriteLine($"User with id {userId} not found!");
                    return;
                }

                decimal allАvailableMoneyOfUser = 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();

                allАvailableMoneyOfUser += bankAccounts.Sum(b => b.Balance);
                allАvailableMoneyOfUser += creditCards.Sum(c => c.LimitLeft);

                if (allАvailableMoneyOfUser < amount)
                {
                    throw new InvalidOperationException("Insufficient funds!");
                }

                bool isPayBills = false;

                foreach (var bankAccount in bankAccounts.OrderBy(b => b.BankAccountId))
                {
                    var currentAccount = context.BankAccounts.Find(bankAccount.BankAccountId);

                    if (amount <= currentAccount.Balance)
                    {
                        currentAccount.Withdraw(amount);
                        isPayBills = true;
                    }
                    else
                    {
                        amount -= currentAccount.Balance;
                        currentAccount.Withdraw(currentAccount.Balance);
                    }

                    if (isPayBills)
                    {
                        context.SaveChanges();
                        Console.WriteLine("Bills have been successfully paid.");
                        return;
                    }
                }

                foreach (var creditCard in creditCards.OrderBy(c => c.CreditCardId))
                {
                    var currentCreditCard = context.CreditCards.Find(creditCard.CreditCardId);

                    if (amount <= currentCreditCard.LimitLeft)
                    {
                        currentCreditCard.Withdraw(amount);
                        isPayBills = true;
                    }
                    else
                    {
                        amount -= currentCreditCard.LimitLeft;
                        currentCreditCard.Withdraw(currentCreditCard.LimitLeft);
                    }

                    if (isPayBills)
                    {
                        context.SaveChanges();
                        Console.WriteLine("Bills have been successfully paid.");
                        return;
                    }
                }
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine(e.Message);
            }
        }
Exemplo n.º 2
0
        private static void Seed(BillsPaymantSystemContext context)
        {
            if (context.Users.Any())
            {
                Console.WriteLine("Database already was created!");
                return;
            }

            var user = new User()
            {
                FirstName = "Kamen",
                LastName  = "Donev",
                Email     = "*****@*****.**",
                Password  = "******",
            };

            var creditCard = new CreditCard[]
            {
                new CreditCard()
                {
                    ExpirationDate = DateTime.ParseExact("20.01.2022", "dd.MM.yyyy", CultureInfo.InvariantCulture),
                    //Limit = 1000M,
                    //MoneyOwed = 100M,
                },



                new CreditCard()
                {
                    ExpirationDate = DateTime.ParseExact("23.10.2022", "dd.MM.yyyy", CultureInfo.InvariantCulture),
                    //Limit = 2000M,
                    //MoneyOwed = 200M,
                },

                new CreditCard()
                {
                    ExpirationDate = DateTime.ParseExact("01.01.2018", "dd.MM.yyyy", CultureInfo.InvariantCulture),
                    //Limit = 30000M,
                    //MoneyOwed = 300M,
                }
            };

            creditCard[0].Deposit(1000);
            creditCard[0].Withdraw(100);

            creditCard[1].Deposit(2000);
            creditCard[1].Withdraw(200);

            creditCard[2].Deposit(3000);
            creditCard[2].Withdraw(300);

            var bankAccount = new BankAccount()
            {
                BankName  = "KaT_Ebe",
                SWIFTCode = "SusMa",
            };

            bankAccount.Deposit(3000);

            var paymantMethods = new PaymentMethod[]
            {
                new PaymentMethod()
                {
                    User       = user,
                    CreditCard = creditCard[0],
                    Type       = PaymentMethodType.CreditCard
                },

                new PaymentMethod()
                {
                    User       = user,
                    CreditCard = creditCard[1],
                    Type       = PaymentMethodType.CreditCard
                },

                new PaymentMethod()
                {
                    User       = user,
                    CreditCard = creditCard[2],
                    Type       = PaymentMethodType.CreditCard
                },

                new PaymentMethod()
                {
                    User        = user,
                    BankAccount = bankAccount,
                    Type        = PaymentMethodType.BankAccount
                }
            };

            context.Users.Add(user);
            context.CreditCards.AddRange(creditCard);
            context.BankAccounts.Add(bankAccount);
            context.PaymentMethods.AddRange(paymantMethods);

            context.SaveChanges();
        }