CurrencyForm.java


Back
/**
 * CurrencyForm.Java
 * @author j2meSalsa.com
 * Form where currency calculations are done.
 * This code also demonstrates the Float manipulations for MIDP1.0
 */

package com.j2me.salsa.form;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Form;

public class CurrencyForm extends Form implements CommandListener
{
	private UIDemo instance;
	private Command exitCmd;
	private Command calcCmd;
	private ChoiceGroup fromCC;
	private TextField fromTxt;
	private TextField toTxt;
	private ChoiceGroup toCC;
	// List Item Arrays
	private String[] ccFromStrings = {"PESO", "USD", "GBP", "YEN"};
	private String[] ccToStrings = {"PESO", "USD", "GBP", "YEN"};

	/**
	 * CurrencyForm Constructor
	 */
	public CurrencyForm(String title, UIDemo gInstance, Display display)
	{
		super(title);

		instance = gInstance;
		fromCC = new ChoiceGroup("From Currency", Choice.EXCLUSIVE, ccFromStrings, null);
		toCC = new ChoiceGroup("To Currency", Choice.EXCLUSIVE, ccToStrings, null);
		fromTxt = new TextField("Amount", "", 30, TextField.NUMERIC);
		toTxt = new TextField("Amount", "", 30, TextField.ANY);
		toCC = new ChoiceGroup("To Currency", Choice.EXCLUSIVE, ccToStrings, null);
		exitCmd = new Command("Exit", Command.EXIT, 0);
		calcCmd = new Command("Calc", Command.SCREEN, 0);

		append(fromCC);
		append(fromTxt);
		append(toCC);
		append(toTxt);

		addCommand(calcCmd);
		addCommand(exitCmd);

		setCommandListener(this);
	}
	/**
	 * Event Handling method
	 * Find the selected list Item and call respective convertion method
	 */
	public void commandAction(Command c, Displayable s)
	{
		if (c == exitCmd)
		{
			instance.destroyApp(false);
			instance.notifyDestroyed();
		}
		else if (c == calcCmd)
		{
			int fromIndex = fromCC.getSelectedIndex();
			int toIndex = toCC.getSelectedIndex();

			String calcAmt="0";

			if (fromIndex == toIndex)
			{
				toTxt.setString(fromTxt.getString());
			}
			else
			{
				switch (toIndex)
				{
					case 0:
					{
						calcAmt = calculatePESO(fromIndex, toIndex);
						break;
					}
					case 1:
					{
						calcAmt = calculateUSD(fromIndex, toIndex);
						break;
					}
					case 2:
					{
						calcAmt = calculateGBP(fromIndex, toIndex);
						break;
					}
					case 3:
					{
						calcAmt = calculateYEN(fromIndex, toIndex);
						break;
					}

				}
				toTxt.setString(calcAmt);
			}
		}
	}

	/**
	 * Calculate Peso
	 * Floating point manipulations are done here
	 * These calculations are indicative
	 */
	private String calculatePESO(int fromIndex, int toIndex)
	{
		int calcAmt=0;
		String result = fromTxt.getString();
		switch (fromIndex)
		{
			case 1:
			{
				calcAmt = Integer.parseInt(fromTxt.getString())*45;
				result = calcAmt + "";
				break;
			}
			case 2:
			{
				calcAmt = Integer.parseInt(fromTxt.getString())*80;
				result = calcAmt + "";
				break;
			}
			case 3:
			{
				calcAmt = Integer.parseInt(fromTxt.getString())/100;
				int tmpDiff = Integer.parseInt(fromTxt.getString()) - calcAmt*100;
				result = calcAmt + "." +tmpDiff;
				break;

			}
		}
		return result;
	}

