如何切换结束功能?

时间:2010-10-20 04:15:55

标签: java

同样,作为结尾的价值,它说“X美元转换为Y俄罗斯卢布”(在两个数字新的十进制格式之后) - 问题是它可以是俄罗斯卢布,英镑或欧元。我该如何区分? (插入问题的文字)

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.text.DecimalFormat;

public class CurrencyConversion extends Applet implements ItemListener
{
 //declare variables and color
 double dollars, answer;
 int empCode;
 Image dollarSign;
 Color darkRed = new Color(160, 50, 0);

 //Create components for applet
 Label promptLabel = new Label("Enter the dollar amount (do not use commas or dollar signs):");
  TextField currencyField = new TextField(20);

 Label codeLabel = new Label ("Select the desired currency:");

 CheckboxGroup codeGroup = new CheckboxGroup () ;
  Checkbox britishpoundBox = new Checkbox("British Pound",false,codeGroup);
  Checkbox euroBox = new Checkbox("Euro",false,codeGroup);
  Checkbox russianrubleBox = new Checkbox("Russian Ruble",false,codeGroup);
  Checkbox hiddenBox = new Checkbox("",true,codeGroup);


 Label outputLabel = new Label("Click an option to convert the desired currency.");

 public void init()
 {
  setBackground(darkRed);
  setForeground(Color.white);
  add(promptLabel);
  add(currencyField);
  currencyField.requestFocus();
  currencyField.setForeground(Color.black);
  add(codeLabel);
  add(britishpoundBox);
  britishpoundBox.addItemListener(this);
  add(euroBox);
  euroBox.addItemListener(this);
  add(russianrubleBox);
  russianrubleBox.addItemListener(this);
  add(outputLabel);
 }

 //This method is triggered by click
 public void itemStateChanged(ItemEvent choice)
 {
  try
  {
   dollars = getCurrency();
   empCode = getCode();
   answer = getComm(dollars,empCode);
   output(answer, dollars);
  }

  catch (NumberFormatException e)
  {
   outputLabel.setText("You must enter a dollar amount greater than zero.");
   hiddenBox.setState(true);
   currencyField.setText("");
   currencyField.requestFocus();
  }
 }

 public double getCurrency()
 {
  double currency = Double.parseDouble(currencyField.getText());

  if (currency <= 0) throw new NumberFormatException();

  return currency;
 }

 public int getCode()
 {
  int code = 0;
  if (britishpoundBox.getState()) code = 1;
  else
   if (euroBox.getState()) code = 2;
   else
    if (russianrubleBox.getState()) code = 3;
  return code;
 }

 public double getComm(double currency, int code)
 {
  double amount = 0.0;
  switch(code)
  {
   case 1:
    amount = .79610 * currency;
    break;
   case 2:
    amount = .70880 * currency;
    break;
   case 3:
    amount = 35.88240 * currency;
    break;
  }
  return amount;
 }

 public void output(double amount, double currency)
 {
  DecimalFormat twoDigits = new DecimalFormat("##.00");
  outputLabel.setText("Your amount of " + twoDigits.format(currency) + " dollars converts to " + twoDigits.format(amount) RIGHT HERE - what do I do? );
 }

 public void paint(Graphics g)
 {
  dollarSign = getImage(getDocumentBase(), "dollarsign.gif");
  g.drawImage(dollarSign,12,28,this);
 }
}

3 个答案:

答案 0 :(得分:0)

创建从IntegerString的映射,使用各种货币描述填充它,并将Integer传递给输出函数,以便您可以从映射中获取描述。

答案 1 :(得分:0)

将货币概念包装到自己的对象中。重写ToString()方法并返回货币名称或定义显式属性以返回货币类型。传递货币对象而不是不那么复杂的双倍,并使之前为对象的双重属性的费率。

答案 2 :(得分:0)

我建议你使用java.util.Currency类。每种货币的汇率不应硬编码。对于第一阶段,只需将其放入属性文件中,即: RUB = 35.88240 GBP = 0.79610

避免使用硬编码的货币列表。而是解析此属性文件,提取所有键,然后使用Currency.getInstance(currencyCode)。 UI和逻辑都是通用的。您的申请将至少缩短两倍并且更加灵活。