二维数组实现点菜

private static void demo4() {
    String[][] menu = {
            {"糖醋带鱼", "12", "加辣", "加葱"},
            {"麻婆豆腐", "15", "加辣"},
            {"南昌拌粉", "13", "加葱"},
            {"农家小炒肉", "17"},
            {"梅菜扣肉", "23", "加辣", "加葱"},
    };
    boolean flag = true;String[][] desert=new String[0][];
    while (flag) {
        System.out.println("欢迎使用**点餐系统");
        System.out.println("1、显示菜单");
        System.out.println("2、点餐");
        System.out.println("3、显示购物车");
        System.out.println("4、结账");
        System.out.println("0、退出系统");
        int choice = InputUtil.intRegex("请选择:", "[0-4]{1}");
        String inputDesert;
        switch (choice) {
            case 1:
                System.out.println("-->显示菜单");
                for (String[] strings : menu) {
                    System.out.println(Arrays.toString(strings));
                }
                break;
            case 2:
                System.out.println("-->点餐");
                String answer="y";int index=0;
                while ("y".equals(answer)) {
                    System.out.println("请输入你想点的菜品名称:");
                    inputDesert = input.next();
                    int have=isHave(inputDesert,menu);
                    if (have==-1){
                        System.out.println("不好意思,没有这道菜,请重新输入");
                    }
                    else {
                        desert=Arrays.copyOf(desert,desert.length+1);
                        desert[index++]=menu[have];
                        System.out.println("请问是否继续点菜?(y/n)");
                        answer=input.next();
                    }
                }
                break;
            case 3:
                System.out.println("-->显示购物车");
                System.out.println("您点的菜为:");
                for (String[] strings : desert) {
                    System.out.println(Arrays.toString(strings));
                }
                break;
            case 4:
                System.out.println("-->结账");
                int fee=0;
                for (String[] strings : desert) {
                    fee+=Integer.valueOf(strings[1]);
                }
                System.out.println("您需要支付的总费用为:"+fee);
                break;
            case 0:
                System.out.println("谢谢使用,系统已退出");
                flag = false;
                break;
        }
    }
}
 private static int isHave(String name,String[][] ags) {
        int index=0;
        for (String[] ag : ags) {
            for (String s : ag) {
                if(s.equals(name))return index;
            }
            index++;
        }
        return -1;
    }

posted @ 2022-11-15 17:50  Liku007  阅读(19)  评论(0编辑  收藏  举报