The Account Class

Introduction

In this lesson we will define the Account class as discussed in the previous lesson. Member data (properties) will be added to the class and member methods are developed and inserted into the class.

How define a new Class?

A new class can be inserted in a program by selecting Class from the Insert button on the toolbar (see red arrow on screenshot) or from the Insert menu item.

In the Inspector we change the default name of the class (Class1) into Account.

Add a property to a Class

Insert property

To add a property to the class, you must first select the class in the Navigator. The small + button above the coding pane (see blue arrow on the screenshot) allows you to add various code items (Menu Handler, Method, Note, Property…). Select Property (see red arrow on the screenshot).

In the Inspector we can change the characteristics of the new property: the default Name (default Untitled) into AccountBalance , the default data Type (default Integer) into Currency and we limit the Scope of the property to Private.

It is good practice to set properties and methods to Private if they are not needed outside the class.

Add a method to a Class

To add a method to a class, you must first select the class in the Navigator. The small + button above the coding pane (see blue arrow on the screenshot) allows you to add various code items (Menu Handler, Method, Note, Property…). Select Method (see red arrow on the screenshot).

DepositMoney

Now, the Method Name, the Parameters, the Return Type and the Scope of the new method can be defined in the Inspector (see screenshot).

  • Method Name: the name of the method. This name must be used in the code to call the method;
  • Parameters: the name and data type of variables that will receive data passed from the calling code. Parameters are separated by commas. These variables will be used in the new method (for instance amount As Currency : amount is a variable (parameter) that can be used in the method);
  • Return Type: some methods will return a value to the line of code that called the method. The Return Type specifies the data type of the returned value;
  • Scope: indicates the accessibility (public, private, protected) of the method in the application.

The method code can be entered in the blank code editor. Our code is simple: increase the property AccountBalance with the deposited value available in the parameter amount.

AccountBalance = AccountBalance + amount

WithdrawMoney

The definition of the method WithdrawMoney is similar to the previous definition.

However, no more money can be withdrawn than the balance of the account. The following code has been entered in the code editor:

//the value of amount cannot be greater than the value of AccountBalance
If amount > AccountBalance Then
  amount = AccountBalance
End If
AccountBalance = AccountBalance - amount
// Is used to add comments to the code

GetBalance

Since the property AccountBalance is a private property, we need a public method (GetBalance) to access this private property. The method GetBalance will return the value of AccountBalance (data type Currency) to the calling line of code.

The code in the code editor is limited to one line:

Return AccountBalance

The AccountWindow

AccountWindow

The window named AccountWindow contains 3 buttons and one label:

  • Button named DepositButton with Caption ‘Deposit‘ (Scope = Private);
  • Button named WithdrawButton with Caption ‘WithDraw‘ (Scope = Private);
  • Button named ShowButton with Caption ‘Balance‘ (Scope = Private);
  • Label named BalanceLabel with empty Text property (Scope = Private).

To simplify our application, we use fixed amounts to deposit (1500.00) and withdraw (275.00).

CheckingAccount

To create a reference variable CheckingAccount we can add a Private property to AccountWindow. This declaration creates an empty (Nil) variable. The variable will be accessible by all methods of AccountWindow. To create an instance of the class Account the reference variable has to refer to the Account Class. This can be done with the New keyword.

In the Opening event of AccountWindow we can instantiate CheckingAccountwith the following statement:

CheckingAccount =  New Account

DepositButton: Pressed

The Pressed event of DepositButton must increase the balance of checkingAccountwith the value (1500.00) copied to the parameter amount:

CheckingAccount.DepositMoney(1500.00)

WithdrawButton: Pressed

The Pressed event of WithdrawButton must decrease the balance of checkingAccountwith the value (275.00) copied to the parameter amount:

CheckingAccount.WithdrawMoney(275.00)

ShowButton: Pressed

The Pressed event of ShowButton must show the value of AccountBalance. The value will be returned by the method GetBalance.

BalanceLabel.Text = "The balance of the checking account = " + _ 
   CheckingAccount.GetBalance.ToString(locale.current)
The underscore character (_) is used to indicate that the current line continues on the next line.

Because the Text property of BalanceLabel only accepts String values, the value returned by GetBalance (Currency) must be converted to a String (ToString). The ToString parameter locale.current uses the settings of the system to display the number format and the currency sign.

Your application is ready to run. Check your starting balance by pressing the ShowButton. Then add 1500.00 by pressing the DepositButton and decrease your balance with 275.00 by pressing the WithDrawButton. The balance of your account must be calculated correctly.