```java
import java.sql.*;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import java.util.*;
// 商品类
class Product {
private String name;
private int quantity;
public Product(String name, int quantity) {
this.name = name;
this.quantity = quantity;
}
public String getName() {
return name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
// 订单类
class Order {
private Map<String, Integer> orderItems;
public Order() {
orderItems = new HashMap<>();
}
public void addOrderItem(String name, int quantity) {
orderItems.put(name, quantity);
System.out.println(quantity + " units of " + name + " added to order.");
}
public void displayOrder() {
System.out.println("Current Order:");
for (Map.Entry<String, Integer> entry : orderItems.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
public Map<String, Integer> getOrderItems() {
return orderItems;
}
}
// 仓库管理系统
class Warehouse {
private DataSource dataSource;
private Map<String, Product> inventory;
private Map<String, Order> orders;
private Map<String, String> userPermissions;
public Warehouse() {
// 初始化数据库连接池
BasicDataSource ds = new BasicDataSource();
ds.setUrl("jdbc:mysql://localhost:3306/your_database");
ds.setUsername("username");
ds.setPassword("password");
dataSource = ds;
// 初始化数据结构
inventory = new HashMap<>();
orders = new HashMap<>();
userPermissions = new HashMap<>();
}
// 添加商品到库存并保存到数据库
public void addProduct(String name, int quantity) {
Product product = new Product(name, quantity);
inventory.put(name, product);
try (Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement("INSERT INTO products (name, quantity) VALUES (?, ?)")) {
ps.setString(1, name);
ps.setInt(2, quantity);
ps.executeUpdate();
System.out.println(quantity + " units of " + name + " added to inventory and saved to database.");
} catch (SQLException e) {
System.out.println("Error adding product to database: " + e.getMessage());
}
}
// 更新商品信息并保存到数据库
public void updateProduct(String name, int newQuantity) {
if (inventory.containsKey(name)) {
Product product = inventory.get(name);
product.setQuantity(newQuantity);
try (Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement("UPDATE products SET quantity = ? WHERE name = ?")) {
ps.setInt(1, newQuantity);
ps.setString(2, name);
ps.executeUpdate();
System.out.println(name + " updated in inventory and database.");
} catch (SQLException e) {
System.out.println("Error updating product in database: " + e.getMessage());
}
} else {
System.out.println(name + " does not exist in inventory.");
}
}
// 删除商品并从数据库中删除记录
public void deleteProduct(String name) {
if (inventory.containsKey(name)) {
inventory.remove(name);
try (Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement("DELETE FROM products WHERE name = ?")) {
ps.setString(1, name);
ps.executeUpdate();
System.out.println(name + " deleted from inventory and database.");
} catch (SQLException e) {
System.out.println("Error deleting product from database: " + e.getMessage());
}
} else {
System.out.println(name + " does not exist in inventory.");
}
}
// 创建订单并保存到数据库
public void createOrder(String orderId) {
Order order = new Order();
orders.put(orderId, order);
System.out.println("Order " + orderId + " created.");
try (Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement("INSERT INTO orders (order_id) VALUES (?)")) {
ps.setString(1, orderId);
ps.executeUpdate();
System.out.println("Order " + orderId + " saved to database.");
} catch (SQLException e) {
System.out.println("Error creating order in database: " + e.getMessage());
}
}
// 添加商品到订单并保存到数据库
public void addProductToOrder(String orderId, String name, int quantity) {
if (orders.containsKey(orderId)) {
Order order = orders.get(orderId);
order.addOrderItem(name, quantity);
try (Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement("INSERT INTO order_items (order_id, product_name, quantity) VALUES (?, ?, ?)")) {
ps.setString(1, orderId);
ps.setString(2, name);
ps.setInt(3, quantity);
ps.executeUpdate();
System.out.println(quantity + " units of " + name + " added to order " + orderId + " and saved to database.");
} catch (SQLException e) {
System.out.println("Error adding product to order in database: " + e.getMessage());
}
} else {
System.out.println("Order " + orderId + " does not exist.");
}
}
// 编辑订单中的商品数量并保存到数据库
public void editOrder(String orderId, String name, int newQuantity) {
if (orders.containsKey(orderId)) {
Order order = orders.get(orderId);
if (order.getOrderItems().containsKey(name)) {
order.getOrderItems().put(name, newQuantity);
try (Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement("UPDATE order_items SET quantity = ? WHERE order_id = ? AND product_name = ?")) {
ps.setInt(1, newQuantity);
ps.setString(2, orderId);
ps.setString(3, name);
ps.executeUpdate();
System.out.println(name + " quantity updated in order " + orderId + " and database.");
} catch (SQLException e) {
System.out.println("Error updating order item in database: " + e.getMessage());
}
} else {
System.out.println(name + " does not exist in order " + orderId + ".");
}
} else {
System.out.println("Order " + orderId + " does not exist.");
}
}
// 取消订单并从数据库中删除订单及订单项
public void cancelOrder(String orderId) {
if (orders.containsKey(orderId)) {
orders.remove(orderId);
try (Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement("DELETE FROM orders WHERE order_id = ?")) {
ps.setString(1, orderId);
ps.executeUpdate();
System.out.println("Order " + orderId + " canceled and removed from database.");
} catch (SQLException e) {
System.out.println("Error canceling order from database: " + e.getMessage());
}
} else {
System.out.println("Order " + orderId + " does not exist.");
}
}
// 设置用户权限并保存到数据库
public void setUserPermission(String username, String permission) {
userPermissions.put(username, permission);
System.out.println("Permission set for user " + username + ": " + permission);
try (Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement("INSERT INTO user_permissions (username, permission) VALUES (?, ?)")) {
ps.setString(1, username);
ps.setString(2, permission);
ps.executeUpdate();
System.out.println("Permission for user " + username + " saved to database.");
} catch (SQLException e) {
System.out.println("Error setting user permission in database: " + e.getMessage());
}
}
// 更新用户权限级别
public void updateUserPermission(String username, String newPermission) {
if (userPermissions.containsKey(username)) {
userPermissions.put(username, newPermission);
try (Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement("UPDATE user_permissions SET permission = ? WHERE username = ?")) {
ps.setString(1, newPermission);
ps.setString(2, username);
ps.executeUpdate();
System.out.println("Permission updated for user " + username + " in database.");
} catch (SQLException e) {
System.out.println("Error updating user permission in database: " + e.getMessage());
}
} else {
System.out.println("User " + username + " does not exist.");
}
}
// 记录日志并保存到数据库
public void log(String username, String action) {
String logMessage = username + " performed " + action;
System.out.println("Log: " + logMessage);
try (Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement("INSERT INTO logs (username, action) VALUES (?, ?)")) {
ps.setString(1, username);
ps.setString(2, action);
ps.executeUpdate();
System.out.println("Log message saved to database.");
} catch (SQLException e) {
System.out.println("Error saving log message to database: " + e.getMessage());
}
}
// 批量处理示例:批量添加商品到数据库
public void addProductsBatch(List<Product> products) {
try (Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement("INSERT INTO products (name, quantity) VALUES (?, ?)")) {
for (Product product : products) {
ps.setString(1, product.getName());
ps.setInt(2, product.getQuantity());
ps.addBatch();
}
ps.executeBatch();
System.out.println("Batch products added to database.");
} catch (SQLException e) {
System.out.println("Error adding batch products to database: " + e.getMessage());
}
}
// 执行性能优化:使用数据库连接池
public void performanceOptimizationExample() {
// 使用已配置的数据源进行数据库操作
try (Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM products")) {
while (rs.next()) {
String name = rs.getString("name");
int quantity = rs.getInt("quantity");
System.out.println(name + ": " + quantity);
}
} catch (SQLException e) {
System.out.println("Error fetching products from database: " + e.getMessage());
}
}
// 主程序演示
public static void main(String[] args) {
Warehouse warehouse = new Warehouse();
// 添加商品示例
warehouse.addProduct("Laptop", 10);
warehouse.addProduct("Monitor", 20);
// 更新商品示例
warehouse.updateProduct("Laptop", 8);
// 删除商品示例
warehouse.deleteProduct("Monitor");
// 创建订单示例
warehouse.createOrder("ORD-001");
// 添加商品到订单示例
warehouse.addProductToOrder("ORD-001", "Laptop", 2);
// 编辑订单中的商品数量示例
warehouse.editOrder("ORD-001", "Laptop", 3);
// 取消订单示例
warehouse.cancelOrder("ORD-001");
// 设置用户权限示例
warehouse.setUserPermission("user1", "admin");
// 更新用户权限示例
warehouse.updateUserPermission("user1", "super admin");
// 记录日志示例
warehouse.log("admin", "added Laptop to inventory");
// 批量添加商品示例
List<Product> products = new ArrayList<>();
products.add(new Product("Mouse", 50));
products.add(new Product("Keyboard", 30));
warehouse.addProductsBatch(products);
// 性能优化示例
warehouse.performanceOptimizationExample();
}
}
```
一个简易的仓库管理系统,结合了数据库操作、商品管理、订单管理、用户权限控制、日志记录和性能优化等功能。每个方法都包含了与数据库的交互,确保数据的持久性和一致性。
Java简易仓管系统
最新推荐文章于 2024-09-19 11:51:06 发布