CODING JOY

The stunning potpourri of coding and mundane life

CURRENCY CONVERTER GUI USING JAVA AWT



AMRUHA AHMED
29th May,2023.



This article will help you to create a Currency Converter GUI using Java AWT.

Java AWT (Abstract Window Toolkit) is an API that aids in creation of window-based applications in Java Language.
Based on the current currency of a given amount, the task at hand is to convert it into its equivalent in another currency.

blog1

COMPONENTS OF THE INTERFACE:


DELVING DEEPER:

3 important parameters are required for making the Currency Converter:

In this program, given currency is represented as curr1 and desired currency by curr2.

In this program, I have considered six different currencies:

These are stored in a String array named currency.

By a hunch, you may use the concept of if -else -if ladder for the conversions but there is a faster, more efficient way for converting the amount as you will observe.

If the if-else-if ladder is used then at least 35 conditions will be required for making the converter.

But, if we convert the amount in the given currency to an intermediate currency, then convert it to the desired currency, we can skip the hassle of writing a lengthy code.

Here, I am considering the intermediate currency to be Indian Rupees.

The conversion rates from the given currency to INR are stored in the given_to_rupee array.

I am also storing the conversion rates from INR to the desired currency in the rupee_to_final array.

Note: These conversion rates should be written in the same order as the currency array.

Whenever the user selects the current and desired currencies, their indexes in the currency array are fetched and stored in c1 and c2 respectively.

Now, using the conversion rate given at index c1 in the given_to_rupee array, “amount” is converted into INR . The result of this operation is stored in the current_to_rupee variable.

Using the conversion rate at index c2 in the rupee_to_final array, the “current_to_rupee” amount is converted to the desired currency. This final answer is stored in the rupee_to_desired variable.

This entire logic resides in the converter method.

PACKAGES TO BE IMPORTED:

FUNCTIONS REQUIRED:

Firstly, a class named currencyconverter needs to be created that implements the ActionListener interface. The ActionListener interface is notified whenever the button is clicked.The functions used are:

PROGRAM:

The following program demonstrates how a currency converter GUI can be made using Java AWT:

                
import java.awt.*;
import java.awt.event.*;
class currencyconverter implements ActionListener
{
    Frame f;
    Button b;
    Label title,l1,l2,l3,last;
    TextField tf;
    Choice c1,c2;
     double converter(String curr1,String curr2,double amount)//converts amount from one currrency to another
    {//curr1 is current/given currency, curr2 is the desired currency, "amount" is the amount to be converted
       if(curr1.equals(curr2))// in case the current and desired currencies are the same
       return amount;
        String currency[]={"","Indian Rupee","United States Dollar","Euro","Pound Sterling","Chinese Yuan","Japanese Yen"};//array to store names of currencies
        double given_to_rupee[]={0,1,82.60,88.52,101.88,11.69,0.59};//array to store conversion rates from given currency to INR
        double rupee_to_final[]={0,1,0.012,0.011,0.0098,0.086,1.70};//array to store conversion rates from INR to desired currency
        int c1=0;// stores index of given currency in currency array
        int c2=0;// stores index of desired currency in currency array
        for(int i=1;i<=6;i++)//loop to fetch indexes of given and desired currencies
        {
            if(curr1.equals(currency[i]))
            c1=i;
            if(curr2.equals(currency[i]))
            c2=i;
            
        }
        double current_to_rupee=amount*given_to_rupee[c1];//converting amount in given currency to INR
        double rupee_to_desired=current_to_rupee*rupee_to_final[c2];//converting current_to_rupee to desired currency
        
        return(rupee_to_desired);
        
    }
    currencyconverter()
    {//initializing all the components 
        f=new Frame("Currency Conversion");
        title=new Label("Currency Converter GUI using Java AWT");
        l1=new Label("Enter the amount:");
        l2=new Label("Select the current currency :");
        l3=new Label("Select the final currency :");
        last=new Label();
        b=new Button("Submit");
        tf=new TextField();
        c1=new Choice();
        c2=new Choice();
        //adding options in Choice 
        c1.add("Indian Rupee");
        c1.add("United States Dollar");
        c1.add("Euro");
        c1.add("Pound Sterling");
        c1.add("Chinese Yuan");
        c1.add("Japanese Yen");
        //adding options in Choice
        c2.add("Indian Rupee");
        c2.add("United States Dollar");
        c2.add("Euro");
        c2.add("Pound Sterling");
        c2.add("Chinese Yuan");
        c2.add("Japanese Yen");
        b.addActionListener(this);//adding action listener to button
        f.setVisible(true);//setting Visibility of Frame
        f.setLayout(null);//setting Layout of Frame
        // calling appropriate functions
        boundSetter();
        fontSetter();
        addComponents();
        
        
    }
    void addComponents()
    {//to add Components to the Frame
         f.add(l1);
        f.add(l2);
        f.add(l3);
        f.add(last);
        f.add(title);
        f.add(tf);
        f.add(b);
        f.add(c1);
        f.add(c2);
        f.add(b);
     
    }
    
    void fontSetter()
    {//setting font style and size of the various components
        title.setFont(new Font("Arial",Font.BOLD,35));
        l1.setFont(new Font("Arial",Font.PLAIN,22));
        l2.setFont(new Font("Arial",Font.PLAIN,22));
        l3.setFont(new Font("Arial",Font.PLAIN,22));
        tf.setFont(new Font("Arial",Font.BOLD,25));
        c1.setFont(new Font("Arial",Font.PLAIN,22));
        c2.setFont(new Font("Arial",Font.PLAIN,22));
        b.setFont(new Font("Arial",Font.BOLD,25));
        last.setFont(new Font("Arial",Font.PLAIN,22));
        f.setBackground(Color.YELLOW);
        b.setBackground(Color.PINK);
        
    }
    void boundSetter()
    {//setting bounds of the various components
       f.setSize(800,800);
        title.setBounds(50,50,700,50);
        l1.setBounds(50,150,300,50);
        tf.setBounds(410,150,300,30);
        l2.setBounds(50,250,300,50);
        l3.setBounds(400,250,350,50);
        c1.setBounds(50,350,250,200);
        c2.setBounds(450,350,250,200);
        last.setBounds(100,550,600,25);
        b.setBounds(350,450,100,50);
    }
    
    public void actionPerformed(ActionEvent e)
    {//executed when button is clicked
        String amttext=tf.getText();//fetching text from TextField
        double amount=Double.valueOf(amttext);//converting amttext to double
        String currency1=c1.getSelectedItem();//get the option selected by user from 1st choice box
        String currency2=c2.getSelectedItem();//get the option selected by user from 2nd choice box
        double final_amt=converter(currency1,currency2,amount);//passing the required parameters to converter function
        last.setText(amount+" of "+currency1+" in "+currency2+" is "+final_amt);//setting text of "last" label as the final amount
        
        
    }
    
    void main()
    {//creating an object of current class
        currencyconverter ob=new currencyconverter();
    }
}

                   
             
         
CODE COPIED

OUTPUT: