Ada 95 :: x11_acc6.ada

-- Initialisation of an object item
-- Remember discriminants must have descreit or access types
--
package Class_account is

  subtype Money  is Float;
  subtype PMoney is Float range 0.0 .. Float'Last;
  type Account( number: Natural:= 0 ) is private;

  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;
  procedure new_number( the: in out Account; n:in Natural );
  function  new_account( n:in Natural;
                         amount:in PMoney:=0.0 ) return Account;

private
  type Account( number: Natural:= 0) is record
    balance_of : Float := 0.00;
  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: Account #"); put( the.number ); new_line;
    put("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;

  procedure new_number( the: in out Account; n:in Natural ) is
  begin
    the := Account'( n, the.balance_of );
  end new_number;

  function  new_account( n:in Natural;
                         amount:in PMoney:=0.0 ) return Account is
    an_account : Account := Account'( n, amount );
  begin
    return an_account;
  end new_account;

end Class_account;


with Ada.Text_io, Class_account;
use  Ada.Text_io, Class_account;
procedure main1 is
  my_account: Account(10001);
  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;
use  Ada.Text_io, Class_account;
procedure main2 is
  my_account: Account(10001);
begin
  begin
    deposit( my_account, 200.00 );
    statement( my_account );
    new_number( my_account, 10002 );
    statement( my_account );
  exception
   when CONSTRAINT_ERRor =>
     put("raised CONSTRAINT_ERRor It when wrong"); new_line;
   when others =>
     put("Unknow error"); new_line;
  end;
end main2;


with Ada.Text_io, Class_account;
use  Ada.Text_io, Class_account;
procedure main3 is
  my_account : Account := new_account( 10001, 20.0 );
begin
  statement( my_account );
end main3;

with Ada.Text_io, Class_account;
use  Ada.Text_io, Class_account;
procedure main4 is
begin
  null;
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]