2012-11-08 98 views
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); 
      } 
     } 
    } 
} 

回答

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

你也可以填充從數據庫表中讀取組合的字典,所以你不必硬編碼他們。因人而異。

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); 
      } 
     } 
    } 
} 
+0

Dang,Stackoverflow因爲我提交我的答案而失敗......必須等待提交。 – RickL