bank-account/bank-account-properties/accumulating-bank-account.cs - A BankAccount class with a Balance property - without a balance instance variable. | Lecture 5 - slide 7 : 29 Program 2 |
using System; public class BankAccount { private string owner; private decimal[] contributions; private int nextContribution; public BankAccount(string owner, decimal balance) { this.owner = owner; contributions = new decimal[100]; contributions[0] = balance; nextContribution = 1; } public decimal Balance { get {decimal result = 0; foreach(decimal ctr in contributions) result += ctr; return result; } } public void Deposit(Decimal amount){ contributions[nextContribution] = amount; nextContribution++; } public void Withdraw(Decimal amount){ contributions[nextContribution] = -amount; nextContribution++; } public override string ToString() { return owner + "'s account holds " + + Balance + " kroner"; } }