2010-10-20 84 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和邏輯都是通用的。你的應用程序至少會縮短兩倍,並且更加靈活。