Post 8 -Currency Converter Using Java

In this post, we will learn to build "Currency Converter"  java.

✅ Problem Statement: 

John deals with international customers. So he needs to perform Currency conversion to deal with his customers. To make this conversions easier, he need Currency Converter which will help him to perform conversions.


✅ Concepts Covered in this Post:

  • MAVEN -  Project Management Tool
  • Enum 
  • Switch Case

🔴Before we start, Let's have a quick look on What MAVEN is ?

⇒ Maven is a Project Management Tool, based on Project Object Model(POM). It is used for project's build, dependency and documentation.

For Detailed Information please visit,


📑 Let's Code :)

For Currency Converter, We will Create MAVEN Project instead of Creating Simple Java Project.

Step 1 : Create Maven Project in Eclipse.

Open Eclipse → File → New → Others → Maven → Maven Project .

Follow the steps as shown below :

Fig 1(a)

Fig 1(b)



New Pop-up window will appear for creating New Maven Project. Follow the steps as shown below -

Fig 1 (c)


In this window , we need to provide Group Id , Artifact Id , Name of  Project and short Description.

👉Group Id follows Java's Package Name rules. GroupId starts with a reversed domain name.

👉 Artifact Id specifies the id of a project.

Once you filled all the details , Click on NEXT. This will create a new Maven Project.
 

Fig 1 (d)


Every Maven Project has pom.xml file which contains the information of  project and configuration information for the maven to build the project such as dependencies, build directory, source directory , plugins , goals , etc. 


Step 2 : Create Required Packages and Java Classes.

Our Project Structure will look like this - 




🔹 This project will have TWO Packages in src/main/java folder - 

Right Click on the src/main/java and create these two packages.

1] com.thecodingbuddy.blog.java.demo  -  Application.java class

2] com.thecodingbuddy.blog.java.demo.model - CurrencyConverter.java class

3]  com.thecodingbuddy.blog.java.demo.service - CurrencyConverterService.java class


🔹 Right Click on com.thecodingbuddy.blog.java.demo  and create new java class Application.java

This Class will contain main() method.



Similarly , Create remaining two Packages and respective Classes inside those packages.


Step 3 : Let's Code for CurrencyConverter.java class.

As CurrencyConverter is a model class. It will only contain fields/ variables , getter and setters , constructor.


CurrencyConverter.java


Step 4 : Now let's complete CurrencyConverterService.java class.

As this is a service class, this will contain business logic to process the request/ data.

Logic - 
  1. This class will contain method to convert one currency to another
  2. Method will accept object of model class.
  3. Currently we are supporting 4 currencies - Dollar , Eur , Pound and Yen. 
  4. If user has entered currency other than this, we will display message as - "Currency Not Supported" else Amount will be converted to required currency.

CurrencyConverterService.java


Step 5 : Let's code for Application.java class

This class will contain main() method.

This will perform following functions -
  1. Accept input from user
  2. Create object of CurrencyConverter class using parameterized constructor.
  3. Call method of CurrencyConverterService to perform conversion.
  4. Display Output on console


Application.java



Step 6 : Let's Run the Project and Check Output :)

🔶 Sample Output when User enters Supported currencies -

Output-1.1


🔶 Sample Output when User enters currency other than supported currencies -

Output-1.2



Here we are done with our new project :)

Find source code of project👉 here 

Hope you like this post 😉

You can reach us by entering your details in Contact Us box in the side panel💬

All the Best 👍👍👍👍
Happy Learning 😇😇😇

Post a Comment

Comments :

Previous Post Next Post