/*
Generated by the Cosmo Visual Builder, Version 0.1.
*/

import java.awt.*;
/* import java.jvb.*; */
import java.applet.*;

/**
* Put the class description here.
* @author  David R. Henke
* @version VERSIONDATA
**/
public class Amort extends Applet {

   private String NULLSTRING = "";
   private String BLANK = " ";
   private String BLANKBLANK = "  ";
   private String TAB = "     ";
   private String NEWLINE =  "\n";
   private int current_year = 00;

   /* these are the possible user inputs */
   private double amount; /* amount of the loan */
   private double rate;   /* annual interest rate */
   private int years;     /* length of loan in years */
   private int month;     /* start month of loan, 1 == Jan */
   private int year;      /* start year of loan, 96 == 1996 */

   private static String months[] =
      {
       "NOP",
       "Jan",
       "Feb",
       "Mar",
       "Apr",
       "May",
       "Jun",
       "Jul",
       "Aug",
       "Sep",
       "Oct",
       "Nov",
       "Dec"
      };

   private static String FormatMoney(double input)
      {
       /* a truly ridiculous routine for formatting money strings */
       String money;
       long dollar_val;
       int cent_val;
       dollar_val = (long) Math.floor(input);
       cent_val =  (int) ((input - dollar_val + .005) * 100);
       if (cent_val == 100)
          {
           dollar_val = dollar_val++;
           cent_val = 0;
          }
       if (cent_val >=10)
          {
           money = new String("$" + dollar_val + "." + cent_val);
          }
       else
          {
           money = new String("$" + dollar_val + ".0" + cent_val);
          }
       int length = money.length();
       int uniform_length = 11;
       if (length < uniform_length)
          {
           /* pad with blanks */
           int num_blanks = uniform_length - length;
           for (int i = 0; i <  num_blanks; i++)
              {
               money = money.concat(" ");
              }
           }

       return(money);
      };

     private void calculateMortgage()
        {
         double monthly;
         double num_payments;
         double interest = 0;
         double principal = 0;
         double cum_interest = 0;
         double ytd_interest;
         double balance;
         double local_rate;
         int count = 0;
         int roundoff;
         int last_month;
         int start_month;
         int current_month;
         int end_month;
         int end_year;
         int official_year;
         boolean last;
         String countString;

         balance = amount;
         local_rate = rate;
         official_year = year + 2000;

         textarea2.setText("$"+ balance +
                           " at %" + local_rate +
                           " annually for " + years + " years " +
                           " starting " + month + "/" + year + NEWLINE);
         num_payments = 12.0 * years;
         local_rate = local_rate / 1200.00;
         monthly = (balance*local_rate*Math.pow((1.0 + local_rate),num_payments)) /
				(Math.pow((1.0     + local_rate),num_payments) - 1.0);
         roundoff = (int) ((monthly + 0.005) * 100);
         monthly = roundoff/100.0;
         textarea2.appendText(" in " + num_payments +
                              " monthly payments of " +
                              FormatMoney(monthly) + NEWLINE);
         /* if we are not starting on 1st month of the year,
          * we need to compute beginning year and last year specially
          */
         if (month == 1)
            {
             end_year = years;
             last_month = 12;
            }
         else
            {
             end_year = years + 1;
             last_month = 12 - month - 1;
            }

         textarea1.setText(NEWLINE);
         for (current_year = 1; current_year <= end_year; current_year++)
            {
             ytd_interest = 0;
             if (current_year == 1)
                {
                 start_month = month;
                }
             else
                {
                 start_month = 1;
                 official_year++;
                }
             if (current_year == end_year)
                {
                 end_month = last_month;
                }
             else
                {
                 end_month = 12;
                }

             textarea1.appendText(NEWLINE);
             for (current_month = start_month;
                  current_month <= end_month; current_month++)
                {
                 count++;
                 interest = balance * local_rate;
                 roundoff = (int) ((interest + 0.005) * 100);
                 interest = roundoff/100.0;
                 /* on very last month, principal equals balance,
                  * which is principal + interest
                  */
                 if (num_payments == count)
                    {
                     principal = balance;
                     balance = 0;
                    }
                 else
                    {
                     principal = monthly - interest;
                     balance -= principal;
                    }
                 cum_interest += interest;
                 ytd_interest += interest;
                 /**************************************************/
                 /* another truly ridiculous snippet of code
                  * until I can figure out how to format output in Java
                  */
                 if (count < 10)
                    {
                     countString = new String(BLANKBLANK);
                    }
                 else if (count < 100)
                    {
                     countString = new String(BLANK);
                    }
                 else
                    {
                     countString = new String(NULLSTRING);
                    }
                 /**************************************************/

                 textarea1.appendText(countString +  count + TAB +
                    months[current_month] + "/"+ official_year + TAB +
                    FormatMoney(principal) + TAB +
                    FormatMoney(interest)  + TAB +
                    FormatMoney(balance)   + TAB +
                    FormatMoney(cum_interest) + TAB +
                    FormatMoney(ytd_interest) + NEWLINE);
                }
            }
         textarea1.appendText("Final payment: " +
                              FormatMoney(principal + interest) + NEWLINE);

        }

