program FINANCES;
var
  ans, sel: char;
  month, year, years: byte;
  interest, amount: real;

procedure InsertComma (quantity: real);
  var number: string[10];
begin
  Str (quantity:00:00, number);
  Insert(',', number, Length(number)-2);
  write (number);
end;

procedure Amort;
var
  principal, payments, a,b: real;
begin
  GotoXY(15,10); write ('      ');
  GotoXY(15,10); read (principal);
  GotoXY(11,11); write ('      ');
  GotoXY(11,11); read (interest); write ('%'); interest:= interest/1200;
  GotoXY(8,12); write ('      ');
  GotoXY(8,12); read (years);
  repeat
    a:= 1.0+interest; b:= years*12.0;
payments:= ((interest/((exp(ln(interest+1)*years*12))-1))+interest)*principal;
    GotoXY(19,14);
    if payments > 999 then InsertComma (payments)
    else write (payments:5:2);
    GotoXY(25,19); read (Kbd,ans);
    case UpCase(ans) of
      'A': begin
             GotoXY(15,10); write ('      ');
             GotoXY(15,10); read (principal);
           end;
      'I': begin
             GotoXY(11,11); write ('      ');
             GotoXY(11,11); read (interest);
             interest:= interest/1200; write ('%');
           end;
      'Y': begin
             GotoXY(8,12); write ('      ');
             GotoXY(8,12); read (years);
           end;
       end;
  until  UpCase(ans)= 'Q';
end;

procedure Savings;
var
  savings, gain: real;
begin
  GotoXY(42,10); write ('      ');
  GotoXY(42,10); read (amount);
  GotoXY(37,11); write ('      ');
  GotoXY(37,11); read (interest); write ('%'); interest:= (interest/12)*0.01;
  GotoXY(34,12); write ('      ');
  GotoXY(34,12); read (years);
  repeat
    savings:= 0; gain:= 0;
    GotoXY(37,14); write ('(Calculating)'); GotoXY(37,14);
    for year:= 1 to years do
    begin
      for month:= 1 to 12 do
      begin
        savings:= amount+savings+(interest*savings);
      end;
      gain:= savings-(amount*12*year);
      gain:= gain*0.70; (* taxes *)
      savings:= gain+(amount*12*year);
    end;
    write ('             '); GotoXY(37,14);
    if savings > 999 then InsertComma (savings)
    else write (savings:00:00);
    GotoXY(34,15); write ('      ');
    GotoXY(34,15);
    if gain > 999 then InsertComma (gain)
    else write (gain:00:00);
    GotoXY(25,19); read (Kbd,ans);
    case UpCase(ans) of
     'A': begin
            GotoXY(42,10); write ('      ');
            GotoXY(42,10);  read (amount);
          end;
     'I': begin
            GotoXY(37,11); write ('      ');
            GotoXY(37,11);  read (interest);
            interest:= interest/1200; write ('%');
          end;
     'Y': begin
            GotoXY(34,12); write ('      ');
            GotoXY(34,12);  read (years);
          end;
      end;
  until  UpCase(ans)= 'Q';
end;

procedure Inflation;
  var initAmount, infl: real;
begin
  GotoXY(62,10); ClrEol; read (initAmount);
  GotoXY(64,11); ClrEol; read (infl); write ('%'); infl:= infl*0.01;
  GotoXY(60,12); ClrEol; read (years);
  repeat
    amount:= initAmount;
    GotoXY(66,14); ClrEol;
    for year:= 1 to years do
    begin
      amount:= amount+(infl*amount);
    end;
    if amount > 999 then InsertComma (amount)
    else write (amount:00:00);
    GotoXY(25,19); read (Kbd,ans);
    case UpCase(ans) of
     'A': begin GotoXY(62,10); ClrEol; read (initAmount); end;
     'I': begin GotoXY(64,11); ClrEol; read (infl);
            infl:= infl*0.01; write ('%'); end;
     'Y': begin GotoXY(60,12); ClrEol; read (years); end;
      end;
  until  UpCase(ans)= 'Q';
end;

begin (*MAIN PART *)
  ClrScr;
  writeln ('  SELECT: ');
  writeln ('(A)mortization');
  writeln ('(S)avings');
  writeln ('(I)nflation');
  write ('(Q)uit program');
  GotoXY(1,8); writeln ('AMORTIZATION'); writeln;
  writeln ('Loan Amount: $');
  writeln ('Interest:   %');
  writeln ('Years:  '); writeln;
  write ('Monthly Payment: $');
  GotoXY(27,8); write ('SAVINGS');
  GotoXY(27,10); write ('Amount/Month: $');
  GotoXY(27,11); write ('Interest:   %');
  GotoXY(27,12); write ('Years: ');
  GotoXY(27,14); write ('Savings: $');
  GotoXY(27,15); write ('Gain: $');
  GotoXY(27,16); write ('(after 30% annual taxes)');
  GotoXY(53,8); write ('INFLATION');
  GotoXY(53,10); write ('Amount: $');
  GotoXY(53,11); write ('Inflation:   %');
  GotoXY(53,12); write ('Years: ');
  GotoXY(53,14); write ('Equivalent: $');
  GotoXY (1,19); writeln ('  CHANGES TO SELECTION:');
  writeln ('(A)mount');
  writeln ('(I)nterest (or inflation)');
  writeln ('(Y)ears');
  write ('(Q)uit selection');
  repeat
    GotoXY(11,1); read (Kbd, sel); sel:= UpCase(sel);
    case sel of
      'A': Amort;
      'S': Savings;
      'I': Inflation;
      end;
  until sel= 'Q';
  ClrScr;
end.