根据以下要求,使用MySQL编写一个餐馆管理系统,并使用JAVA或C语言给出合适的代码。要求:主要有前台、顾客、后厨三个方面。在前台部分,餐馆对外有一个相对稳定的菜单,上面有菜品的照片和价格,但是也会时不时增加、减少或者修改菜单。顾客可以点菜和用餐结束之后核实票据并付款。票据中有菜品及其价格的列表、下单时间、总费用等信息。后厨主要负责材料的维护,餐馆内部记录着菜单上每道菜需要消耗的材料量。收到顾客订单后,依据菜品准备材料。
时间: 2024-03-14 21:46:34 浏览: 14
首先,我们需要创建数据库。以下是一个简单的MySQL数据库表结构,包含3个表:menu、order、material。
menu表:
| 字段名 | 数据类型 | 说明 |
| --- | --- | --- |
| id | int | 菜品ID |
| name | varchar | 菜品名称 |
| price | decimal | 菜品价格 |
| image | varchar | 菜品图片路径 |
order表:
| 字段名 | 数据类型 | 说明 |
| --- | --- | --- |
| id | int | 订单ID |
| create_time | datetime | 下单时间 |
| total_price | decimal | 总费用 |
order_menu表:
| 字段名 | 数据类型 | 说明 |
| --- | --- | --- |
| order_id | int | 订单ID |
| menu_id | int | 菜品ID |
| quantity | int | 菜品数量 |
material表:
| 字段名 | 数据类型 | 说明 |
| --- | --- | --- |
| id | int | 材料ID |
| name | varchar | 材料名称 |
| quantity | int | 材料数量 |
接下来是JAVA代码示例,实现了一个简单的餐馆管理系统:
```java
import java.sql.*;
import java.util.*;
public class RestaurantManager {
private static final String DB_URL = "jdbc:mysql://localhost:3306/restaurant";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "password";
private static final String MENU_TABLE = "menu";
private static final String ORDER_TABLE = "orders";
private static final String ORDER_MENU_TABLE = "order_menu";
private static final String MATERIAL_TABLE = "material";
private static Connection conn = null;
private static Statement stmt = null;
private static ResultSet rs = null;
public static void main(String[] args) {
try {
// 连接数据库
conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
// 显示菜单
showMenu();
// 下订单
placeOrder();
// 显示订单
showOrder();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private static void showMenu() throws SQLException {
System.out.println("菜单:");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT id, name, price, image FROM " + MENU_TABLE);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
double price = rs.getDouble("price");
String image = rs.getString("image");
System.out.println(id + "\t" + name + "\t" + price + "\t" + image);
}
}
private static void placeOrder() throws SQLException {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要点的菜品ID和数量,用逗号隔开,多个菜品用分号隔开,例如:1,2;3,1");
String input = scanner.nextLine();
Map<Integer, Integer> orderMap = new HashMap<>();
String[] items = input.split(";");
for (String item : items) {
String[] fields = item.split(",");
int menuId = Integer.parseInt(fields[0]);
int quantity = Integer.parseInt(fields[1]);
orderMap.put(menuId, quantity);
}
// 计算订单总价
double totalPrice = 0.0;
for (Map.Entry<Integer, Integer> entry : orderMap.entrySet()) {
int menuId = entry.getKey();
int quantity = entry.getValue();
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT price FROM " + MENU_TABLE + " WHERE id=" + menuId);
if (rs.next()) {
double price = rs.getDouble("price");
totalPrice += price * quantity;
}
}
// 插入订单记录
stmt = conn.createStatement();
stmt.executeUpdate("INSERT INTO " + ORDER_TABLE + " (create_time, total_price) VALUES (NOW(), " + totalPrice + ")", Statement.RETURN_GENERATED_KEYS);
rs = stmt.getGeneratedKeys();
int orderId = 0;
if (rs.next()) {
orderId = rs.getInt(1);
}
// 插入订单菜品记录
for (Map.Entry<Integer, Integer> entry : orderMap.entrySet()) {
int menuId = entry.getKey();
int quantity = entry.getValue();
stmt = conn.createStatement();
stmt.executeUpdate("INSERT INTO " + ORDER_MENU_TABLE + "(order_id, menu_id, quantity) VALUES (" + orderId + ", " + menuId + ", " + quantity + ")");
}
System.out.println("下单成功,订单号为" + orderId);
}
private static void showOrder() throws SQLException {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查询的订单号:");
int orderId = scanner.nextInt();
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT create_time, total_price FROM " + ORDER_TABLE + " WHERE id=" + orderId);
if (rs.next()) {
Timestamp createTime = rs.getTimestamp("create_time");
double totalPrice = rs.getDouble("total_price");
System.out.println("订单详情:");
System.out.println("下单时间:" + createTime);
System.out.println("总价:" + totalPrice);
System.out.println("菜品列表:");
rs = stmt.executeQuery("SELECT m.name, om.quantity, m.price FROM " + MENU_TABLE + " m JOIN " + ORDER_MENU_TABLE + " om ON m.id=om.menu_id WHERE om.order_id=" + orderId);
while (rs.next()) {
String name = rs.getString("name");
int quantity = rs.getInt("quantity");
double price = rs.getDouble("price");
System.out.println(name + "\t" + quantity + "\t" + price);
}
} else {
System.out.println("订单不存在");
}
}
}
```
以上代码中,showMenu()方法用于显示菜单,placeOrder()方法用于下订单,showOrder()方法用于显示订单。其中,placeOrder()方法中会计算订单总价,并插入订单记录和订单菜品记录。showOrder()方法中会查询订单记录和订单菜品记录,并输出订单详情。