    /**
    * Initialize this object.
    **/
    public void init() {
        super.init();

	// BEGIN init_applet
        setLayout(null);
        resize(700, 600);
        setBackground(new Color(0xc1c1c1));
	// END init_applet

	// BEGIN init_objects

        // Initialize label1
        add(label1 = new java.awt.Label());
        label1.move(10, 7);
        label1.resize(70, 25);
        label1.setForeground(new Color(0x000000));
        label1.setBackground(new Color(0xc1c1c1));
        label1.setText("Amount");
        label1.show();

        // Initialize label2
        add(label2 = new java.awt.Label());
        label2.move(126, 7);
        label2.resize(70, 25);
        label2.setForeground(new Color(0x000000));
        label2.setBackground(new Color(0xc1c1c1));
        label2.setText("Rate");
        label2.show();

        // Initialize label3
        add(label3 = new java.awt.Label());
        label3.move(249, 7);
        label3.resize(70, 25);
        label3.setForeground(new Color(0x000000));
        label3.setBackground(new Color(0xc1c1c1));
        label3.setText("Years");
        label3.show();

        // Initialize label4
        add(label4 = new java.awt.Label());
        label4.move(373, 7);
        label4.resize(85, 24);
        label4.setForeground(new Color(0x000000));
        label4.setBackground(new Color(0xc1c1c1));
        label4.setText("Start-month");
        label4.show();

        // Initialize label5
        add(label5 = new java.awt.Label());
        label5.move(493, 7);
        label5.resize(70, 25);
        label5.setForeground(new Color(0x000000));
        label5.setBackground(new Color(0xc1c1c1));
        label5.setText("Start-year");
        label5.show();

        // Initialize textfield1
        add(textfield1 = new java.awt.TextField());
        textfield1.move(7, 38);
        textfield1.resize(65, 30);
        textfield1.setForeground(new Color(0x000000));
        textfield1.setBackground(new Color(0xb95c18));
        textfield1.show();

        // Initialize textfield2
        add(textfield2 = new java.awt.TextField());
        textfield2.move(129, 38);
        textfield2.resize(65, 30);
        textfield2.setForeground(new Color(0x000000));
        textfield2.setBackground(new Color(0xb95c18));
        textfield2.show();

        // Initialize textfield3
        add(textfield3 = new java.awt.TextField());
        textfield3.move(251, 38);
        textfield3.resize(65, 30);
        textfield3.setForeground(new Color(0x000000));
        textfield3.setBackground(new Color(0xb95c18));
        textfield3.show();

        // Initialize textfield4
        add(textfield4 = new java.awt.TextField());
        textfield4.move(373, 38);
        textfield4.resize(65, 30);
        textfield4.setForeground(new Color(0x000000));
        textfield4.setBackground(new Color(0xb98e8e));
        textfield4.show();

        // Initialize textfield5
        add(textfield5 = new java.awt.TextField());
        textfield5.move(495, 38);
        textfield5.resize(65, 30);
        textfield5.setForeground(new Color(0x000000));
        textfield5.setBackground(new Color(0xb98e8e));
        textfield5.show();

        // Initialize button1
        add(button1 = new java.awt.Button());
        button1.move(608, 10);
        button1.resize(56, 56);
        button1.setForeground(new Color(0x000000));
        button1.setBackground(new Color(0x999999));
        button1.setLabel("Run");
        button1.show();

        // Initialize textarea1
        add(textarea1 = new java.awt.TextArea());
        textarea1.move(5, 174);
        textarea1.resize(690, 420);
        textarea1.setForeground(new Color(0x000000));
        textarea1.setBackground(new Color(0x8e8e8e));
        textarea1.setFont(new Font("Courier", 0, 12));
        textarea1.setEditable(false);
        textarea1.show();

        // Initialize textarea2
        add(textarea2 = new java.awt.TextArea());
        textarea2.move(5, 77);
        textarea2.resize(691, 64);
        textarea2.setForeground(new Color(0x000000));
        textarea2.setBackground(new Color(0x8e8e8e));
        textarea2.setEditable(false);
        textarea2.show();

        // Initialize label8
        add(label8 = new java.awt.Label());
        label8.move(54, 148);
        label8.resize(74, 26);
        label8.setForeground(new Color(0x000000));
        label8.setBackground(new Color(0xc1c1c1));
        label8.setText("Month/Year");
        label8.show();

        // Initialize label9
        add(label9 = new java.awt.Label());
        label9.move(158, 148);
        label9.resize(69, 25);
        label9.setForeground(new Color(0x000000));
        label9.setBackground(new Color(0xc1c1c1));
        label9.setText("Principal");
        label9.show();

        // Initialize label10
        add(label10 = new java.awt.Label());
        label10.move(280, 148);
        label10.resize(69, 26);
        label10.setForeground(new Color(0x000000));
        label10.setBackground(new Color(0xc1c1c1));
        label10.setText("Interest");
        label10.show();

        // Initialize label11
        add(label11 = new java.awt.Label());
        label11.move(390, 148);
        label11.resize(69, 25);
        label11.setForeground(new Color(0x000000));
        label11.setBackground(new Color(0xc1c1c1));
        label11.setText("Balance");
        label11.show();

        // Initialize label13
        add(label13 = new java.awt.Label());
        label13.move(500, 148);
        label13.resize(86, 25);
        label13.setForeground(new Color(0x000000));
        label13.setBackground(new Color(0xc1c1c1));
        label13.setText("Cum Interest");
        label13.show();

        // Initialize label14
        add(label14 = new java.awt.Label());
        label14.move(610, 148);
        label14.resize(82, 27);
        label14.setForeground(new Color(0x000000));
        label14.setBackground(new Color(0xc1c1c1));
        label14.setText("YTD Interest");
        label14.show();

	// END init_objects

        /* My initialization */
        amount = 0;
        rate = 0;
        years = -1;
        month = 1;
        year = current_year;
    }


