{ Assignment 7 }
{ Written by Bryan Bridges }

program assign07;

{ Declare Constants }
const
   Title1 = '             Your Friendly Tax Collection Agency';
   Title2 = '                    Make It and We Take It';
   Title3 = '   Income       Base Tax       Incremental Tax       Total Tax';
   Title4 = '    ($)           ($)               ($)                 ($)';
   Divider = '-----------------------------------------------------------------';

{ Declare Variables }
var
   income, base_tax, incremental_tax, total_tax : real;
   Input, Output : text;
   income_array : array [1..24] of real;
   bt_array : array [1..24] of real;
   it_array : array [1..24] of real;
   I : integer;

procedure low_end;
{ Non-Table Tax }
  begin
       base_tax := 0.00;
       incremental_tax := income * 0.14;
       total_tax := incremental_tax;
  end;

procedure high_end;
{ Non-Table Tax }
  begin
       base_tax := 53090;
       incremental_tax := (income - 100000) * 0.70;
       total_tax := incremental_tax + base_tax;
  end;

procedure table_tax;
{ Table Tax }
   begin
        for I := 1 to 24 do
        begin
             if income = income_array[I] then
                begin
                     base_tax := bt_array[I];
                     incremental_tax := 0.00;
                     total_tax := base_tax;
                end
             else
                 begin
                       if (income > income_array[I]) and (income < income_array[I+1]) then
                          begin
                               base_tax := bt_array[I];
                               incremental_tax := (income - income_array[I]) * (it_array[I]/100);
                               total_tax := base_tax + incremental_tax;
                          end;
                 end;
        end;
   end;

procedure tax_output;
{ Write Tax Output }
  begin
       writeln (Output, income:9:2, base_tax:13:2, incremental_tax:18:2, total_tax:20:2);
  end;

{ Start Main Program }
begin

{ Assign Input & Output Files }
  assign (Input, 'D:\FILES\BAB''S\PASCAL\ASSIGN07.INP');
  reset (Input);
  assign (Output, 'D:\FILES\BAB''S\PASCAL\ASSIGN07.OUT');
  rewrite (Output);

{ Write Output Header Part 1 }
  writeln (Output, Title1);
  writeln (Output, Title2);
  writeln (Output, Divider);
  writeln (Output, Title3);
  writeln (Output, Title4);
  writeln (Output, Divider);

{ Read Tables from Input File }
  for I := 1 to 24 do
      begin
           readln (Input, income_array[I], bt_array[I], it_array[I]);
      end;

{ 1st Keyboard Input }
  write ('Enter Income Amount (0.00 When Done):  ');
  readln (income);

while income <> 0.00 do
begin
    { Tax Calculations }
    if income < 500.00 then
        low_end { Non-Table Tax }
    else
        if income > 100000.00 then
           high_end { Non-Table Tax }
    else
        table_tax;

    { Write To Output File }
    tax_output;

    { Subsequent Keyboard Input }
    write ('Enter Income Amount:  ');
    readln (income);
end;

{ Write Output Header Part 2 }
  writeln (Output, Divider);

{ End Main Program }
end.
