Beispiel #1
0
        public static bool TransferMoney(Account starting, Account destination, float amount)
        {
            if (amount > max_transfer)
            {
                MessageBox.Show("Amount exceeds maximum single transaction limit.");
                return(false);
            }

            //Attempt to make changes to the account balance itself thread safe.

            Mutex mutex = new Mutex();

            mutex.WaitOne();
            //Critical Section - Begin
            bool transferComplete = false;

            using (var context = new AccountManagerContext()) {
                destination.Balance += amount;
                starting.Balance    -= amount;

                if (starting.GetType() == typeof(SavingsAccount))
                {
                    SavingsAccount s = (SavingsAccount)starting;
                    s.WithdrawalsThisMonth++;
                }
                context.Entry(starting).State    = System.Data.Entity.EntityState.Modified;
                context.Entry(destination).State = System.Data.Entity.EntityState.Modified;
                context.SaveChanges();
                transferComplete = true;
            }
            //Critical Section - End
            mutex.ReleaseMutex();
            return(transferComplete);
        }
Beispiel #2
0
        public static bool CreditAccount(Account account, float creditAmount)
        {
            if (creditAmount > max_transfer)
            {
                MessageBox.Show("Amount exceeds maximum single transaction limit.");
                return(false);
            }

            //Attempt to make changes to the account balance itself thread safe.

            Mutex mutex = new Mutex();

            mutex.WaitOne();

            //Critical Section - Begin
            bool balanceChanged = false;

            using (var context = new AccountManagerContext())
            {
                account.Balance += creditAmount;
                context.Entry(account).State = System.Data.Entity.EntityState.Modified;
                context.SaveChanges();
            }
            balanceChanged = true;
            //Critical Section - End

            mutex.ReleaseMutex();
            return(balanceChanged);
        }
Beispiel #3
0
        public static bool CreateAccount(int pin, String name, String businessName, Bank_Name bank)
        {
            Mutex mutex = new Mutex();

            mutex.WaitOne();
            using (var context = new AccountManagerContext())
            {
                if (!VerifyPin(pin))
                {
                    return(false);
                }

                if (FindAccount(pin, name, bank, AccountType.Business) != null)
                {
                    return(false);
                }

                BusinessAccount account = new BusinessAccount
                {
                    PinNumber       = pin,
                    AccountHolder   = name,
                    BankName        = bank,
                    BusinessName    = businessName,
                    Balance         = 0,
                    AccountOpenDate = DateTime.Parse(DateTime.Today.ToString())
                };

                context.BusinessAccounts.Add(account);
                context.SaveChanges();
            }
            mutex.ReleaseMutex();

            return(true);
        }
Beispiel #4
0
        //Function for fishing a row out of the database
        //Takes in the neccesary data to perform a linq query

        public static Account FindAccount(int pin, String name, Bank_Name bank, AccountType type)
        {
            using (var context = new AccountManagerContext())
            {
                Account account;
                switch (type)
                {
                case AccountType.Checking:
                    account = context.CheckingAccounts.Where(s => s.PinNumber == pin && s.AccountHolder == name && s.BankName == bank).FirstOrDefault <CheckingAccount>();
                    break;

                case AccountType.Savings:
                    account = context.SavingsAccounts.Where(s => s.PinNumber == pin && s.AccountHolder == name && s.BankName == bank).FirstOrDefault <SavingsAccount>();
                    break;

                case AccountType.Business:
                    account = context.BusinessAccounts.Where(s => s.PinNumber == pin && s.AccountHolder == name && s.BankName == bank).FirstOrDefault <BusinessAccount>();
                    break;

                default:
                    account = null;
                    break;
                }
                return(account);
            }
        }
Beispiel #5
0
        /*
         *
         * Overloaded functions for creating accounts
         * These methods were just for quick testing, a user creating a bank account from an application would be strange from a real world perspective
         *
         */


        public static bool CreateAccount(int pin, float monthlyTransfer, String name, Bank_Name bank)
        {
            Mutex mutex = new Mutex();

            mutex.WaitOne();
            using (var context = new AccountManagerContext())
            {
                if (!VerifyPin(pin))
                {
                    return(false);
                }

                if (FindAccount(pin, name, bank, AccountType.Checking) != null)
                {
                    return(false);
                }

                CheckingAccount account = new CheckingAccount
                {
                    PinNumber             = pin,
                    AccountHolder         = name,
                    BankName              = bank,
                    Balance               = 0,
                    AccountOpenDate       = DateTime.Parse(DateTime.Today.ToString()),
                    MonthlyTransferAmount = monthlyTransfer,
                };

                context.CheckingAccounts.Add(account);
                context.SaveChanges();
            }
            mutex.ReleaseMutex();

            return(true);
        }