基本效果和之前一样
将使用的数组改为集合
直接附上代码
Ordering
package cn.bdqn.work;
import java.util.ArrayList;
import java.util.Scanner;
public class OrderingMsg {
// public static Order[] order = new Order[4];
public static ArrayList<Order> order=new ArrayList<Order>();
public static ArrayList<Food> food = new ArrayList<Food>();
static Scanner input = new Scanner(System.in);
static boolean isAdd = false;
static {
Order order1 = new Order("张三", "狮子头 一份", 12, "A01", 0, 38.0);
Order order2 = new Order("李四", "大鸡腿 六份", 15, "A02", 1, 60.0);
order.add(order1);
order.add(order2);
order.add(null);
order.add(null);
Food food1 = new Food("大鸡腿", 6.0, 0);
Food food2 = new Food("狮子头", 38.0, 0);
Food food3 = new Food("油泼面", 9.0, 0);
food.add(food1);
food.add(food2);
food.add(food3);
}
public static void main(String[] args) {
int num = -1;// 记录用户输入的数字
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 = input.nextInt();
// true 记录找到一个为空的位置 可以插入 false没有找到
boolean isAdd = false;
switch (choose) {
case 1:
orderingFood();
break;
case 2:
seeOrder();
break;
case 3:
deletedOrder();
break;
case 4:
signInOrder();
break;
case 5:
pariseOrder();
break;
case 6:
// 6.推出系统
break;
default:
break;
}
if (choose < 1 || choose > 5) {
break;
} else {
// 提示输入 0 返回
System.out.println("输入0返回主菜单");
num = input.nextInt();
}
} while (num == 0);
System.out.println("谢谢使用,欢迎下次光临");
}
static void orderingFood() {
// 1.我要订餐
System.out.println("-------我要订餐---------");
for (int i = 0; i < order.size(); i++) {
// 如果找到为空的位置,记录下来
if (order.get(i) == null) {
// 找到一个 为空的位置
isAdd = true;
// 执行插入操作
// 显示所有可选择的餐品信息
System.out.println("序号\t餐品名\t\t单价\t点赞数");
for (int j = 0; j < food.size(); j++) {
System.out.println((j + 1) + "\t\t" + food.get(j).getDishName()
+ "\t\t" + food.get(j).getPrice() + "\t"
+ food.get(j).getPraiseNum());
}
// 输入所选餐品编号 以及份数
System.out.println("请选择所定参品序号");
int chooseDish = input.nextInt();
System.out.println("请选择所定份数");
int number = input.nextInt();
String dishMeg = food.get(chooseDish - 1).getDishName() + " "
+ number + "份";
// 输入订餐人的姓名
System.out.println("请输入订餐人的姓名");
String name = input.next();
// 输入送餐时间
System.out.println("输入送餐时间(10-20整点送餐)");
int time = input.nextInt();
while (time < 10 || time > 20) {
System.out.println("请输入10-20 之间的整数");
time = input.nextInt();
}
// 输入送餐地址
System.out.println("输入送餐地址");
String address = input.next();
// 计算金额
double sumPrice = food.get(chooseDish - 1).getPrice() * number;
// 送餐费 金额达到50 免去订餐费 否则加6元送餐费
double deliCharge = sumPrice >= 50 ? 0.0 : 6.0;
// 提示订单信息
System.out.println("订餐成功:");
System.out.println("您订的是:" + dishMeg);
System.out.println("订餐人:" + name);
System.out.println("送餐时间:" + time + "点");
System.out.println("送餐地址:" + address);
System.out.println("餐费:" + sumPrice + " 送餐费:" + deliCharge
+ "元");
System.out.println("总金额:" + (sumPrice + deliCharge) + "元");
// 保存数据
Order o = new Order(name, dishMeg, time, address, 0, sumPrice);
order.add(i,o);
break;
}
}
if (!isAdd) {
System.out.println("对不起 您的餐袋已经满了");
}
}
static void seeOrder() {
// 2.查看餐袋
System.out.println("-------查看餐袋---------");
System.out.println("序号\t订餐人\t所订餐品信息\t送餐时间\t送餐地址\t总金额\t状态\t");
// 遍历数组
for (int i = 0; i < order.size(); i++) {
if (order.get(i) != null) {
String time = order.get(i).getTime() + "点";
String sumPrice = order.get(i).getSumPrices() + "元";
String state = order.get(i).getState() == 0 ? "已预订" : "已完成";
System.out.println((i + 1) + "\t\t " + order.get(i).getName()
+ "\t" + order.get(i).getDishMegs() + "\t\t" + time
+ "\t\t" + order.get(i).getAddress() + "\t" + sumPrice
+ "\t" + state);
}
}
}
static void deletedOrder() {
// 3.删除订单
// 输入要删除的订单序号(从1开始)
System.out.println("-------删除订单---------");
System.out.println("请输入要删除的订单序号");
int delId = input.nextInt();
int delIndex = -1;// 删除订单下标
boolean isDelFind = false;// true找到 false未找到
// 循环查找订单
for (int i = 0; i < order.size(); i++) {
// 找打此订单 删除操作
if (order.get(i) != null && order.get(i).getState() == 1 && delId == i + 1) {
delIndex = i;
isDelFind = true;
break;
} else if (order.get(i) != null && order.get(i).getState() == 0
&& delId == i + 1) {
System.out.println("您的订单未签收 不能删除");
isDelFind = true;
break;
}
}
// 未找到订单
if (!isDelFind) {
System.out.println("对不起此条订单未找到");
return;
}
// 执行删除操作
if (delIndex != -1) {
// 循环操作(循环移位)
// 要删除的元素的后面移位开始往前移动一位
for (int i = delIndex + 1; i <= order.size() - 1; i++) {
order.set(i-1, order.get(i));
}
// 最后一位清空
order.remove(order.size() - 1);
System.out.println("删除完成");
}
}
static void signInOrder() {
// 4.签收订单
System.out.println("-------签收订单---------");
System.out.println("请输入要签收的订单");
int signOrderId = input.nextInt();
boolean isFind = false;// 记录是否找到这条订单 true找到 false 没有找到
// 查找这条订单(循环遍历)
for (int i = 0; i < order.size(); i++) {
// 1 找到 状态已完成 提示不能在签收
// 2找到 状态未完成 可以签收
if (order.get(i) != null && order.get(i).getState() == 1
&& signOrderId == i + 1) {
System.out.println("对不起 您选择的订单已经完成了签收 不能二次签收");
isFind = true;
break;
} else if (order.get(i) != null && order.get(i).getState() == 0
&& signOrderId == i + 1) {
order.get(i).setState(1);// 更改状态值
System.out.println("订单签收成功");
isFind = true;
break;
}
}
// 条件判断
// 3 没有找到 提示一下
if (!isFind) {
System.out.println("对不起,此订单不存在");
}
}
static void pariseOrder() {
// 5.我要点赞
System.out.println("-------我要点赞---------");
// 显示点赞的餐品列表
// 显示所有可选择的餐品信息
System.out.println("序号\t餐品名\t\t单价\t点赞数");
for (int j = 0; j < food.size(); j++) {
System.out.println((j + 1) + "\t\t" + food.get(j).getDishName()
+ "\t\t" + food.get(j).getPrice() + "\t"
+ food.get(j).getPraiseNum());
}
// 输入要点赞的餐品序号
System.out.println("请输入要点赞的餐品序号");
int dishId = input.nextInt();
// 该序号的餐品点赞数加1
int praiseNums = food.get(dishId - 1).getPraiseNum();
praiseNums++;
Food f = new Food(food.get(dishId - 1).getDishName(),
food.get(dishId - 1).getPrice(), praiseNums);
food.set(dishId - 1, f);
System.out.println("点赞成功");
System.out.println(food.get(dishId - 1).getDishName() + " "
+ food.get(dishId - 1).getPraiseNum() + "赞");
}
}
Food
package cn.bdqn.work;
public class Food {
private String dishName;
private double price;
private int praiseNum;
public Food() {
}
public Food(String dishName, double price, int praiseNum) {
this.dishName = dishName;
this.price = price;
this.praiseNum = praiseNum;
}
public String getDishName() {
return dishName;
}
public void setDishName(String dishName) {
this.dishName = dishName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getPraiseNum() {
return praiseNum;
}
public void setPraiseNum(int praiseNum) {
this.praiseNum = praiseNum;
}
}
Order
package cn.bdqn.work;
public class Order {
private String name;
private String dishMegs;
private int time;
private String address;
private int state;
private double sumPrices;
public Order() {
}
public Order(String name, String dishMegs, int time, String address,
int state, double sumPrices) {
this.name = name;
this.dishMegs = dishMegs;
this.time = time;
this.address = address;
this.state = state;
this.sumPrices = sumPrices;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDishMegs() {
return dishMegs;
}
public void setDishMegs(String dishMegs) {
this.dishMegs = dishMegs;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public double getSumPrices() {
return sumPrices;
}
public void setSumPrices(double sumPrices) {
this.sumPrices = sumPrices;
}
}