四个类
1.食物菜单Food类:
菜名,价格,点赞数
String name ;
int price;
int praiseNum;
2.订单类Order:
String name; // 订餐人
Food food;
int num;// 定了几份
int time; // 送餐时间
String address; // 送餐地址
int state; // 订单状态 0:已预订 1:已完成
int sumPrice; // 总金额
3.餐厅管理类(操作类)
4.测试类
代码如下:
public class Food {
String name ;
int price;
int praiseNum;
public Food() {
}
public Food(String name, int price, int praiseNum) {
this.name = name;
this.price = price;
this.praiseNum = praiseNum;
}
}
//订单
public class Order {
String name; // 订餐人
Food food;
int num;// 定了几份
int time; // 送餐时间
String address; // 送餐地址
int state; // 订单状态 0:已预订 1:已完成
int sumPrice; // 总金额
public Order(String name, Food food, int num, int time, String address,
int state, int sumPrice) {
super();
this.name = name;
this.food = food;
this.num = num;
this.time = time;
this.address = address;
this.state = state;
this.sumPrice = sumPrice;
}
public Order() {
super();
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class DiningRoom {
//菜单是餐厅的属性
ArrayList<Food> foods = new ArrayList<Food>();
ArrayList<Order> orders = new ArrayList<Order>();
public int orderNum;//表示 该餐厅 目前的订单数量
Scanner input = new Scanner(System.in);
//使用 构造代码块 给餐厅 foods 初始化
{
Food f1 = new Food("红烧带鱼", 38, 0);
Food f2 = new Food("鱼香肉丝", 20, 0);
Food f3 = new Food("时令蔬菜", 10, 0);
foods.add(f1);
foods.add(f2);
foods.add(f3);
}
{
Food food = foods.get(0);
Food food1 = foods.get(1);
orders.add(new Order("苏雨", food, 2, 3, "安徽", 1, food.price*2));
orders.add( new Order("苏雨", food1, 1, 4, "安徽", 1, food.price*1));
// orderNum=2;
}
//显示菜单
public int start(){
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 in = input.nextInt();
return in;
}
//根据 用户的选择,分发到具体的功能部门
public void choose(int in){
// boolean flag=false;
switch(in){
case 1:
//我要订餐
System.out.println("\n******我要订餐***********");
//调用 执行 点餐功能方法
chooseFood();
break;
case 2:
System.out.println("\n******查看餐袋***********");
eachOrders();
break;
case 3:
System.out.println("\n******签收订单***********");
receive();
break;
case 4:
System.out.println("\n******删除订单***********");
deleteOrder();
break;
case 5:
System.out.println("\n******我要点赞***********");
praise();
break;
case 6:
// flag = true;
default:
System.out.println("程序结束");
System.exit(0);
// flag = true;
break;
}
}
//我要订餐
public Order chooseFood(){
eachFoods();
}
System.out.print("请输入要选择的菜品:");
int index = input.nextInt();
//客服选择的菜品为
Food food = foods.get(index-1);
System.out.println("你选择 的菜品为:"+food.name);
System.out.print("请输入,你要订餐的份数:");
int sum = input.nextInt();
//输入 个人信息 ,用于接收订单
System.out.print("请输入您的姓名:");
String name = input.next();
System.out.print("您希望什么时间送达:");
int time = input.nextInt();
System.out.print("请输入您的地址:");
String address = input.next();
//接收所有订单信息,封装为对象
//计算总价
int sumPrice = food.price*sum;
Order order = new Order(name, food, sum, time, address, 0, sumPrice);
//餐厅要添加订单
orders.add(order);
orderNum++;
System.out.println("订单已添加,请耐心等待~~");
return order;
}
//是否退出功能
public boolean isExit(){
System.out.println("是否返回上级菜单(按0 返回)");
int i = input.nextInt();
return i==0?false : true;
}
//查看订单(餐袋)
public void eachOrders(){
System.out.println("序号\t订餐人\t餐品信息\t\t送餐日期\t送餐地址\t\t总金额\t订单状态");
for (int i = 0; i < orders.size(); i++) {
String states = orders.get(i).state==0?"已预定":"已完成";
System.out.println((i+1)+"\t"+orders.get(i).name+"\t"+orders.get(i).food.name+" "+orders.get(i).num+"份"+"\t"+orders.get(i).time+"\t"+orders.get(i).address+"\t "+orders.get(i).sumPrice+"\t\t"+states);
}
}
//签收功能 接收
public void receive(){
//先打印餐袋,让用户选择 修改的订单
eachOrders();
System.out.print("请输入你要修改的订单序号:");
int i = input.nextInt();
//判断 该订单的状态是否已经完成
if(orders.get(i-1).state==1){
System.out.println("该订单已经签收成功!");
}else{
orders.get(i-1).state=1;
System.out.println("订单签收成功!");
}
}
//删除订单功能
public void deleteOrder(){
//1.删除的订单 之后的向前一个位置
eachOrders();
System.out.print("请输入你要删除的订单:");
int i = input.nextInt();
orders.remove(i-1);
//2.最后一个 删除
// 3.有效数量
orderNum--;
System.out.println("订单删除成功!");
}
//点赞功能
public void praise(){
//1.看菜单
eachFoods();
// 2.点赞哪一道菜
System.out.println("请输入你想点赞的菜品:");
int i = input.nextInt();
// 3.根据用户输入,修改属性值
foods.get(i-1).praiseNum++;
eachFoods();
}
//看菜单
public void eachFoods(){
// 查看菜单
System.out.println("序号\t菜品\t单价\t点赞数");
for (int i = 0; i < foods.size(); i++) {
System.out.println((i+1)+"\t"+foods.get(i).name+"\t"+foods.get(i).price+"\t"+foods.get(i).praiseNum);
}
}
}
public class Text {
public static void main(String[] args) {
//创建餐厅对象
DiningRoom diningRoom = new DiningRoom();
do{
//输出菜单
int start = diningRoom.start();
//内容分发
diningRoom.choose(start);
}while(!diningRoom.isExit());
}
}