Java之吃货联盟实战代码
一、覆盖范围
程序基本概念 :变量、数据类型
流程控制:顺序、选择、循环、跳转语句
数组
实现代码
package 吃货联盟;
import java.util.Scanner;
/**
* @Author lichangxin
* @Date 2020-07-16
* @Description
*/
public class chihuo {
public static void main(String[] args) {
final double NO_SEND_MONEY=80;
String[] dishes={"东坡肉","地锅鸡","水煮鱼","佛跳墙"};
double[] prices={45,38,28,99};
int choosenum=0;
String[][] bills=new String[10][];
String name=null;
int pcs=0;//份数
int times=0;
String address=null;
String status=null;//订单状态
int[] like={11,22,33,44};
Scanner sc=new Scanner(System.in);
int choose=0;
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.println("请选择:");
choose=sc.nextInt();
switch (choose){
case 1:
System.out.println("***我要订餐***");
System.out.print("请输入订餐人姓名:");
name=sc.next();
System.out.println("序号\t菜品\t单价");
for (int i = 0; i < dishes.length; i++) {
System.out.println(i+1+"\t"+dishes[i]+"\t"+prices[i]+"元");
}
do {
System.out.print("请选择你要点的菜品编号:");
choosenum=sc.nextInt();
if (choosenum>0&&choosenum<=dishes.length) break;
System.out.println("你的输入有误,重新输入!");
}while (true);
do {
System.out.println("请选择你需要的份数:");
pcs=sc.nextInt();
if (pcs>0)break;
System.out.println("份数不能小于0,重新输入");
}while (true);
do {
System.out.println("请输入送餐时间(10-20整):");
times=sc.nextInt();
if (times>=10&×<=20)break;
System.out.println("你的输入有误,重新输入!");
}while (true);
System.out.println("请输入送餐地址:");
address=sc.next();
System.out.println("订餐成功!");
System.out.println("你订的是:"+dishes[choosenum-1]);
System.out.println("送餐时间:"+times+"点");
int sendMoney=0;
double dishPrices=pcs*prices[choosenum-1];
if (dishPrices<NO_SEND_MONEY){
sendMoney=6;
}
double totalPrices=dishPrices+sendMoney;
System.out.println("餐费:"+dishPrices+"元"+"送餐费"+sendMoney+"元"+"总计:"+totalPrices+"元");
//插入订单,先遍历订单,如果为空,则插入;
for (int i = 0; i < bills.length; i++) {
if (bills[i]==null){
bills[i]=new String[6];
bills[i][0]=name;
bills[i][1]=dishes[choosenum-1]+" "+pcs+"份";
bills[i][2]=times+"点";
bills[i][3]=address;
bills[i][4]=totalPrices+"元";
bills[i][5]="已预订";
break;
}
}
System.out.print("输入0返回:");
choose=sc.nextInt();
break;
case 2:
System.out.println("***查看餐袋***");
System.out.println("序号\t订餐人\t餐品信息\t送餐日期\t送餐地址\t总金额\t订单状态");
for (int i = 0; i < bills.length; i++) {
if (bills[i]==null){
break;
}
System.out.println(i+1+"\t"+bills[i][0]+"\t"+bills[i][1]+"\t"+bills[i][2]+"\t"+bills[i][3]+"\t"+
bills[i][4]+"\t"+bills[i][5]);
}
System.out.print("输入0返回:");
choose=sc.nextInt();
break;
case 3:
System.out.println("***签收订单***");
if (bills[0]==null){
System.out.println("订单为空,不可签收");
System.out.println("请输入0返回:");
choose=sc.nextInt();
break;
}
System.out.println("序号\t订餐人\t餐品信息\t总金额\t订单状态");
int i=0;
for (i = 0; i < bills.length; i++) {
if (bills[i]==null){
break;
}
System.out.println(i+1+"\t"+bills[i][0]+"\t"+bills[i][1]+"\t"+
bills[i][4]+"\t"+bills[i][5]);
}
int signNum;
do {
System.out.println("请选择需要签收的订单编号:");
signNum=sc.nextInt();
if (signNum>0&&signNum<=i)break;
System.out.println("你的输入有误,重新输入!");
}while (true);
if (bills[signNum-1][5].equals("已完成")){
System.out.println("订单已经完成签收,不需要重复签收");
}else{
bills[signNum-1][5]="已完成";
}
System.out.print("输入0返回:");
choose=sc.nextInt();
break;
case 4:
System.out.println("***删除订单***");
//删除订单需要考虑,选择的订单删除后,后面的订单前移
//未完成的订单不能删除
if (bills[0]==null){
System.out.println("订单为空,不可删除");
System.out.println("请输入0返回:");
choose=sc.nextInt();
break;
}
System.out.println("序号\t订餐人\t餐品信息\t总金额\t订单状态");
int k;
for (k = 0; k < bills.length; k++) {
if (bills[k]==null){
break;
}
System.out.println(k+1+"\t"+bills[k][0]+"\t"+bills[k][1]+"\t"+
bills[k][4]+"\t"+bills[k][5]);
}
int delNum;
do {
System.out.println("请选择需要删除的订单编号:");
delNum=sc.nextInt();
if (bills[0]==null){
}
if (delNum>0&&delNum<=k)break;
System.out.println("你的输入有误,重新输入!");
}while (true);
if (!bills[delNum-1][5].equals("已完成")){
System.out.println("未完成的订单不能删除!");
}else {
for (int s = delNum-1; bills[s]!=null ; s++) {
if (delNum==bills.length){
bills[delNum-1]=null;
}else {
bills[s]=bills[s+1];
}
}
System.out.println("删除完成");
}
System.out.print("输入0返回:");
choose=sc.nextInt();
break;
case 5:
System.out.println("***我要点赞***");
System.out.println("序号\t菜品\t单价");
for (int j = 0; j < dishes.length; j++) {
System.out.println(j+1+"\t"+dishes[j]+"\t"+prices[j]+"元");
}
int likeNum;
do {
System.out.print("请选择要点赞的菜品:");
likeNum=sc.nextInt();
if (likeNum>0&&likeNum<=like.length)break;
System.out.println("没有这个菜品,请重新输入");
}while (true);
like[likeNum-1]++;
System.out.println("谢谢点赞!");
System.out.print("输入0返回:");
choose=sc.nextInt();
break;
}
}while (choose==0);
System.out.println("谢谢使用,欢迎下次光临");
}
}