How to implement currency conversion in java?

               If you traveling to another country,then you need to know the currency conversion. Let us start with US Dollar to many country currencies.

1 USD = 84.05 rupees

1 USD = 0.92 Euro

1 USD = 0.77 GBP

1 USD = 1.32 SGD

Where, Rupees -indian currency, Euro -European Currency, GBP- Pound Sterling, SGD- Singapore Dollar

How to create currency conversion program in java??

  • Include the built in  java package “java.util.Scanner”.
  • Create a class “CurrencyConversion”. Declare four double variables for currency conversion.
  • Write the main function. create an object for Scanner and read the system input from user.
  • Pass the input to convertit function. Based on the requirement, it convert the US dollar to another currency.

Program:

 import java.util.Scanner;

 public class CurrencyConversion {

    // Conversion rates may vary

    static final double D_TO_R = 84.05; //Indian  Rupees

    static final double D_TO_E = 0.92; // Euro

    static final double D_TO_P = 0.77; //Pound sterling

    static final double D_TO_SD = 1.32; //Singapore Dollar

     public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Currency Converter java Program");

        System.out.print("Enter amount in US Dollars: ");

        double usdollar = scanner.nextDouble();

        double inrupee = convertit(usdollar, D_TO_R);

        double euro = convertit(usdollar, D_TO_E);

        double gbpster = convertit(usdollar, D_TO_P);

        double Sgdollar = convertit(usdollar,D_TO_SD);

        System.out.println("The Equivalent currency of given $ in INR: " + inrupee);

        System.out.println("The Equivalent currency of given $ in EUR: " + euro);

        System.out.println("The Equivalent currency of given $ in GBP: " + gbpster);

        System.out.println("The Equivalent currency of given $ in SGD: " + Sgdollar);

        scanner.close();

    }

     public static double convertit(double amount, double current_rate) {

        return amount * current_rate;

    }

}            

Here, is the output.

C:\raji\blog>javac CurrencyConversion.java

C:\raji\blog>java CurrencyConversion

Currency Converter java Program

Enter amount in US Dollars: 45

The Equivalent currency of given $ in INR: 3782.25

The Equivalent currency of given $ in EUR: 41.4

The Equivalent currency of given $ in GBP: 34.65

The Equivalent currency of given $ in SGD: 59.400000000000006

This is the basic way of converting the currencies around the world.

No comments:

Post a Comment