package com.kgc.day08;

import java.util.Scanner;

public class FoodOrder {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        //数据初始化
        String[] names = new String[4];//顾客姓名
        String[] dishName = {"红烧带鱼","鱼香肉丝","时令鲜蔬"};//菜名
        String[] order = new String[4];//下单内容
        String[] addresses = new String[4];//送餐地址
        int[] praiseNum = new int[3];//点赞数
        int[] time = new int[4];//送餐时间
        int[] dishNum = new int[4];//点菜份数
        int[] state = new int[4];//订单状态,0表示已定,1表示已送出
        double[] dishPrice = {30.0,25.0,15.0};//菜品价格
        double[] sumPrice = new double[4];//点菜总价

        //初始下单信息
        names[0]="张三";
        addresses[0]="安德门小区";
        time[0]=14;
        order[0]="红烧带鱼 1份";
        state[0]=1;
        dishNum[0]=1;
        sumPrice[0]=30.0;

        int num=1;//num为0返回系统
        boolean exit = false;//是否退出,false为不退出,true为退出

        do{
            System.out.println("*********************");
            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.print("请选择:");
            int choose = input.nextInt();
            //不正确的数字重新输入
            while (choose<1 || choose> 6){
                System.out.print("您的输入有误,请重新输入:");
                choose = input.nextInt();
            }

            switch (choose){
                //实现我要订餐功能
                case 1:
                    System.out.println("*********************");
                    System.out.println("已进入订餐系统");
                    boolean orderFlag = false;//是否能够点餐,true为能点餐
                    for(int i=0;i< names.length;i++){
                        if(names[i]==null){
                            orderFlag=true;
                            System.out.print("请输入订餐姓名:");
                            names[i]= input.next();
                            System.out.println("序号\t"+"菜名\t\t"+"单价\t"+"点赞数");
                            for(int j=0;j< dishName.length;j++){
                                System.out.println((j+1)+"\t"+dishName[j]+"\t"+dishPrice[j]+"\t"+praiseNum[j]);
                            }
                            System.out.print("请输入您想点的菜的序号:");
                            int dishOrder = input.nextInt();
                            while (dishOrder<1 || dishOrder>3) {
                                System.out.print("输入的序号不正确,请重新输入:");
                                dishOrder = input.nextInt();
                            }
                            System.out.print("请输入您想点的份数(整数):");
                            dishNum[i]= input.nextInt();
                            //计算总价
                            //满50就免除配送费
                            double sumDishPrice = dishNum[i]*dishPrice[dishOrder-1];//总菜费
                            double deliCharge=((sumDishPrice>=50) ? 0 : 5);//配送费
                            sumPrice[i]=sumDishPrice+deliCharge;//总价格
                            //输入订单信息
                            order[i]=dishName[dishOrder-1]+" "+dishNum[i]+"份";

                            System.out.print("请输入您的送餐地址:");
                            addresses[i]= input.next();
                            System.out.print("请输入您的送餐时间(10点至20点,请输入整数):");
                            time[i] = input.nextInt();
                            while(time[i]<10 || time[i]>20){
                                System.out.print("不在配送时间段,请重新输入:");
                                time[i] = input.nextInt();
                            }
                            System.out.println("订餐成功!!!");
                            System.out.println("您点的是"+order[i]+",餐费"+sumDishPrice+"元,配送费"+deliCharge+"元,"+"共"+sumPrice[i]+"元");
                            System.out.println("送餐时间:"+time[i]+"点");
                            break;
                        }
                    }
                    if(!orderFlag){
                        System.out.println("订单已满,暂时无法点单");
                    }
                    break;

                //实现查看餐单功能
                case 2:
                    System.out.println("*********************");
                    System.out.println("已进入餐单系统");
                    System.out.println("序号\t订餐人\t餐品信息\t\t送餐时间\t送餐地址\t\t总金额\t订单状态");
                    for(int i=0;i< names.length;i++) {
                        if (names[i] != null) {
                            System.out.println((i + 1) + "\t" + names[i] + "\t\t" + order[i] + "\t" + time[i] + "\t\t" + addresses[i] + "\t" + sumPrice[i] + "\t" + ((state[i] == 0) ? "已预定" : "已完成"));
                        }
                    }
                    break;

                //实现签收订单功能
                case 3:
                    System.out.println("*********************");
                    System.out.println("已进入签收订单系统");
                    System.out.print("请输入你想要签收的订单序号:");
                    int signNum = input.nextInt();
                    boolean signFlag=false;//签收Flag,为false说明未找到
                    for(int i=0;i< names.length;i++){
                        if (names[i] != null && state[i] == 0 && signNum == (i + 1)) {
                            state[i]=1;
                            signFlag=true;//为true说明找到了
                            System.out.println("已签收"+(i+1)+"号订单");
                        }else if(state[i]==1 && signNum==(i+1)) {
                            System.out.println("该订单已经签收过了!不能再次签收");
                            signFlag=true;
                        }
                    }
                    if(!signFlag){
                        System.out.println("未找到该订单,无法签收,请确认您的订单序号输入正确");
                    }
                    break;

                //实现删除订单功能
                //不论要删除的是哪一位的订单,都只需要把指定序列后的订单前移一位,再将最后一位的订单清空就行
                case 4:
                    System.out.println("*********************");
                    System.out.println("已进入删除订单系统");
                    System.out.print("请输入你想要删除的订单序号:");
                    int delNum = input.nextInt();
                    boolean delFlag=false;//删除Flag,为true说明找到要删除的订单
                    for(int i=0;i< names.length;i++){
                        if(names[i] != null && state[i] == 1 && delNum == (i + 1)){
                            delFlag=true;
                            for(int j=delNum-1;j< names.length-1;j++){
                                names[j]=names[j+1];
                                addresses[j]=addresses[j+1];
                                time[j]=time[j+1];
                                order[j]=order[j+1];
                                state[j]=state[j+1];
                                dishNum[j]=dishNum[j+1];
                                sumPrice[j]=sumPrice[j+1];
                            }
                            names[names.length-1]=null;
                            addresses[names.length-1]=null;
                            time[names.length-1]=0;
                            order[names.length-1]=null;
                            state[names.length-1]=0;
                            dishNum[names.length-1]=0;
                            sumPrice[names.length-1]=0;
                            System.out.println("删除订单成功");
                        }else if(names[i] != null && state[i] == 0 && delNum == (i + 1)){
                            delFlag=true;
                            System.out.println("该订单尚未签收,不可删除");
                        }
                    }
                    if(!delFlag){
                        System.out.println("未找到该订单,无法删除,请确认您的订单序号输入正确");
                    }
                    break;

                //实现我要点赞功能
                case 5:
                    System.out.println("*********************");
                    System.out.println("已进入我要点赞系统");
                    System.out.println("序号\t"+"菜名\t\t"+"单价\t"+"点赞数");
                    for(int j=0;j< dishName.length;j++){
                        System.out.println((j+1)+"\t"+dishName[j]+"\t"+dishPrice[j]+"\t"+praiseNum[j]);
                    }
                    System.out.print("请输入你想要点赞的菜单序号:");
                    int praise = input.nextInt();//想点赞的菜单序号
                    while (praise<1 || praise> dishName.length){
                        System.out.print("您输入的菜单号有误,请重新输入:");
                        praise = input.nextInt();
                    }
                    praiseNum[praise-1]++;
                    System.out.println("点赞成功,"+praise+"号"+dishName[praise-1]+"现在的点赞数为:"+praiseNum[praise-1]);
                    break;

                //实现退出系统功能
                case 6:
                    exit=true;
                    System.out.println("成功退出系统!");
                    break;
            }
            if(!exit){
                System.out.println("请输入0返回系统(其他数字退出系统):");
                num = input.nextInt();
                if(num!=0){
                    System.out.println("成功退出系统!");
                }
            }else {
                break;
            }
        }while(num==0);

    }
}