Friday, September 12, 2008

Google Web Toolkit - Monthly Payment Calculator

This example displays a simple Loan Calculator. When the user clicks calculate a ClickListener is fired which calculates the monthly payment and dispays it.

This example illustrates GWT Labels, TextBox, and Button controls.


/*
* mcintoshEntryPoint.java
*
* Created on June 29, 2008, 1:46 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.client;

import com.google.gwt.core.client.EntryPoint;
import com.client.time.*;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
*
* @author avalanche
*/
public class mcintoshEntryPoint implements EntryPoint {

final Label lblLoanAmount = new Label("Loan Amount:");
final TextBox txtLoanAmount = new TextBox();
final Label lblLoanLength = new Label("Loan Length (Month):");
final TextBox txtLoanLength = new TextBox();
final Label lblInterestRate = new Label("Interest Rate (Yearly):");
final TextBox txtInterestRate = new TextBox();
final Button calculate = new Button("Calculate");
final Label lblResult = new Label("Payment per month:");

public mcintoshEntryPoint() {
calculate.addClickListener(new ClickListener() {

public void onClick(Widget arg0) {

//P = A (1 + r ) ^ N /( 1 + r ) ^ N -1
int loanAmount = Integer.parseInt(txtLoanAmount.getText());
int loanLength = Integer.parseInt(txtLoanLength.getText());
double interestRate = Double.parseDouble(txtInterestRate.getText())/1200;
double temp = Math.pow(( 1 + interestRate ) , loanLength);
Double result = new Double((loanAmount * interestRate * temp) / (temp -1));
lblResult.setText(result.toString());
}
});


}

/**
* The entry point method, called automatically by loading a module
* that declares an implementing class as an entry-point
*/
public void onModuleLoad() {
RootPanel.get().add(lblLoanAmount);
RootPanel.get().add(txtLoanAmount);
RootPanel.get().add(lblLoanLength);
RootPanel.get().add(txtLoanLength);
RootPanel.get().add(lblInterestRate);
RootPanel.get().add(txtInterestRate);
RootPanel.get().add(calculate);
RootPanel.get().add(lblResult);

}
}

No comments: