根据以下要求,使用MySQL编写一个餐馆管理系统,并使用JAVA或C语言给出合适的代码,然后给出其数据库及连接。要求:主要有前台、顾客、后厨三个方面;在前台部分,餐馆对外有一个相对稳定的菜单,上面有菜品的照片和价格,但是也会时不时增加、减少或者修改菜单;顾客可以点菜和用餐结束之后核实票据并付款;票据中有菜品及其价格的列表、下单时间、总费用等信息;后厨主要负责材料的维护,餐馆内部记录着菜单上每道菜需要消耗的材料量。收到顾客订单后,依据菜品准备材料。
时间: 2024-03-15 22:41:33 浏览: 70
以下是基于MySQL的餐馆管理系统的数据库设计:
1. 菜品表(menu)
- id:菜品ID
- name:菜品名称
- price:菜品价格
- image:菜品图片
- status:菜品状态(上架/下架)
2. 材料表(material)
- id:材料ID
- name:材料名称
- quantity:材料数量
3. 菜品-材料关联表(menu_material)
- menu_id:菜品ID
- material_id:材料ID
- quantity:材料数量
4. 订单表(order)
- id:订单ID
- create_time:下单时间
- total_price:总价
- status:订单状态(未付款/已付款)
- customer_id:顾客ID
5. 订单-菜品关联表(order_menu)
- order_id:订单ID
- menu_id:菜品ID
- quantity:菜品数量
6. 顾客表(customer)
- id:顾客ID
- name:顾客姓名
- phone:顾客电话
7. 厨房表(kitchen)
- id:厨房ID
- name:厨房名称
8. 厨房-菜品关联表(kitchen_menu)
- kitchen_id:厨房ID
- menu_id:菜品ID
以下是Java语言的代码示例:
1. 菜品类(Menu)
```java
public class Menu {
private int id;
private String name;
private double price;
private String image;
private boolean status;
// getter and setter methods
}
```
2. 材料类(Material)
```java
public class Material {
private int id;
private String name;
private int quantity;
// getter and setter methods
}
```
3. 订单类(Order)
```java
public class Order {
private int id;
private Date createTime;
private double totalPrice;
private boolean status;
private int customerId;
private List<Menu> menus;
// getter and setter methods
}
```
4. 顾客类(Customer)
```java
public class Customer {
private int id;
private String name;
private String phone;
// getter and setter methods
}
```
5. 厨房类(Kitchen)
```java
public class Kitchen {
private int id;
private String name;
private List<Menu> menus;
// getter and setter methods
}
```
6. 数据库连接类(DBConnection)
```java
public class DBConnection {
private static final String URL = "jdbc:mysql://localhost:3306/restaurant";
private static final String USERNAME = "root";
private static final String PASSWORD = "password";
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return connection;
}
}
```
7. 数据库操作类(DBOperation)
```java
public class DBOperation {
public static List<Menu> getMenuList() {
List<Menu> menuList = new ArrayList<>();
try (Connection connection = DBConnection.getConnection()) {
String sql = "SELECT * FROM menu WHERE status = 1";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Menu menu = new Menu();
menu.setId(resultSet.getInt("id"));
menu.setName(resultSet.getString("name"));
menu.setPrice(resultSet.getDouble("price"));
menu.setImage(resultSet.getString("image"));
menu.setStatus(resultSet.getBoolean("status"));
menuList.add(menu);
}
} catch (SQLException e) {
e.printStackTrace();
}
return menuList;
}
public static List<Material> getMaterialList() {
List<Material> materialList = new ArrayList<>();
try (Connection connection = DBConnection.getConnection()) {
String sql = "SELECT * FROM material";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Material material = new Material();
material.setId(resultSet.getInt("id"));
material.setName(resultSet.getString("name"));
material.setQuantity(resultSet.getInt("quantity"));
materialList.add(material);
}
} catch (SQLException e) {
e.printStackTrace();
}
return materialList;
}
public static boolean placeOrder(Order order) {
boolean success = false;
try (Connection connection = DBConnection.getConnection()) {
connection.setAutoCommit(false);
String sql1 = "INSERT INTO `order` (create_time, total_price, status, customer_id) VALUES (?, ?, ?, ?)";
PreparedStatement statement1 = connection.prepareStatement(sql1, Statement.RETURN_GENERATED_KEYS);
statement1.setTimestamp(1, new Timestamp(order.getCreateTime().getTime()));
statement1.setDouble(2, order.getTotalPrice());
statement1.setBoolean(3, order.isStatus());
statement1.setInt(4, order.getCustomerId());
statement1.executeUpdate();
ResultSet resultSet = statement1.getGeneratedKeys();
if (resultSet.next()) {
int orderId = resultSet.getInt(1);
String sql2 = "INSERT INTO order_menu (order_id, menu_id, quantity) VALUES (?, ?, ?)";
PreparedStatement statement2 = connection.prepareStatement(sql2);
for (Menu menu : order.getMenus()) {
statement2.setInt(1, orderId);
statement2.setInt(2, menu.getId());
statement2.setInt(3, 1);
statement2.addBatch();
}
statement2.executeBatch();
success = true;
}
if (success) {
connection.commit();
} else {
connection.rollback();
}
} catch (SQLException e) {
e.printStackTrace();
}
return success;
}
}
```
阅读全文