C++ 仓库管理系统实现:货物入库、出库和租金计算

本文将介绍使用 C++ 语言实现一个简单的仓库管理系统,该系统包含以下功能:

  • 定义仓库类,包含库房容量、状态、日租金和入库时间等信息。
  • 实现货物入库操作,选择合适的仓库进行存储,并记录入库时间。
  • 实现货物出库操作,释放仓库并计算租金。

代码实现

#include <iostream>
#include <vector>

using namespace std;

class Warehouse {
private:
    int capacity;   // 库房容量
    bool isOccupied;    // 库房状态
    double rent;    // 库房日租金
    int storageTime;    // 入库时间

public:
    Warehouse(int cap, double r) {
        capacity = cap;
        isOccupied = false;
        rent = r;
        storageTime = 0;
    }

    bool isAvailable() {
        return !isOccupied;
    }

    void occupy(int time) {
        isOccupied = true;
        storageTime = time;
    }

    void release() {
        isOccupied = false;
        storageTime = 0;
    }

    double calculateRent(int time) {
        int days = time - storageTime;
        return days * rent;
    }

    int getCapacity() {
        return capacity;
    }
};

class WarehouseManager {
private:
    vector<Warehouse> warehouses;

public:
    WarehouseManager(vector<int> capacities, vector<double> rents) {
        for (int i = 0; i < capacities.size(); i++) {
            Warehouse w(capacities[i], rents[i]);
            warehouses.push_back(w);
        }
    }

    int findAvailableWarehouse(int weight) {
        int minCapacity = INT_MAX;
        int minIndex = -1;

        for (int i = 0; i < warehouses.size(); i++) {
            if (warehouses[i].isAvailable() && warehouses[i].getCapacity() >= weight) {
                if (warehouses[i].getCapacity() < minCapacity) {
                    minCapacity = warehouses[i].getCapacity();
                    minIndex = i;
                }
            }
        }

        return minIndex;
    }

    void storeGoods(int weight, int time) {
        int warehouseIndex = findAvailableWarehouse(weight);
        if (warehouseIndex != -1) {
            warehouses[warehouseIndex].occupy(time);
            cout << 'Goods stored in Warehouse ' << warehouseIndex + 1 << endl;
        } else {
            cout << 'No available warehouse for the goods' << endl;
        }
    }

    void releaseGoods(int warehouseIndex, int time) {
        if (warehouseIndex >= 0 && warehouseIndex < warehouses.size() && warehouses[warehouseIndex].isAvailable()) {
            double rent = warehouses[warehouseIndex].calculateRent(time);
            warehouses[warehouseIndex].release();
            cout << 'Goods released from Warehouse ' << warehouseIndex + 1 << ', rent: ' << rent << endl;
        } else {
            cout << 'Invalid warehouse index' << endl;
        }
    }
};

int main() {
    vector<int> capacities = {100, 200, 300};
    vector<double> rents = {10.0, 20.0, 30.0};

    WarehouseManager manager(capacities, rents);

    manager.storeGoods(150, 1);
    manager.storeGoods(250, 2);
    manager.storeGoods(100, 3);

    manager.releaseGoods(0, 5);
    manager.releaseGoods(1, 6);
    manager.releaseGoods(2, 7);

    return 0;
}

代码说明

  1. Warehouse 类

    • capacity: 仓库容量
    • isOccupied: 仓库状态,true 表示占用,false 表示空闲
    • rent: 仓库日租金
    • storageTime: 货物入库时间
    • isAvailable(): 检查仓库是否空闲
    • occupy(int time): 占用仓库,并记录入库时间
    • release(): 释放仓库
    • calculateRent(int time): 计算租金
    • getCapacity(): 获取仓库容量
  2. WarehouseManager 类

    • warehouses: 仓库列表,使用 vector 存储
    • WarehouseManager(vector<int> capacities, vector<double> rents): 构造函数,初始化仓库列表
    • findAvailableWarehouse(int weight): 查找可用的仓库,优先选择容量最小的仓库
    • storeGoods(int weight, int time): 将货物存入仓库,记录入库时间
    • releaseGoods(int warehouseIndex, int time): 释放仓库,并计算租金

示例说明

  • main 函数中创建了一个 WarehouseManager 对象,并指定了三个仓库的容量和日租金。
  • 然后,调用 storeGoods 函数将不同重量的货物存入仓库。
  • 最后,调用 releaseGoods 函数释放仓库,并计算相应的租金。

总结

本文介绍了使用 C++ 实现仓库管理系统的基本方法,包括仓库类的定义、货物入库、出库操作以及租金计算。该系统可以方便地管理多个仓库,并根据货物重量和租金进行合理选择和费用结算。

注意:

  • 上述代码仅为简化示例,没有进行输入验证和错误处理等。在实际应用中,可能需要更完善的设计和代码实现。
  • 可以根据实际需求扩展功能,例如添加货物信息、用户管理、库存管理等。
  • 可以使用数据库来存储仓库和货物数据,提高数据安全性并方便管理。
C++ 仓库管理系统实现:货物入库、出库和租金计算

原文地址: https://www.cveoy.top/t/topic/cHWR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录