物品库存管理(纯原创)

程序功能介绍

用文件系统实现物品库存管理程序。一种商品的品种里面包括三个品牌,以品种为单位,可以实现商品品种的添加、删除、查找、显示等功能。

(1) 组成商品品种的品牌不限数量,设置一个最大值(比如10),增加一个成员变量指示品牌的具体数量,商品品种的数量是每个品牌的数量之和

(2)设计输出每日进货与出货的统计标,要求进货与出货分别为两个文本文件,清单上列有进出货物的时间、品种描述、具体品牌的进货数量,如果是出货清单,在每笔出货的清单后还要有该次提取货物的总价格。

(3)商品品种以品种代码的顺序存放。

源码

#include <iostream>
#include <fstream>
#include <cstring>
#include <ctime>
using namespace std;

#define MAXNUM 10

// 品牌
class Brand{
private:
    char brnd_name[20];//品牌名称
    int Brand_code;// 品牌代号
    int quantity;// 数量
    float brnd_price;//价格
public:
    Brand(){quantity=0;strcpy(brnd_name,"\0");}
    friend class CItem;
};

class CItem{
private:
    char Item_name[10];//品种名称
    int Item_code;//品种代号
    char colour[10];//颜色
    int quantity;//数量,所有子brnd中的quantity之和
    Brand brnd[MAXNUM];//品牌
    int nBrnd;//品牌的数量
public:
    CItem(){ strcpy(Item_name,"\0");quantity=0;nBrnd=0;}
    CItem(char *name){ strcpy(Item_name,name);}
    CItem(int code){Item_code=code;}

    // n=0表示要设置品种名称,品种代号,颜色。不设置其他属性。
    // n=1表示要设置品种名称,品种代号,颜色。和输入brnd相关信息
    int SetItem(int n=0) {
        cout << "输入品种名字: ";
        cin >> Item_name;
        cout << "输入品种代号: ";
        cin >> Item_code;
        cout << "输入颜色: ";
        cin >> colour;
        if (n == 1) {
            cout << "输入有多少个品牌: ";
            int numBrands;
            cin >> numBrands;
            if (numBrands < 1 || numBrands > MAXNUM) {
                cout << "非法输入,品牌数不能大于" << MAXNUM<<"或小于1" << endl;
                return 0;
            }
            for (int i = 0; i < numBrands; i++) {
                Brand newBrand;
                cout<<"请输入品牌"<<i+1<<"的信息"<<endl;
                cout << "请输入品牌名称: ";
                cin >> newBrand.brnd_name;
                cout << "请输入品牌代号: ";
                cin >> newBrand.Brand_code;
                cout << "请输入数量: ";
                cin >> newBrand.quantity;
                cout << "请输入价格: ";
                cin >> newBrand.brnd_price;
                brnd[nBrnd] = newBrand;
                nBrnd++;
                quantity+=newBrand.quantity;
            }
        }
        return 1;
    }

    char *GetItemName(){return Item_name;}
    int GetItemCode(){return Item_code;}

    int Compare(CItem &item) {
        // 比较品种名称,如果相同返回1,否则返回0
        return (strcmp(Item_name, item.Item_name) == 0) ? 1 : 0;
    }

    int CompareCode(CItem &item) {
        // 比较品种代码,如果相同返回1,否则返回0
        return (Item_code == item.Item_code) ? 1 : 0;
    }

    void Show() {
        cout << "品种名称: " << Item_name << "   ";
        cout << "品种代码: " << Item_code << "   ";
        cout << "颜色: " << colour<<"   ";
        cout<<"库存:"<<quantity<<endl;
        if(nBrnd!=0){
            cout << "品牌信息:" << endl;
            for (int i = 0; i < nBrnd; i++) {
                cout << "\t名称: " << brnd[i].brnd_name<<" " ;
                cout << "代号: " << brnd[i].Brand_code<<" " ;
                cout << "数量: " << brnd[i].quantity<<" ";
                cout << "价格: " << brnd[i].brnd_price << endl;
            }
            cout<<endl;
        }
    }
    void OutModify() {
        int brandCode;
        int quantitySold=0;

        cout << "请输入品牌代号:";
        cin >> brandCode;

        for (int i = 0; i < nBrnd; i++) {
            if (brnd[i].Brand_code == brandCode) {
                cout << "请输入出库数量:";
                cin >> quantitySold;

                if (brnd[i].quantity >= quantitySold) {
                    brnd[i].quantity -= quantitySold;
                    cout << "成功出库 " << quantitySold << " 个品牌 " << brandCode << "。" << endl;
                    quantity-=quantitySold;
                    SaveOutTransaction("出库清单.txt",brandCode,quantitySold);
                } else {
                    cout << "库存不足,无法出库品牌 " << brandCode << "。" << endl;
                }
                return;
            }
        }
        cout << "未找到品牌 " << brandCode << "。" << endl;
    }

    void InModify() {
        int brandCode;
        int quantityRestocked=0;

        cout << "请输入品牌代号:";
        cin >> brandCode;

        for (int i = 0; i < nBrnd; i++) {
            if (brnd[i].Brand_code == brandCode) {
                cout << "请输入入库数量:";
                cin >> quantityRestocked;

                brnd[i].quantity += quantityRestocked;
                cout << "成功入库 " << quantityRestocked << " 个品牌 " << brandCode << "。" << endl;
                SaveInTransaction("进货清单.txt",brandCode,quantityRestocked);
                return;
            }
        }
        cout << "未找到品牌 " << brandCode << "。正在创建新品牌..." << endl;

        if (nBrnd < MAXNUM) {
            Brand newBrand;
            newBrand.Brand_code = brandCode;
            cout << "请输入品牌名称:";
            cin >> newBrand.brnd_name;
            cout << "请输入品牌价格:";
            cin >> newBrand.brnd_price;
            cout<<"请输入库存数量:";
            cin>>quantityRestocked;
            newBrand.quantity = quantityRestocked;
            brnd[nBrnd] = newBrand;
            nBrnd++;
            cout << "创建了新的品牌 " << brandCode << " 并入库 " << quantityRestocked << " 个。" << endl;
            SaveInTransaction("进货清单.txt",brandCode,quantityRestocked);
        } else {
            cout << "品牌数量已达到上限,无法创建新条目。" << endl;
        }
        quantity+=quantityRestocked;
    }

    void SaveInTransaction(const char* filename, int brandCode, int quantityRestocked) {
        ofstream file(filename, ios::out | ios::app);
        int brndIdx=-1;
        for (int i = 0; i < nBrnd; i++) {
            if(brandCode==brnd[i].Brand_code){
                brndIdx=i;
                break;
            }
        }
        if (file.is_open()) {
            time_t now = time(0);
            tm* timeInfo = localtime(&now);
            file << "时间: " << asctime(timeInfo);
            file << "品种名称: " << Item_name << "   ";
            file << "品种代码: " << Item_code << "   ";
            file << "品牌名称: " << brnd[brndIdx].brnd_name << "   ";
            file << "品牌数量: " << quantityRestocked << endl;
            file.close();
        } else {
        }
    }

    void SaveOutTransaction(const char* filename, int brandCode, int quantitySold) {
        ofstream file(filename, ios::out | ios::app);
        int brndIdx=-1;
        for (int i = 0; i < nBrnd; i++) {
            if(brandCode==brnd[i].Brand_code){
                brndIdx=i;
                break;
            }
        }
        if (file.is_open()) {
            time_t now = time(0);
            tm* timeInfo = localtime(&now);
            file << "时间: " << asctime(timeInfo);
            file << "品种名称: " << Item_name << "   ";
            file << "品种代码: " << Item_code << "   ";
            file << "品牌名称: " << brnd[brndIdx].brnd_name << "   ";
            file << "品牌数量: " << quantitySold << "   ";
            file << "价格: " << brnd[brndIdx].brnd_price * (float )quantitySold << endl;
            file <<endl;
            file.close();
        } else {
        }
    }

};

