用JAVA简单写一个餐饮管理系统,附上代码
时间: 2024-04-21 11:23:01 浏览: 150
下面是一个简单的使用Java编写的餐饮管理系统的示例代码:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class MenuItem {
private String name;
private double price;
public MenuItem(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
class Order {
private int tableNumber;
private List<MenuItem> items;
public Order(int tableNumber) {
this.tableNumber = tableNumber;
this.items = new ArrayList<>();
}
public void addItem(MenuItem item) {
items.add(item);
}
public void removeItem(MenuItem item) {
items.remove(item);
}
public double getTotalAmount() {
double total = 0.0;
for (MenuItem item : items) {
total += item.getPrice();
}
return total;
}
public void display() {
System.out.println("Table Number: " + tableNumber);
System.out.println("Items:");
for (MenuItem item : items) {
System.out.println("- " + item.getName() + ": $" + item.getPrice());
}
System.out.println("Total Amount: $" + getTotalAmount());
}
}
class Restaurant {
private List<MenuItem> menu;
private List<Order> orders;
public Restaurant() {
this.menu = new ArrayList<>();
this.orders = new ArrayList<>();
}
public void addMenuItem(MenuItem item) {
menu.add(item);
}
public void displayMenu() {
System.out.println("Menu:");
for (MenuItem item : menu) {
System.out.println("- " + item.getName() + ": $" + item.getPrice());
}
}
public void placeOrder(Order order) {
orders.add(order);
}
public void displayOrders() {
System.out.println("Orders:");
for (Order order : orders) {
order.display();
System.out.println("-------------------");
}
}
}
public class RestaurantManagementSystem {
public static void main(String[] args) {
Restaurant restaurant = new Restaurant();
// 添加菜单项
restaurant.addMenuItem(new MenuItem("Burger", 10.99));
restaurant.addMenuItem(new MenuItem("Fries", 4.99));
restaurant.addMenuItem(new MenuItem("Coke", 2.99));
// 显示菜单
restaurant.displayMenu();
Scanner scanner = new Scanner(System.in);
// 下订单
Order order1 = new Order(1);
System.out.println("Select items for Order 1 (enter item numbers, -1 to finish):");
while (true) {
int itemNumber = scanner.nextInt();
if (itemNumber == -1) {
break;
}
MenuItem selectedItem = restaurant.getMenu().get(itemNumber-1);
order1.addItem(selectedItem);
}
restaurant.placeOrder(order1);
Order order2 = new Order(2);
System.out.println("Select items for Order 2 (enter item numbers, -1 to finish):");
while (true) {
int itemNumber = scanner.nextInt();
if (itemNumber == -1) {
break;
}
MenuItem selectedItem = restaurant.getMenu().get(itemNumber-1);
order2.addItem(selectedItem);
}
restaurant.placeOrder(order2);
// 显示订单
restaurant.displayOrders();
}
}
```
这个示例代码实现了一个简单的餐饮管理系统,包括菜单管理、下订单和显示订单等功能。你可以根据需要进行进一步的修改和扩展。运行这段代码,你可以根据菜单项的编号选择菜单,创建订单并显示订单信息。
阅读全文