重构switch语句的问题

时间:2017-06-07 08:31:30

标签: java for-loop if-statement switch-statement

以下是执行切换的方法(注意切换语句可正常工作并符合要求)

public void performChecks() {
        priceInput = priceReader.nextDouble();
       loop: while(true){
           nameInput = nameReader.nextLine();
            switch(nameInput){
                case "A1": Cola colaItem = new Cola();
                                    if(colaItem.checkPrice(priceInput)){
                                            System.out.println("You ordered " + colaItem.getName()+ ", here is it");
                                    } else {
                                            System.out.println("Sorry, the amount is not enough for this purchase. Please add more money and try again");
                                    }
                                    break loop;

                case "B2": Chips chipsItem = new Chips();
                                    if(chipsItem.checkPrice(priceInput)){
                                        System.out.println("You ordered " + chipsItem.getName()+ ", here is it");
                                    } else {
                                        System.out.println("Sorry, the amount is not enough for this purchase. Please add more money and try again");
                                    }
                                    break loop;

                case "C3": Crackers crackerItem = new Crackers();
                                    if(crackerItem.checkPrice(priceInput)){
                                        System.out.println("You ordered " + crackerItem.getName()+ ", here is it");
                                    } else {
                                        System.out.println("Sorry, the amount is not enough for this purchase. Please add more money and try again");
                                    }
                                        break loop;
                default:
                    System.out.println("Sorry, we don't have item with such a code");
            }
        }
     }

现在我开始进行一些重构并将方法放在项目的超类中(Cola,Chips和Crackers),它看起来像这样:

public void performChecks(){
    inputPrice = priceReader.nextDouble();
    inputCode = codeReader.nextLine();
    initializeItems();
    for(int i = 0; i < items.length; i++){
        if(items[i].checkCode(inputCode)){
                if(items[i].checkPrice(inputPrice)){
                    System.out.println("You ordered " + items[i].getName() + " here it is");
                    break;
                } else {
                    System.out.println("Sorry, the amount is not enough for this purchase. Please add more money and try again");
                    break;
                }
        } else if(!items[i].checkCode(inputCode)){
            continue;
        } else {
            System.out.println("Sorry, we don't have item with such a code");
            inputCode = codeReader.nextLine();
        }
    }
}

问题如下:当我输入正确/错误的价格和不正确的商品代码时,我应该收到&#34;抱歉,我们没有这样的商品代码&#34;消息和再次输入项目代码的选项。我已经没有关于如何在

中实现此选项的想法
else if(!items[i].checkCode(inputCode)){
                continue;

由于我非常确定,它只会卡在那里并且不会返回任何内容(对于错误的商品代码)。

2 个答案:

答案 0 :(得分:0)

我不是很确定,但我想你可以这样做:

 mankind.stream()
        .map(Individual::getId)
        .map(Borg::new)
        .collect(Collectors.toList());

答案 1 :(得分:0)

您想要实现的目标可以通过一个简单的&#34;找到&#34;布尔值。像这样:

[RegularExpression(@"^([0-9]{2,3})[-. ]?[0-9]{3}[-. ]?([0-9]{4,6})$"