program Bank (input,output);
(* FILE: Bank.p
  AUTHOR: K.BECKER
  DATE:   16/08/02
  PURPOSE: Program to emulate a simple ATM - C-Version
  Functions supported:
            print current balance
            process withdrawal [permit overdrafts but make them obvious]
            process deposit
*)

var	acct,	 (* bank account *) 
      trans  (* amount of transaction *)
                : real;
	ans,	 (* y/n answer to questions *)
      what   (* user's option *)
		: char;
	more,  (* loop control to allow more than one value per run *)
	dep    (* true if transaction is a deposit *)
		: boolean;

procedure doDeposit(var acct : real );
var   amount :real;
begin			    
   write('Amount to deposit : ');
   readln( amount);
   if (amount < 0.0)
      then begin
	 writeln('Sorry, invalid amount.');
      end
      else begin
	 acct := acct + amount;
	 writeln('Transaction successful, thank you.');
      end;
   
end; { doDeposit }

procedure doWithdrawal(var acct: real );
var   amount :real;
begin			     
   write('Amount to withdraw : ');
   readln( amount);
   if (amount < 0.0)
      then begin
	 writeln('Sorry, invalid amount.');
      end
      else if (acct - amount) < -100.00
	 then begin
	    writeln('Sorry, invalid amount.');	    
	    end
      else begin
	 acct := acct - amount;
	 writeln('Transaction successful, thank you.');
      end;
end; { doWithdrawal }

procedure showBalance(var acct : real) ;
begin
   writeln('Your current balance is: ');
   writeln('        $',acct:10:2);
   if acct < 0.0
      then begin
	 writeln;
	 writeln('!!! NOTE: you are overdrawn.');
      end;
   writeln;
end; { showBalance }


begin
   (* headers and instructions *)
   writeln ('BECKER''S ATM SERVICE');
   writeln;
   writeln ('Please enter all dollar values as real numbers with no $ signs.');
   writeln;

   (* initialize account to nothing *)
   acct := 0.0;
   
   (* set up for loop entry *)
   more := true;
   while more do
   begin
      writeln   ('d == deposit ');
      writeln   ('w == withdrawal ');
      writeln   ('b == print balance ');
      writeln   ('q == quit ');
      writeln;
      write     ('please choose an option: ');
      readln (ans);

      case ans of
	'd'	  : doDeposit(acct);
	'w'	  : doWithdrawal(acct);
	'b'	  : showBalance(acct);
	'q'	  : more := false;
	otherwise  begin
	               writeln;
		       writeln('Sorry, unrecognized request.');
		       writeln('please try again, or type ''q'' to quit.');
		    end;
      end; (* case *)

   end; (* while more *)

   writeln ('Thank You.');

end.
