吃货联盟订餐系统
import java.util.*;
public class OrderingUtil { //点餐类
Scanner sc = new Scanner (System.in);
OrderingSet oSet = new OrderingSet();
String[] dishNames = { "红烧鱼", "油豆腐炒肉", "土豆丝" }; // 菜品名称
double[] prices = new double[] { 38.0, 15.0, 12.0 }; // 菜品价格
int[] prainseNums = new int[3]; // 点赞数
public void initial() {
// 初始化第一条订单信息
oSet.names[0] = "张晴";
oSet.dishMegs[0] = "红烧鱼2份";
oSet.times[0] = 12;
oSet.addresses[0] = "杨柳路207号";
oSet.sumPrinces[0] = 76.0;
oSet.states[0] = 1;
// 初始化第二条订单信息
oSet.names[0] = "张晴";
oSet.dishMegs[0] = "油豆腐炒肉2份";
oSet.times[0] = 18;
oSet.addresses[0] = "杨柳路207号";
oSet.sumPrinces[0] = 30;
oSet.states[0] = 0;
}
public boolean add(){ //点餐方法
boolean isAdd = false;// 记录是否可以点餐
for (int j = 0; j < oSet.names.length; j++) {
if (oSet.names[j]==null) { //找到第一个位置,可添加订单信息
isAdd = true; //置标志位,可以订餐
System.out.print("请输入订餐人姓名:");
String name = sc.next();
//显示供选择的菜品信息
System.out.println("序号"+"\t"+"菜品"+"\t"+"单价"+"\t"+"点赞数");
for (int i = 0; i < dishNames.length; i++) {
String price = prices[i]+"元";
String priaiseNum = (prainseNums[i])>0? prainseNums[i]+"赞":"0";
System.out.println((i+1)+"\t"+dishNames[i]+"\t"+price+"\t"+priaiseNum);
}
//用户点餐
System.out.print("请选择您要点的菜品编号:");
int chooseDish = sc.nextInt();
System.out.print("请选择您需要的份数:");
int number = sc.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点至12点间整点送餐):");
int time = sc.nextInt();
while (time<10 || time>20) {
System.out.println("您的输入有误,请输入10~20之间的整数!");
time=sc.nextInt();
}
System.out.print("请输入送餐地址:");
String address = sc.next();
//无需添加状态,默认是0,即已预定状态
System.out.println("订餐成功!");
System.out.println("您订的是:"+dishMeg);
System.out.println("送餐时间:"+time+"点");
System.out.println("餐费:"+sumPrice+"元,送餐费"+deliCharge+"元,总计:"+(sumPrice+deliCharge)+"元。");
//添加数据
oSet.names[j] = name;
oSet.dishMegs[j] = dishMeg;
oSet.times[j] =time;
oSet.addresses[j] = address;
oSet.sumPrinces[j]=sumPrice+deliCharge;
break;
}
}
if(!isAdd){
System.out.println("对不起,您的餐袋已满!");
return false;
}else
return true;
}
public void display(){
System.out.println("序号\t订餐人\t餐品信息\t\t送餐时间\t送餐地址\t\t总金额\t订单状态");
for (int i = 0; i < oSet.names.length; i++) {
if (oSet.names[i]!=null) {
String state =(oSet.states[i]==0)?"已预定":"已完成";
String date = oSet.times[i]+"点";
String sumPrice = oSet.sumPrinces[i]+"元";
System.out.println((i+1)+"\t"+oSet.names[i]+"\t"+oSet.dishMegs[i]+"\t"+date+"\t"
+oSet.addresses[i]+"\t"+sumPrice+"\t"+state);
}
}
}
public void sigh(){
boolean isSignFind = false; //找到要签收的订单
System.out.print("请选择要签收的订单序号:");
int signOrderId = sc.nextInt();
for (int i = 0; i if (oSet.names[i]!=null&&oSet.states[i]==0&&signOrderId == i+1) {
oSet.states[i]=1;//将状态值置为已完成
System.out.println("订单签收成功!");
isSignFind = true; //标记已找到订单
}else if(oSet.names[i]!=null&&oSet.states[i]==1&&signOrderId ==i+1){
System.out.println("请选择的订单已完成签收,不能再次签收!");
isSignFind = true; //标记已找到订单
}
}
if(!isSignFind){
System.out.println("您选择的订单不存在!");
}
}
public void delete(){
boolean isDelFind = false; //标记是否找到要删除的订单
System.out.print("请输入要删除的订单序号:");
int delId = sc.nextInt();
for (int i = 0; i < oSet.names.length; i++) {
if (oSet.names[i]!=null&&oSet.states[i]==1&&delId == i+1) {
isDelFind = true; //标记已找到次订单
//执行删除操作:删除位置后的元素依次前移
for (int j = delId-1; j < oSet.names.length-1; j++) {
oSet.names[j] = oSet.names[j+1];
oSet.dishMegs[j]= oSet.dishMegs[j+1];
oSet.times[j] = oSet.times[j+1];
oSet.addresses[j] = oSet.addresses[j+1];
oSet.states[j] = oSet.states[j+1];
oSet.sumPrinces[j] = oSet.sumPrinces[j+1];
}
//最后一位清空
int endIndex = oSet.names.length-1;
oSet.names[endIndex] =null;
oSet.dishMegs[endIndex] = null;
oSet.times[endIndex] = 0;
oSet.addresses[endIndex]=null;
oSet.states[endIndex] = 0;
oSet.sumPrinces[endIndex]=0;
System.out.println("删除订单成功!");
break;
}else if(oSet.names[i]!=null && oSet.states[i]==0&&delId==i+1){
System.out.println("您选择的订单未签收,不能删除!");
isDelFind = true; //标记找到此订单
break;
}
}
//未找到该序列号的订单:不能删除
if (!isDelFind) {
System.out.println("您要删除的订单不存在!");
}
}
public void praise(){
//显示菜品信息
System.out.println("序号"+"\t"+"菜名"+"\t"+"单价");
for (int i = 0; i < dishNames.length; i++) {
String price = prices[i]+"元";
String priaiseNum = (prainseNums[i])>0?prainseNums[i]+"赞":"";
System.out.println((i + 1) + "\t" + dishNames[i] + "\t" + prices[i] + "元" + priaiseNum);
}
System.out.print("请选择您要点赞的菜品序号:");
int number = sc.nextInt();
prainseNums[number - 1]++;
System.out.println("点赞成功");
}
public void startMenu() {
int num = -1; //用户输入0返回主菜单,否则退出系统
boolean isExit = false; //标志用户是否退出系统,ture为退出系统
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.print("请选择:");
int choose = sc.nextInt(); //记录用户选择的功能编号
switch (choose) {
case 1:
System.out.println("***我要订餐***");
add();
break;
case 2:
System.out.println("***查看餐袋***");
display();
break;
case 3:
System.out.println("****签收订单***");
sigh();
break;
case 4:
System.out.println("***删除订单***");
delete();
break;
case 5:
System.out.println("***我要点赞***");
praise();
break;
case 6:
System.out.println("系统退出");
isExit = true;
break;
default:
System.out.println("请输入正确的数字!");
isExit = true;
break;
}
if(!isExit){
System.out.print("输入0返回:");
num = sc.nextInt();
}else{
break;
}
} while (num == 0);
}
}
public class OrderingSet {
//数据主体:一组订单信息
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表示预定,表示已完成
double [] sumPrinces = new double [4]; //保存订单的总金额
}
import java.util.*;
public class OrderingMgr {
public static void main(String[] args) {
// TODO Auto-generated method stub
OrderingUtil om = new OrderingUtil();
om.initial();
om.startMenu();
}
}