如何在 if 条件下应用组合技术
How to apply combine technique with if condition
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;
}
评论描述的业务逻辑,要结合if (IsGoldCustomer() && LessThan(24)) 和if (!IsGoldCustomer() &&LessThan(48)) 条件。有什么建议吗?
下面两个if条件都修改了,但是修改不满足我的要求
//for gold customer IsCanceled= false
IsCanceled = !(IsGoldCustomer() && LessThan(24));
//for not gold customer IsCanceled= true
IsCanceled = !IsGoldCustomer() &&LessThan(48);
IsCancelled = IsGoldCustomer()? !LessThan( 24 ) : !LessThan( 48 );
甚至:
IsCancelled = !LessThan( IsGoldCustomer()? 24 : 48 );
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;
}
评论描述的业务逻辑,要结合if (IsGoldCustomer() && LessThan(24)) 和if (!IsGoldCustomer() &&LessThan(48)) 条件。有什么建议吗?
下面两个if条件都修改了,但是修改不满足我的要求
//for gold customer IsCanceled= false
IsCanceled = !(IsGoldCustomer() && LessThan(24));
//for not gold customer IsCanceled= true
IsCanceled = !IsGoldCustomer() &&LessThan(48);
IsCancelled = IsGoldCustomer()? !LessThan( 24 ) : !LessThan( 48 );
甚至:
IsCancelled = !LessThan( IsGoldCustomer()? 24 : 48 );