实现功能:
使用选择结构,循环结构,数组的知识实现一个外卖订餐系统
运行结果
import java.util.Scanner;
public class OrderingMsg {
public static void main(String[] args) {
String [] names=new String[4];//订餐人 餐品信息 送餐时间 送餐地址 总金额 订单状态
String []dishes=new String[4];//餐品信息
int []times=new int[4];//送餐时间
String []addresses=new String[4];//送餐地址
double []sumPrices=new double[4];//总金额
int []states=new int[4];//订单预定状态信息
int []praiseNums=new int[4];//点赞
String dishNames[]={"红烧茄子","红烧排骨","鱼香肉丝"};
double price[]={24.0,36.0,32};
int num=0;
double scPrice=6.0;
boolean isAdd=false;
boolean isSignFind=false;
boolean isDelFind=false;
boolean isExit=false;
Scanner sc = new Scanner(System.in);
System.out.println("欢迎使用外卖订餐系统");
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 = sc.nextInt();
switch (choose) {
case 1:
System.out.println("****我要订餐****");
for (int i = 0; i < names.length; i++) {
if (names[i] == null) {
isAdd = true;
System.out.println("请输入订餐人姓名:");
String name = sc.next();
System.out.println("序号\t菜品\t\t单价");
for (int j = 0; j < dishNames.length; j++) {
System.out.println((j + 1) + "\t\t" + dishNames[j] + "\t\t" + price[j] + "元");
}
System.out.print("请选择你要点的菜品编号:");
int id = sc.nextInt();
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("您定的是:");
System.out.println(dishNames[id - 1] + "\t" + number + "份");
System.out.println("送餐时间:" + time + "点");
System.out.println("餐费:" + price[id - 1] * number + "元,送餐费" + scPrice + "元,送餐地址:" + address + ",总计:" + (price[id - 1] * number + scPrice));
//添加数据
names[i] = name;
dishes[i] = dishNames[id - 1];
times[i] = time;
addresses[i] = address;
sumPrices[i] = (price[id - 1] * number + scPrice);
break;
}
}
if (!isAdd) {
System.out.println("餐袋已满,不能添加订单!");
}
break;
case 2:
System.out.println("****查看餐袋****");
System.out.println("序号\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\t" + names[i] +"\t\t"+ dishes[i] + "\t\t" + times[i] + "\t\t" + addresses[i] + "\t\t" + sumPrices[i] + "元" + "\t\t" + state);
}
}
break;
case 3:
System.out.println("****签收订单****");
System.out.println("请选择要签收的订单序号:");
int ddId = sc.nextInt();
for (int i = 0; i <names.length ; i++) {
if(names[i]!=null&&states[i]==0&&ddId==i+1){
states[i]=1;
System.out.println("订单签收成功!");
isSignFind=true;
}else if(names[i]!=null&&states[i]==1&&ddId==i+1){
System.out.println("您的订单已完成签收,不能再次签收!");
isSignFind=true;
}
}
if(!isSignFind){
System.out.println("您选择的订单不存在!");
}
break;
case 4:
System.out.println("****删除订单****");
System.out.println("请选择你要删除的订单序号:");
int delId = sc.nextInt();
for (int i = 0; i <names.length ; i++) {
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];
dishes[j]=dishes[j+1];
times[j]=times[j+1];
addresses[j]=addresses[j+1];
sumPrices[j]=sumPrices[j+1];
names[names.length-1 ]=null;
dishes[names.length-1 ]=null;
times[names.length-1 ]=0;
addresses[names.length-1 ]=null;
sumPrices[names.length-1 ]=0;
}
System.out.println("删除订单成功!");
break;
}else if(names[i]!=null&&states[i]==0&&delId==i+1){
System.out.println("您选择的订单未签收,不能删除!");
isDelFind=true;
break;
}
}
if(!isDelFind){
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]+"赞":"0赞";
System.out.println((i + 1) + "\t\t" + dishNames[i] + "\t\t" + price[i] + "元"+praiseNum);
}
System.out.println("请选择你要点赞的菜品序号:");
int praiseNum=sc.nextInt();
praiseNums[praiseNum-1]++;
System.out.println("点赞成功!");
break;
case 6:
System.out.println("****退出系统****");
isExit=true;
break;
default:
isExit=true;
return;
}
if(!isExit){
System.out.println("输入0返回,输入其他值退出系统:");
num = sc.nextInt();
}else {
break;
}
}while(num==0);
System.out.println("欢迎下次光临!");
}
}