package day06; import java.util.*; public class Demo1 { public static void main(String[] args) { String[] dishNames = {"红烧带鱼","鱼香肉丝","时令蔬菜"};//菜品名称 double[] prices = new double[] {38.0,20.0,10.0};//菜品价格 int[] praiseNums = new int[3];//点赞数 //存储订单信息 String[] names = new String[4];//保存订单人姓名 String[] dishMegs = new String[4];//保存菜品和份数 int[] times = new int[4];//保存送餐时间 String[] addresses = new String[4];//保存送餐地址 int[] states = new int[4];//保存订单状态;0表示已经预定,1表示已经完成 double[] sumPrieces = new double[4];//保存订单的总金额 Scanner input = new Scanner(System.in); int num = -1;//用户输入9时返回主菜单,否则退出系统 boolean isExit = false;//标志用户是否退出系统;true为退出系统 System.out.println("\n 欢迎使用“吃货联盟订餐系统”"); //循环,现实菜单,根据用户选择的数字执行相应功能 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("请选择:"); int choose = input.nextInt();//记录用户选择的功能 //根据用户输入的功能,执行相应的命令 switch (choose){ case 1: //我要订餐 System.out.println("***我要订餐***"); Boolean isAdd = false; for(int j =0;j<names.length;j++){ if(names[j]==null){ isAdd =true; System.out.print("请输入订单人姓名:"); String name =input.next(); //显示可以提供选择的菜单 System.out.println("序号"+"\t"+"菜名"+"\t"+"单价"+"\t"+"点赞数"); for(int i =0;i<dishNames.length;i++){ String price = prices[i] +"元"; String priaiseNum = (praiseNums[i] > 0? praiseNums[i]+"赞":"0"); System.out.println((i+1)+"\t" +dishNames[i]+"\t" +price+ "\t" +priaiseNum); } //用户点菜 System.out.print("请选择您要点的菜单编号:"); int chooseDish = input.nextInt(); System.out.print("请选择您需要的份数:"); int number =input.nextInt(); String dishMeg =dishNames[chooseDish -1]+""+number+"份"; double sumPrice =prices[chooseDish -1]*number; //餐费满50元,免送餐费5元 double deliCharge = (sumPrice>=50)?0:5; System.out.print("请输入送餐时间(送餐时间是10点到20点整点送餐):"); int time =input.nextInt(); while(time<10|| time>20){ System.out.print("您输入有误,请输入10-20点!"); time =input.nextInt(); } System.out.print("请输入送餐地址:"); String address =input.next(); //无需添加状态,默认是0,则为已经预订 System.out.println("订餐成功!"); System.out.println("您的订餐时间是:"+dishMeg); System.out.println("送餐时间:"+time+"点"); System.out.println("餐费:"+sumPrice+"元,送餐费"+deliCharge+"元,总计:"+(sumPrice+deliCharge)+"元。"); //添加数据 names[j] = name; dishMegs[j] = dishMeg; times[j] = time; addresses[j] = address; sumPrieces[j] = sumPrice; break; } } if(!isAdd){ System.out.println("对不起,您的餐袋已经满了!"); } break; case 2: //查看菜单 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){ String state = (states[i]==0?"已经预定":"已经完成"); String date = times[i]+"点"; String sumPrice = sumPrieces[i]+"元"; System.out.println((i+1)+"\t"+names[i]+"\t" + dishMegs[i]+"\t" +date+"\t" +addresses[i]+"" + "\t" +sumPrice+"\t" +state); } } break; case 3: //签收订单 System.out.println("***签收订单***"); boolean isSignFind = false;//找到要签收的订单 System.out.print("请选择要签收的订单序号:"); int signOrderId = input.nextInt(); for(int i = 0;i<names.length;i++){ //订单状态为预订,序号为用户输入的订单号减1,可签收 //订单状态为完成,序号为用户输入的订单号减1,不可签收 if(names[i]!=null&& states[i]==0&&signOrderId==i+1){ states[i] =1; System.out.println("订单已经签收成功!"); isSignFind = true; }else if(names[i]!=null && states[i]==1 && signOrderId==i+1){ System.out.println("你选择的订单已经完成签收,不能再次签收!"); isSignFind = true; } } //未找到的订单序号,不能签收 if(!isSignFind){ System.out.println("您选择的订单不存在!"); } break; case 4: //删除订单 System.out.println("***删除订单***"); boolean isDelFind = false; System.out.print("请输入要删除的订单序号:"); int delId = input.nextInt(); for(int i =0;i<names.length;i++){ //状态值为已完成,序号值为用户输入的序号减1,可删除 //状态值为已经预定,序号值为用户输入的序号减1,不可删除 if(names[i]!=null&&states[i]==1&&delId==i+1){ isDelFind=true; //执行删除操作,删除位置后的元素一次前移 for(int j=delId-1;j<names.length-1;j++){ names[j] = names[j+1]; dishMegs[j] = dishMegs[j+1]; times[j] = times[j+1]; addresses[j] = addresses[j+1]; states[j] = states[j+1]; sumPrieces[j] = sumPrieces[j+1]; } //最后一位清空 int endIndex = names.length-1; names[endIndex] = null; dishMegs[endIndex] = null; times[endIndex] = 0; addresses[endIndex] = null; states[endIndex] = 0; sumPrieces[endIndex] = 0; System.out.println("删除订单成功!"); break; } } //未找到订单序号,不能删除! if(!isDelFind){ System.out.println("您要删除的订单不存在!"); } break; case 5: //我要点赞 System.out.println("***我要点赞***"); //显示菜单名 System.out.println("序号"+"\t"+"菜名"+"\t"+"单价"); for(int i =0;i<dishNames.length;i++){ String price = prices[i] + "元"; String priaiseNum = (praiseNums[i])>0?praiseNums[i] +"赞":""; System.out.println((i+1)+"\t" + dishNames[i]+"\t" +price+"\t" +priaiseNum); } System.out.print("请选择您要点赞的菜品序号:"); int priaiseNum = input.nextInt(); praiseNums[priaiseNum-1]++;//点赞数加1 System.out.println("点赞成功!"); break; case 6: //退出系统 isExit =true;
System.out.println("欢迎下次再使用!!!祝你贪吃不胖!!!"); break; default: isExit=true; break; } if(!isExit){ System.out.print("输入0返回:"); num =input.nextInt(); }else { break; } }while (num == 0); } }