MATLAB Object-Oriented Programming

(Joyce) #1

types(k) = obj(k).Type;
values(k) = obj(k).getCurrentValue;
end
t = table;
t.Description = descs';
t.Type = types';
t.Value = values';
disp(t)
end


The Assets class pie method calls the getCurrentValue method element-wise on
objects in the array to obtain the data for the pie chart.


pie Method Code


function pie(assetArray)
stockAmt = 0; bondAmt = 0; cashAmt = 0;
for k=1:length(assetArray)
if isa(assetArray(k),'financial.Stocks')
stockAmt = stockAmt + assetArray(k).getCurrentValue;
elseif isa(assetArray(k),'financial.Bonds')
bondAmt = bondAmt + assetArray(k).getCurrentValue;
elseif isa(assetArray(k),'financial.Cash')
cashAmt = cashAmt + assetArray(k).getCurrentValue;
end
end
k = 1;
if stockAmt ~= 0
label(k) = {'Stocks'};
pieVector(k) = stockAmt;
k = k +1;
end
if bondAmt ~= 0
label(k) = {'Bonds'};
pieVector(k) = bondAmt;
k = k +1;
end
if cashAmt ~= 0
label(k) = {'Cash'};
pieVector(k) = cashAmt;
end
pie(pieVector,label)
tv = stockAmt + bondAmt + cashAmt;
stg = {['Total Value of Assets: $',num2str(tv,'%0.2f')]};


A Class Hierarchy for Heterogeneous Arrays
Free download pdf