2

I'm writing a currency converter but I'm having a bit of trouble caculating the exchange rate for each currency. basically I want the user to select a currecy first then enter an amount and press "go" button to calculate the rate. but i'm having trouble with the listeners on JMenuItem and JButton. I've declared two listeners for menuItem and JButton. how do i use the listener on the button to look out for the selection made on the menuIten so that it makes the right currecy calculation?

thanks.

CODE:

    private class selectionListener implements ActionListener
    {
        double EuroToSterling(double euro)
        {
            double total = Double.parseDouble(amountField.getText());
            return total;
        }
        public void actionPerformed(ActionEvent e)
        {
            if (e.getActionCommand().equals("Euros"))
               // result = EuroToSterling(10*euro);
                currencyMenu.setLabel("Euros");
               // answerLabel.setText("this" + EuroToSterling(1.22*2));

            if (e.getActionCommand().equals("Japanese Yen"))
                currencyMenu.setLabel("Japanese Yen");

        }
    }



    private class GoButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent evt)
        {

//please help with this section
Maxi90
  • 33
  • 1
  • 6
  • Why do you have one currency to select? You are converting to the selected currency or from the selected currency? – khachik Dec 08 '10 at 16:44
  • I think you should use `JComboBox'es instead of menus. However, if select the menu label to the selected currency, you can just get the menu label in your button listener. But I still don't understand how you can convert using one currency :) – khachik Dec 08 '10 at 16:48
  • I'm converting any selected currency to sterling. So if the user selected USD it would calculate amount entered into sterling. – Maxi90 Dec 08 '10 at 16:59

4 Answers4

3

The usual approach is that the menu listener changes the state of the application (i.e. calls a method that will save the exchange rate in a field).

Then the calculation code can read this value and use it.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
1

Try this out with the Euros. Should give you a place to get started.


/*
 *
 * Currency converting
 *
 */

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;

import javax.swing.UIManager;

public class CurrencyConverterWin extends JFrame {

    private JLabel promptLabel;
    private JTextField amountField;
    private JButton goButton;
    private JPanel inputPanel;
    private JPanel answerPanel;
    private JLabel answerLabel;
    private JLabel selectLabel;
    private JComboBox currencyMenuBar;
    private JPanel menuPanel;
    private double result = 0.0;
    private double euro = 1.22257;
    private double japYen = 152.073;
    private double rusRuble = 42.5389;
    private double usd = 1.5577;

    public CurrencyConverterWin() {
        super();
        this.setSize(500, 200);
        this.setTitle("Currency Converter Window");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setLayout(new GridLayout(3, 1));

        this.selectLabel = new JLabel("Select a currency to convert to: ", JLabel.RIGHT);
        this.answerLabel = new JLabel(" ", JLabel.CENTER);

        currencyMenuBar = new JComboBox(new String[]{"Euros","Japanese Yen","Russian Rubles","US Dollars"});

        this.menuPanel = new JPanel();
        this.menuPanel.add(this.selectLabel);
        this.menuPanel.add(this.currencyMenuBar);
        this.add(this.menuPanel);

        this.promptLabel = new JLabel("(select a currency first) ", JLabel.RIGHT);
        this.answerLabel = new JLabel(" ", JLabel.CENTER);

        this.amountField = new JTextField("", 8);
        this.goButton = new JButton("GO");
        goButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClicked(evt);
            }
        });

        this.inputPanel = new JPanel();
        this.inputPanel.add(this.promptLabel);
        this.inputPanel.add(this.amountField);
        this.inputPanel.add(this.goButton);

        this.add(this.inputPanel);

        this.answerPanel = new JPanel();
        this.answerPanel.add(this.answerLabel);
        this.add(this.answerPanel);
    }

    double EuroToSterling() {
        double total = Double.parseDouble(amountField.getText()) * euro;
        return total;
    }

    double JapYenToSterling()
    {
        double japToSterlingTotal = Double.parseDouble(amountField.getText()) * japYen;
        return japToSterlingTotal;
    }


//String currencyEntered = yearField.getText();
    public void buttonClicked(ActionEvent evt) {
        if(currencyMenuBar.getSelectedItem().equals("Euros"))
        {
            answerLabel.setText(EuroToSterling() + " Euros");
        }
        if(currencyMenuBar.getSelectedItem().equals("Japanese Yen"))
        {
            answerLabel.setText(JapYenToSterling() + " Japanese Yen");
        }
    }

    public static void main(String[] args) {        
        try{UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");}
        catch (Exception e){e.printStackTrace();}
        CurrencyConverterWin win = new CurrencyConverterWin();
        win.setVisible(true);
    }
}

user489041
  • 27,916
  • 55
  • 135
  • 204
  • Hi thanks for your post, but I'm having a little problem with other currencies it seems to only work for "euro" please have a look at the change i've made: else if(currencyMenu.getText().equals("Japanes Yen")) { answerLabel.setText(JapYenToSterling(Double.valueOf(amountField.getText())) + "Japanese Yen"); } } – Maxi90 Dec 08 '10 at 18:51
  • double JapYenToSterling(double japYen){ double japToSterlingTotal = Double.parseDouble(amountField.getText()) * 152.07; return japToSterlingTotal; } – Maxi90 Dec 08 '10 at 18:53
  • Ive updated the code above to reflect the JapYenToSterling(). The only parts of the code that you should touch are the buttonClicked function and add your conversion functions. Other than that, you program is fine. Just look into what is being done with the Euro and the Yen. All other conversion are done in exactly the same way. – user489041 Dec 08 '10 at 19:10
  • Also, as others have suggested, you should be using a JComboBox. I have edited the code above again. Look how much simpler your code can be if you use the correct Swing component for the job. – user489041 Dec 08 '10 at 19:21
0

I would personally add in an Enumeration to denote the currency conversion type. eg:

public enum ConversionType {
   DOLLARS,
   EUROS,
   RUBLES
   //ETC...
}

Using this, you can keep a state variable in the class:

private ConversionType fromType;

This is what you set in your selection listener.

From there it's a matter of doing the different conversions in your action listener based on the state variable (fromType). Something like this:

if( fromType== EUROS ) {
 convertEurosToSterling( value1, value2 );
} 

This is sort of a general approach - I hope this helps.

javamonkey79
  • 17,443
  • 36
  • 114
  • 172
  • Hi, thanksI forgot to mention that I'm converting any selected currency to sterling. how would i do that with your solution? – Maxi90 Dec 08 '10 at 17:03
0

I would also suggest you use a JComboBox to store the currencies. You would create an object to store both the currency name and the conversion rate. Then when you need to calculate the converted amount you get the selected item from the combo and use its conversion rate in your calculation. With this approach you can easily expand the number of currencies you support.

Check out: How to use Map element as text of a JComboBox for an example to get you start on using an object in the combo box.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288