// Exercise 4a from Lecture 4: bank account // see also extension of this example in the C# revision lecture using System; class BankAccount { private ulong accountNo; private long balance; private string name; public void Deposit(long x) { this.balance += x; } public void Withdraw(long x) { // could use an exception here if (this.balance > x) { this.balance -= x; } else { Console.WriteLine("Current balance {0} is too low for withdrawing {1}", this.balance, x); } } public long GetBalance() { return this.balance; } public BankAccount(ulong no, string name) { this.accountNo = no; this.name = name; this.balance = 0; } public void ShowBalance() { Console.WriteLine("Current Balance: " + this.balance.ToString()); } public void ShowAccount() { Console.WriteLine("Account Number: {0}\tAccount Name: {1}\tCurrent Balance: {2}", this.accountNo, this.name, this.balance.ToString()); } } class Tester { public static void Main(){ BankAccount mine = new BankAccount(1234567, "MyAccount"); int amount = 0; mine.ShowAccount(); mine.ShowBalance(); amount = 8; Console.WriteLine("Depositing " + amount.ToString()); mine.Deposit(amount); mine.ShowBalance(); amount = 4; Console.WriteLine("Withdrawing " + amount.ToString()); mine.Withdraw(amount); mine.ShowBalance(); amount = 4; Console.WriteLine("Withdrawing " + amount.ToString()); mine.Withdraw(amount); mine.ShowBalance(); mine.ShowAccount(); } }