2017-05-07 127 views
0

C#代碼的語法是波紋管相結合的技術如何申請使用,如果條件

 public void Cancel() 
     { 

      // If reservation already started throw exception 
      if (DateTime.Now > From) 
      { 
       throw new InvalidOperationException("It's too late to cancel."); 
      } 
//for gold customer IsCanceled= false 
      if (IsGoldCustomer() && LessThan(24)) 
      { 
       IsCanceled = false; 
      } 
//for not gold customer IsCanceled= true 
      if (!IsGoldCustomer() &&LessThan(48)) 
      { 

       IsCanceled = true; 
      } 

     } 

     private bool IsGoldCustomer() 
     { 
      return Customer.LoyaltyPoints > 100; 
     } 

     private bool LessThan(int maxHours) 
     { 
      return (From - DateTime.Now).TotalHours < maxHours; 
     } 

評論描述業務邏輯,要合併,如果(IsGoldCustomer()& &每種不超過(24)),如果(!IsGoldCustomer() & & LessThan(48))條件。有什麼建議如何?

修改既如果條件波紋管,但修改不滿足我的要求。

//for gold customer IsCanceled= false 
      IsCanceled = !(IsGoldCustomer() && LessThan(24)); 
//for not gold customer IsCanceled= true 
      IsCanceled = !IsGoldCustomer() &&LessThan(48); 
+0

看起來喜歡的事,需要重構成'CanCancel'函數返回一個布爾值。在取消條件下,取消功能可能不會失效。 –

回答

0
IsCancelled = IsGoldCustomer()? !LessThan(24) : !LessThan(48); 

甚至:

IsCancelled = !LessThan(IsGoldCustomer()? 24 : 48);