Jump to content

need help on class project

- - - - -

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

#1
javic

javic

    Newbie

  • Members
  • Pip
  • 5 posts
Hello there, I've been working on a assignment given to me two weeks ago.
as of now I'm still unable to arrange the code in its consecutive order. this assignment basically was to setup a currency converter from american dollar to YEN, EURO and PESO. Everything is already setup except the formula code that will do the convertion process( If...else statement, or so ) In order to avoid confusion or for better visibility I've pasted the whole code which I'm working on. I would really appreciate your help.


public class CurrencyConverter extends JFrame
{
   // JLabel and JTextField to input dollar amount
   private JLabel dollarJLabel;
   private JTextField dollarJTextField;
   
   // JLabel and JTextField to input currency type to convert to
   private JLabel typeJLabel;
   private JTextField typeJTextField;
   
   // JLabel and JTextField to display converted value
   private JLabel convertedJLabel;
   private JTextField convertedJTextField;
   
   // JButton to initiate conversion
   private JButton convertJButton;
   
   // no-argument constructor
   public CurrencyConverter()
   {
      createUserInterface();
   }

   // create and position GUI components; register event handlers
   private void createUserInterface()
   {
      // get content pane for attaching GUI components
      Container contentPane = getContentPane();

      // enable explicit positioning of GUI components
      contentPane.setLayout( null ); 
      
      // set up dollarJLabel
      dollarJLabel = new JLabel();
      dollarJLabel.setBounds( 16, 16, 128, 21 );
      dollarJLabel.setText( "Dollars to convert:" );
      contentPane.add( dollarJLabel );
            
      // set up dollarJTextField
      dollarJTextField = new JTextField();
      dollarJTextField.setBounds( 175, 16, 96, 21 );
      dollarJTextField.setHorizontalAlignment( JTextField.RIGHT );
      contentPane.add( dollarJTextField );
      
      // set up typeJLabel
      typeJLabel = new JLabel();
      typeJLabel.setBounds( 16, 56, 150, 21 );
      typeJLabel.setText( "Convert from dollars to:" );
      contentPane.add( typeJLabel );
      
      // set up typeJTextField
      typeJTextField = new JTextField();
      typeJTextField.setBounds( 175, 56, 96, 21 );
      typeJTextField.setHorizontalAlignment( JTextField.RIGHT );
      contentPane.add( typeJTextField );
      
      // set up convertedJLabel
      convertedJLabel = new JLabel();
      convertedJLabel.setBounds( 16, 96, 112, 24 );
      convertedJLabel.setText( "Converted amount:" );
      contentPane.add( convertedJLabel );

      // set up convertedJTextField
      convertedJTextField = new JTextField();
      convertedJTextField.setBounds( 175, 96, 96, 21 );
      convertedJTextField.setHorizontalAlignment( 
         JTextField.RIGHT );
      convertedJTextField.setEditable( false );
      contentPane.add( convertedJTextField );
      
      // set up convertJButton and register its event handler
      convertJButton = new JButton();
      convertJButton.setText( "Convert" );
      convertJButton.setBounds( 175, 136, 96, 24 );
      contentPane.add( convertJButton );
      convertJButton.addActionListener(
         
         new ActionListener() // anonymous inner class
         {
             // event handler called when convertJButton is pressed
             public void actionPerformed ( ActionEvent event )
             {
                convertJButtonActionPerformed( event );
             }

         } // end anonymous inner class

      ); // end call to addActionListener
            
      // set properties of application's window
      setTitle( "Currency Converter" ); // set title bar text
      setSize( 300, 200 );              // set window size
      setVisible( true );               // display window
      
   } // end method createUserInterface
   
   // this method is called when the user clicks convertJButton
   private void convertJButtonActionPerformed( ActionEvent event )
   {
	//FORMULA NEEDEDHERE

  
   } 
   
   // 
   public static void main( String[] args )
   {
      CurrencyConverter application = new CurrencyConverter();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

   } 

} 

thank you

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
This is not a tutorial so I'm moving it to the Java Help forum, add the [ CODE ] [ /CODE ] tags to format your code better and ill have a look at it tomorrow.

And what exactly are you asking? For the conversion formula or how to implement it?

#3
javic

javic

    Newbie

  • Members
  • Pip
  • 5 posts
Sorry about that Sidewinder.
And for your question, actually I need both. The implementation as well as the conversion. Please get back at me, thank you.