class CNode
{
private:
    CItem *pData;
    CNode *pNext;
public:
    CNode(){pData= 0;pNext= 0;}
    CNode(CNode &node){
        pData=node.pData;
        pNext=node.pNext;
    }
    void InputData(CItem *pSal){pData=pSal;}
    void ShowNode(){pData->Show();}
    CItem * GetData(){return pData;}
    friend class CList;
};

class CList{
protected:
    CNode *pHead;
public:
    CList(){pHead= 0;};
    ~CList(){DeleteList();};
    void AddNode(CNode* pNode) {
        if (pHead== 0) {
            pHead = pNode;
        } else {
            CNode* temp = pHead;
            while (temp->pNext!= 0) {
                temp = temp->pNext;
            }
            temp->pNext = pNode;
        }
    }
    CNode* DeleteNode(CNode* pNode) {
        if (pHead == pNode) {
            pHead = pNode->pNext;
            pNode->pNext = 0;
            return pNode;
        } else {
            CNode* current = pHead;
            while (current->pNext && current->pNext != pNode) {
                current = current->pNext;
            }
            if (current->pNext!= 0) {
                current->pNext = pNode->pNext;
                pNode->pNext = 0;
                return pNode;
            } else {
                return 0;
            }
        }
    }
    CNode* LookUp(CItem& item) {
        CNode* current = pHead;
        while (current!= 0) {
            if (item.GetItemCode()!=0){
                if (current->pData->CompareCode(item) == 1) {
                    return current;
                }
            }else {
                if (current->pData->Compare(item) == 1) {
                    return current;
                }
            }
            current = current->pNext;
        }
        return 0;
    }
    void ShowList() {
        CNode* current = pHead;
        while (current!= 0) {
            current->ShowNode();
            current = current->pNext;
        }
    }
    void DeleteList() {
        while (pHead) {
            CNode* current = pHead;
            pHead = pHead->pNext;
            delete current;
        }
    }
    CNode* GetListHead() {
        return pHead;
    }
    CNode* GetListNextNode(CNode* pNode) {
        if (pNode) {
            return pNode->pNext;
        } else {
            return 0;
        }
    }
    void Insert(CNode* pNode) {
        pNode->pNext = pHead;
        pHead = pNode;
    }
    // 添加保存数据到文件的方法
    void SaveToFile(const char* filename) {
        ofstream file(filename, ios::out | ios::binary);
        if (file.is_open()) {
            // 遍历链表并将数据写入文件
            CNode* currentNode = pHead;
            while (currentNode) {
                CItem* item = currentNode->GetData();
                file.write(reinterpret_cast<char*>(item), sizeof(CItem));
                currentNode = currentNode->pNext;
            }
            file.close();
        } else {
        }
    }

    // 添加从文件加载数据的方法
    void LoadFromFile(const char* filename) {
        ifstream file(filename, ios::in | ios::binary);
        if (file.is_open()) {
            while (true) {
                CItem *item=new CItem();
                file.read(reinterpret_cast<char*>(item), sizeof(CItem));
                if (file.eof()) {
                    break; // 文件结束,退出循环
                }
                CNode* newNode = new CNode();
                newNode->InputData(item);
                AddNode(newNode);
            }
            file.close();
            cout << "从文件 " << filename << " 成功加载数据。" << endl;
        } else {
        }
    }
};

