Beispiel #1
0
 public static void UpdateAccounts(Account primaryAccount, Account secondaryAccount, BankContext btc)
 {
     btc.Accounts.Update(primaryAccount);
     btc.Accounts.Update(secondaryAccount);
 }
Beispiel #2
0
        //Admin withdraws funds from users, adds them to internal bank account
        public void Withdraw(string currentUser)
        {
            using (BankContext btx = new BankContext())
            {
                while (true)
                {
                    Console.Clear();

                    Console.WriteLine("Select user account you wish to withdraw from");
                    string suser = Console.ReadLine();

                    //Checks to see if admin tries to withdraw from the internal bank account
                    if (currentUser == "admin" && suser == "admin")
                    {
                        Console.WriteLine("Invalid operation.Cannot withdraw from Internal Bank Account.");
                        Console.ReadKey();
                        continue;
                    }


                    else
                    {
                        try
                        {
                            var userToWithdrawFrom    = CreateUser(btx, suser);
                            var accountToWithdrawFrom = CreateAccount(btx, suser);
                            var ActiveUser            = CreateUser(btx, currentUser);
                            var ActiveAccount         = CreateAccount(btx, currentUser);
                            do
                            {
                                Console.Clear();
                                try
                                {
                                    Console.Write("Select the amount you wish to withdraw:\n>");
                                    decimal withdrawnAmount = decimal.Parse(Console.ReadLine().Replace(",", "."));

                                    //Checks for negative amount entries and if the normal user has more money than the requested amount
                                    if (withdrawnAmount > 0 && accountToWithdrawFrom.Amount >= withdrawnAmount)
                                    {
                                        Account.UpdateAccounts(ActiveAccount, accountToWithdrawFrom, btx);
                                        Account.UpdateAmount(ActiveAccount, accountToWithdrawFrom, withdrawnAmount, false);
                                        Account.UpdateTime(ActiveAccount, accountToWithdrawFrom);

                                        Console.Clear();
                                        Console.WriteLine($"Successfully withdrawn {Account.FormatAmount(withdrawnAmount)}");

                                        //Add transaction to buffer
                                        FileAccess.AddToBuffer(GenerateReport(ActiveUser, withdrawnAmount, userToWithdrawFrom, false, false));
                                        btx.SaveChanges();
                                        break;
                                    }

                                    else
                                    {
                                        Console.WriteLine("Invalid amount, please try again");
                                        Console.ReadKey();
                                        continue;
                                    }
                                }
                                catch (Exception)
                                {
                                    Console.WriteLine("This is not a valid entry.Please try again.");
                                    Console.ReadKey();
                                }
                            } while (true);

                            break;
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("Invalid entry, user does not exist");
                            Console.ReadKey();
                        }
                    }
                }
            }
        }
Beispiel #3
0
        //Initialize user
        public User CreateUser(BankContext btc, string xuser)
        {
            var user = new User();

            return(user = btc.Users.SingleOrDefault(r => r.username == xuser));
        }
Beispiel #4
0
        public void Deposit(string currentUser, bool depositToOther)
        {
            using (BankContext btx = new BankContext())
            {
                while (true)
                {
                    Console.Clear();
                    //True equals to  despositing to other accounts and not to internal
                    if (depositToOther)
                    {
                        Console.WriteLine("Select user account you wish to deposit to");
                        string suser = Console.ReadLine();

                        //Checks to determine if admin tries to deposit to the internal
                        if (currentUser == "admin" && suser == "admin")
                        {
                            Console.WriteLine("Invalid operation.Administrators cannot deposit to Internal Bank Account.");
                            Console.ReadKey();
                            continue;
                        }

                        //Checks to determine if user tries to deposit to themselves
                        else if (currentUser != "admin" && currentUser == suser && suser != "admin")
                        {
                            Console.WriteLine("Invalid operation.Only deposit to other members allowed");
                            Console.ReadKey();
                            continue;
                        }

                        //Checks to determine if user tries to deposit to internal
                        else if (currentUser != "admin" && suser == "admin")
                        {
                            Console.WriteLine("Invalid operation.Please use the Deposit to Internal Bank Account option.");
                            Console.ReadKey();
                            continue;
                        }
                        else
                        {
                            try
                            {
                                var userToDeposit    = CreateUser(btx, suser);
                                var accountToDeposit = CreateAccount(btx, suser);
                                var ActiveUser       = CreateUser(btx, currentUser);
                                var ActiveAccount    = CreateAccount(btx, currentUser);
                                do
                                {
                                    Console.Clear();
                                    try
                                    {
                                        Console.Write("Select the amount you wish to deposit:\n>");
                                        decimal depositedAmount = decimal.Parse(Console.ReadLine().Replace(",", "."));

                                        //Checks for negative amount entries and if the active user has sufficient money to make the deposit
                                        if (depositedAmount > 0 && ActiveAccount.Amount >= depositedAmount)
                                        {
                                            Account.UpdateAccounts(ActiveAccount, accountToDeposit, btx);
                                            Account.UpdateAmount(ActiveAccount, accountToDeposit, depositedAmount, true);
                                            Account.UpdateTime(ActiveAccount, accountToDeposit);

                                            Console.Clear();
                                            Console.WriteLine($"Successfully deposited {Account.FormatAmount(depositedAmount)}");

                                            //Adds transaction to buffer
                                            FileAccess.AddToBuffer(User.GenerateReport(ActiveUser, depositedAmount, userToDeposit, true, false));
                                            btx.SaveChanges();
                                            break;
                                        }

                                        else
                                        {
                                            Console.WriteLine("Invalid entry, please retype the amount");
                                            Console.ReadKey();
                                            continue;
                                        }
                                    }
                                    catch (Exception)
                                    {
                                        Console.WriteLine("This is not a valid entry.Please try again.");
                                        Console.ReadKey();
                                    }
                                } while (true);

                                break;
                            }
                            catch (Exception)
                            {
                                Console.WriteLine("Invalid entry, user does not exist");
                                Console.ReadKey();
                            }
                        }
                    }
                    //Deposit to Internal
                    else
                    {
                        try
                        {
                            var Internalaccount    = CreateAccount(btx, "admin");
                            var InternalUser       = CreateUser(btx, "admin");
                            var user               = CreateUser(btx, currentUser);
                            var CurrentUserAccount = CreateAccount(btx, currentUser);

                            Console.Write("Select the amount you wish to deposit:\n>");

                            decimal depositedAmount = decimal.Parse(Console.ReadLine().Replace(",", "."));

                            //Checks for negative amount entries and if the active user has sufficient money to make the deposit
                            if (depositedAmount > 0 && CurrentUserAccount.Amount >= depositedAmount)
                            {
                                Account.UpdateAccounts(CurrentUserAccount, Internalaccount, btx);
                                Account.UpdateAmount(CurrentUserAccount, Internalaccount, depositedAmount, true);
                                Account.UpdateTime(CurrentUserAccount, Internalaccount);

                                Console.Clear();
                                Console.WriteLine($"Successfully deposited {Account.FormatAmount(depositedAmount)}");

                                //Adds transaction to buffer
                                FileAccess.AddToBuffer(GenerateReport(user, depositedAmount, InternalUser, false, true));
                                btx.SaveChanges();
                                break;
                            }

                            else
                            {
                                Console.WriteLine("Invalid entry, please retype the amount");
                                Console.ReadKey();
                            }
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("This is not a valid entry.Please try again.");
                            Console.ReadKey();
                        }
                    }
                }
            }
        }
Beispiel #5
0
 public Repository()
 {
     _context = new BankContext();
 }