Jump to content

Currency converter!!!!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
PascalNouma

PascalNouma

    Newbie

  • Members
  • Pip
  • 1 posts
Hey everyone,

I've an assignment that is coding a "currency converter". I managed to generate the program screen, but I don't know where and how to code in order to make calculations for currency conversion. If you've any solution or idea about it, please leave your comments on that topic page..

Thanks a lot for everything from now on.

Have a nice Sunday!!!!

package assignment;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import java.text.DecimalFormat;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class MoneyConverter extends JFrame {

private JFrame mainFrame;
private JLabel inLabel;
private JTextField inField;
private JLabel tinLabel;
private JTextField tinField;
private JLabel zinLabel;
private JTextField zinField;
private JButton closeButton;
private JButton usdtoaudButton;
private JButton audtousdButton;

public MoneyConverter() {


mainFrame = new JFrame("Money Converter");
inLabel = new JLabel(" AUD ");
inField = new JTextField(20);
tinLabel = new JLabel(" Rate ");
tinField = new JTextField(20);
zinLabel = new JLabel(" USD ");
zinField = new JTextField(20);
usdtoaudButton = new JButton("USD to AUD");
audtousdButton = new JButton("AUD to USD");
closeButton = new JButton("Close");

Container c = mainFrame.getContentPane();
c.setLayout(new FlowLayout());


c.add(inLabel);
c.add(inField);
c.add(tinLabel);
c.add(tinField);
c.add(zinLabel);
c.add(zinField);
c.add(usdtoaudButton);
c.add(audtousdButton);
c.add(closeButton);

mainFrame.setSize(400,180);

mainFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {System.exit(0);
}
});

USDtoAUDButtonHandler uhandler = new USDtoAUDButtonHandler();
usdtoaudButton.addActionListener(uhandler);
CloseButtonHandler chandler = new CloseButtonHandler();
closeButton.addActionListener(chandler);
AUDtoUSDButtonHandler ahandler = new AUDtoUSDButtonHandler();
audtousdButton.addActionListener(ahandler);

mainFrame.show();
}

class USDtoAUDButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}

class AUDtoUSDButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}

class CloseButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void main(String args [])
{
MoneyConverter app = new MoneyConverter();

}


}

Edited by John, 29 March 2009 - 04:43 PM.
Please use [code] tags when posting code.


#2
Stu_328

Stu_328

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
I doubt you wrote that yourself, but can't work out where to write button action code.

#3
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
Just went in and made it worse for you :P

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class MoneyConverter extends JFrame implements ActionListener{


private JLabel inLabel = new JLabel(" AUD ");

private JTextField inField = new JTextField(20);

private JLabel tinLabel = new JLabel(" Rate ");

private JTextField tinField = new JTextField(20);

private JLabel zinLabel = new JLabel(" USD ");

private JTextField zinField = new JTextField(20);

private JButton closeButton = new JButton("Close");

private JButton usdtoaudButton = new JButton("USD to AUD");

private JButton audtousdButton = new JButton("AUD to USD");


public MoneyConverter() {

setLayout(new FlowLayout());

setTitle("Money Converter");

add(inLabel);

add(inField);

add(tinLabel);

add(tinField);

add(zinLabel);

add(zinField);

add(usdtoaudButton);

add(audtousdButton);

add(closeButton);

setSize(400,180);

usdtoaudButton.addActionListener(this);

audtousdButton.addActionListener(this);

closeButton.addActionListener(this);

show();

}

public void actionPerformed(ActionEvent e) {

	if(e.getSource()==usdtoaudButton) {

		//read in the textfield and parse to an integer...

	}

	else if(e.getSource()==audtousdButton) {

		//read in the textfield and parse to an integer...

	}

	else if(e.getSource()==closeButton) {

		System.exit(0);

	}

}

public static void main(String args []) {

	MoneyConverter MC = new MoneyConverter();

	MC.show();

	}

}
I suggest you don't use flowlayout and set the layout to null, and use setbounds to allow you place your "objects" where ever you want.
Plus I recommend while reading in data do an exception on alphabetic words in your field.
Good luck !
Posted Image