Java简易仓管系统

```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();
    }
}
```

一个简易的仓库管理系统,结合了数据库操作、商品管理、订单管理、用户权限控制、日志记录和性能优化等功能。每个方法都包含了与数据库的交互,确保数据的持久性和一致性。

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VB6.0仓管系统界面是指使用Visual Basic 6.0编写的一个用于仓库管理的软件界面。该界面主要用于展示仓库库存情况、管理进出货物以及生成相关报表等功能。 在仓管系统界面中,通常会包括以下几个主要模块: 1. 登录界面:用户需要输入正确的用户名和密码才能进入系统。这样可以确保只有授权人员可以访问仓库管理功能。 2. 主界面:主界面是仓管系统的核心界面,通常包括菜单栏、工具栏和数据展示区。菜单栏提供了各种功能模块的快捷访问选项,例如查询库存、管理进出货等;工具栏包含一些常用的操作按钮,便于用户快速执行某些功能;而数据展示区则用于显示当前选中模块的具体信息。 3. 库存管理界面:库存管理界面用于显示仓库中各个货物的库存情况。用户可以查询某个货物的库存数量、位置、采购日期等信息。同时,还可以进行库存盘点、调整库存、新增货物等操作。 4. 进出货管理界面:进出货管理界面可以记录货物的进出仓记录。用户可以输入货物的相关信息,例如名称、数量、供应商等,然后根据需要选择是入库还是出库,并进行相应的操作。 5. 报表界面:仓管系统通常还会提供一些报表功能,用于生成一些统计信息。例如,用户可以生成库存报表、进货报表、出货报表等,以便更好地了解仓库的存货情况和经营状况。 总体来说,VB6.0仓管系统界面通过简洁明了的布局和直观的操作方式,帮助用户更高效地管理仓库的存货情况,确保货物的正常发货和供应链的畅通。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值