Creating a Currency Converter Using Java with Code

In this article, we will walk through the process of creating a simple currency converter application using Java. This application will allow users to convert amounts between different currencies, such as USD, EUR, and INR. We will use the console for input and output, making it easy to run in any Java environment.

Currency Converter

The currency converter will:

  1. Take user input for the amount and the currency to convert from.
  2. Provide options for different currencies to convert to.
  3. Perform the conversion based on predefined exchange rates.
  4. Display the converted amount.

Prerequisites

  • Basic knowledge of Java programming.
  • A Java development environment (like IntelliJ IDEA, Eclipse, or any text editor with JDK installed).

Steps to Implement Currency Converter

Step 1. Create the CurrencyConverter Class

First, create a class named CurrencyConverter. This class will contain methods for converting currencies and handling user input.

Step 2. Define Exchange Rates

We will define some static exchange rates for simplicity. In a real-world application, you would typically fetch these rates from an API.

Step 3. Implement User Input and Conversion Logic

Here is the complete code for our currency converter:

import java.util.Scanner;

public class CurrencyConverter {

    // Exchange rates (for simplicity)
    private static final double USD_TO_INR = 85.04;
    private static final double EUR_TO_INR = 88.43;
    private static final double INR_TO_USD = 0.012;
    private static final double INR_TO_EUR = 0.011;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // User input for amount
        System.out.print("Enter amount: ");
        double amount = scanner.nextDouble();
        
        // User input for currency type
        System.out.println("Select currency to convert from:");
        System.out.println("1. USD");
        System.out.println("2. EUR");
        System.out.println("3. INR");
        System.out.print("Choose an option (1-3): ");
        int fromCurrency = scanner.nextInt();
        
        // User input for target currency type
        System.out.println("Select currency to convert to:");
        System.out.println("1. USD");
        System.out.println("2. EUR");
        System.out.println("3. INR");
        System.out.print("Choose an option (1-3): ");
        int toCurrency = scanner.nextInt();

        // Perform conversion
        double convertedAmount = convertCurrency(amount, fromCurrency, toCurrency);
        
        // Display result
        System.out.printf("Converted Amount: %.2f\n", convertedAmount);
        
        scanner.close();
    }

    private static double convertCurrency(double amount, int fromCurrency, int toCurrency) {
        // Convert from selected currency to INR first
        double amountInINR = amount;
        
        switch (fromCurrency) {
            case 1: // USD to INR
                amountInINR = amount * USD_TO_INR;
                break;
            case 2: // EUR to INR
                amountInINR = amount * EUR_TO_INR;
                break;
            case 3: // INR to INR (no conversion)
                break;
            default:
                System.out.println("Invalid currency selection.");
                return 0;
        }

        // Now convert from INR to target currency
        switch (toCurrency) {
            case 1: // INR to USD
                return amountInINR * INR_TO_USD;
            case 2: // INR to EUR
                return amountInINR * INR_TO_EUR;
            case 3: // INR to INR (no conversion)
                return amountInINR;
            default:
                System.out.println("Invalid currency selection.");
                return 0;
        }
    }
}

Explanation of the Code

  • Importing Scanner: We import java.util.Scanner for reading user inputs.
  • Exchange Rates: We define static variables for exchange rates between USD, EUR, and INR.
  • User Input: The program prompts the user for an amount and which currencies they want to convert from and to.
  • Conversion Logic:
    The convertCurrency method first converts the input amount into Indian Rupees (INR) based on the selected "from" currency.
    It then converts that amount from INR into the desired "to" currency.
  • Displaying Results: Finally, it prints out the converted amount formatted to two decimal places.

Output

Currency converter in Java

Output 2

Currency converter in Java

Conclusion

In this article, we created a simple currency converter in Java that allows users to convert amounts between different currencies based on predefined exchange rates. This project demonstrates basic concepts such as user input handling, arithmetic operations, and control flow in Java.

Note. Feel free to expand this project by integrating real-time exchange rate APIs or enhancing the user interface! Happy coding!


Similar Articles