::
x09_acc2.ada
package Class_account is
type Account is private;
subtype Money is Float;
subtype PMoney is Float range 0.0 .. Float'Last;
procedure statement( the:in Account );
procedure deposit ( the:in out Account; amount:in PMoney );
procedure withdraw( the:in out Account; amount:in PMoney;
get:out PMoney );
function balance ( the:in Account ) return Money;
private
type Account is record
balance_of : Money := 0.00; -- Amount in account
end record;
end Class_account;
with Simple_io; use Simple_io;
package body Class_account is
procedure statement( the:in Account ) is
begin
put("Mini statement: The amount on deposit is $" );
put( the.balance_of, aft=>2, exp=>0 );
new_line(2);
end statement;
procedure deposit ( the:in out Account; amount:in PMoney ) is
begin
the.balance_of := the.balance_of + amount;
end deposit;
procedure withdraw( the:in out Account; amount:in PMoney;
get:out PMoney ) is
begin
if the.balance_of >= amount then
the.balance_of := the.balance_of - amount;
get := amount;
else
get := 0.00;
end if;
end withdraw;
function balance( the:in Account ) return Money is
begin
return the.balance_of;
end balance;
end Class_account;
package Class_account_other is
type Account is private;
subtype Money is Float;
procedure statement( the:in Account );
private
type Account is record
amount : Money := 0.00; -- Amount in account
end record;
end Class_account_other;
with Simple_io; use Simple_io;
package body Class_account_other is
procedure statement( the:in Account ) is
begin
put("Other account system "); new_line(2);
end statement;
end Class_account_other;
with Ada.Text_io, Class_account;
use Ada.Text_io, Class_account;
procedure main1 is
my_account: Account;
obtain : Money;
begin
statement( my_account );
put("Deposit $100.00 into account"); new_line; -- Deposit
deposit( my_account, 100.00 );
statement( my_account );
put("Withdraw $80.00 from account"); new_line; -- Withdraw
withdraw( my_account, 80.00, obtain );
statement( my_account );
put("Deposit $200.00 into account"); new_line; -- Deposit
deposit( my_account, 200.00 );
statement( my_account );
end main1;
with Ada.Text_io, Class_account;
procedure main2 is
my_account: Class_account.Account;
obtain : Class_account.Money;
begin
Class_account.statement( my_account );
Ada.Text_io.put("Deposit $100.00 into account");
Ada.Text_io.new_line;
Class_account.deposit( my_account, 100.00 );
Class_account.statement( my_account );
Ada.Text_io.put("Withdraw $80.00 from account");
Ada.Text_io.new_line;
Class_account.withdraw( my_account, 80.00, obtain );
Class_account.statement( my_account );
Ada.Text_io.put("Deposit $200.00 into account");
Ada.Text_io.new_line;
Class_account.deposit( my_account, 200.00 );
Class_account.statement( my_account );
end main2;
with Class_account, Class_account_other;
use Class_account, Class_account_other;
procedure main3 is
my_account :Class_account.Account;
other_account :Class_account_other.Account;
begin
statement( my_account ); -- statement in Class_account
statement( other_account ); -- statement in Class_account_other
end main3;
with Class_account;
use Class_account;
procedure main4 is
my_account : Account;
other_account: Account;
obtain : PMoney;
begin
deposit( my_account, 100.00 );
other_account := my_account; -- Copy and
withdraw(other_account, 100.00, obtain);-- Withdraw 100.00
other_account := my_account; -- Copy again and
withdraw(other_account, 100.00, obtain);-- Withdraw 100.00
end main4;
with Ada.Text_io, main1, main2, main3, main4;
use Ada.Text_io;
procedure main is
begin
put("Example Account 1 "); new_line; main1;
put("Example Account 2 "); new_line; main2;
put("Example Account 3 "); new_line; main3;
put("Example Account 4 "); new_line; main4;
end main;
© M.A.Smith University of Brighton.
Created September 1995 last modified May 1997.
Comments, suggestions, etc.
M.A.Smith@brighton.ac.uk
*
[Home page]