2012-05-31 49 views
-2
public static final int FALSE = 0; 
    public static final int TRUE = 10; 

請告訴我一件事我有這個以下的關於一個條件的檢查

 if (!walgreensRewardsSupport.isPreProdOrTrainingMode() 
     && walgreensRewardsSupport.isWagRewardsRedeemptionEnabled() 
     && null != walgreensRewardsInfoLine 
     && null != walgreensRewardsInfoLine.getMemberFullProfile() 
     && (null != walgreensRewardsInfoLine.getMemberFullProfile().getRewardPoints()) 
     && (walgreensRewardsInfoLine.getMemberFullProfile().getRewardPoints().getCurrentBalance() != 0) 
     && !transaction 
      .getBooleanValue(ITransactionHashtableWag.REWARDS_REDEMPTION_ZIPCODE_LIMIT_REACHED) 
     && !walgreensRewardsRedemptionSupport.isLoyaltyRedemptionTenderLineExist(transaction) 
     // No need to force recalculation each time, recalculation happens after any tender line is 
     // added to transaction. 
     && walgreensRewardsRedemptionSupport.getRedemptionEligibleTransactionAmount(transaction, 
      true, false).isGreaterThanZero()) { 
     return TRUE; 
    } 
else { 
     return FALSE; 
    } 

    } 

一段代碼,現在我要在這之間加一個條件......檢查 walgreensRewardsInfoLine。 getMemberFullProfile()。getRewardPoints()的GetValues()。大小() 是0,那麼就返回False..I已經通過以下方式完成..

 if (!walgreensRewardsSupport.isPreProdOrTrainingMode() 
     && walgreensRewardsSupport.isWagRewardsRedeemptionEnabled() 
     && null != walgreensRewardsInfoLine 
     && null != walgreensRewardsInfoLine.getMemberFullProfile() 
     && (null != walgreensRewardsInfoLine.getMemberFullProfile().getRewardPoints()) 
     && (walgreensRewardsInfoLine.getMemberFullProfile().getRewardPoints().getCurrentBalance() != 0) 
     && !transaction 
      .getBooleanValue(ITransactionHashtableWag.REWARDS_REDEMPTION_ZIPCODE_LIMIT_REACHED) 
     && !walgreensRewardsRedemptionSupport.isLoyaltyRedemptionTenderLineExist(transaction) 
     // No need to force recalculation each time, recalculation happens after any tender line is 
     // added to transaction. 
     && walgreensRewardsRedemptionSupport.getRedemptionEligibleTransactionAmount(transaction, 
      true, false).isGreaterThanZero()) { 
     return TRUE; 
    }else if (walgreensRewardsInfoLine.getMemberFullProfile().getRewardPoints().getCurrentBalance() == 0) 
{ 
return FALSE 
} 
else { 
     return FALSE; 
    } 

    } 

請告知是心病RECT .. !!

+3

1.發佈問題後,您不能期望答案少於120秒。 2.你有沒有測試過它是否按預期工作?如果沒有,你注意到了什麼意外的行爲? – assylias

+5

什麼是真和假?如果他們只是真的和錯誤的,爲什麼不使用普通的Java文字?在那個和「null!= ...」之間,這看起來更像C而不是Java。 –

+0

可能的重複用戶:http://stackoverflow.com/users/1370546/user1370546和http://stackoverflow.com/users/1188021/neera –

回答

2

在第一個代碼塊,你必須:

if (condition) { 
    return TRUE; 
} else { 
    return FALSE; 
} 

這可以簡化爲:

return (condition) ? TRUE : FALSE; 

如果真和假都只是布爾,

return (condition); 

在第二個代碼塊,您有:

if (condition1) { 
    return TRUE; 
} else if (condition2) { 
    return FALSE; 
} else { 
    return FALSE; 
} 

其他 - 如果沒有價值,除非您希望從第二個條件的評估產生副作用。

還要注意一些私人方法的深思熟慮可以使這段代碼更容易理解。你可以封裝的你的大條件的一部分這樣的:

private boolean hasRewardsPointsBalance(InfoLine infoLine) { 
    if (null == infoLine) return false; 
    MemberProfile profile = infoLine.getMemberFullProfile(); 
    if (null == profile) return false; 
    RewardsPoints rewards = profile..getRewardPoints(); 
    if (null == rewards) return false; 
    return (rewards.getCurrentBalance() != 0); 
} 
0

這是更好地只需添加你想要什麼假到設定的條件正在邏輯聯繫在一起,返回true邏輯補。

例如:

if (!walgreensRewardsSupport.isPreProdOrTrainingMode() 

    && walgreensRewardsInfoLine.getMemberFullProfile().getRewardPoints() 
     .getValues().size() != 0 

    && walgreensRewardsSupport.isWagRewardsRedeemptionEnabled() 
    && null != walgreensRewardsInfoLine 
    && null != walgreensRewardsInfoLine.getMemberFullProfile() 
    && (null != walgreensRewardsInfoLine.getMemberFullProfile().getRewardPoints()) 
    && (walgreensRewardsInfoLine.getMemberFullProfile().getRewardPoints().getCurrentBalance() != 0) 
    && !transaction 
     .getBooleanValue(ITransactionHashtableWag.REWARDS_REDEMPTION_ZIPCODE_LIMIT_REACHED) 
    && !walgreensRewardsRedemptionSupport.isLoyaltyRedemptionTenderLineExist(transaction) 
    // No need to force recalculation each time, recalculation happens after any tender line is 
    // added to transaction. 
    && walgreensRewardsRedemptionSupport.getRedemptionEligibleTransactionAmount(transaction, 
     true, false).isGreaterThanZero()) { 

    return TRUE; 


這比你編碼什麼那麼複雜。