关于If Else的最低使用率

时间:2012-05-23 17:33:41

标签: java

  

可能重复:
  Excessive use of If else staements

我有下面的代码,过度使用if else语句......

 if (cardType == AARP_CARD_TYPE) {
  userResponse = messageBox.showMessage("CandidateAARPCardAttachCardToExistingTransaction",
      null, IMessageBox.YESNO); // MSG:31.59
  transaction.setValue(ITransactionHashtableWag.LOYALTY_MESSAGE_DISPLAYED,
      WalgreensRewardsConstants.ATTACH_CANDIDATE_AARP_CARD);
} else if ((cardType == PSC_CARD_TYPE) && ((!PosHelper.isRunningAsService()))) {
  userResponse = messageBox.showMessage("PendingPSCCardAttachCardToExistingTransaction", null,
      IMessageBox.YESNO); // MSG:31.60
  transaction.setValue(ITransactionHashtableWag.LOYALTY_MESSAGE_DISPLAYED,
      WalgreensRewardsConstants.ATTACH_PENDING_PSC_CARD);

} else if ((cardType == DR_CARD_TYPE) && ((!PosHelper.isRunningAsService()))) {
  userResponse = messageBox.showMessage("PendingDRCardAttachCardToExistingTransaction", null,
      IMessageBox.YESNO); // MSG:31.63
  transaction.setValue(ITransactionHashtableWag.LOYALTY_MESSAGE_DISPLAYED,
      WalgreensRewardsConstants.ATTACH_PENDING_DR_CARD);

} else if ((cardType == WAG_LOYALTY_CARD_TYPE)){
            transaction.setValue(ITransactionHashtableWag.LOYALTY_MESSAGE_DISPLAYED,
              WalgreensRewardsConstants.ATTACH_NOT_ON_FILE);

        if((!PosHelper.isRunningAsService())) {
  userResponse = messageBox.showMessage("CardNotOnFileToAttach", null, IMessageBox.YESNO); // MSG:31.32
  // BUC
  // 1.22.1
}


} else { // If the device is neither of these, POS displays Message 1
  // Button, MSG 31.14. [BUC
  // 1.23.2]
  displayMessage("InvalidLoyaltyCard");
  transaction.setValue(ITransactionHashtableWag.LOYALTY_MESSAGE_DISPLAYED,
      NOT_VALID_LOYALTY_CARD);
  userResponse = -1;
}

我想删除过度使用if else staement以便在下面的表格中加入一点点

byte cardType ;

    switch (cardType) {
    case  AARP_CARD_TYPE:
         userResponse = messageBox.showMessage("CandidateAARPCardAttachCardToExistingTransaction",
                  null, IMessageBox.YESNO); // MSG:31.59
              transaction.setValue(ITransactionHashtableWag.LOYALTY_MESSAGE_DISPLAYED,
                  WalgreensRewardsConstants.ATTACH_CANDIDATE_AARP_CARD);
                    // blah
        break;

    case PSC_CARD_TYPE:
        if ((!PosHelper.isRunningAsService())
                {

             userResponse = messageBox.showMessage("PendingPSCCardAttachCardToExistingTransaction", null,
                      IMessageBox.YESNO); // MSG:31.60
                  transaction.setValue(ITransactionHashtableWag.LOYALTY_MESSAGE_DISPLAYED,
                      WalgreensRewardsConstants.ATTACH_PENDING_PSC_CARD);
                }   
        // blah
        break;

        case  DR_CARD_TYPE :                
            if ((!PosHelper.isRunningAsService())
                    {                   
                userResponse = messageBox.showMessage("PendingDRCardAttachCardToExistingTransaction", null,
                          IMessageBox.YESNO); // MSG:31.63
                      transaction.setValue(ITransactionHashtableWag.LOYALTY_MESSAGE_DISPLAYED,
                          WalgreensRewardsConstants.ATTACH_PENDING_DR_CARD);
            // blah
            break;


                    case   WAG_LOYALTY_CARD_TYPE :

                        transaction.setValue(ITransactionHashtableWag.LOYALTY_MESSAGE_DISPLAYED,
                                  WalgreensRewardsConstants.ATTACH_NOT_ON_FILE);
                        if((!PosHelper.isRunningAsService())) {
                              userResponse = messageBox.showMessage("CardNotOnFileToAttach", null, IMessageBox.YESNO); // MSG:31.32
                              // BUC
                              // 1.22.1
                            }
                        // blah
                        break;                  


    // ...

    default:
         displayMessage("InvalidLoyaltyCard");
          transaction.setValue(ITransactionHashtableWag.LOYALTY_MESSAGE_DISPLAYED,
              NOT_VALID_LOYALTY_CARD);
          userResponse = -1;
        break;
    }

请指教是否正确.. !!

3 个答案:

答案 0 :(得分:2)

而不是if else语句或switch case 我更喜欢使用键映射到委托。 您可以创建一个包含所有委托的hashmap,只需调用正确的委托即可。 见this StackOverflow question about delegating

这将为您提供干净的代码

PointersToMethods.get(key).call();

答案 1 :(得分:0)

有用吗?它的修改方式肯定看起来更清晰

答案 2 :(得分:0)

这两种方法并不相同。在第一种情况下,if (cardType == PSC_CARD_TYPE && PosHelper.isRunningAsService())然后代码转到默认的else子句,而在第二种情况下,它什么都不做(break;)。

ps:你没有运行单元测试来查看它是否按预期工作?