2010-10-20 67 views
0

与之相同,它表示“X美元转换为Y俄罗斯卢布”(在两个Digits新的十进制格式之后)的结尾,问题是它可以是俄罗斯卢布,英镑或欧元。我如何区分? (文在有关行插入)如何切换结束功能?

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); 
} 
} 
+0

如果您在程序中对汇率进行硬编码,则需要非常频繁地进行更新。至少把它们放在可以外部更新的属性文件中。 – Thilo 2010-10-20 04:24:50

回答

0

创建一个映射,从IntegerString,与各种货币的描述填充它,并通过Integer的输出功能,让你可以从映射的说明。

+0

喜欢什么?这可能吗? “字符码,俄罗斯卢布”等? – user473973 2010-10-20 04:23:12

+0

像'Map currencyMap = new HashMap (); currencyMap.put(1,“俄罗斯卢布”); ......或者看起来有效的Java代码。 – 2010-10-20 04:25:48

+0

这是一堂课,我们还不知道那个大声笑。 – user473973 2010-10-20 04:32:01

0

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

+0

如果我们这样做,一个枚举可能是一个想法。 – Thilo 2010-10-20 06:29:34

0

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

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