
{**********************************************************************} 
{* filename:                  LOANCALC.PAS                            *}
{*--------------------------------------------------------------------*}
{* this program calculates the monthly installment payments on a loan *} 
{* for automobiles , houses, and  etc.   ..<note>..the formula for    *} 
{* determining this came from the library book of financial tables for*} 
{* monthly loans..and was verified by checking specific loans with    *} 
{* comerica bank...the formula is actually the ANNUITY formula........*} 
{**********************************************************************}

PROGRAM loancalc(input,output);

{$I auxsubs.pas}
{$I gemsubs.pas}


VAR
	grand_total, total_interest : real;
	months, years : integer;
	total_payment, interest_percent : real;
	interest_rate, loan_amount, temp_power, monthly_escrow : real;
	escrow, period_interest_rate, payment : real;
	h, b : Boolean;
	ch : char;
	x,y : integer;


FUNCTION power(interest, per: real) : real;
    	{we pass 2 real parameters to this function, and get the }
		{value (power) in return}
	
    begin
	power := exp(per*ln(1 + interest));
		{we use natural logs here because pascal doesnt have}
		{power functions, and we need to raise a value to an}
		{exponantial power                                  }
    end;


PROCEDURE initialize ;
	
    begin
	escrow := 0;
	total_payment := 0;
	monthly_escrow := 0;
	total_interest := 0;
	grand_total := 0;
	months := 0;
	years := 0;
	interest_percent := 0;
	interest_rate := 0;
	loan_amount := 0;
	temp_power := 0;
	period_interest_rate := 0;
	payment := 0;
	b := true;
	h := false;
    end;

PROCEDURE pause;
		{this gives me a little time delay that i can easily}
		{change without getting into the internal timers    }
   VAR
	    delay : real;
	
	BEGIN {pause}
  		delay := 1;
  		while delay < 25 do
  		delay := delay + 1/delay;
	END; {pause}


PROCEDURE intro;
   VAR
		delay : real;

   BEGIN{intro}
		delay := 1;
		clear_screen;
		frame_Rect (150,20,320,100); {integers are pixel values}
		paint_style (1);   { 1=solid color }
		paint_color (2);   { 0=white,1=black,2=red,3=green }
		paint_Rect(160,25,300,90);
		move_to (0,36);
		draw_string (200,54,'        LOAN CALC         ');
		draw_string (200,62,'   program developed by   ');
		draw_string (200,70,'   Bunkys Board ST-BBS    ');
		draw_string (200,78,'     <313>-546-3689       ');
		draw_string (200,86,'    ver.1.3-12/24/87      ');
		While delay <90 do
			BEGIN
	 			delay := delay + 1/delay;
			END;
   END;{intro}


PROCEDURE select_loan_type;
    VAR
		x,y : integer;

    BEGIN 
		ClrScr;
        GotoXY(10,10);
		writeln ;
		writeln('     This program is intended to assist in determining the');
		writeln('     monthly installemnt payments due on either an Automobile ');
		writeln('     or House Loan...');
		writeln ;
		writeln ;
		writeln('     hit <RETURN> for Automobile Loan ............');
		writeln ;
		writeln('     hit <H> and <RETURN> for House Loan..........');
		read(ch);
		if ord(ch) > 96 then
			BEGIN
				ch := chr(ord(ch) - 32);
	    	END;
		if ch = 'H' then 
			BEGIN
				h := true;
			END;
	END; {select_loan_type}	


PROCEDURE get_data ;
	
	BEGIN {get_data}
		ClrScr ;
		writeln ;
		write('     enter percent interest rate ==> % ');
		readln(interest_percent);
		writeln ;
		interest_rate := interest_percent/100;
		write('     enter the amount of the loan ==> $ ');
		read(loan_amount);
		writeln ;
		write('     enter the number of years of the loan ==> ');
		readln(years);
		if h = true then 
		BEGIN
			writeln ;
			writeln('     enter the amount set aside for [escrow]...the total of ');
			writeln('     property tax and loan insurance...if you dont know and want');
			writeln('     me to estimate hit <1> & <Return>..or if you pay these items');
			writeln('     separately at the end of the year, enter <0>');
			writeln ;
			write('     Escrow amount ==> $ ');
			readln(escrow);
			if escrow = 1 then
			BEGIN
				escrow := 2000;
		    END;
			writeln ;
	    END;
		ClrScr ;
		writeln ;
		writeln ;
		writeln('      LOAN amount............................. $ ', loan_amount:6:2);
		writeln ;
		writeln('      INTEREST rate .......................... % ', interest_percent:2:2);
		writeln ;
		writeln('      YEARS of loan............................. ', years);
		months := years*12;
		period_interest_rate := interest_rate/12;
		temp_power := power(period_interest_rate, months);
		payment := loan_amount*((period_interest_rate*temp_power)/(temp_power - 1));
		grand_total := payment*months;
		total_interest := grand_total - loan_amount;
		monthly_escrow := escrow/12;
		total_payment := payment + monthly_escrow;
		writeln ;
		writeln('      Monthly Loan Payment ................... $ ', payment:6:2);
		if h = true then 
		BEGIN
			writeln ;
			writeln('      Monthly Escrow ......................... $ ', monthly_escrow:6:2);
			writeln ;
			writeln('      Total MonthlHouse Payment    ........... $ ', total_payment:6:2);
	    END;
		writeln ;
		writeln('      Total Interest Paid on Loan............. $ ', total_interest:6:2);
		writeln ;
		writeln('      Grand Total Paid with Interest.......... $ ', grand_total:6:2);
		writeln ;
		writeln ;
		writeln ;
		writeln('     ...hit return to repeat,...hit <Q> to quit ');
		read(ch);
		b := true;
		if ch = 'q' then 
			BEGIN
				b := false;
	    	END;
		if ch = 'Q' then 
			BEGIN
                b := false;
	    	END;
	
    END;

BEGIN {main}
	if Init_Gem >= 0 then
		BEGIN
			Hide_Mouse;
			ClrScr ;
			initialize ;
			intro ;
			pause;
			ClrScr;
			select_loan_type;
			while b = true do
				BEGIN
					get_data ;
					if b = false then
						Show_Mouse;
                END;       
            Show_Mouse;      
			Exit_Gem;
		END;
END.