int main() {
    CList itemList;
    itemList.LoadFromFile("商品品种数据.txt");
    while (true) {
        cout << "-------------------" << endl;
        cout << "商品管理系统" << endl;
        cout << "-------------------" << endl;
        cout << "1. 添加商品品种" << endl;
        cout << "2. 显示商品品种" << endl;
        cout << "3. 查询商品" << endl;
        cout << "4. 删除商品" << endl;
        cout << "5. 商品出库" << endl;
        cout << "6. 商品进库" << endl;
        cout << "0. 退出并保存" << endl;

        int choice;
        cout << "请选择操作: ";
        cin >> choice;

        switch (choice) {
            case 1: {
                CItem* newItem=new CItem();
                if (newItem->SetItem(0) == 1) {
                    CNode* newNode = new CNode();
                    newNode->InputData(newItem);
                    itemList.AddNode(newNode);
                    cout << "商品品种添加成功!" << endl;
                }else{
                    cout << "商品品种添加失败!" << endl;
                }
                break;
            }
            case 2: {
                cout << "商品品种列表:" << endl;
                itemList.ShowList();
                break;
            }
            case 3: {
                int subMenuChoice;
                while (true) {
                    cout << "-------------------" << endl;
                    cout << "查询方式" << endl;
                    cout << "-------------------" << endl;
                    cout << "1. 按商品品种名称查询商品" << endl;
                    cout << "2. 按商品品种代码查询商品" << endl;
                    cout << "0. 退出" << endl;
                    cout << "请选择出库方式: ";
                    cin >> subMenuChoice;
                    switch (subMenuChoice) {
                        case 1: {
                            char outName[10];
                            cout << "请输入要删除的商品品种名称: ";
                            cin >> outName;
                            CItem searchItem(outName);
                            CNode* foundNode = itemList.LookUp(searchItem);
                            if (foundNode) {
                                cout << "找到以下商品品种:" << endl;
                                foundNode->ShowNode();
                            } else {
                                cout << "未找到指定的商品品种。" << endl;
                            }
                            break;
                        }
                        case 2: {
                            int searchCode;
                            cout << "请输入要查询的商品品种代码: ";
                            cin >> searchCode;
                            CItem searchItem(searchCode);
                            CNode* foundNode = itemList.LookUp(searchItem);
                            if (foundNode) {
                                cout << "找到以下商品品种:" << endl;
                                foundNode->ShowNode();
                            } else {
                                cout << "未找到指定的商品品种。" << endl;
                            }
                            break;
                        }
                        case 0: {
                            break;
                        }
                        default: {
                            cout << "非法选项,请重新选择出库方式。" << endl;
                        }
                    }
                    if (subMenuChoice == 0) {
                        break;
                    }
                }

                break;
            }
            case 4: {
                int subMenuChoice;
                while (true) {
                    cout << "-------------------" << endl;
                    cout << "删除方式" << endl;
                    cout << "-------------------" << endl;
                    cout << "1. 按商品品种名称删除商品" << endl;
                    cout << "2. 按商品品种代码删除商品" << endl;
                    cout << "0. 退出" << endl;
                    cout << "请选择出库方式: ";
                    cin >> subMenuChoice;
                    switch (subMenuChoice) {
                        case 1: {
                            char outName[10];
                            cout << "请输入要删除的商品品种名称: ";
                            cin >> outName;
                            CItem deleteItem(outName);
                            CNode* deletedNode = itemList.LookUp(deleteItem);
                            if (deletedNode) {
                                itemList.DeleteNode(deletedNode);
                                delete deletedNode;
                                cout << "商品品种删除成功!" << endl;
                            } else {
                                cout << "未找到指定的商品品种。无法删除。" << endl;
                            }
                            break;
                        }
                        case 2: {
                            int deleteCode;
                            cout << "请输入要删除的商品品种代码: ";
                            cin >> deleteCode;
                            CItem deleteItem(deleteCode);
                            CNode* deletedNode = itemList.LookUp(deleteItem);
                            if (deletedNode) {
                                itemList.DeleteNode(deletedNode);
                                delete deletedNode;
                                cout << "商品品种删除成功!" << endl;
                            } else {
                                cout << "未找到指定的商品品种。无法删除。" << endl;
                            }
                            break;
                        }
                        case 0: {
                            break;
                        }
                        default: {
                            cout << "非法选项,请重新选择出库方式。" << endl;
                        }
                    }
                    if (subMenuChoice == 0) {
                        break;
                    }
                }
                break;
            }
            case 5: {
                int subMenuChoice;
                while (true) {
                    cout << "-------------------" << endl;
                    cout << "出库方式" << endl;
                    cout << "-------------------" << endl;
                    cout << "1. 按商品品种名称查询商品出库" << endl;
                    cout << "2. 按商品品种代码查询商品出库" << endl;
                    cout << "0. 退出" << endl;
                    cout << "请选择出库方式: ";
                    cin >> subMenuChoice;
                    switch (subMenuChoice) {
                        case 1: {
                            char outName[10];
                            cout << "请输入要出库的商品品种名称: ";
                            cin >> outName;
                            CItem outItem(outName);
                            CNode* outNode = itemList.LookUp(outItem);
                            if (outNode) {
                                outNode->GetData()->OutModify();
                            } else {
                                cout << "未找到指定的商品品种。无法出库。" << endl;
                            }
                            break;
                        }
                        case 2: {
                            int outCode;
                            cout << "请输入要出库的商品品种代码: ";
                            cin >> outCode;
                            CItem outItem(outCode);
                            CNode* outNode = itemList.LookUp(outItem);
                            if (outNode) {
                                outNode->GetData()->OutModify();
                            } else {
                                cout << "未找到指定的商品品种。无法出库。" << endl;
                            }
                            break;
                        }
                        case 0: {
                            break;
                        }
                        default: {
                            cout << "非法选项,请重新选择出库方式。" << endl;
                        }
                    }
                    if (subMenuChoice == 0) {
                        break;
                    }
                }
                break;
            }
            case 6: {
                int subMenuChoice;
                while (true) {
                    cout << "-------------------" << endl;
                    cout << "入库方式" << endl;
                    cout << "-------------------" << endl;
                    cout << "1. 按商品品种名称入库商品" << endl;
                    cout << "2. 按商品品种代码出库商品" << endl;
                    cout << "0. 退出" << endl;
                    cout << "请选择出库方式: ";
                    cin >> subMenuChoice;
                    switch (subMenuChoice) {
                        case 1: {
                            char outName[10];
                            cout << "请输入要入库的商品品种名称: ";
                            cin >> outName;
                            CItem inItem(outName);
                            CNode* inNode = itemList.LookUp(inItem);
                            if (inNode) {
                                inNode->GetData()->InModify();
                            } else {
                                cout << "未找到指定的商品品种。正在创建新品种..." << endl;
                                CItem newItem;
                                newItem.SetItem(1);
                                CNode* newNode = new CNode();
                                newNode->InputData(&newItem);
                                itemList.AddNode(newNode);
                                cout << "新商品品种已创建并入库。" << endl;
                            }
                            break;
                        }
                        case 2: {
                            int inCode;
                            cout << "请输入要入库的商品品种代码: ";
                            cin >> inCode;
                            CItem inItem(inCode);
                            CNode* inNode = itemList.LookUp(inItem);
                            if (inNode) {
                                inNode->GetData()->InModify();
                            } else {
                                cout << "未找到指定的商品品种。正在创建新品种..." << endl;
                                CItem newItem;
                                newItem.SetItem(1);
                                CNode* newNode = new CNode();
                                newNode->InputData(&newItem);
                                itemList.AddNode(newNode);
                                cout << "新商品品种已创建并入库。" << endl;
                            }
                            break;
                        }
                        case 0: {
                            break;
                        }
                        default: {
                            cout << "非法选项,请重新选择出库方式。" << endl;
                        }
                    }
                    if (subMenuChoice == 0) {
                        break;
                    }
                }
                break;
            }
            case 0: {

                cout << "退出系统。" << endl;
                return 0;
            }
            default: {
                cout << "非法选项,请重新选择。" << endl;
            }
        }
        itemList.SaveToFile("商品品种数据.txt");
    }
}


结尾

如有需帮助,请私信我

赞助

如果您觉得有帮助,就给小编打打赏吧

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值