簡體   English   中英

如何縮短代碼?

[英]How can I shorten code?

我可以縮短這段代碼嗎? 我不想為每個組合制作美元,歐元,瑞士法郎,英鎊到美元,歐元,瑞士法郎,英鎊的所有組合,因為所有轉換的代碼都太長了。

if (Convert.ToString(comboBoxInputMoney.Text) == "USD")
{
    if (Convert.ToString(comboBoxOutputMoney.Text) == "EUR")
    {
        double USD = double.Parse(textBoxInputMoney.Text);
        USD = USD * 0.74;
        textBoxResultMoney.Text = USD.ToString() + " EUR ";
    }
}

if (Convert.ToString(comboBoxInputMoney.Text) == "USD")
{
    if (Convert.ToString(comboBoxOutputMoney.Text) == "CHF")
    {
        double USD = double.Parse(textBoxInputMoney.Text);
        USD = USD * 0.92;
        textBoxResultMoney.Text = USD.ToString() + " CHF ";
    }
}

您可以使用Dictionary來存儲輸入貨幣和匯率:

var dict = new Dictionary<string, Dictionary<string, decimal>>();
var usDict = new Dictionary<string, decimal>();
usDict.Add("EUR", 0.74m);
usDict.Add("CHF", 0.92m);
dict.Add("USD", usDict); 
// and so on ...

您不需要始終創建它,您可以重復使用它並在某些內容發生變化時更新匯率。 那么輸入或輸出貨幣的數量無關緊要。

這始終是相同的代碼:

Dictionary<string, decimal> currencyExchange;
if (dict.TryGetValue(comboBoxInputMoney.Text, out currencyExchange))
{
    decimal rate;
    decimal value;
    if(decimal.TryParse(textBoxInputMoney.Text, out value)
      && currencyExchange.TryGetValue(comboBoxOutputMoney.Text, out rate))
    {
        textBoxResultMoney.Text = string.Format("{0} {1}"
            , value * rate 
            , comboBoxOutputMoney.Text);
    }
}

你的問題只是要求少量貨幣能夠被轉換,所以我的回答可能比你需要的更多,但希望它可能有用。

正如一些評論者已經說過的那樣,字典似乎是要走的路,因為你的應用程序中不需要大量的貨幣匯率。 但是,我還建議將其中的一些問題分開,這樣如果您確實需要在將來添加新的貨幣匯率,那么它很容易就可以,並且在一個地方(ExchangeRateRepository類)。

通過使用存儲庫模式來訪問您的數據,這也不會將您與任何特定的持久性技術聯系起來,您可以轉換到xml / DB / etc。 將來無需對該存儲庫的任何消費者進行更改......並且可以輕松進行單元測試。

正如漢斯所說,以OO術語思考也是有益的....而不是字符串(我在下面使用過)考慮使用Currency對象。

// Your calling code would now just be...

double amountToConvert = double.Parse(textBoxInputMoney.Text);
CurrencyConverter converter = new CurrencyConverter(new ExchangeRateRepository());
textBoxResultMoney.Text = converter.Convert(amountToConvert, comboBoxInputMoney.Text, comboBoxOutputMoney.Text);

// ...

public class CurrencyConverter
{
    private IExchangeRateRepository exchangeRateRepository;

    public CurrencyConverter(IExchangeRateRepository repository)
    {
        exchangeRateRepository = repository;
    }

    public string Convert(double amount, string fromCurrency, string toCurrency)
    {
        var fromRates = exchangeRateRepository.Rates.SingleOrDefault(a => a.Key == fromCurrency);

        if (fromRates.Key != null)
        {
            var toRate = fromRates.Value.SingleOrDefault(a => a.Key == toCurrency);

            if (toRate.Key != null)
            {
                return (amount * toRate.Value) + toCurrency;
            }
        }

        return string.Empty;
    }
}

public interface IExchangeRateRepository
{
    Dictionary<string, Dictionary<string, double>> Rates { get; }
}

public class ExchangeRateRepository : IExchangeRateRepository
{
    private Dictionary<string, Dictionary<string, double>> exchangeRatesLookup = new Dictionary
        <string, Dictionary<string, double>>
                                                                                     {
                                                                                         {
                                                                                             "USD",
                                                                                             new Dictionary
                                                                                             <string, double>()
                                                                                                 {
                                                                                                     {"EUR", 0.74},
                                                                                                     {"CHF", 0.92},
                                                                                                     {"GBP", 0.6}
                                                                                                 }
                                                                                         },
                                                                                         {
                                                                                             "EUR",
                                                                                             new Dictionary
                                                                                             <string, double>()
                                                                                                 {
                                                                                                     {"USD", 1.35},
                                                                                                     {"CHF", 1.23},
                                                                                                     {"GBP", 1.1}
                                                                                                 }
                                                                                         },
                                                                                     };

    public Dictionary<string, Dictionary<string, double>> Rates
    {
        get
        {
            return exchangeRatesLookup;
        }
    }
}
double rate;
if (comboBoxInputMoney.Text == "USD")
{
    if (comboBoxOutputMoney.Text == "EUR")
    {
        rate = 0.74
    }
    if (comboBoxOutputMoney.Text == "CHF")
    {
        rate = 0.92
    }

    double result = double.Parse(textBoxInputMoney.Text) * rate;
    textBoxResultMoney.Text = result.ToString() + comboBoxOutputMoney.Text;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM