1

下面的代码代表了我正在尝试编写的内容。要求是税收概况应与正确的货币匹配。我在下面的代码中编写了 if 条件。但是,有 100 多个税务档案可通过数据库读取。我应该为所有 100 个条件编写 if 条件还是有更好的编码方式?

using System;

namespace MatchCondition
{
    class MatchCondition
    {
        private const int TaxAmerica1 = 100;
        private const int TaxAmerica2 = 200;
        private const int TaxIndia1 = 300;
        private const int TaxIndia2 = 400;

        private const int Rupee =100;
        private const int Dollar =200;

        static void Main(string[] args)
        {
            try
            {

                int currencyId = int.Parse(args[0]);
                int taxProfileId = int.Parse(args[1]);
                if (currencyId == Rupee && (taxProfileId == TaxIndia1 || taxProfileId == TaxIndia2))
                {
                    Console.WriteLine("All is well!");
                }
                else if(currencyId == Dollar && (taxProfileId == TaxAmerica1 || taxProfileId == TaxAmerica2))
                {
                    Console.WriteLine("All is well!");
                }
                else if (taxProfileId == 0)
                {
                    Console.WriteLine("All is well!");
                }
                else
                {
                    Console.WriteLine("Mismatch Detected!");
                }

            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }
        }
    }
}
4

2 回答 2

3

您可以将所有有效组合放入哈希表中,即IDictionary从那里开始。

例如:

var validCombinations = new Dictionary<int, List<int>>();
validCombinations.Add(Rupee, new List<int> { TaxIndia1, TaxIndia2 });
validCombinations.Add(Dollar, new List<int> { TaxAmerica1, TaxAmerica2 });

int currencyId = int.Parse(args[0]);
int taxProfileId = int.Parse(args[1]);

List<int> validTaxes;

if (taxProfileId == 0 ||
    (validCombinations.TryGetValue(currencyId, out validTaxes) &&
     validTaxes.Contains(taxProfileId)))
{
    Console.WriteLine("All is well!");
}
else
{
    Console.WriteLine("Mismatch Detected!");
}

您还可以使用从数据库表中读取的组合填充字典,因此您不必对它们进行硬编码。YMMV。

于 2012-11-08T12:45:46.813 回答
0

您可以使用列表的映射字典作为替代

你可以像这样重组你的代码:

因此,您只需为每组有效的货币/税收配置文件添加到 validCurrencies 字典。请注意,这只是为您提供编写 100 个条件的替代方法,您需要自己测试并使代码健壮。

using System;
using System.Collections.Generic;

namespace MatchCondition
{
    class MatchCondition
    {
        private enum TaxProfileEnum
        {
            Default = 0,
            America1 = 100,
            America2 = 200,
            India1 = 300,
            India2 = 400,
        }

        private enum CurrencyEnum
        {
            Rupee = 100,
            Dollar = 200,
        }

        static void Main(string[] args)
        {
            try
            {
                Dictionary<CurrencyEnum, List<TaxProfileEnum>> validCurrencies = new Dictionary<CurrencyEnum, List<TaxProfileEnum>>()
                {
                    { CurrencyEnum.Rupee, new List<TaxProfileEnum>()  { TaxProfileEnum.India1, TaxProfileEnum.India2 } },
                    { CurrencyEnum.Dollar, new List<TaxProfileEnum>() { TaxProfileEnum.America1, TaxProfileEnum.America2 } },
                };

                CurrencyEnum currency = (CurrencyEnum)int.Parse(args[0]);
                TaxProfileEnum taxProfile = (TaxProfileEnum)int.Parse(args[1]);

                if (taxProfile == TaxProfileEnum.Default)
                {
                    Console.WriteLine("All is well!");
                    return;
                }

                List<TaxProfileEnum> validTaxes;
                if (validCurrencies.TryGetValue(currency, out validTaxes) && validTaxes.Contains(taxProfile))
                {
                    Console.WriteLine("All is well!");
                    return;
                }

                Console.WriteLine("Mismatch Detected!");
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }
        }
    }
}
于 2012-11-08T13:24:24.647 回答