//Execute a given transaction, logs the transaction, and invoke the balancewarning //if the users balance gets under 50 private T ExecuteTransaction <T>(T transaction) where T : Transaction { try { transaction.Execute(); _transactions.Add(transaction); LogTransaction(transaction, "Completed"); if (transaction.User.Balance <= 50) { UserBalanceWarning?.Invoke(transaction.User, transaction.User.Balance); } return(transaction); } catch (DeactivatedProductExcetion ex) { LogTransaction(transaction, $"Failed: {ex.Message}"); throw; } catch (InsufficientCreditsException ex) { LogTransaction(transaction, $"Failed: {ex.Message}"); throw; } }
public void WarnUserOfLowBalance(decimal amount) { foreach (IUser user in Users) { UserBalanceWarning?.Invoke(user, amount); } }
private void OnUserBalanceChangedEvent(IUser user) { if (user.Balance < 50) { UserBalanceWarning?.Invoke(user); } }
private void SubscribeEvents() { foreach (IUser user in Users) { user.LowFundsWarning += (amount) => UserBalanceWarning?.Invoke(user, amount); } }
//Helper function for executing both kinds of transactions, while adding them to the log file private void ExecuteTransaction(Transaction transaction) { transaction.Execute(); if (transaction.User.Balance <= 50) { UserBalanceWarning?.Invoke(transaction.User, transaction.User.Balance); } WriteToLogFile(transaction); allTransactions.Add(transaction); }
//Find the user with the given username public User GetUserByUsername(string username) { User user = _users.Find(p => p.Username == username); if (user != null) { if (user.Balance < 50) { UserBalanceWarning?.Invoke(user, user.Balance); } return(user); } throw new UserNotFoundException("User was not found", username); }
public bool ExecuteTransaction(Transaction trans) { bool success = trans.Execute(); if (success == true) { if (trans.User.Balance <= 50) { UserBalanceWarning?.Invoke(trans.User, trans.User.Balance); } using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"Data\Transactions.txt", true)) { file.WriteLine(trans.LogToString()); } DaAccess.Add(trans); } return(success); }
public delegate void UserBalanceNotification(object source, int balance); // delegate to pass when user balance fall below critical levels protected virtual void OnUserBalanceWarning(int balance) { UserBalanceWarning?.Invoke(this, balance); }
protected virtual void OnUserBalanceWarning(User user) { UserBalanceWarning?.Invoke(user, user.Balance); }
protected virtual void OnUserBalanceNotification(LowBalanceEventArgs e) { UserBalanceWarning?.Invoke(this, e); }