/*
	Project 2:	Inputting Data
	Programmer:	Gary Haines
	Date: 		January 17, 2003
	Program Name:	BertApplet
*/

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class BertApplet extends Applet implements ActionListener
{

	Label custNameLabel = new Label("Please enter your name:");
	  TextField custNameField = new TextField(25);
	
	Label priceLabel = new Label("Enter the price of the car:");
	  TextField priceField = new TextField(25);
	
	Label downPaymentLabel = new Label("Enter the down payment:");
	  TextField downPaymentField = new TextField(25);

	Label tradeInLabel = new Label("Enter the trade in value:");
	  TextField tradeInField = new TextField(25);

	Label monthsLabel = new Label("Enter the number of months:");
	  TextField monthsField = new TextField(25);

	Label annualInterestLabel = new Label("Enter the yearly interest rate in decimal form:");
	  TextField annualInterestField = new TextField(25);

	Button calcButton = new Button("Calculate Monthly Payment");

	Label outputLabel = new Label("Enter the requested data and click the button");

	public void init()
	{
	  add(custNameLabel);
	  add(custNameField);
	  add(priceLabel);
	  add(priceField);
	  add(downPaymentLabel);
	  add(downPaymentField);
	  add(tradeInLabel);
	  add(tradeInField);
	  add(annualInterestLabel);
	  add(annualInterestField);
	  add(monthsLabel);
	  add(monthsField);
	  add(calcButton);
	  add(outputLabel);
	  calcButton.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e)
	{
	  //Converting input to values
	  int price = Integer.parseInt(priceField.getText());
	  int downPayment = Integer.parseInt(downPaymentField.getText());
	  int tradeIn = Integer.parseInt(tradeInField.getText());
	  double annualInterest = Double.parseDouble(annualInterestField.getText());
	  int months = Integer.parseInt(monthsField.getText());

	  //Variables used in formulas and output
	  double interest;
	  int loanAmt;
	  double payment;

	  //Calculations
	  interest = annualInterest/12;
	  loanAmt = price-downPayment-tradeIn;
	  payment = loanAmt/((1/interest)-(1/(interest*Math.pow(1+interest,months))));

	  //Output
	  outputLabel.setText("The monthly payment is $" + Math.round(payment));
	}
}