2017-05-07 130 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);