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; } // Constructor to initialize a BankAccount from a stream. public BankAccount(StreamReader reader) { // Read account holder from file, as a single line. accountHolder = reader.ReadLine(); // Read balance from file, as a single line, and convert to a double. balance = double.Parse(reader.ReadLine()); // Read transactions from file, and split at spaces. string allTxStr = reader.ReadLine(); string[] splitTxStr = allTxStr.Split(' '); // Loop through the transaction strings read from file, and add each to the transactions list (as a double). foreach (string txstr in splitTxStr) { if (!string.IsNullOrEmpty(txstr)) { double tx = double.Parse(txstr); transactions.Add(tx); } } } // Method to write a BankAccount to a stream. public void WriteToStream(StreamWriter writer) { // Write account holder to file, as a single line. writer.WriteLine("{0}", accountHolder); // Write balance to file, as a single line. writer.WriteLine("{0}", balance); // Write transactions to file (space-separated), as a single line. foreach (double transaction in transactions) { writer.Write("{0} ", transaction); } // Append a newline after writing the last transaction amount. writer.WriteLine(); } // 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); } } }