Previous topic | Ada Home Page | Index

If-then-elsif example

Problem specification

A program is required which will ask the user for the amount of money (positive integer only) in a bank account. It will then ask for the amount of money (integers greater than zero) to be withdrawn.

Alternative user interfaces

Enter the amount on deposit 100

Enter the withdrawal 50

Accepted. Balance is 50

Enter the amount on deposit 76

Enter the withdrawal 120

Refused! Balance is 76

Enter the amount on deposit 50

Enter the withdrawal 75

Overdraft! Balance is -25

Algorithm


1. get balance & withdrawal
	1.1 get balance
	1.2 get withdrawal
2. calculate resulting balance
	2.1 new balance = old balance - withdrawal
3. if new balance is >= zero
  then 
	3.1 indicate transaction accepted
  else if new balance between zero and overdraft limit
	3.2 indicate overdraft is used
  else
	3.3 indicate transaction rejected

Data design

NAMETYPENotes
overdraft_limitINTEGER-50 (for ease of change)
zeroINTEGER0 (for readability only)
balanceINTEGERbalance in the account
withdrawalINTEGERamount requested by user
resulting_balanceINTEGERbalance after withdrawal

Resulting Program



-- Fintan Culwin Sept '88 v1.0
-- program to illustrate simple selection
-- b1s4p2 see text p48-49

with TEXT_IO;
use  TEXT_IO;

procedure b1s4p2 is

   package int_io is new TEXT_IO.INTEGER_IO( INTEGER );
   use int_io;

   overdraft_limit : constant INTEGER := -50;
                            -- limit for overdraft refusal
   zero            : constant INTEGER := 0;

   resulting_balance,       -- result of transaction
   balance,                 -- balance in the account
   withdrawl  : integer;    -- withdrawl from the account

begin -- of b1s4p2 

   -- get balance & withdrawal
   PUT ( "Enter balance of the account ");
   GET ( balance ); SKIP_LINE;
   PUT ( "Enter the withdrawal         ");
   GET ( withdrawl ); SKIP_LINE;

   -- calculate resulting balance
   resulting_balance := balance - withdrawl;

   -- decide upon action
   if resulting_balance >= zero  then
      PUT ( "Accepted balance is ");
      PUT ( resulting_balance ); NEW_LINE;
   elsif resulting_balance >= overdraft_limit then
      PUT ( "Overdraft balance is ");
      PUT ( resulting_balance ); NEW_LINE;
   else
      PUT ( "Refused balance is ");
      PUT ( balance ); NEW_LINE;
   end if;
end b1s4p2;

Previous topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 13 Feb 96