w3resource

Currency Converter in Java: Two Approaches with If-Else and Switch-Case

Java Project - Currency Converter:

Currency Converter:

Convert between different currencies based on user input.

This project converts amounts between different currencies based on user input. Users provide the amount and the desired currencies for conversion, and the program calculates the result based on current exchange rates.

Input: Amount, currency to convert from, and currency to convert to (e.g., USD to EUR).

Output: The converted amount.

Example:

  • Input: 100 USD to EUR
  • Output: 92.50 EUR (depends on current exchange rate)

Here are two different solutions for the Currency Converter project in Java:

Solution 1: Using If-Else Statements

Code:

import java.util.Scanner;

public class CurrencyConverterIfElse {
    public static void main(String[] args) {
        // Create a Scanner object to capture user input
        Scanner scanner = new Scanner(System.in);

        // Input the amount to convert
        System.out.print("Enter the amount: ");
        double amount = scanner.nextDouble();

        // Input the source currency (currency to convert from)
        System.out.print("Enter the currency to convert from (USD, EUR, INR): ");
        String fromCurrency = scanner.next().toUpperCase();

        // Input the target currency (currency to convert to)
        System.out.print("Enter the currency to convert to (USD, EUR, INR): ");
        String toCurrency = scanner.next().toUpperCase();

        // Variable to store the conversion rate
        double conversionRate = 0;

        // Perform currency conversion using if-else statements
        if (fromCurrency.equals("USD") && toCurrency.equals("EUR")) {
            conversionRate = 0.92; // Example conversion rate from USD to EUR
        } else if (fromCurrency.equals("USD") && toCurrency.equals("INR")) {
            conversionRate = 82.75; // Example conversion rate from USD to INR
        } else if (fromCurrency.equals("EUR") && toCurrency.equals("USD")) {
            conversionRate = 1.08; // Example conversion rate from EUR to USD
        } else if (fromCurrency.equals("EUR") && toCurrency.equals("INR")) {
            conversionRate = 89.65; // Example conversion rate from EUR to INR
        } else if (fromCurrency.equals("INR") && toCurrency.equals("USD")) {
            conversionRate = 0.012; // Example conversion rate from INR to USD
        } else if (fromCurrency.equals("INR") && toCurrency.equals("EUR")) {
            conversionRate = 0.011; // Example conversion rate from INR to EUR
        } else {
            // Handle invalid currency combinations
            System.out.println("Invalid currency combination.");
            return;
        }

        // Calculate the converted amount
        double convertedAmount = amount * conversionRate;

        // Output the converted amount
        System.out.println("Converted amount: " + convertedAmount + " " + toCurrency);
    }
}   

Output:

Enter the amount:  1000
Enter the currency to convert from (USD, EUR, INR):  USD
Enter the currency to convert to (USD, EUR, INR):  EUR
Converted amount: 920.0 EUR

Explanation :

  • Input: User enters an amount, a source currency, and a target currency.
  • If-Else Structure: The conversion rate is chosen using a series of if-else statements based on the currency pair.
  • Conversion: The amount is multiplied by the conversion rate to calculate the final converted amount.
  • Invalid Input Handling: If an invalid currency combination is provided, an error message is displayed.
  • Result: The result is printed as the converted amount in the target currency.

Solution 2: Using Switch-Case Statements

Code:

import java.util.Scanner;

public class CurrencyConverterSwitch {
    public static void main(String[] args) {
        // Create a Scanner object to capture user input
        Scanner scanner = new Scanner(System.in);

        // Input the amount to convert
        System.out.print("Enter the amount: ");
        double amount = scanner.nextDouble();

        // Input the source currency
        System.out.print("Enter the currency to convert from (USD, EUR, INR): ");
        String fromCurrency = scanner.next().toUpperCase();

        // Input the target currency
        System.out.print("Enter the currency to convert to (USD, EUR, INR): ");
        String toCurrency = scanner.next().toUpperCase();

        // Variable to store the conversion rate
        double conversionRate = 0;

        // Switch-case for currency conversion based on source and target currencies
        switch (fromCurrency) {
            case "USD":
                switch (toCurrency) {
                    case "EUR":
                        conversionRate = 0.92; // USD to EUR conversion rate
                        break;
                    case "INR":
                        conversionRate = 82.75; // USD to INR conversion rate
                        break;
                    default:
                        System.out.println("Invalid target currency.");
                        return;
                }
                break;

            case "EUR":
                switch (toCurrency) {
                    case "USD":
                        conversionRate = 1.08; // EUR to USD conversion rate
                        break;
                    case "INR":
                        conversionRate = 89.65; // EUR to INR conversion rate
                        break;
                    default:
                        System.out.println("Invalid target currency.");
                        return;
                }
                break;

            case "INR":
                switch (toCurrency) {
                    case "USD":
                        conversionRate = 0.012; // INR to USD conversion rate
                        break;
                    case "EUR":
                        conversionRate = 0.011; // INR to EUR conversion rate
                        break;
                    default:
                        System.out.println("Invalid target currency.");
                        return;
                }
                break;

            default:
                System.out.println("Invalid source currency.");
                return;
        }

        // Calculate the converted amount
        double convertedAmount = amount * conversionRate;

        // Output the converted amount
        System.out.println("Converted amount: " + convertedAmount + " " + toCurrency);
    }
}  

Output:

Enter the amount:  1000
Enter the currency to convert from (USD, EUR, INR):  EUR
Enter the currency to convert to (USD, EUR, INR):  USD
Converted amount: 1080.0 USD

Explanation:

  • Input: User inputs an amount, a source currency, and a target currency.
  • Switch-Case Structure: The switch-case block is used to select the conversion rate based on currency pairs.
  • Conversion: The input amount is multiplied by the conversion rate to calculate the result.
  • Error Handling: If an invalid currency is entered, an error message is displayed.
  • Result: The converted amount is printed based on the selected currencies.


Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/projects/java/java-project-currency-converter-between-usd-eur-and-inr.php