用Java来实现点餐系统,有菜品信息类已附值(编号,菜名,菜品价格),有餐桌类(编号,状态等),完成预订功能,点餐功能,结账功能
时间: 2024-05-04 07:22:33 浏览: 93
好的,那我们可以首先定义菜品信息类和餐桌类,代码如下:
```java
public class MenuItem {
private int id;
private String name;
private double price;
public MenuItem(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
public class Table {
private int id;
private boolean isAvailable;
public Table(int id) {
this.id = id;
this.isAvailable = true;
}
public int getId() {
return id;
}
public boolean isAvailable() {
return isAvailable;
}
public void setAvailability(boolean isAvailable) {
this.isAvailable = isAvailable;
}
}
```
接下来,我们可以定义一个点餐系统类,来实现预订、点餐和结账功能。代码如下:
```java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class OrderSystem {
private List<MenuItem> menu;
private Map<Integer, Table> tables;
private Map<Integer, List<MenuItem>> orders;
public OrderSystem() {
// 初始化菜单
menu = new ArrayList<>();
menu.add(new MenuItem(1, "鱼香肉丝", 25.0));
menu.add(new MenuItem(2, "宫保鸡丁", 28.0));
menu.add(new MenuItem(3, "糖醋排骨", 35.0));
menu.add(new MenuItem(4, "红烧肉", 30.0));
// 初始化餐桌
tables = new HashMap<>();
tables.put(1, new Table(1));
tables.put(2, new Table(2));
tables.put(3, new Table(3));
// 初始化订单
orders = new HashMap<>();
}
public void bookTable(int tableId) {
if(tables.containsKey(tableId)) {
Table table = tables.get(tableId);
if(table.isAvailable()) {
table.setAvailability(false);
System.out.println("预订成功!");
} else {
System.out.println("该餐桌已被预订,请选择其他餐桌。");
}
} else {
System.out.println("该餐桌不存在,请重新输入餐桌编号。");
}
}
public void placeOrder(int tableId, List<Integer> itemIds) {
if(tables.containsKey(tableId)) {
Table table = tables.get(tableId);
if(!table.isAvailable()) {
List<MenuItem> items = new ArrayList<>();
for(Integer itemId : itemIds) {
MenuItem item = findMenuItem(itemId);
if(item != null) {
items.add(item);
} else {
System.out.println("菜品" + itemId + "不存在,请重新输入。");
return;
}
}
orders.put(tableId, items);
System.out.println("点餐成功!");
} else {
System.out.println("该餐桌未被预订,无法下单。");
}
} else {
System.out.println("该餐桌不存在,请重新输入餐桌编号。");
}
}
public double checkOut(int tableId) {
if(orders.containsKey(tableId)) {
List<MenuItem> items = orders.get(tableId);
double total = 0.0;
for(MenuItem item : items) {
total += item.getPrice();
}
System.out.println("餐桌" + tableId + "的消费金额为:" + total + "元。");
tables.get(tableId).setAvailability(true);
orders.remove(tableId);
return total;
} else {
System.out.println("该餐桌未下单,无法结账。");
return 0.0;
}
}
private MenuItem findMenuItem(int itemId) {
for(MenuItem item : menu) {
if(item.getId() == itemId) {
return item;
}
}
return null;
}
public static void main(String[] args) {
OrderSystem orderSystem = new OrderSystem();
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.println("请选择操作:1-预订餐桌,2-点餐,3-结账,0-退出。");
int option = scanner.nextInt();
if(option == 1) {
System.out.println("请输入餐桌编号:");
int tableId = scanner.nextInt();
orderSystem.bookTable(tableId);
} else if(option == 2) {
System.out.println("请输入餐桌编号:");
int tableId = scanner.nextInt();
System.out.println("请输入菜品编号(多个编号用空格隔开):");
String itemStr = scanner.nextLine();
String[] itemIds = itemStr.split(" ");
List<Integer> itemList = new ArrayList<>();
for(String itemId : itemIds) {
itemList.add(Integer.parseInt(itemId));
}
orderSystem.placeOrder(tableId, itemList);
} else if(option == 3) {
System.out.println("请输入餐桌编号:");
int tableId = scanner.nextInt();
orderSystem.checkOut(tableId);
} else if(option == 0) {
System.out.println("谢谢惠顾,再见!");
break;
} else {
System.out.println("无效操作,请重新选择。");
}
}
}
}
```
这个点餐系统类中实现了预订、点餐和结账功能,并且在 main 方法中提供了一个简单的命令行交互界面,方便用户使用。
阅读全文