1、代码:
import java.util.Scanner;
public class Order_improvement {
/**基本信息:
序号 订餐人 餐品信息 送餐时间 送餐地址 总金额 订单状态
1 张晴 红烧带鱼 2份 13点 苏苑大厦12楼 76.0元 已完成
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//------第一部分:基本信息准备------
int notice = -1;//实现每次选择结束后,返回主菜单的输入“0”返回(思考:如用int类型,会有什么影响)
//定义数组接收信息
String[] names = new String[4];//接收姓名
String[] meals = new String[4];//接收菜品
int[] quantities = new int[4];//接收份数
int[] times = new int[4];//接收送餐时间
String[] addresses = new String[4];//接收送餐地址
double[] totalPrices = new double[4];//接收订单总金额
int[] state = new int[4];//接收订单状态。1表示已预定,0表示已完成
int[] praiseNumbers = new int[6];//点赞数,在每个菜品名后显示点赞数。
//并且写入已存在的前两个信息
names[0] = "张晴";
meals[0] = "红烧带鱼";
quantities[0] = 2;
times[0] = 13;
addresses[0] = "苏苑大厦12楼";
totalPrices[0] = 76.0;
state[0] = 1;
names[1] = "张锐";
meals[1] = "鱼香肉丝";
quantities[1] = 2;
times[1] = 10;
addresses[1] = "苏苑大厦10楼";
totalPrices[1] = 60.0;
state[1] = 0;
//可选菜单
String[] mealList = {"红烧带鱼","鱼香肉丝","蚂蚁上树","红烧肉","素三鲜","青菜鸡蛋汤"};
// int[] mealNum = {1,2,3,4,5,6};
//价格
int[] mealPrices = {38,30,20,35,20,10};
//------第二部分:选择进入,并且实现可循环操作------
//外部do-while循环,内部switch选择
do{
//主菜单界面显示;
System.out.println("\n\t欢迎进入吃货联盟系统");
System.out.println("*****************************");
System.out.println("1、我要订餐\n2、查看订单\n3、签收订单\n4、删除订单\n5、我要点赞\n6、退出系统");
System.out.println("*****************************");
System.out.println("请选择:");
boolean isAdd = false;//--判断用户是否可以订餐的标记--
boolean isSigned = false;//--是否已签收--
boolean isDelFind = false;
int choice = sc.nextInt();
boolean isExit = false;//是否退出系统
switch(choice){
//---功能1:我要订餐---
//写入订单信息并输出订单信息。
//注意先判断是否还有写入空间,if选择结构。
case 1:
//先看下是否还可点,即names数组内是否还有空值//--这里是我遇到的第一个卡顿的地方--
//先挨个遍历,找到第一个空值的地方
for(int j=0;j<names.length;j++){
if(names[j]==null){
isAdd = true;
//显示菜品及价格
System.out.println("*****我要订餐*****");
System.out.println("序号\t菜名\t单价(元)");
for(int i=0;i<mealList.length;i++){
System.out.println((i+1)+"\t"+mealList[i]+"\t"+mealPrices[i]);
}
//接收用户输入的姓名、菜品、份数、送餐时间、送餐地址信息
System.out.println("请输入您的姓名:");
names[j] = sc.next();
System.out.println("请输入您要点的菜的序号:");
int mealNum = sc.nextInt();
meals[j] = mealList[mealNum-1];
System.out.println("请输入您所要点的菜的份数:");
quantities[j] = sc.nextInt();
//判断时间是否符合要求-----此处有误
System.out.println("请输入您需要的送餐时间(请选择10点到22点之间的整数):");
times[j] = sc.nextInt();
while(times[j]<10||times[j]>22){
System.out.println("您输入的时间有误,请输入10到22之间的整数");
times[j] = sc.nextInt();
}
System.out.println("请输入送餐地址:");
addresses[j] = sc.next();
double prices = mealPrices[mealNum-1]*quantities[j];//未算配送费的价格
//判断菜品金额是否可减免配送费
int delCharge = (prices>=50) ? 0 : 6;
totalPrices[j] = prices+delCharge;
//输出订单信息:
System.out.println("您的订单信息为:");
System.out.println(names[j]+" "+meals[j]+" "+quantities[j]+"份"+",送餐时间"+times[j]
+",送餐地址"+addresses[j]+",配送费是"+delCharge+"元,"+",总金额"+totalPrices[j]+"元");
break;
}//注意:判断餐带是否已满的外围,不在此处
}
//--容易出错的地方:这里是餐袋是否已满if判断的外围--
if(!isAdd){
System.out.println("您的购物袋已满。");
}
break;
//---功能2:查看餐袋---
//for遍历
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&&state[i]==1){
System.out.println((i+1)+"\t"+names[i]+"\t"+meals[i]+" "+quantities[i]+"份"+
"\t"+times[i]+"\t"+addresses[i]+"\t"+totalPrices[i]+"\t"+"已完成");
}else if(names[i]!=null&&state[i]==0){
System.out.println((i+1)+"\t"+names[i]+"\t"+meals[i]+" "+quantities[i]+"份"+
"\t"+times[i]+"\t"+addresses[i]+"\t"+totalPrices[i]+"\t"+"已预定");
}
}
break;
//---功能3:签收订单---
case 3:
System.out.println("*****签收订单*****");
System.out.println("请选择您要签收的订单号:");
int isSignedId = sc.nextInt();
for(int i=0;i<names.length;i++){
if(names[i]!=null&&i==(isSignedId-1)&&state[isSignedId-1]==0){
//--思考:如果是if(names[i]!=null&&state[isSignedId-1]==1),会发生什么--
state[isSignedId-1] = 1;
System.out.println("您的订单已签收。");
isSigned = true;
}else if(names[i]!=null&&i==(isSignedId-1)&&state[isSignedId-1]==1){
System.out.println("您的订单已签收,无需重新签收。");
isSigned = true;
}
}
if(!isSigned){
System.out.println("您输入的订单号不存在");
}
break;
//功能4:删除订单
//注意订单状态,if选择结构。先输入订单号,再进行判断如果已完成则删除,如为已预定状态,提示先签收再删除
case 4:
System.out.println("*****删除订单*****");
System.out.println("请选择您要删除的订单号:");
int delId = sc.nextInt();
for(int i=0;i<names.length;i++){
if(names[i]!=null&&i==(delId-1)&&state[delId-1]==0){
System.out.println("您的订单尚未签收,请先确认签收。");
isDelFind = true;
}else if(names[i]!=null&&i==(delId-1)&&state[delId-1]==1){
System.out.println("确认删除该订单吗(y/n)");
String delConfirm = sc.next();
if(delConfirm.equals("y")){
//如何删除,
//分两步,先置空,再把后面的订单号补上来
//考虑到如果delId是最后一条,如果直接使用覆盖会覆盖不到。故先置空很有必要
names[delId-1] = null;
meals[delId-1] = null;
quantities[delId-1] = 0;
times[delId-1] = delId-1;
addresses[delId-1] = null;
totalPrices[delId-1] = 0;
state[delId-1] = 0;
for(int j=delId-1;j<names.length-1;j++){
names[j] = names[j+1];
meals[j] = meals[j+1];
quantities[j] = quantities[j+1];
times[j] = times[j+1];
addresses[j] = addresses[j+1];
totalPrices[j] = totalPrices[j+1];
state[j] = state[j+1];
}
System.out.println("您的订单已删除。");
isDelFind = true;
}
}
}
if(!isDelFind){
System.out.println("您输入的订单号不存在。");
}
break;
case 5:
System.out.println("*****我要点赞*****");
//遍历一次点赞数
System.out.println("序号\t菜名\t单价(元)\t点赞数");
for(int i=0;i<mealList.length;i++){
String praiseMeg = praiseNumbers[i]>0 ? praiseNumbers[i]+"赞" : "";
System.out.println((i+1)+"\t"+mealList[i]+"\t"+mealPrices[i]+"\t"+praiseMeg);
}
//输入序号菜品序号点赞
System.out.println("请输入要点赞的菜品序号:");
int mealNum = sc.nextInt();
if(mealNum>=1&&mealNum<=mealList.length){
praiseNumbers[mealNum-1]++;
System.out.println("点赞成功。");
System.out.println("序号\t菜名\t单价(元)\t点赞数");
for(int i=0;i<mealList.length;i++){
String praiseMeg = praiseNumbers[i]>0 ? praiseNumbers[i]+"赞" : "";
System.out.println((i+1)+"\t"+mealList[i]+"\t"+mealPrices[i]+"\t"+praiseMeg);
}
}else{
System.out.println("您输入的菜品序号不存在。");
}
break;
case 6:
System.out.println("系统退出");
isExit = true;
break;
default:
isExit = true;
break;
}//switch选择结构的外围
if(!isExit){
System.out.println("输入0返回主菜单");
notice = sc.nextInt();
}else{
break;//跳出do-while循环
}
}while(notice==0);
System.out.println("谢谢使用,欢迎下次光临!");
}
}
2、动图演示: