Reputation: 1005
I'm reading the pdf book of Matlab OOP pdf and after implementing the code for the bank account chapter 3, it's complaining of something.
>> BA = BankAccount(1234567, 500)
Undefined function 'addListener' for input arguments of type 'BankAccount'.
Error in AccountManager.addAccount (line 20)
lh = addListener(BA, 'InsufficientFunds', @(src,
~)AccountManager.assignStatus(src))
Error in BankAccount (line 21)
BA.AccountListener = AccountManager.addAccount(BA);
I have no idea why is this as I followed the example given, as:
classdef BankAccount < handle
%UNTITLED Summary of this class goes here
% Detailed explanation goes here
properties (Access = ?AccountManager)
AccountStatus = 'open'
end
properties (SetAccess='private')
AccountNumber
AccountBalance
end
properties (Transient) %not saved
AccountListener
end
events
InsufficientFunds
end
methods
function BA = BankAccount(AccountNumber, InitialBalance)
BA.AccountNumber = AccountNumber;
BA.AccountBalance = InitialBalance;
BA.AccountListener = 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 newBal < 0
notify(BA, 'InsufficientFunds')
end
end
function getStatement(BA)
disp('-----------')
disp(['Account', num2str(BA.AccountNumber)])
ab = sprintf('%0.2f', BA.AccountBalance)
disp(['Current Balance', 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
And:
classdef AccountManager
%UNTITLED2 Summary of this class goes here
% Detailed explanation goes here
properties
end
methods (Static)
function assignStatus(BA)
if BA.AccountBalance < 0
if BA.AccountBalance < -200
BA.AccountStatus = 'closed'
else
BA.AccountStatus = 'overdrawn'
end
end
end
function lh = addAccount(BA)
lh = addListener(BA, 'InsufficientFunds', @(src, ~)AccountManager.assignStatus(src))
end
end
end
So could anyone tell me what is going on here? Is on Matlab R2014a(8.3.0.532). I think I have implemented correctly but not copy pasted, maybe I overlooked a line. Thanks.
Upvotes: 2
Views: 268
Reputation: 104474
That's because the way you spelled the method is slightly incorrect. It's called addlistener
- lower case l
. You have an upper case L when you spelled it.
The book you're referencing is MATLAB's official Object Oriented Programming guide - http://www.mathworks.com/help/pdf_doc/matlab/matlab_oop.pdf. The code in question is on Page 3-16.
Remember, MATLAB is case-sensitive. Even if the characters are different cases, it will be interpreted as different variables, functions, etc. Don't worry - I think addListener
is more natural as there are two words. addlistener
is just... weird!
Upvotes: 3