    /**
    * Handle an event on an object in this applet.
    * @param event The event which occurred.
    * @return true if the event is handled, false if not
    **/
    public boolean handleEvent(Event event) {
        Object arg = event.arg;
        boolean returnValue = false;

	// BEGIN connections

	// END connections

        try
           {
            if ((event.id == Event.ACTION_EVENT) ||
                (event.id == Event.LOST_FOCUS))
               {
                if (event.target == textfield1)
                   {
                    String string_amount = textfield1.getText();
                    Double double_amount = Double.valueOf(string_amount);
                    amount = double_amount.doubleValue();
                    /* bool_amount = true; */
                    return true;
                   }
                else if (event.target == textfield2)
                   {
                    String string_rate = textfield2.getText();
                    Double double_rate = Double.valueOf(string_rate);
                    rate = double_rate.doubleValue();
                    return true;
                   }
                else if (event.target == textfield3)
                   {
                    String string_year = textfield3.getText();
                    years = Integer.parseInt(string_year);
                    return true;
                   }
                else if (event.target == textfield4)
                   {
                    String string_start_month = textfield4.getText();
                    month = Integer.parseInt(string_start_month);
                    return true;
                   }
                else if (event.target == textfield5)
                   {
                    String string_start_year = textfield5.getText();
                    year = Integer.parseInt(string_start_year);
                    return true;
                   }
                else if ((event.target == button1) &&
                         (event.id == Event.ACTION_EVENT))
                   {
                    /* make sure required input data set */
                    if ((amount != -1) && (rate != 0) && (years != 0))
                       {
                        /* clear status area */
                        textarea2.setText("");
                        /* runit */
                        calculateMortgage();
                       }
                    else
                       {
                        textarea2.setText("ERROR: Invalid input, required fields not entered");
                       }
                   }
                }
              return returnValue;
             }
        catch (java.lang.NumberFormatException e)
           {
            /* issue error to status area */

            textarea2.setText("ERROR: Invalid input, not numeric");
            return returnValue;
           }
    }


    /**
    * The main method for running this class as a standalone application
    * @param args The argument list sent to the program.
    **/
    public static void main (String args[]) {
        Frame f = new Frame();
        Amort app = new Amort();
        app.init();
        app.start();
        f.add("Center", app);
        f.resize(500, 507);
        f.show();
    }

    // BEGIN declarations

    /**
    * label1.
    **/
    java.awt.Label label1;

    /**
    * label2.
    **/
    java.awt.Label label2;

    /**
    * label3.
    **/
    java.awt.Label label3;

    /**
    * label4.
    **/
    java.awt.Label label4;

    /**
    * label5.
    **/
    java.awt.Label label5;

    /**
    * textfield1.
    **/
    java.awt.TextField textfield1;

    /**
    * textfield2.
    **/
    java.awt.TextField textfield2;

    /**
    * textfield3.
    **/
    java.awt.TextField textfield3;

    /**
    * textfield4.
    **/
    java.awt.TextField textfield4;

    /**
    * textfield5.
    **/
    java.awt.TextField textfield5;

    /**
    * button1.
    **/
    java.awt.Button button1;

    /**
    * textarea1.
    **/
    java.awt.TextArea textarea1;

    /**
    * textarea2.
    **/
    java.awt.TextArea textarea2;

    /**
    * label8.
    **/
    java.awt.Label label8;

    /**
    * label9.
    **/
    java.awt.Label label9;

    /**
    * label10.
    **/
    java.awt.Label label10;

    /**
    * label11.
    **/
    java.awt.Label label11;

    /**
    * label13.
    **/
    java.awt.Label label13;

    /**
    * label14.
    **/
    java.awt.Label label14;

    // END declarations
}
