Pages

Subscribe:

Wednesday, February 2, 2011

Android - Simple Interest Calculation


Screen


Layout

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<EditText
android:id="@+id/pAmount"
android:layout_width="141px"
android:layout_height="35px"
android:textSize="18sp"
android:layout_x="150px"
android:layout_y="42px"
android:inputType="numberDecimal"
>
</EditText>
<EditText
android:id="@+id/pRateofInterest"
android:layout_width="142px"
android:layout_height="37px"
android:textSize="18sp"
android:layout_x="150px"
android:layout_y="112px"
android:inputType="numberDecimal"
>
</EditText>
<EditText
android:id="@+id/pPeriod"
android:layout_width="142px"
android:layout_height="38px"
android:textSize="18sp"
android:layout_x="150px"
android:layout_y="182px"
android:inputType="numberDecimal"
>
</EditText>
<Button
android:id="@+id/bCalculate"
android:layout_width="107px"
android:layout_height="43px"
android:text="Calculate"
android:layout_x="100px"
android:layout_y="252px"
>
</Button>
<TextView
android:id="@+id/widget31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Principal Amount:"
android:layout_x="27px"
android:layout_y="47px"
>
</TextView>
<TextView
android:id="@+id/widget38"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rate of Interest:"
android:layout_x="37px"
android:layout_y="119px"
>
</TextView>
<TextView
android:id="@+id/widget67"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Period:"
android:layout_x="94px"
android:layout_y="188px"
>
</TextView>

<EditText
android:id="@+id/pSimpleInterest"
android:layout_width="130px"
android:layout_height="37px"
android:text=""
android:textSize="18sp"
android:layout_x="160px"
android:layout_y="342px"
>
</EditText>
<TextView
android:id="@+id/widget93"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple Interest:"
android:layout_x="50px"
android:layout_y="352px"
>
</TextView>

</AbsoluteLayout>

Java Code

package com.simple;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class simpleinterest extends Activity {
/** Called when the activity is first created. */
Button calculate;
Button messageBox;
EditText principal;
EditText rate;
EditText period;
EditText simpleinterest;
TextView simpleIntrest;
TextView siText;
Context context;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this.getApplicationContext();

// Get all the UI components

siText = (TextView) findViewById(R.id.pSimpleInterest);
simpleIntrest = (TextView) findViewById(R.id.pSimpleInterest);

messageBox = (Button) findViewById(R.id.bCalculate);
messageBox.setOnClickListener((OnClickListener) new clicker()); 
}
class clicker implements Button.OnClickListener {

public void onClick(View v) {

principal = (EditText) findViewById(R.id.pAmount);
rate = (EditText) findViewById(R.id.pRateofInterest);
period = (EditText) findViewById(R.id.pPeriod);
simpleinterest = (EditText) findViewById(R.id.pSimpleInterest);

try {
if (principal.getText().toString().equals("")) {
Toast.makeText(context, "Principal Missing",
Toast.LENGTH_SHORT).show();
} else if (rate.getText().toString().equals("")) {
Toast.makeText(context, "Rate Of Intrest Missing",
Toast.LENGTH_SHORT).show();
} else if (period.getText().toString().equals("")) {
Toast.makeText(context, "Period Missing",
Toast.LENGTH_SHORT).show();
} else {

Double p = Double.parseDouble(principal.getText()
.toString());
Double r = Double
.parseDouble(rate.getText().toString());
Double t = Double.parseDouble(period.getText()
.toString());

Double si;
System.out.println("P = " + p + "\nR = " + r + "\nT = "
+ t);

si = (p * r * t) / 100;

// prepare the alert box
AlertDialog.Builder alertbox = new AlertDialog.Builder(
v.getContext());
System.out.println("Alert builder");
// set the message to display
alertbox.setMessage("Simple Interest = "
+ si.toString());
simpleinterest.setText(si.toString());
simpleinterest.setVisibility(TextView.VISIBLE);

// add a neutral button to the alert box and assign a
// click listener

alertbox.setNeutralButton("Ok",
new DialogInterface.OnClickListener() {
// click listener on the alert box
public void onClick(DialogInterface arg0,
int arg1) {
// the button was clicked
Toast.makeText(getApplicationContext(),
"Thank You !!!!",
Toast.LENGTH_LONG).show();
}
});

// show it
alertbox.show();

System.out.println("Alert show");

// simpleIntrest.setText(si.toString());

// simpleIntrest.setVisibility(TextView.VISIBLE);
// siText.setVisibility(TextView.VISIBLE);
}
} catch (Exception e) {
System.out.println("In catch block");
e.printStackTrace();
}
}
}

@Override
protected void onStart() {
super.onStart();

System.out.println("In OnStart");
}
}

1 comments:

sairam said...

How do I can round off the digits to two decimal places?

Post a Comment