如何缩短代码?

时间:2013-11-30 23:12:44

标签: c#

我可以缩短此代码吗?我不想为每一个组合从美元,欧元,瑞士法郎,英镑到美元,欧元,瑞士法郎,英镑的所有组合,因为代码对于所有转换都太长了。

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 ";
    }
}

3 个答案:

答案 0 :(得分:3)

您可以使用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);
    }
}

答案 1 :(得分:1)

你的问题只是要求少量货币能够被转换,所以我的回答可能比你需要的更多,但希望它可能有用。

正如一些评论者已经说过的那样,在您的应用程序中不需要大量的货币汇率时,字典似乎是要走的路。但是,我还建议将其中的一些问题分开,这样如果您确实需要在将来添加新的货币汇率,那么它很容易实现,并且在一个地方(ExchangeRateRepository类)。

通过使用repository pattern访问您的数据,这也不会将您绑定到任何特定的持久性技术,您可以转换到xml / DB / etc。将来无需对该存储库的任何消费者进行更改......并且可以轻松进行单元测试。

正如汉斯所说,以O-O术语思考也是有益的....而不是字符串(我在下面使用过)考虑使用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;
        }
    }
}

答案 2 :(得分:0)

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;
}