Java中switch case中使用startsWith()方法

问题描述 投票:0回答:2

我想在Java中的switch case中使用

startsWith()
,但它需要一个字符串而不是布尔值。我尝试为它创建一个包装函数并返回一个字符串,但我收到一条错误消息:
constant expected

我想在 switch case 中转换以下 if-else 块:

if (currCmd.equals(allProduct)) {
    warehouseObj.viewItems();

} else if (currCmd.startsWith(buyProduct)) { // todo
    String productId = currCmd.substring(4);
    long lProductId=Long.parseLong(productId);
    cartProcessor.addItem(currUser.getUserid(),lProductId,currCart,warehouseObj);

} else if (currCmd.equals(viewCart)) {
    cartProcessor.viewItems(currUser.getUserid(),currCart);

} else if (currCmd.equals(checkout)) {
    cartProcessor.checkOut(currUser.getUserid(),currCart);

} else if (currCmd.startsWith(delete)) { // todo
    String productId = currCmd.substring(7);
    long lProductId =Long.parseLong(productId);
    warehouseObj.deleteItem(lProductId, currUser.getIsAdmin());

} else if (currCmd.equals(addProduct)) {
    if (!currUser.getIsAdmin()) {
        System.out.println("PERMISSION DENIED");

    } else {
        // remove sc
        Scanner sc = new Scanner(System.in);
        addProduct2Warehouse(warehouseObj, sc);
    }
} else  {
    System.out.println("INVALID COMMAND!");
}
java switch-statement
2个回答
0
投票

假设

currCmd
是一个字符串,与
"buy $PRODUCT_ID"
相比,对于命令来说可能看起来像
"delete $PRODUCT_ID"
startsWith
,则可能值得从命令中提取“动词”并在
switch
中使用它(使用 Java 12+ 的较短箭头语法):

String cmd = currCmd.split(" ")[0];
switch (cmd) {
    case ALL_PRODUCT -> warehouseObj.viewItems();
    case BUY_PRODUCT -> {
        String productId = currCmd.substring(4);
        long lProductId = Long.parseLong(productId);
        cartProcessor.addItem(currUser.getUserid(), lProductId, currCart, warehouseObj);
    }
    case VIEW_CART -> cartProcessor.viewItems(currUser.getUserid(), currCart);
    case CHECKOUT -> cartProcessor.checkOut(currUser.getUserid(), currCart);
    case DELETE -> {
        String productId = currCmd.substring(7);
        long lProductId = Long.parseLong(productId);
        warehouseObj.deleteItem(lProductId, currUser.getIsAdmin());
    }
    case ADD_PRODUCT -> {
        if (!currUser.getIsAdmin()) {
            System.out.println("PERMISSION DENIED");
        } else {
            // remove sc
            Scanner sc = new Scanner(System.in);
            addProduct2Warehouse(warehouseObj, sc);
        }
    }
    default -> System.out.println("INVALID COMMAND!");
}

0
投票

假设 currCmd 是一个字符串并使用带有预览版的 java 14/15 或 16+,你可以这样做:

switch (currCmd) {
       case null -> System.out.println("INVALID COMMAND!");
       case "allProduct" -> warehouseObj.viewItems();
       case String s when s.startsWith("buyProduct") -> {
              String productId = currCmd.substring(4);
              long lProductId=Long.parseLong(productId);      
              cartProcessor.addItem(currUser.getUserid(),lProductId,currCart,warehouseObj);
            }
       case "viewCart" -> cartProcessor.viewItems(currUser.getUserid(),currCart);
       case "checkout" -> cartProcessor.checkOut(currUser.getUserid(),currCart);
       case String s when s.startsWith("delete") -> {
              String productId = currCmd.substring(7);
              long lProductId =Long.parseLong(productId);
              warehouseObj.deleteItem(lProductId, currUser.getIsAdmin());
            }
       case "addProduct" -> {
              if (!currUser.getIsAdmin()) {
                System.out.println("PERMISSION DENIED");

              } else {
                // remove sc
                Scanner sc = new Scanner(System.in);
                addProduct2Warehouse(warehouseObj, sc);
              }
            }
       default -> {
              System.out.println("INVALID COMMAND!");
       }

个人使用java 8到13我会实现如下:

switch (currCmd) {
            case "allProduct": {
                warehouseObj.viewItems();
            }
            case "viewCart": {
                cartProcessor.viewItems(currUser.getUserid(),currCart);
            }
            case "checkout": {
                cartProcessor.checkOut(currUser.getUserid(),currCart);
            }
            case "addProduct": {
                if (!currUser.getIsAdmin()) {
                    System.out.println("PERMISSION DENIED");
                } else {
                    // remove sc
                    Scanner sc = new Scanner(System.in);
                    addProduct2Warehouse(warehouseObj, sc);
                }
            }
            default: {
                if (currCmd.startsWith(buyProduct)) { // todo
                    String productId = currCmd.substring(4);
                    long lProductId=Long.parseLong(productId);
                    cartProcessor.addItem(currUser.getUserid(),lProductId,currCart,warehouseObj);
                } else if (currCmd.startsWith(delete)) { // todo
                    String productId = currCmd.substring(7);
                    long lProductId =Long.parseLong(productId);
                    warehouseObj.deleteItem(lProductId, currUser.getIsAdmin());
                } else  {
                    System.out.println("INVALID COMMAND!");
                }
            }
        }

希望它能帮助别人。

© www.soinside.com 2019 - 2024. All rights reserved.