代码给出 点菜管理模块:该模块涉及到多个实体之间的关联操作,使用Java的GUI类实现点菜的界面,包含CRUD
时间: 2024-04-06 14:35:18 浏览: 11
以下是一个简单的Java餐饮管理系统的点菜管理模块代码示例:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class OrderManagementGUI extends JFrame implements ActionListener {
private JLabel nameLabel, priceLabel, quantityLabel, totalLabel;
private JTextField nameField, priceField, quantityField, totalField;
private JButton addButton, updateButton, deleteButton, clearButton;
private JTable orderTable;
private DefaultTableModel tableModel;
private JScrollPane scrollPane;
private Connection conn;
private Statement stmt;
private ResultSet rs;
public OrderManagementGUI() {
super("Order Management System");
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Initialize GUI components
nameLabel = new JLabel("Dish Name:");
priceLabel = new JLabel("Price:");
quantityLabel = new JLabel("Quantity:");
totalLabel = new JLabel("Total:");
nameField = new JTextField(10);
priceField = new JTextField(10);
quantityField = new JTextField(10);
totalField = new JTextField(10);
addButton = new JButton("Add");
updateButton = new JButton("Update");
deleteButton = new JButton("Delete");
clearButton = new JButton("Clear");
tableModel = new DefaultTableModel();
orderTable = new JTable(tableModel);
scrollPane = new JScrollPane(orderTable);
// Add GUI components to JFrame
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 2));
panel.add(nameLabel);
panel.add(nameField);
panel.add(priceLabel);
panel.add(priceField);
panel.add(quantityLabel);
panel.add(quantityField);
panel.add(totalLabel);
panel.add(totalField);
panel.add(addButton);
panel.add(updateButton);
panel.add(deleteButton);
panel.add(clearButton);
add(panel, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
// Add action listeners to buttons
addButton.addActionListener(this);
updateButton.addActionListener(this);
deleteButton.addActionListener(this);
clearButton.addActionListener(this);
// Connect to database
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/orderdb", "root", "password");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM orders");
populateTable(rs);
} catch (Exception ex) {
ex.printStackTrace();
}
setVisible(true);
}
private void populateTable(ResultSet rs) throws SQLException {
// Clear table
tableModel.setRowCount(0);
// Populate table with data from ResultSet
while (rs.next()) {
String name = rs.getString("name");
double price = rs.getDouble("price");
int quantity = rs.getInt("quantity");
double total = rs.getDouble("total");
Object[] row = {name, price, quantity, total};
tableModel.addRow(row);
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
try {
String name = nameField.getText();
double price = Double.parseDouble(priceField.getText());
int quantity = Integer.parseInt(quantityField.getText());
double total = price * quantity;
// Add new order to database
String query = "INSERT INTO orders (name, price, quantity, total) VALUES ('" + name + "', " + price + ", " + quantity + ", " + total + ")";
stmt.executeUpdate(query);
// Update table
rs = stmt.executeQuery("SELECT * FROM orders");
populateTable(rs);
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == updateButton) {
try {
int row = orderTable.getSelectedRow();
String name = nameField.getText();
double price = Double.parseDouble(priceField.getText());
int quantity = Integer.parseInt(quantityField.getText());
double total = price * quantity;
// Update order in database
String query = "UPDATE orders SET name='" + name + "', price=" + price + ", quantity=" + quantity + ", total=" + total + " WHERE name='" + orderTable.getValueAt(row, 0) + "'";
stmt.executeUpdate(query);
// Update table
rs = stmt.executeQuery("SELECT * FROM orders");
populateTable(rs);
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == deleteButton) {
try {
int row = orderTable.getSelectedRow();
// Delete order from database
String query = "DELETE FROM orders WHERE name='" + orderTable.getValueAt(row, 0) + "'";
stmt.executeUpdate(query);
// Update table
rs = stmt.executeQuery("SELECT * FROM orders");
populateTable(rs);
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == clearButton) {
// Clear input fields
nameField.setText("");
priceField.setText("");
quantityField.setText("");
totalField.setText("");
}
}
public static void main(String[] args) {
new OrderManagementGUI();
}
}
```
以上代码仅为示例,实际开发中需要根据实际需求进行修改和完善。