0

可能重复:
过度使用 If else 语句

我有下面一段过度使用 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 语句,所以我在下面的表格中做了一点改动

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;
    }

请指教是否正确..!!

4

3 回答 3

2

而不是 if else 语句或 switch case,我更喜欢使用键到委托的映射。您可以创建一个包含所有代表的哈希图,然后调用正确的代表。请参阅有关委派的 StackOverflow 问题

这会给你一个干净的代码

PointersToMethods.get(key).call();

于 2012-05-23T17:46:46.143 回答
0

它有效吗?你修改它的方式肯定看起来更干净

于 2012-05-23T17:36:03.763 回答
0

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

ps:您是否没有要运行的单元测试以查看它是否按预期工作?

于 2012-05-23T17:36:36.673 回答