public bool MakePayment(string paymentName, decimal amount, Account backupAccount = null) { // Ensures we have enough money if (Balance >= amount) { _transactions.Add($"Withdrew { string.Format("{0:C2}", amount) } for { paymentName }"); Balance -= amount; TransactionApprovedEvent?.Invoke(this, paymentName); return(true); } else { // We don't have enough money so we check to see if we have a backup account if (backupAccount != null) { // Checks to see if we have enough money in the backup account if ((backupAccount.Balance + Balance) >= amount) { // We have enough backup funds so transfar the amount to this account // and then complete the transaction. decimal amountNeeded = amount - Balance; OverdraftEventArgs args = new OverdraftEventArgs(amountNeeded, "Extra Info"); OverdraftEvent?.Invoke(this, args); if (args.CancelTransaction == true) { return(false); } bool overdraftSucceeded = backupAccount.MakePayment("Overdraft Protection", amountNeeded); // This should always be true but we will check anyway if (overdraftSucceeded == false) { // The overdraft failed so this transaction failed. return(false); } AddDeposit("Overdraft Protection Deposit", amountNeeded); _transactions.Add($"Withdrew { string.Format("{0:C2}", amount) } for { paymentName }"); Balance -= amount; TransactionApprovedEvent?.Invoke(this, paymentName); return(true); } else { // Not enough backup funds to do anything return(false); } } else { // No backup so we fail and do nothing return(false); } } }
public bool MakePayment(string paymentName, decimal amount, Account backupAccount = null) { // Ensures we have enough money if (Balance >= amount) { _transactions.Add($"Withdrew { string.Format("{0:C2}", amount) } for { paymentName }"); Balance -= amount; /*can pass an empty argument into the Invoke(this, EventArgs.Empty) * 2nd param if our EventHandler<T> type is a class that inherits from EventArgs object*/ TransactionApprovedEvent?.Invoke(this, paymentName); return(true); } else { // We don't have enough money so we check to see if we have a backup account if (backupAccount != null) { // Checks to see if we have enough money in the backup account if ((backupAccount.Balance + Balance) >= amount) { // We have enough backup funds so transfar the amount to this account // and then complete the transaction. decimal amountNeeded = amount - Balance; OverdraftEventArgs args = new OverdraftEventArgs(amountNeeded, "Extra Info"); /*Fire off Overdraft event*/ OverdraftEvent?.Invoke(this, args); /* allow cancelation in Dashbord.cs line 68*/ if (args.CancelTransaction == true) { return(false); } bool overdraftSucceeded = backupAccount.MakePayment("Overdraft Protection", amountNeeded); // This should always be true but we will check anyway if (overdraftSucceeded == false) { // The overdraft failed so this transaction failed. return(false); } AddDeposit("Overdraft Protection Deposit", amountNeeded); _transactions.Add($"Withdrew { string.Format("{0:C2}", amount) } for { paymentName }"); Balance -= amount; TransactionApprovedEvent?.Invoke(this, paymentName); return(true); } else { // Not enough backup funds to do anything return(false); } } else { // No backup so we fail and do nothing return(false); } } }