	/**
	 * Calculate USD
	 * Floating point manipulations are done here
	 * These calculations are indicative
	 */
	private String calculateUSD(int fromIndex, int toIndex)
	{
		int calcAmt = 0;
		String result = fromTxt.getString();
		switch (fromIndex)
		{
			case 0:
			{
				calcAmt = Integer.parseInt(fromTxt.getString())/45;
				int tmpDiff = Integer.parseInt(fromTxt.getString()) - calcAmt*45;
				if(tmpDiff > 0 && tmpDiff <= 15)
					result = calcAmt + ".15";
				else if(tmpDiff > 15 && tmpDiff <= 30)
					result = calcAmt + ".30";
				else if(tmpDiff > 30 && tmpDiff <= 45)
					result = (calcAmt + 1) + "";
				break;

			}
			case 2:
			{
				calcAmt = Integer.parseInt(fromTxt.getString())+(Integer.parseInt(fromTxt.getString())/2);
				result = calcAmt + "";
				break;
			}
			case 3:
			{
				calcAmt = Integer.parseInt(fromTxt.getString())/4500;
				int tmpDiff = Integer.parseInt(fromTxt.getString()) - calcAmt*4500;
				if(tmpDiff > 0 && tmpDiff <= 1500)
					result = calcAmt + ".15";
				else if(tmpDiff > 1500 && tmpDiff <= 3000)
					result = calcAmt + ".30";
				else if(tmpDiff > 3000 && tmpDiff <= 4500)
					result = (calcAmt + 1) + "";
				break;
			}
		}
		return result;
	}

	/**
	 * Calculate GBP
	 * Floating point manipulations are done here
	 * These calculations are indicative
	 */
	private String calculateGBP(int fromIndex, int toIndex)
	{
		int calcAmt=0;
		String result = fromTxt.getString();
		switch (fromIndex)
		{
			case 0:
			{
				calcAmt = Integer.parseInt(fromTxt.getString())/80;
				int tmpDiff = Integer.parseInt(fromTxt.getString()) - calcAmt*80;
				if(tmpDiff > 0 && tmpDiff <= 20)
					result = calcAmt + ".25";
				else if(tmpDiff > 20 && tmpDiff <= 40)
					result = calcAmt + ".50";
				else if(tmpDiff > 40 && tmpDiff <= 60)
					result = (calcAmt + ".75") + "";
				else if(tmpDiff > 60 && tmpDiff <= 80)
					result = (calcAmt + 1) + "";
				break;

			}
			case 1:
			{
				calcAmt = Integer.parseInt(fromTxt.getString())/(15/10);
				result = calcAmt + "";
				break;
			}
			case 3:
			{
				calcAmt = Integer.parseInt(fromTxt.getString())/8000;
				int tmpDiff = Integer.parseInt(fromTxt.getString()) - calcAmt*8000;
				if(tmpDiff > 0 && tmpDiff <= 2000)
					result = calcAmt + ".25";
				else if(tmpDiff > 2000 && tmpDiff <= 4000)
					result = calcAmt + ".50";
				else if(tmpDiff > 4000 && tmpDiff <= 6000)
					result = (calcAmt + ".75") + "";
				else if(tmpDiff > 6000 && tmpDiff <= 8000)
					result = (calcAmt + 1) + "";
				break;
			}
		}
		return result;
	}

	/**
	 * Calculate YEN
	 * Floating point manipulations are done here
	 * These calculations are indicative
	 */
	private String calculateYEN(int fromIndex, int toIndex)
	{
		int calcAmt=0;
		String result = fromTxt.getString();
		switch (fromIndex)
		{
			case 0:
			{
				calcAmt = Integer.parseInt(fromTxt.getString())*100;
				result = calcAmt + "";
				break;

			}
			case 1:
			{
				calcAmt = Integer.parseInt(fromTxt.getString())*4500;
				result = calcAmt + "";
				break;
			}
			case 2:
			{
				calcAmt = Integer.parseInt(fromTxt.getString())*8000;
				result = calcAmt + "";
				break;
			}
		}
		return result;
	}

}

Back

This page is a part of a frames based web site. If you have landed on this page from a search engine click here to view the complete page.