using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections.ObjectModel; using System.IO; namespace BankSystem { public class BankAccountEventArgs : EventArgs { private double transactionAmount; private DateTime timestamp; public BankAccountEventArgs(double transactionAmount) { this.transactionAmount = transactionAmount; timestamp = DateTime.Now; } public override string ToString() { return string.Format("Transaction amount {0}, timestamp {1}", transactionAmount, timestamp); } } public class BankAccount { // Fields. private string accountHolder; private double balance; private List transactions = new List(); // Events. public event EventHandler ProtectionLimitExceeded; public event EventHandler Overdrawn; // Simple constructor. public BankAccount(string accountHolder) { this.accountHolder = accountHolder; this.balance = 0; } // Other methods. public void Deposit(double amount) { // If attempt to deposit more than 100000, disallow this deposit! if (amount > 100000) { throw new BankException("Cannot deposit more than 100000.", accountHolder, amount); } // Deposit money, and store transaction amount. balance += amount; transactions.Add(amount); // If balance has exceeded the government's protection limit, raise a ProtectionLimitExceeded event. if (balance >= 50000 && ProtectionLimitExceeded != null) { ProtectionLimitExceeded(this, new BankAccountEventArgs(amount)); } } public void Withdraw(double amount) { // If account is already overdrawn, disallow this withdrawal! if (balance < 0) { throw new BankException("Cannot withdraw from an overdrawn account.", accountHolder, amount); } // Withdraw money, and store transaction amount as a negative amount (to denote a withdrawal). balance -= amount; transactions.Add(-amount); // If account is now negative, raise an Overdrawn event. if (balance < 0 && Overdrawn != null) { Overdrawn(this, new BankAccountEventArgs(amount)); } } // Return a read-only wrapper for the transaction list. Prevents client app from meddling... public ReadOnlyCollection Transactions { get { return transactions.AsReadOnly(); } } public override string ToString() { return string.Format("Account {0}, balance {1}", accountHolder, balance); } } }