//CS5-HMC-Sp.,00-Lab 10 //A. Khakpour, Apr., 00 import java.applet.*; import java.awt.*; import java.awt.event.*; //================================================================== // Converts US Dollars to other currency. //================================================================== public class Currency3 extends Applet { private CurrencyGUI gui; public void init () { gui = new CurrencyGUI (this); } } //================================================================== // Creates the graphical interface for the applet. //================================================================== class CurrencyGUI { private static Label titleLabel; private static Choice countryChoice; private static Label resultLabel; private static Label resultLabel1; private static TextField quote; private static Label enter; private static Button button; private ConversionActionListener listener; public CurrencyGUI (Applet app) { FlowLayout tempLayout = (FlowLayout) app.getLayout(); tempLayout.setHgap (10); app.setBackground (Color.yellow); titleLabel = new Label ("US Dollars Converter"); resultLabel = new Label ("Result of Conversion"); resultLabel1 = new Label ("Result: "); quote = new TextField(10); button = new Button("Convert"); listener = new ConversionActionListener (); quote.addActionListener (listener); button.addActionListener (listener); enter = new Label ("Enter US Dollars: $"); countryChoice = new Choice(); countryChoice.add ("English pound"); countryChoice.add ("Japanese yen"); countryChoice.add ("French franc"); // add the components to the applet app.add (titleLabel); resultLabel.setAlignment(Label.LEFT); app.add (resultLabel1); app.add (enter); app.add (quote); app.add (resultLabel); app.add (countryChoice); app.add (button); // app.setSize (250, 250); } public static void setResultLabel (String str) { resultLabel.setText(str); } public static int getCurrencyChoice() { return countryChoice.getSelectedIndex(); } public static String getUSDollars() { return quote.getText(); } } //================================================================== // Contains the code to perform the conversion. //================================================================== class CurrencyConverter { final static double POUNDSTOUSD = 0.61203; final static double YENSTOUSD = 138.509; final static double FRANCSTOUSD = 5.97415; final static int POUND = 0; final static int YEN = 1; final static int FRANC = 2; public static double convert (double dollars) { double conversionFactor = 0.0; int convertTo = CurrencyGUI.getCurrencyChoice(); switch(convertTo) { case POUND: conversionFactor = POUNDSTOUSD; break; case YEN: conversionFactor = YENSTOUSD; break; case FRANC: conversionFactor = FRANCSTOUSD; } return dollars * conversionFactor; } } class ConversionActionListener implements ActionListener { public void actionPerformed (ActionEvent e) { CurrencyGUI.setResultLabel ("ERROR"); String text = CurrencyGUI.getUSDollars(); Double x = new Double(text); double val = x.doubleValue(); double value = CurrencyConverter.convert(val); CurrencyGUI.setResultLabel(Double.toString(value)); } }