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(( 条件。有什么建议吗?
如果条件为波纹管,则修改了两者,但修改不符合我的要求。
//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 );