18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Inheritance 161<br />

10.8.1 Implementation<br />

The procedure Initialize is called whenever an <strong>in</strong>stance of Account_At is elaborated. This procedure<br />

checks if this is the first elaboration, determ<strong>in</strong>ed by the reference count The_Active. If it is the first concurrent<br />

elaboration then the audit trail file log.txt is opened <strong>in</strong> append mode and associated with the file descriptor<br />

The_Audit_Trail.<br />

with <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Integer_Text_Io, <strong>Ada</strong>.Float_Text_Io;<br />

use <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Integer_Text_Io, <strong>Ada</strong>.Float_Text_Io;<br />

package body Class_Account_At is<br />

procedure Initialize( The:<strong>in</strong> out Account_At ) is<br />

beg<strong>in</strong><br />

The_Active := The_Active + 1; --Another object<br />

if The_Active = 1 then -- first time for class<br />

Open( File=>The_Audit_Trail,<br />

Mode=>Append_File, Name=>"log.txt" );<br />

end if;<br />

end Initialize;<br />

The procedure F<strong>in</strong>alize is called when the elaborated storage for an <strong>in</strong>stance of Account_At goes out of<br />

scope. The reference count of the number of active <strong>in</strong>stantiations of Account_At is checked and when the last<br />

concurrent <strong>in</strong>stance goes out of scope the file descriptor The_Audit_Trail is closed.<br />

procedure F<strong>in</strong>alize( The:<strong>in</strong> out Account_At ) is<br />

beg<strong>in</strong><br />

if The_Active = 1 then Close( The_Audit_Trail ); end if;<br />

The_Active:=The_Active-1;<br />

end F<strong>in</strong>alize;<br />

The rest of the implementation follows very closely the previous implementation of the class Account with the<br />

additional functionality of writ<strong>in</strong>g the audit trail record for each transaction performed.<br />

procedure Deposit( The:<strong>in</strong> out Account_At; Amount:<strong>in</strong> Pmoney ) is<br />

beg<strong>in</strong><br />

The.Balance_Of := The.Balance_Of + Amount;<br />

Audit_Trail( The, " Deposit : Amount = ", Amount );<br />

end Deposit;<br />

procedure Withdraw( The:<strong>in</strong> out Account_At;<br />

Amount:<strong>in</strong> Pmoney; Get:out Pmoney ) is<br />

beg<strong>in</strong><br />

if The.Balance_Of >= Amount then<br />

The.Balance_Of := The.Balance_Of - Amount;<br />

Get := Amount;<br />

else<br />

Get := 0.00;<br />

end if;<br />

Audit_Trail( The, " Withdraw : Amount = ", Get );<br />

end Withdraw;<br />

function Balance( The:<strong>in</strong> Account_At ) return Money is<br />

beg<strong>in</strong><br />

Audit_Trail( The, " Balance : Balance = ", The.Balance_Of );<br />

return The.Balance_Of;<br />

end Balance;<br />

end Class_Account_At;<br />

© M A Smith - May not be reproduced without permission

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!