1

以下のコードは、私が書き込もうとしているものを表しています。要件は、税プロファイルが正しい通貨と一致する必要があることです。以下のコードにif条件を記述しました。ただし、データベースを介して読み取られる100を超える税プロファイルがあります。それらの100個すべての条件を書く必要がありますか、それともコーディングするためのより良い方法がありますか?

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 に答える