- Version
- Download 165
- File Size 4.12 KB
- Create Date May 10, 2016
- Download
Currency Converter Program in Java
This is a sample program in java that converts currency to another currency. It is a peso to dollar, dollar to peso converter program. The user will be asked what operation he/she wants (peso to dollar or dollar to peso). The final result will be the converted amount of the currency.
All of these programs will be discussed in a step by step manner.
Source code:
import javax.swing.*;
public class CurrencyConverterGUI
{
public static void main(String args[]) {
String input1, input2;
double dollarValue=0, pesoToConvert=0, dollarToConvert=0;
double result=0;
String cont="n";
String option;
do
{
option=JOptionPane.showInputDialog(null, "Please select an operation:" + "
" + "1 for peso to dollar" + "
" + "2 for us dollar to peso" );
if (option.matches("1")){
input1 = JOptionPane.showInputDialog(null,"Enter amount of peso to convert: ");
pesoToConvert=Double.parseDouble(input1);
input2 = JOptionPane.showInputDialog(null,"Enter peso to dollar value: ");
dollarValue=Double.parseDouble(input2);
result = pesoToConvert / dollarValue ;
JOptionPane.showMessageDialog(null, "your " + pesoToConvert + " pesos is equal to " + result + " dollars");
} else if (option.matches("2")){
input1 = JOptionPane.showInputDialog(null,"Enter amount of peso to convert: ");
dollarToConvert=Double.parseDouble(input1);
input2 = JOptionPane.showInputDialog(null,"Enter peso to dollar value: ");
dollarValue=Double.parseDouble(input2);
result = dollarToConvert * dollarValue ;
JOptionPane.showMessageDialog(null, "your " + dollarToConvert + " dollars is equal to " + result + " pesos");
}
cont = JOptionPane.showInputDialog(null,"Try Again? (press y or n) ");
}while (cont.matches("y"));
JOptionPane.showMessageDialog(null,"Program closing" );
}
}