/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   This package calculates the Future value or Present value for a given dollar amount, at a set interest rate,
   over a given number of years.
   The package is meant for use in a script but can be run independently in sql*plus.
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
Create or Replace Package Value_Of_Money as

	   Procedure Future_Value(amount in varchar2, rate in number, time in number);
	   Procedure Present_Value(amount in varchar2, rate in number, time in number);
	   
END Value_Of_Money;
/

Create or Replace Package Body Value_Of_Money
as

  Procedure Future_Value(amount in varchar2, rate in number, time in number)
  as
       FV			number(15,2) default null;
	 FVIF		      number default null;
	 l_rate		number default null;
	 
  BEGIN
    
	dbms_output.put_line('Amount: '||amount);
      dbms_output.put_line('Interest Rate: '||rate);
	dbms_output.put_line('Time(yrs): '||time);
	--dbms_output.put_line('Compounding '||compound);
	
	if rate < 1 then
	 l_rate := rate;
	else l_rate := rate * .01;
	end if;
	
	if time > 1 then
	    FVIF := 1 + l_rate;
		for i in 2..time loop
		FVIF := FVIF * (1 + l_rate);
		end loop;
	else
	   FVIF := 1 + l_rate;
	end if;
	
	FV := amount * FVIF;
	
	dbms_output.put_line('Future Value: '||FV);
  
         exception when others then
			begin
                dbms_output.put_line( 'ERROR MSG: ' || sqlerrm );
			end;

	     

	
	
END Future_Value;

  Procedure Present_Value(amount in varchar2, rate in number, time in number)
	as
	  PV			number(15,2) default null;
	  FVIF			number(7,5) default null;
	  PVIF			number(7,5) default null;
	  l_rate		number default null;
	  
	BEGIN
	  
	  dbms_output.put_line('Amount: '||amount);
	  dbms_output.put_line('Interest Rate: '||rate);
        dbms_output.put_line('Time(yrs): '||time);
	  --dbms_output.put_line('Compounding '||compound);
		  
	  if rate < 1 then
	   l_rate := rate;
	  else l_rate := rate * .01;
	  end if;
	  
	  if time > 1 then
	    FVIF := 1 + l_rate;
		for i in 2..time loop
		FVIF := FVIF * (1 + l_rate);
		end loop;
	  else
	   FVIF := 1 + l_rate;
	  end if;
	  
	  PV := (amount / FVIF);
	  dbms_output.put_line('Present Value: '||PV);
	  
	  exception when others then
			begin
                dbms_output.put_line( 'ERROR MSG: ' || sqlerrm );
			end;
	
  END Present_Value;
  
End Value_Of_Money;	
/