代码:吃货联盟订餐系统

package Temporary.cn.kgc;

import java.util.Scanner;

public class Eater_Alliance {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //初始化三个数组存储菜品,价格,点赞数
        String[] dishNames = {"苦瓜鸡蛋", "红烧排骨", "辣子鸡丁", "石锅鱼", "麻婆豆腐"};
        int[] prices = {15, 30, 35, 90, 20};
        int[] praiseNums = new int[5];

        //初始化6个数组用于存储订单信息,这个后面可以用集合做
        String[] names = new String[5];//保存订单人姓名
        String[] dishMegs = new String[5];//保存所选菜名
        int[] dishNums = new int[5];//保存所选菜品份数
        int[] times = new int[5];//保存送餐时间
        String[] addresses = new String[5];//保存送餐地址
        int[] states = new int[5];//保存订单状态:0表示已预定未签收,1表示已签收
        double[] sumPrices = new double[5];//保存订单的总净额

        //初始化2条订单信息
        //第一条
        names[0] = "王大锤";
        dishMegs[0] = "辣子鸡丁";
        dishNums[0] = 2;
        times[0] = 12;
        addresses[0] = "花园南路7号";
        states[0] = 0;
        sumPrices[0] = 70;

        //第一条
        names[1] = "王大柱";
        dishMegs[1] = "麻婆豆腐";
        dishNums[1] = 1;
        times[1] = 19;
        addresses[1] = "建设路2号";
        states[1] = 1;
        sumPrices[1] = 25;


        System.out.println("欢迎使用吃货联盟订餐系统");
        int num = 1;//用户输入0返回主菜单退出系统
        boolean isExit = false;//表示用户是否退出系统,false为不退出,true为退出