#4
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
I got rid of your anonymous inner class (because I don't like them) and implemented ActionListener (in the class header). I also got rid of (just commented it out) your typeJTextField and replaced it with typeJComboBox (to avoid any input confusion). You may want to implement a try/catch to make sure the value inputed by the user can be converted to a Double

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

@SuppressWarnings("serial")
public class CurrencyConverter extends JFrame implements ActionListener {
   // JLabel and JTextField to input dollar amount
   private JLabel dollarJLabel;
   private JTextField dollarJTextField;
   
   // JLabel and JTextField to input currency type to convert to
   private JLabel typeJLabel;
   private JComboBox typeJComboBox;
   private String[] currencyCodes = new String[] {"EUR", "CAN"};
   //private JTextField typeJTextField;
   
   // JLabel and JTextField to display converted value
   private JLabel convertedJLabel;
   private JTextField convertedJTextField;
   
   // JButton to initiate conversion
   private JButton convertJButton;
   
   // Double value of dollarJTextField
   double value = 0;
   
   // no-argument constructor
   public CurrencyConverter() {
      createUserInterface();
   }

   // create and position GUI components; register event handlers
   public void createUserInterface() {
      // get content pane for attaching GUI components
      Container contentPane = getContentPane();

      // enable explicit positioning of GUI components
      contentPane.setLayout( null ); 
      
      // set up dollarJLabel
      dollarJLabel = new JLabel();
      dollarJLabel.setBounds( 16, 16, 128, 21 );
      dollarJLabel.setText( "Dollars to convert:" );
      contentPane.add( dollarJLabel );
            
      // set up dollarJTextField
      dollarJTextField = new JTextField();
      dollarJTextField.setBounds( 175, 16, 96, 21 );
      dollarJTextField.setHorizontalAlignment( JTextField.RIGHT );
      contentPane.add( dollarJTextField );
      
      // set up typeJLabel
      typeJLabel = new JLabel();
      typeJLabel.setBounds( 16, 56, 150, 21 );
      typeJLabel.setText( "Convert from dollars to:" );
      contentPane.add( typeJLabel );
      
      // set up typeJComboBox
      typeJComboBox = new JComboBox(currencyCodes);
      typeJComboBox.setBounds( 175, 56, 96, 21 );
      contentPane.add( typeJComboBox );
      
      // set up typeJTextField
      //typeJTextField = new JTextField();
      //typeJTextField.setBounds( 175, 56, 96, 21 );
      //typeJTextField.setHorizontalAlignment( JTextField.RIGHT );
      //contentPane.add( typeJTextField );
      
      // set up convertedJLabel
      convertedJLabel = new JLabel();
      convertedJLabel.setBounds( 16, 96, 112, 24 );
      convertedJLabel.setText( "Converted amount:" );
      contentPane.add( convertedJLabel );

      // set up convertedJTextField
      convertedJTextField = new JTextField();
      convertedJTextField.setBounds( 175, 96, 96, 21 );
      convertedJTextField.setHorizontalAlignment( 
         JTextField.RIGHT );
      convertedJTextField.setEditable( false );
      contentPane.add( convertedJTextField );
      
      // set up convertJButton and register its event handler
      convertJButton = new JButton();
      convertJButton.setText( "Convert" );
      convertJButton.setBounds( 175, 136, 96, 24 );
      contentPane.add( convertJButton );
      convertJButton.addActionListener(this);
            
      // set properties of application's window
      setTitle( "Currency Converter" ); // set title bar text
      setSize( 300, 200 );              // set window size
      setVisible( true );               // display window
      
   } // end method createUserInterface
   
   // this method is called when the user clicks convertJButton
   public void actionPerformed(ActionEvent e) {
           value = Double.parseDouble(dollarJTextField.getText());

	   //Convert from USD to EUR
	   if(typeJComboBox.getSelectedItem().equals("EUR")){
		   convertedJTextField.setText(String.valueOf(value * 0.761499).substring(0, 4)); 
	   } 
	   
	   //Convert from USD to CAN
	   else if(typeJComboBox.getSelectedItem().equals("CAN")){
		   convertedJTextField.setText(String.valueOf(value * 1.16385).substring(0, 4));
	   }
	   
	   //Continue the same "else if" process for all the currenct you want to convert
	} 
   
   // 
   public static void main( String[] args )
   {
      CurrencyConverter application = new CurrencyConverter();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

   }
}


#5
javic

javic

    Newbie

  • Members
  • Pip
  • 5 posts
thnx alot sidewinder that really helps.