Building a Currency Converter in Java - Java Projects
In this article, we are going to create a Java project that converts currency. This currency calculator can convert between the dollar,euro, rupee, andyen. We `are going to use a switch and function to create a currency converter.
What are we building?
We are building a currency converter that allows the user to convert one currency to another, such as the dollar, yen, and rupee.
Pre-requisites
- Switch statements in java We are going to use switch to get user choices
- function in java: We are going to use the function to convert one current to another currency.
- arithmetic operation in java: To convert one currency to another, we need to perform some arithmetic operation on the currency.
How Are We Going to Build Currency-Converter?
- First, we are going to take the user currency input in which currency he wants to enter the amount
- Then will take the amount of input from the user
- Then, using the switch case and function calculate the amount in the destinated currency
Final Output
Requirements
- Scanner Class: Scanner class in the java.util package is used to get input of primitive types like int, double, etc., and strings. The simplest method to read input in a Java program is this one.
Building a Currency Converter in java
Create a class name CurrencyConverter
Create a class called CurrencyConverter in the Java file. Import the Scanner class and add the main method in the file.
Create a Switch case
We will ask the user for currency and amount and create a switch case.
Code Explanation
In the above code, we have taken input from the user about the currency he wants to convert and the amount of currency. Then, based on his currency, we are going to convert it to other currencies. For example, if the user has Ruppe, we are going to convert it to Dollars and Euros.
Add methods to convert to other currencies
We only need to add a method in CurrencyConverter class, so our code works perfectly.
Output:
Code Explanation
In the above code, using simple multiplication, we created three functions that convert the rupee to other currencies, the dollar to other currencies, and the euro to other currencies.
What's next?
We have created a Currency Converter in Java, but that doesn't look too well. Users can't interact better with this. By using a graphical user interface users can interact better. He can easily use our application. This allows our application to be more user-friendly. Our Application will be more attractive than before. To make it better, try to create a GUI interface using Swing in java The Java Swing Framework is a component of the Java Foundation Classes (JFC), a collection of classes that help programmers build Java-based applications.
Conclusion
- In this article, we have created a currency converter
- This converter can convert between Ruppe, euro and US Dollar.
- We created this using Simple switches and methods in Java.