Currency Converter in Java

 

Introduction

In today’s global economy, converting between different currencies is a fundamental task for businesses and individuals. Whether you are traveling, making international purchases, or engaging in foreign investments, currency conversion tools are essential.

This Java program is designed to help you easily convert amounts between different currencies. The program allows users to input an amount and choose the currencies they wish to convert from and to, then outputs the converted amount based on current exchange rates. In this version, exchange rates are hardcoded for simplicity, but this can be expanded to fetch live rates using APIs.

Objective

The objective of this program is to provide a simple tool for currency conversion between a set of predefined currencies. The program takes the amount, the source currency, and the target currency from the user and calculates the equivalent amount in the target currency.

Java Code for Currency Converter


        import java.util.Scanner;

        public class CurrencyConverter {

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

                // Display the available currencies
                System.out.println("Welcome to the Currency Converter!");
                System.out.println("Available currencies: ");
                System.out.println("1. USD - US Dollar");
                System.out.println("2. EUR - Euro");
                System.out.println("3. GBP - British Pound");
                System.out.println("4. INR - Indian Rupee");

                // Prompt user for the source currency
                System.out.print("\nEnter the number corresponding to the source currency: ");
                int sourceCurrencyChoice = scanner.nextInt();

                // Prompt user for the target currency
                System.out.print("Enter the number corresponding to the target currency: ");
                int targetCurrencyChoice = scanner.nextInt();

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

                // Perform conversion
                double convertedAmount = convertCurrency(sourceCurrencyChoice, targetCurrencyChoice, amount);

                // Display the result
                if (convertedAmount != -1) {
                    System.out.println("\nConverted Amount: " + convertedAmount);
                } else {
                    System.out.println("Invalid currency selection.");
                }

                // Close the scanner object to avoid memory leaks
                scanner.close();
            }

            // Method to handle currency conversion
            public static double convertCurrency(int source, int target, double amount) {
                // Define exchange rates relative to USD
                double[] rates = {1.0, 0.85, 0.75, 74.5}; // USD, EUR, GBP, INR

                // Convert the source amount to USD
                double amountInUSD = amount / rates[source - 1];

                // Convert USD to the target currency
                switch (target) {
                    case 1: return amountInUSD * rates[0]; // USD to USD
                    case 2: return amountInUSD * rates[1]; // USD to EUR
                    case 3: return amountInUSD * rates[2]; // USD to GBP
                    case 4: return amountInUSD * rates[3]; // USD to INR
                    default: return -1; // Invalid target currency
                }
            }
        }
        

Explanation of the Program Structure

The program is divided into two main parts:

  • Input Handling: The program uses the Scanner class to take inputs from the user, including the source currency, target currency, and the amount to be converted.
  • Currency Conversion: The conversion logic is handled by the convertCurrency() method. The method first converts the amount into USD using the exchange rate of the selected source currency, and then it converts the amount in USD to the target currency.

How to Run the Program

To run this Java program, follow these steps:

  1. Install Java Development Kit (JDK): If you haven’t already, download and install the JDK from the official Oracle website or use an OpenJDK distribution.
  2. Save the Code: Copy the Java code and save it in a file named CurrencyConverter.java on your local machine.
  3. Compile the Program: Open a terminal or command prompt, navigate to the directory where your file is saved, and run the following command to compile the program:
    javac CurrencyConverter.java
  4. Run the Program: After compiling, execute the program using the following command:
    java CurrencyConverter
  5. Interact with the Program: The program will ask for user input. Enter the desired currencies, amount, and follow the prompts to get the converted amount.

Conclusion

This Java-based currency converter provides an easy-to-use method for converting between several currencies. The program can be extended to handle more currencies or even fetch live exchange rates using APIs. It serves as a basic but effective example of how to handle user input, perform calculations, and display results in a console-based Java application.

 

Leave a Reply

Your email address will not be published. Required fields are marked *