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); }
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); }
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); }
/* * * 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); }