        do {//循环控制用户输入,选择并执行
            System.out.println("*******************************");
            System.out.println("1.我要订餐");
            System.out.println("2.查看餐袋");
            System.out.println("3.签收订单");
            System.out.println("4.删除订单");
            System.out.println("5.我要点赞");
            System.out.println("6.退出系统");
            System.out.println("*******************************");
            System.out.println("请选择:");
            num = sc.nextInt();//用户输入要进行的选择

            //使用switch结构建立选择的内容
            switch (num) {
                case 1://我要订餐
                    System.out.println("*******我要订餐******");
                    for (int i = 0; i < names.length; i++) {
                        boolean isAdd = false;//boolean变量表示订单是否满了的结果,满了记为false,不能订餐;不满记为true,可以订餐
                        if (names[i] == null) {//为空,订单未满
                            isAdd = false;//true可以点餐
                            //开始订餐
                            System.out.print("请输入订餐人的姓名:");
                            String name = sc.next();
                            //循环显示菜品信息
                            System.out.println("<序号>" + "\t" + "<菜名>" + "\t"  + "  <单价>" + "\t" + "<点赞数>");
                            for (int j = 0;j<dishNames.length; j++) {
                                String praiseNum = (praiseNums[i] == 0) ? "":praiseNums[i] + "赞" ;
                                System.out.println("  "+(j + 1) + "  \t" + dishNames[j]+ "\t\t" + prices[j] + "\t\t" + praiseNum);
                            }
                            System.out.println("请选择您要点的菜品编号:");
                            int chooseDish = sc.nextInt();//菜品编号
                            String dishMeg = dishNames[chooseDish - 1];//disMeg为菜名
                            System.out.println("请选择您需要的份数:");
                            int number = sc.nextInt();
                            System.out.println("请输入送餐时间(送餐时间是10-20点之间整点送餐):");
                            int time = sc.nextInt();
                            while (time < 10 || time > 20) {
                                System.out.println("您输入的格式不对或是超出了配送时间哦,请重新进行选择");
                                time = sc.nextInt();
                            }
                            System.out.println("请输入送餐地址:");
                            String address = sc.next();
                            //默认可以预定,不用写订单状态
                            System.out.println("订餐成功!");
                            System.out.println("您定的是" + dishNames[chooseDish - 1] + number + "份");
                            System.out.println("送餐时间:" + time + "点");
                            double sumPrice = prices[chooseDish - 1] * number;//计算餐费
                            double sendMoney = (sumPrice > 50) ? 0 : 5;//计算配送费
                            System.out.println("餐费" + sumPrice + "元" + ",送餐费" + sendMoney + "元," + "总计:" + (sumPrice + sendMoney) + "元");

                            //添加数据
                            names[i] = name;//订餐人
                            dishMegs[i] = dishMeg;//餐品名称
                            times[i] = time;//送餐时间
                            dishNums[i] = number;//餐品数量
                            addresses[i] = address;//送餐地址
                            sumPrices[i] = sumPrice + sendMoney;//餐品总价Mega
                            break;
                        }
                        if (!isAdd) {//isAdd取反不是true,而是false
                            System.out.println("对不起,您的餐袋已满!");
                        }
                    }
                    break;
                case 2://查看餐袋
                    System.out.println("*******查看餐袋******");
                    System.out.println("序号\t订餐人\t\t餐品名称\t\t餐品数量\t\t送餐时间\t\t送餐地址\t\t\t总金额\t\t订单状态");
                    for (int i = 0; i < names.length; i++) {
                        if (names[i] != null) {
                            String state = (states[i] == 0) ? "已预定" : "已完成";
                            System.out.println((i + 1) + "\t" + names[i] +"\t\t" + dishMegs[i] +"\t\t\t" + dishNums[i] +"\t\t" + times[i] +"\t\t\t" + addresses[i] +"\t\t\t" + sumPrices[i] +"\t\t\t" + states[i]);
                        }
                    }
                    break;
                case 3://签收订单
                    System.out.println("*******签收订单******");
                    boolean isSignFind = false;
                    System.out.println("请选择要签收的订单编号:");
                    int sign = sc.nextInt();
                    for (int i = 0; i < names.length; i++) {
                        if (names[i] != null && states[i] == 1 && sign == i + 1) {
                            System.out.println("您要选择的订单已签收,不能再次签收!");
                            isSignFind = true;//标记已找到的订单
                        }else if (names[i] != null&&states[i] == 0&& sign == i + 1){
                            System.out.println("订单已签收,感谢您使用本程序!");
                            states[i]=1;
                        }
                    }
                    if (!isSignFind) {
                        System.out.println("您选择的订单不存在!");
                    }
                    break;
                case 4://删除订单
                    System.out.println("*******删除订单******");
                    boolean isDelExi = false;//判断订单是否存在,默认false不存在
                    System.out.println("请输入要删除的订单序号:");
                    int delID = sc.nextInt();
                    for (int i = 0; i < names.length-1; i++) {
                        //根据订单存在状态判断能否删除,只有存在才能删除
                        if (names[i] != null && states[i] == 1 && delID == i + 1) {//订单存在且状态为已完成
                            isDelExi = true;//判断为true表示订单存在,可以删除
                            for (int j = i; j < names.length; j++) {//将要删除的订单被后面的订单替换,后面的依次向前进一位
                                names[j] = names[j + 1];
                                dishMegs[j] = dishMegs[j + 1];
                                dishNums[j] = dishNums[j + 1];
                                times[j] = times[j + 1];
                                addresses[j] = addresses[j + 1];
                                states[j] = states[j + 1];
                                sumPrices[j] = sumPrices[j + 1];
                            }
                            //将数组最后一维的元素清空
                            names[names.length - 1] = null;
                            dishMegs[names.length - 1] = null;
                            dishNames[names.length - 1] = null;
                            dishNums[names.length - 1] = 0;
                            times[names.length - 1] = 0;
                            addresses[names.length - 1] = null;
                            states[names.length - 1] = 0;
                            sumPrices[names.length - 1] = 0;
                            System.out.println("删除订单成功!");
                            break;
                        } else if (names[i] != null && states[i] == 0 && delID == i + 1) {//订单存在但是state为0,即已预定未完成状态,
                            isDelExi = true;//这句话其实没啥用
                            System.out.println("您选择的订单未签收,不能删除!");
                            break;
                        }
                    }
                    //未找到的订单,不能删除
                    if (!isDelExi) {//取反表示订单不存在
                        System.out.println("您要删除的订单不存在!");
                    }
                    break;
                case 5://我要点赞
                    System.out.println("*******我要点赞******");
                    //循环显示菜品信息
                    System.out.println("序号\t菜名\t单价\t点赞数");
                    for (int i = 0; i < dishNames.length; i++) {
                        String praiseNum = (praiseNums[i] == 0) ? "":praiseNums[i] + "赞" ;
                        System.out.println((i + 1) + "\t" + dishNames[i] + "\t" + prices[i] +"元\t"+praiseNum);
                    }

                    //点赞
                    System.out.println("请选择您要点赞的菜品编号:");
                    int number = sc.nextInt();
                    while(number<1||number>dishNames.length){
                        System.out.println("本店没有这个菜品,无法点赞,请重新输入一个菜品编号:");
                        number=sc.nextInt();
                    }
                    praiseNums[number-1]++;
                    System.out.println("点赞成功");
                    break;
                case 6://退出系统
                    isExit = true;
                    break;
                default://退出系统
                    isExit = true;
                    break;//上面这7个break是跳出switch循环
            }
            if (!isExit) {
                System.out.println("输入0返回:");
                num = sc.nextInt();
            } else {
                break;//这个break是跳出do-while循环
            }
        } while(num == 0);
        System.out.println("谢谢回顾,欢迎您再次使用~");
        sc.close();//关闭输入的流,释放内存
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值