Back to slide -- Keyboard shortcut: 'u'        next -- Keyboard shortcut: 'n'          interface-documentation/bank/bank-account.cs - The class BankAccount - with documentation comments.Lecture 15 - slide 5 : 13
Program 1

using System;

/// <summary> 
///   A simple bank account created as an everyday example
///   for the OOP course. Used in particular for demonstrating inheritance.
/// </summary>
public class BankAccount {

   private double interestRate;
   private string owner;
   private double balance;

   /// <summary>
   ///  Construct a bank account from owner. 
   ///  The interestRate is given the default value 0.0.
   ///  The balance is given the default value 0.0.
   /// </summary>
   /// <param name = "owner"> The owners name. A string.</param>
   public BankAccount(string owner): 
     this(owner, 0.0, 0.0) {
   }

   /// <summary>
   ///  Construct a bank account from owner, balance, and interestRate.
   ///  The interestRate is given the default value 0.0.
   /// </summary>
   /// <param name = "owner"> The owners name </param>
   /// <param name = "balance"> The initial amount of this account</param>
   public BankAccount(string owner, double balance): 
     this(owner, balance, 0.0) {
   }

   /// <summary>
   ///  Construct a bank account from owner, balance, and interestRate. 
   /// </summary>
   /// <param name = "owner"> The owners name </param>
   /// <param name = "balance"> The initial amount of this account</param>
   /// <param name = "interestRate"> 
   ///    The interest rate. 
   ///    Annual interet is calculated as balance * interestRate 
   /// </param>
   public BankAccount(string owner, double balance, double interestRate) {
      this.interestRate = interestRate;
      this.owner = owner; 
      this.balance = balance;
   }

   /// <summary>
   ///  Returns the current amount of the bank account 
   ///  without affecting the account.
   /// </summary>
   /// <value> Accesses the amount of money in the account</value>
   public double Balance {
     get{
      return balance;
     }
   }

   /// <summary>
   ///   Return the interest rate of the account
   /// </summary>
   /// <value> Accesses the interest rate of the account </value>
   public double InterestRate {
     get{
      return interestRate;
     }
   }

   /// <summary>
   ///   Withdraw an amount of money from the account.
   ///   This decreases the balance of the account.
   /// </summary>
   /// <param name = "amount"> 
   ///  The amount of money to withdraw from the account.
   ///  Precondition: Must be non-negative.
   /// </param>   
   public void Withdraw (double amount) {
      balance -= amount;
   }

   /// <summary>
   ///   Withdraw an amount of money from the account.
   ///   This increases the balance of the account.
   /// </summary>
   /// <param name = "amount"> 
   ///  The amount of money to deposit to the account.
   ///  Precondition: Must be non-negative.
   /// </param>   
   public void Deposit (double amount) {
      balance += amount;
   }

   /// <summary>
   ///   Add the annual interest to the account.
   ///   This may increase the current balance of the account.
   /// </summary>
   public void AddInterests() {
      balance = balance + balance * interestRate;
   }    

   /// <summary>
   ///   Return a text string that represents this account
   ///   for output purposes
   /// </summary>
   public override string ToString() {
      return owner + "'s account holds " +
            + balance + " kroner";
   }
}