| MATLAB® | ![]() |
| On this page… |
|---|
Implementing the BankAccount Class |
This example discusses the design and implementation of a simple class. To design a class that represents a bank account, first determine the elements of data and the operations that form your abstraction of a bank account. For example, a bank account has:
An account number
An account balance
A current status (open, closed, etc.)
You need to perform certain operations on a bank account:
Deposit money
Withdraw money
You might also want the bank account to send a notice if the balance is too low and an attempt is made to withdraw money. When this event occurs, the bank account can broadcast a notice and other entities that are designed to listen for these notices, such as an account manager program, can take action. In this case, the status of all bank accounts is determined by an account manager program that looks at the account balance and assigns one of three values:
open — Account balance is a positive value
overdrawn — Account balance is overdrawn, but by $200 or less.
closed — Account balance is overdrawn by more than $200.
In MATLAB® classes, data is stored in properties, operations are implemented with methods, and notifications are supported with events and listeners. Therefore, the bank account class needs the components discussed in the following sections.
The class needs to define properties to store the account number and the account balance:
AccountNumber — This property is assigned a value when you create an instance of the class.
AccountBalance — This property is modified by the class operation of depositing and withdrawing money.
AccountStatus — This property is set to an initial value when an instance of the class is created. It is then changed by methods from the AccountManager class whenever the value of the AccountBalance falls below 0.
The first two properties contain information that only the class should be able to change, so the SetAccess attribute should be set to private (only class methods can set these values). The AccountStatus is determined by an external program that needs access to the property, so its SetAccess attribute is left as the default, which is public (any code can access this property value).
There are three operations that the class must be able to perform, so there needs to be three methods:
deposit — Update the AccountBalance property when a deposit transaction occurs
withdraw — Update the AccountBalance property when a withdrawal transaction occurs
BankAccount — Create an initialized instance of the class
Bank accounts with negative balances have their status changed by the account manager program, as described above. To implement this action, the BankAccount class triggers an event when a withdrawal causes a negative balance to occur. Therefore, the triggering of the InsufficientsFunds event occurs from within the withdraw method.
To define an event, you simply define a name within an events block. The event is triggered by a call to the notify handle class method. Note that this is not a predefined event; it could be named with any string and you can trigger this event with any action you choose.
It makes sense for there to be only one set of data associated with any instance of a BankAccount class. You would not want independent copies of the object that could have, for example, different values for the account balance. Therefore, the BankAccount class should be implemented as a handle class. All copies of a given handle object refer to the same data.
You can display the code for this example in a popup window that contains detailed comments and links to related sections of the documentation:
You can open both class files in your editor by clicking this link:
classdef BankAccount < handle properties (Hidden) AccountStatus = 'open'; end % The following properties can be set only by class methods properties (SetAccess = private) AccountNumber AccountBalance = 0; end % Define an event called InsufficientFunds events InsufficientFunds end methods function BA = BankAccount(AccountNumber,InitialBalance) BA.AccountNumber = AccountNumber; BA.AccountBalance = InitialBalance; % Calling a static method requires the class name % addAccount registers the InsufficientFunds listener on this instance AccountManager.addAccount(BA); end function deposit(BA,amt) BA.AccountBalance = BA.AccountBalance + amt; if BA.AccountBalance > 0 BA.AccountStatus = 'open'; end end function withdraw(BA,amt) if (strcmp(BA.AccountStatus,'closed')&& BA.AccountBalance < 0) disp(['Account ',num2str(BA.AccountNumber),' has been closed.']) return end newbal = BA.AccountBalance - amt; BA.AccountBalance = newbal; % If a withdrawal results in a negative balance, % trigger the InsufficientFunds event using notify if newbal < 0 notify(BA,'InsufficientFunds') end end % withdraw end % methods end % classdef
The AccountManager class provides two methods that implement and register a listener for the InsufficientsFunds event, which is defined for all BankAccount objects. The BankAccount class constructor method calls addAccount to register the listener for the instance being created.
classdef AccountManager methods (Static) function assignStatus(BA) if BA.AccountBalance < 0 if BA.AccountBalance < -200 BA.AccountStatus = 'closed'; else BA.AccountStatus = 'overdrawn'; end end end function addAccount(BA) % Call the handle addlistener method % Object BA is a handle class addlistener(BA, 'InsufficientFunds', ... @(src, evnt)AccountManager.assignStatus(src)); end end end
Note that the AccountManager class is never instantiated. It serves as a container for the event listener used by all BankAccount objects.
The BankAccount class, while overly simple, demonstrates how MATLAB classes behave. For example, create a BankAccount object with a serial number and an initial deposit of $500:
BA = BankAccount(1234567,500); BA.AccountNumber ans = 1234567 BA.AccountBalance ans = 500 BA.AccountStatus ans = open
Now suppose you make a withdrawal of $600, which results in a negative account balance:
BA.withdraw(600) BA.AccountBalance ans = -100 BA.AccountStatus ans = overdrawn
When the $600 withdrawal occurred, the InsufficientsFunds event was triggered. Because the AccountBalance is not less than –$200, the AccountStatus was set to overdrawn:
BA.withdraw(200) BA.AccountBalance ans = -300 BA.AccountStatus ans = closed
Now the AccountStatus has been set to closed by the listener and further attempts to make withdrawals are blocked:
BA.withdraw(100) Account 1234567 has been closed
If the AccountBalance is returned to a positive value by a deposit, then the AccountStatus is returned to open and withdrawals are allowed again:
BA.deposit(700) BA.AccountStatus ans = open BA.withdraw(100) BA.AccountBalance ans = 300
![]() | Defining Classes — Syntax Overview | Using Objects to Write Data to a File | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |