The Artima Developer Community
Sponsored Link

Java Answers Forum
Currency Converter Help

5 replies on 1 page. Most recent reply: Apr 21, 2002 10:44 PM by Neva Mohammad

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Currency Converter Help Posted: Apr 17, 2002 6:19 PM
Reply to this message Reply
Advertisement
Can someone help me clean up this program? I'm not getting the methods to get the input text and use it correctly.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
//======================================================
//Converts US Dollars to other currency.
//======================================================
public class Currency extends Applet {
	
	private CurrencyGUI gui;
	
	public void init () {
		gui = new CurrencyGUI (this);
	} //method init
	
} //class Currency
 
//======================================================
// Creates the graphical inteface for the applet.
//======================================================
 
class CurrencyGUI {
	
	private static Label titleLabel, conversionLabel,resultLabel,enterLabel;
	private static TextField choiceTextField;
	private static Choice countryChoice;
		
	public CurrencyGUI (Applet app) {
		
		FlowLayout tempLayout = (FlowLayout) app.getLayout();
		tempLayout.setHgap (45);
		
		app.setBackground (Color.yellow);
		
		titleLabel = new Label ("     US Dollars Converter");
		
		conversionLabel = new Label ("Result:", SwingConstants.LEFT);
		conversionLabel.setAlignment (SwingConstants.LEFT);
		
		resultLabel = new Label ("N/A", SwingConstants.LEFT);
		resultLabel.setAlignment (SwingConstants.LEFT);
		
		
		enterLabel = new Label ("Enter US Dollars",SwingConstants.LEFT);
		enterLabel.setAlignment (SwingConstants.LEFT);
		choiceTextField = new TextField (10);
		choiceTextField.addActionListener();
 
 
		
		countryChoice = new Choice();
		countryChoice.add ("English pound");
		countryChoice.add ("Japanese yen");
		countryChoice.add ("French franc");
		
		// add the components to the applet
		
		app.add (titleLabel);
		app.add (countryChoice);
		app.add (conversionLabel);
		app.add (resultLabel);
		app.add (enterLabel);
		app.add (choiceTextField);
		
		app.setSize (250,250);
		
	} //constructor CurrencyGUI
	
	public static int getCurrencyChoice() {
		
		return countryChoice.getSelectedIndex();
	} //method getCurrencyChoice
	
    public static int setresultLabel() {
		
		return setresultLabel();
	} //method getCurrencyChoice
		
	
public class ConversionActionListener implements ActionListener {
    	
    public void actionPerformed (ActionEvent event){	
    	
    	int USDollars;
    	String text = choiceTextField.getText();
        USDollars = Integer.parseInt (text);
        
        resultLabel.setText (Integer.toString (USDollars));
        
        CurrencyConverter.convert();
       }
 } //class conversionActionListener
 
    public static getUSDollars () {
   	
   	 actionPerformed.getText();
   	
   }
   	
   	
} //class CurrencyGUI
 
//==============================================================
// Contains the code to perform the conversion.
//==============================================================
 
class CurrencyConverter {
	
	final static double POUNDSTOUSD = 0.61203;
	final static double YENTOUSD = 138.509;
	final static double FRANCSTOUSD = 5.97415;
	
	final static int Pound = 0;
	final static int Yen = 0;
	final static int Franc = 0;
	
	public static double convert (double dollars) {
		
		double conversionFactor = 0.0;
		int convertTo = CurrencyGUI.getCurrencyChoice();
	
		 
		 switch (convertTo){
		 case 1: conversionFactor = POUNDSTOUSD;
		 break;
		 case 2: conversionFactor = YENTOUSD;
		 break;
		 case 3: conversionFactor = FRANCSTOUSD;
		 break; 
		 default: conversionFactor = 0.0;
		 } 
		 
		 return dollars * conversionFactor;
} //method convert
 
} //class CurrencyConverter
		
		


Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: Currency Converter Help Posted: Apr 18, 2002 1:00 PM
Reply to this message Reply
Well, I've doctored it up some but still can't get the result Label to show any result. Please Help.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
 
//======================================================
//Converts US Dollars to other currency.
//======================================================
 
public class Currency extends Applet {	
	
    private CurrencyGUI gui;		
 
 
    public void init () {	
   	
     gui = new CurrencyGUI (this);	
    
     } //method init	
   } //class Currency
    
//======================================================
// Creates the graphical interface for the applet.
//====================================================== 
 
class CurrencyGUI {		
 
    private static Label titleLabel, conversionLabel,resultLabel,enterLabel;	
    private static TextField amountText;	
    private static Choice countryChoice;
    private static String USDollars;
    public static int  enterDollars; 
 			
    
  public CurrencyGUI (Applet app) {	
  			
    FlowLayout tempLayout = (FlowLayout) app.getLayout();		
    tempLayout.setHgap (45);				
  
    app.setBackground (Color.yellow);
  				
    titleLabel = new Label ("     US Dollars Converter");		
  		
    conversionLabel = new Label ("Result:", SwingConstants.LEFT);	
    conversionLabel.setAlignment (SwingConstants.LEFT);	
  
    resultLabel = new Label ("N/A", SwingConstants.LEFT);		
    resultLabel.setAlignment (SwingConstants.LEFT);		
  				
    enterLabel = new Label ("Enter US Dollars",SwingConstants.LEFT);
    enterLabel.setAlignment (SwingConstants.LEFT);		
  
    amountText = new TextField (10);
    amountText.addActionListener(new ConversionActionListener());		
				
  
  
    countryChoice = new Choice();		
    countryChoice.add ("English pound");		
    countryChoice.add ("Japanese yen");		
    countryChoice.add ("French franc");				
  
    // add the components to the applet				
    app.add (titleLabel);		
    app.add (countryChoice);		
    app.add (conversionLabel);		
    app.add (resultLabel);		
    app.add (enterLabel);		
    app.add (amountText);
    				
    app.setSize (250,250);
    			
  } //constructor CurrencyGUI		
  
  public static int getCurrencyChoice() {	
  			
    return countryChoice.getSelectedIndex();	
   } //method getCurrencyChoice	 
   
   public static String getUSDollars () { 
   
 	 String USDollars = enterLabel.getText();
 	 enterDollars = Integer.parseInt (USDollars);
 	     
     return USDollars;   	   
    }   	   	   
  
  public static String setResultLabel(String resultText) {	
 
    resultLabel.setText(resultText);
    String result;
    return result;	
  
   } 
 
   		
class ConversionActionListener implements ActionListener {
  	    	    
  	   private TextField amountText;
  	   private Button convertButton;
  	   
  	   
  	public ConversionActionListener (TextField textListen, Button buttonListen){ 
  	  
  	  this.amountText = textListen;
  	  this.convertButton = buttonListen;
  	  
  	  } //ConversionActionListener
  	  
  	  	    
  	public void actionPerformed (ActionEvent event){
  	  
  	  Double Dollars; 
  	  double dollars;
  	  double result;
  	  String resultText;
  	  
  	  Dollars = Double.valueOf(CurrencyGUI.getUSDollars()); 
  	  dollars = Dollars.doubleValue();  
  	  result = CurrencyConverter.convert(dollars);
  	  resultText = Double.toString(result);
  	  CurrencyGUI.setResultLabel (resultText);
  	  	
  	             	   
  	 } //actionPerformed
    
    
 } //class conversionActionListener
 } //class CurrencyGUI
 //==============================================================
 // Contains the code to perform the conversion.
 //==============================================================
 class CurrencyConverter {		
 
   final static double POUNDSTOUSD = 0.61203;	
   final static double YENTOUSD = 138.509;	
   final static double FRANCSTOUSD = 5.97415;		
   final static int Pound = 0;	
   final static int Yen = 0;	
   final static int Franc = 0;
   public static double money;		
 
 public static double convert (double dollars) {		
 		
 	double conversionFactor = 0.0;		
 	int convertTo = CurrencyGUI.getCurrencyChoice();	
 			 		 
 	switch (convertTo){		 
 	case 1: conversionFactor = POUNDSTOUSD;		 
 	break;		 
 	case 2: conversionFactor = YENTOUSD;		 
 	break;		 
 	case 3: conversionFactor = FRANCSTOUSD;		 
 	break; 		 
 	default: conversionFactor = 0.0;		 
 	} 	
 	money = dollars * conversionFactor;			 
 	return money; 
 	} //method convert
	
} //class CurrencyConverter	
		

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: Currency Converter Help Posted: Apr 18, 2002 6:14 PM
Reply to this message Reply
Nevermind. I got it.

Neva Mohammad

Posts: 28
Nickname: neva
Registered: Apr, 2002

Re: Currency Converter Help Posted: Apr 21, 2002 12:56 AM
Reply to this message Reply
Hey I just saw your query, the way I got around the Label not showing the result was to show the result in a TextField with setEditable(false).
Out of curiosity how did you manage to do it?

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Here's what I got Posted: Apr 21, 2002 8:59 AM
Reply to this message Reply
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
 
//======================================================
//Converts US Dollars to other currency.
//======================================================
 
public class Currency extends Applet {	
	
    private CurrencyGUI gui;		
 
 
    public void init () {	
   	
     gui = new CurrencyGUI (this);	
    
     } //method init	
   } //class Currency
    
//======================================================
// Creates the graphical interface for the applet.
//====================================================== 
 
class CurrencyGUI {		
 
    private static Label titleLabel, conversionLabel,resultLabel,enterLabel;	
    private static TextField amountText;
    private static Button convertButton;	
    private static Choice countryChoice;
    private static String USDollars;
    private static String result2;
    public static int  enterDollars; 
 			
    
  public CurrencyGUI (Applet app) {	
  			
    FlowLayout tempLayout = (FlowLayout) app.getLayout();		
    tempLayout.setHgap (70);				
  
    app.setBackground (Color.pink);
  				
    titleLabel = new Label ("     US Dollars Converter");		
  		
    conversionLabel = new Label ("Result:", SwingConstants.LEFT);	
    conversionLabel.setAlignment (SwingConstants.LEFT);	
  
    resultLabel = new Label ("------------------------------", SwingConstants.LEFT);		
    resultLabel.setAlignment (SwingConstants.LEFT);		
  				
    enterLabel = new Label ("Enter US Dollars",SwingConstants.LEFT);
    enterLabel.setAlignment (SwingConstants.LEFT);		
  
    amountText = new TextField (10);
    ConversionActionListener actionListener = new ConversionActionListener();
    amountText.addActionListener(actionListener);
    
    convertButton = new Button ("Convert");
  	ConversionActionListener actionListener2 = new ConversionActionListener();
    convertButton.addActionListener(actionListener);
    	
				  
    countryChoice = new Choice();
    countryChoice.add ("Choose currency");		
    countryChoice.add ("English Pound");		
    countryChoice.add ("Japanese Yen");		
    countryChoice.add ("French Franc");				
  
    // add the components to the applet				
    app.add (titleLabel);		
    app.add (countryChoice);		
    app.add (conversionLabel);		
    app.add (resultLabel);		
    app.add (enterLabel);		
    app.add (amountText);
    app.add (convertButton);
    				
    app.setSize (250,250);
    			
  } //constructor CurrencyGUI		
  
  public static int getCurrencyChoice() {	
  			
    return countryChoice.getSelectedIndex();
    	
   } //method getCurrencyChoice	 
   
 public static String getUSDollars () { 
  
 	String USDollars = amountText.getText();
 		     
    return USDollars;   	   
   }   	   	   
  
  public static String setResultLabel(String money) {	
  
    resultLabel.setText(money);
   
    return money;	
  
   } 
 
   		
public class ConversionActionListener implements ActionListener {
  	    	    
  	   	  	    
  	public void actionPerformed (ActionEvent event){
  	  
  	  Double Dollars; 
  	  double dollars;
  	  double result;
  	  String money;
  	  
  	  Dollars = Double.valueOf(CurrencyGUI.getUSDollars()); 
  	  dollars = Dollars.doubleValue();  
  	  result = CurrencyConverter.convert(dollars);
  	  money = Double.toString(result);
  	  CurrencyGUI.setResultLabel (money);
  	  	
  	             	   
  	 } //actionPerformed
    
    
 } //class conversionActionListener
 } //class CurrencyGUI
 //==============================================================
 // Contains the code to perform the conversion.
 //==============================================================
 class CurrencyConverter {		
 
   final static double POUNDSTOUSD = 0.61203;	
   final static double YENTOUSD = 138.509;	
   final static double FRANCSTOUSD = 5.97415;		
   public static double money;		
 
 public static double convert (double dollars) {		
 		
	
 	int convertTo = CurrencyGUI.getCurrencyChoice();	
 		 		 
 	switch (convertTo){		 
 	case 1: 
 	money = dollars * .61203;		 
 	break;		 
 	case 2: 
 	money = dollars * 138.509;			 
 	break;		 
 	case 3: 
 	money = dollars * 5.97415;		 
 	break; 	
 	default:
 	money = dollars;	 
 		 
 	} 	
 		 
 	return money; 
 	} //method convert
	
} //class CurrencyConverter	
		

Neva Mohammad

Posts: 28
Nickname: neva
Registered: Apr, 2002

Re: Here's what I got Posted: Apr 21, 2002 10:44 PM
Reply to this message Reply
Thanks...;o)

Flat View: This topic has 5 replies on 1 page
Topic: Selection in JTable Previous Topic   Next Topic Topic: Still trying to get help

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use