MATLAB Object-Oriented Programming

(Joyce) #1
end
newbal = BA.AccountBalance - amt;
BA.AccountBalance = newbal;
if newbal < 0
notify(BA,'InsufficientFunds')
end
end
function getStatement(BA)
disp('-------------------------')
disp(['Account: ',num2str(BA.AccountNumber)])
ab = sprintf('%0.2f',BA.AccountBalance);
disp(['CurrentBalance: ',ab])
disp(['Account Status: ',BA.AccountStatus])
disp('-------------------------')
end
end
methods (Static)
function obj = loadobj(s)
if isstruct(s)
accNum = s.AccountNumber;
initBal = s.AccountBalance;
obj = BankAccount(accNum,initBal);
else
obj.AccountListener = AccountManager.addAccount(s);
end
end
end
end

Formulating the AccountManager Class


The purpose of the AccountManager class is to provide services to accounts. For the
BankAccount class, the AccountManager class listens for withdrawals that cause the
balance to drop into the negative range. When the BankAccount object triggers the
InsufficientsFunds event, the AccountManager resets the account status.

The AccountManager class stores no data so it does not need properties. The
BankAccount object stores the handle of the listener object.

The AccountManager performs two operations:


  • Assign a status to each account as a result of a withdrawal

  • Adds an account to the system by monitoring account balances.


Class Components

The AccountManager class implements two methods:

3 MATLAB Classes Overview

Free download pdf