Java基础-基于List集合外卖订餐系统
- 代码
- 创建菜品实体类:
- 创建订单实体类:
- 创建菜品集合初始化类:相当于菜品的数据库
- 创建点餐业务类:
- 创建测试类:
- 运行结果
- 订餐模块
- 查看订单状态
- 签收订单
- 删除订单
- 点赞模块
- 退出
此系统具有以下几个功能:
订餐功能,查看订单,签收订单,删除订单,菜品点赞,退出系统功能。
代码
创建菜品实体类:
/*菜品实体类*/
public class Dish {
private int id; /*序号*/
private String name; /*菜名*/
private double price; /*价格*/
private double expressFee; /*送餐费*/
private int giveOK;/*点赞*/
public Dish(int id, String name, double price, double expressFee, int giveOK) {
this.id = id;
this.name = name;
this.price = price;
this.expressFee = expressFee;
this.giveOK = giveOK;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getExpressFee() {
return expressFee;
}
public void setExpressFee(double expressFee) {
this.expressFee = expressFee;
}
public int getGiveOK() {
return giveOK;
}
public void setGiveOK(int giveOK) {
this.giveOK = giveOK;
}
}
创建订单实体类:
/*订单实体类*/
public class Order {
private int oid;/*订单号*/
private String rname;/*订餐人*/
private Dish dish;/*菜品*/
private int number;/*数量*/
private String time; /*订单时间*/
private String address; /*订单配送地点*/
private double money; /*付款*/
private String state;/*订单的状态*/
public Order(int oid, String rname, Dish dish, int number, String time, String address, double money, String state) {
this.oid = oid;
this.rname = rname;
this.dish = dish;
this.number = number;
this.time = time;
this.address = address;
this.money = money;
this.state = state;
}
public int getOid() {
return oid;
}
public void setOid(int oid) {
this.oid = oid;
}
public String getRname() {
return rname;
}
public void setRname(String rname) {
this.rname = rname;
}
public Dish getDish() {
return dish;
}
public void setDish(Dish dish) {
this.dish = dish;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
创建菜品集合初始化类:相当于菜品的数据库
public class Menu {
/*菜品生成方法*/
public Dish[] initMenu(){
Dish dish1 = new Dish(1, "红烧肉", 36.0,6,0);
Dish dish2 = new Dish(2, "红烧茄子", 15.0,4,0);
Dish dish3 = new Dish(3, "鱼香肉丝", 25.0,5,0);
Dish[] dishes={dish1,dish2,dish3};
return dishes;
}
}
创建点餐业务类:
/*餐厅服务类*/
public class RestaurantService {
Scanner scanner = new Scanner(System.in);
/*点餐方法*/
int oid=0;
public Order orderService(Dish[] dishes) {
System.out.println("******我要订餐*****");
System.out.print("请输入订餐人的姓名:");
String name = scanner.next();
System.out.println("序号 菜名 单价");
/*打印所有菜品*/
for (int i = 0; i < dishes.length; i++) {
System.out.println(dishes[i].getId() + " " + dishes[i].getName() + " " + dishes[i].getPrice());
}
System.out.print("请选择你要点的菜品编号:");
int id = scanner.nextInt();
System.out.print("请选择你需要的份数:");
int number = scanner.nextInt();
System.out.print("输入你的送餐时间:");
String time = scanner.next();
System.out.print("请输入送餐的时间地点:");
String address = scanner.next();
System.out.println("订餐成功");
/*打印你需要点的菜的信息和价格*/
Dish dish = null;
Order order=null;
System.out.print("你定的是:");
for (int i = 0; i < dishes.length; i++) {
if (dishes[i].getId() == id) {
dish = dishes[i];
System.out.println(dishes[i].getName() + " " + number);
break;
}
}
System.out.println("送餐时间:" + time + "点");
/*合计*/
double money=dish.getExpressFee() + dish.getPrice() * number;
System.out.println("餐费:" + dish.getPrice() * number + " 送餐费:" + dish.getExpressFee() + " 总计:" +money);
order=new Order(++oid,name,dish,number,time,address,money,"已预订");
System.out.print("输入0返回:");
int back = scanner.nextInt();
if(back==0){
return order;
}
return null;
}
/*查看订单*/
public void selectOrder(List<Order> orders){
System.out.println("****查看餐袋****");
System.out.println("序列号\t订餐人\t餐品信息\t送餐日期\t送餐地址\t总金额\t订单状态");
for (int i = 0; i < orders.size(); i++) {
System.out.print(orders.get(i).getOid()+"\t ");
System.out.print(orders.get(i).getRname()+"\t ");
System.out.print(orders.get(i).getDish().getName()+"\ts"+orders.get(i).getNumber()+"\t ");
System.out.print(orders.get(i).getTime()+"\t ");
System.out.print(orders.get(i).getAddress()+"\t ");
System.out.print(orders.get(i).getMoney()+"\t");
System.out.println(orders.get(i).getState()+"\t");
}
System.out.print("输入0返回:");
int back = scanner.nextInt();
if(back==0){
return;
}
}
/*签收订单*/
public void signOrder(List<Order> orders){
System.out.println("*****签收订单*****");
System.out.print("请输入要签收的订单序号:");
int oid = scanner.nextInt();
for (int i = 0; i < orders.size(); i++) {
if(orders.get(i).getOid()==oid){
orders.get(i).setState("已签收");
System.out.println("订单签收成功!");
break;
}
}
System.out.print("输入0返回:");
int back = scanner.nextInt();
if(back==0){
return;
}
}
/*删除订单*/
public void delOrder(List<Order> orders){
System.out.println("*****删除订单*****");
System.out.print("请输入要删除的订单序号:");
int oid = scanner.nextInt();
for (int i = 0; i < orders.size(); i++) {
if(orders.get(i).getOid()==oid){
orders.get(i).setState("已删除");
System.out.println("订单删除成功!");
break;
}
}
System.out.print("输入0返回:");
int back = scanner.nextInt();
if(back==0){
return;
}
}
/*点赞*/
public void giveOk(Dish[] dishes){
System.out.println("*******我要点赞*******");
System.out.println("序号 菜名 单价");
/*打印所有菜品*/
for (int i = 0; i < dishes.length; i++) {
System.out.println(dishes[i].getId() + " " + dishes[i].getName() + " " + dishes[i].getPrice()+" "+dishes[i].getGiveOK());
}
System.out.print("请选择你要点的菜品序号:");
int id = scanner.nextInt();
for (int i = 0; i < dishes.length; i++) {
if(dishes[i].getId()==id){
dishes[i].setGiveOK(dishes[i].getGiveOK()+1);
System.out.println("点赞成功");
}
}
System.out.print("输入0返回:");
int back = scanner.nextInt();
if(back==0){
return ;
}
}
}
创建测试类:
public class Test {
public static void main(String[] args) {
System.out.println("欢迎使用“外卖订餐系统”");
Scanner scanner = new Scanner(System.in);
RestaurantService restaurantService = new RestaurantService();
/*订单类*/
List<Order> orders=new ArrayList<>();
Menu menu = new Menu();
Dish[] dishes = menu.initMenu();
while (true){
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 id = scanner.nextInt();
if (id == 1) {
/*我要订餐*/
Order order = restaurantService.orderService(dishes);
orders.add(order);
} else if (id == 2) {
/*查看餐袋*/
restaurantService.selectOrder(orders);
} else if (id == 3) {
/*签收订单*/
restaurantService.signOrder(orders);
} else if (id == 4) {
/*删除订单*/
restaurantService.delOrder(orders);
} else if (id == 5) {
/*我要点赞*/
restaurantService.giveOk(dishes);
} else {
System.out.println("谢谢使用,欢迎下次光临");
break;//退出
}
}
}
}
运行结果
订餐模块
查看订单状态
签收订单
删除订单
点赞模块
退出