C++实现商店仓库管理系统

Bianca ·
更新时间:2024-09-20
· 1414 次阅读

本文实例为大家分享了C++实现商店仓库管理系统的具体代码,供大家参考,具体内容如下

一、问题描述

系统应具有下列主要功能:输入记录功能:从键盘输入货物信息:商品代号,商品名称, 数量,价格,所属类别(如家用电器、日用品等)等;修改商品数量、删除记录功能、按商品代号查询、按商品代号排序并显示等功能。

二、基本要求

(1)使用面向对象编程思想编写开发过程中需要用到的类,使用继承的方法构造至少 3个类(即商品类(虚基类),家用电器类和日用品类(派生类)),另外再设计一个管理类,实现对商品的管理;
(2)输入和输出可以使用文本文件重定向输入(保存数据为磁盘文件);也可以使用标准输入输出进行(提交时需要提交TXT格式输入数据)。包含各类商品信息,程序运行时进行初始化数据,使用vector 数组存放对象指针。并能保存数据为磁盘文件。
(3)程序运行时使用菜单显示添加(输入)记录,修改商品数量,浏览商品信息,按商品代号查找 ,删除记录。
(4)编写同名 display() 成员函数既虚函数,用来输出所有商品的信息。要求对<< 和>>
运算符进行重载,实现信息的输入输出。
(5) 基本功能要求具有增、删、改、查。

基本流程图

#include <iostream>//基本的输入输出  #include <fstream> //文件操作  #include <cstring>//strcmp函数,比较两个字符串  #include <conio.h>//用getch(); #include <vector>//vector数组  #define SIZE 100  //采用宏定义,定义char数组的大小  using namespace std; class Goods//Goods类定义  {     public:         Goods(){}//无参数无初值的构造函数 ,缺省构造函数          char Number[SIZE];//编号          char Name[SIZE];//商品名          int Amount;//数量          float Price;//价格          char Type[SIZE];//类别          Goods * Next;//指针          vector<Goods> Manage; //vector数组的定义          friend ostream& operator<<(ostream& out,Goods&  obj)//重载<<输出运算符          {                 out<<obj.Number<<obj.Name<<obj.Amount<<obj.Type;         }         friend istream& operator>>(istream& in,Goods& obj)//重载>>输入运算符          {                 in>>obj.Number>>obj.Name>>obj.Amount>>obj.Type;         }         void SetType()//设置商品类别          {    cout<<" 请选择种类:";    cin>>Type;}         void SetName()//设置商品名          {    cout<<" 请输入商品的名称:"; cin>>Name;}         void SetNumber()//设置商品编号          {    cout<<"  请输入商品的编号:"; cin>>Number;}         void SetPrice()//设置类商品价格          {cout<<" 请输入商品单价:"; cin>>Price;}         void SetAmount()//设置商品数量          {cout<<" 请输入商品库存:"; cin>>Amount;}         void SetOther() //设置其他数据          {    cout<<"  请输入商品价格:"; cin>>Price;             cout<<"  请输入存货数量:"; cin>>Amount;}              void ReadFile(istream & in)//读取文件          {    in>>Name>>Type>>Number>>Price>>Amount;}             void SetAll()//成员函数  功能:输入信息          {             SetName();             SetType();             SetNumber();             SetOther();                  }         void Show()//输出商品信息          {    cout<<"商品名: "<<Name<<endl<<"种类:"<<Type<<endl<<"编号: "<<Number<<endl<<"价格 "<<Price<<endl<<"商品库存: "<<Amount<<endl<<endl;} }; class LifeGoods:public Goods//生活用品类  {     public:         LifeGoods(){}//构造函数          ~ LifeGoods(){} //析构函数          char Number[SIZE];//编号          char Name[SIZE];//商品名          int Amount;//数量          float Price;//价格  }; class electricLifeGoods:public Goods//家用电器类  {     electricLifeGoods() {}//构造函数      ~electricLifeGoods(){}//析构函数      char Number[SIZE];//编号      char Name[SIZE];//商品名      int Amount;//数量      float Price;//价格  }; class Manage:public Goods//管理类  {     public:         Goods * Head,* End;//定义商品类的头结点和尾节点的指针          int i;//记录商品总数目          ifstream in;//打开文件输入信息          ofstream out;//关闭文件储存信息          Manage();//构造函数          ~Manage();//析构函数          void AddGoods();//添加商品信息          void ShowMenu(int n);//显示菜单,参数n用于switch进行增删改查作          void FindGoods();//查找商品          void SaveGoods();//保存商品信息          void ChangeGoods();//修改商品内容          void DelGoods();//删除商品信息          int ListCount();//计算商品个数         void Display()//显示所有商品信息          {             system("cls"); //清空屏幕,美观              i=0;             for(Goods * goods=Head->Next;goods!=End;goods=goods->Next)//从头结点循环到尾节点,输出全部的商品信息              {                 goods->Show(); //输出每一个结点的各条信息                  i++;             }             cout<<"共有"<<i<<"个商品"<<"\n"<<endl;                 cout<<"按任意键继续......";             getch();         }         Goods *FindName(char * Name)//按姓名查找          {             for(Goods * goods=Head;goods->Next!=End;goods=goods->Next)//匹配成功则返回上一个指针,不成功就返回空                 if(!strcmp(goods->Next->Name,Name))return goods;                 return NULL;         }         Goods *FindNumber(char * Number)//按编号查找          {             for(Goods * goods=Head;goods->Next!=End;goods=goods->Next)//匹配成功则返回上一个指针,不成功就返回空                 if(!strcmp(goods->Next->Number,Number))return goods;               return NULL;         } };  void Manage::AddGoods()//从键盘输入商品信息 {     system("cls");//清空屏幕      ShowMenu(1);//调用菜单函数      End->SetName();     End->SetType();     do     {End->SetNumber();}while(FindNumber(End->Number));//当编号不为空时输入每一条信息      End->SetOther();     End->Next = new Goods;//开辟新空间,存储新的商品信息      End=End->Next;     cout<<"添加成功!"<<endl;     SaveGoods();     cout<<"按任意键继续......";     getch(); } Manage::Manage()     //构造函数在类外实现  {     Head=new Goods;//开辟一个新的结点,让头指针指向新结点      Head->Next=new Goods;      End=Head->Next;     in.open("仓库.txt");//打开仓库文件      if(!in)//如果打开失败          cout<<"没有库存"<<endl;     else     {         while(!in.eof())//循环读入仓库文件内的数据,直到空为止          {             End->ReadFile(in);             if(End->Name[0]=='\0')break;//当名字为0是结束读取              End->Next=new Goods;             End=End->Next;         }         in.close();//关闭文件          cout<<" 读取商品信息成功!"<<endl<<endl;     } } void Manage::ShowMenu(int n)//菜单 ,参数不同调用不同的菜单  {     switch(n) //根据n来调用不同的菜单      {         case 1:         {             cout<<"************************************************************"<<endl                 <<"*         1、生活用品              2、家用电器             *" <<endl                  <<"************************************************************"<<endl;         break;}         case 2:         {         system("cls");         cout<<"************************************************************"<<endl             <<"*                   商 店 仓 库 管 理 系 统                *" <<endl              <<"************************************************************"<<endl             <<"*                      1、增 加 商 品                      *" <<endl             <<"*                      2、显 示 商 品                      *" <<endl             <<"*                      3、查 找 商 品                      *" <<endl             <<"*                      4、删 除 商 品                      *" <<endl             <<"*                      5、修 改 商 品                      *" <<endl             <<"*                      6、保 存 商 品                      *" <<endl             <<"*                      0、退 出 系 统                      *" <<endl             <<"************************************************************"<<endl             <<endl<<"  请选择(0-7):  ";         break;}         case 3:         {         system("cls");         cout<<"************************************************************"<<endl             <<"*      1、修改商品名             4、修改价格               *" <<endl              <<"*      2、修改类别               5、修改编商品量           *" <<endl              <<"*      3、修改编号               10、修改全部               *" <<endl              <<"************************************************************"<<endl             <<endl<<"  请选择:  ";         break;         }         case 5:         {         system("cls");         cout<<"************************************************************"<<endl             <<"*                     1、按商品名查找                      *" <<endl              <<"*                     2、按商品编号查找                    *" <<endl              <<"************************************************************"<<endl                 <<endl<<"  请选择:  ";         break;         }     } } Manage::~Manage() //析构函数  {     for(Goods * temp;Head->Next!=End;) //循环遍历,释放所有的指针      {         temp=Head->Next;         Head->Next=Head->Next->Next;         delete temp;     }     delete Head,End; } void Manage::FindGoods() //查找商品  {     system("cls");     char Name[SIZE] ,Number[10];     int Input;     Goods * goods=NULL;//初始化指针      ShowMenu(5);//调用菜单      cin>>Input;//按姓名或者编号查询      switch(Input)     {         case 1:{cout<<" 请输入要查找的商品的名称:";cin>>Name;             if(goods=FindName(Name))             {    goods->Next->Show();                    cout<<"按任意键继续......";                     getch();}             else{                 cout<<" 没有找到该名称的商品!"<<'\n'<<endl;                 cout<<"按任意键继续......";                 getch();}    }break;         case 2:             {     cout<<" 请输入要查找的商品的编号:";cin>>Number;                 if(goods=FindNumber(Number))                 {                     goods->Next->Show();                     cout<<"按任意键继续......";                     getch();                 }                 else{                     cout<<" 没有找到该编号的商品!"<<'\n'<<endl;                     cout<<"按任意键继续......";                     getch();                 }          }break;     }         } void Manage::ChangeGoods() //修改商品信息 {     ShowMenu(3);//调用菜单      int Input;     cin>>Input;     switch(Input)     {         case 1:         {             char Number[SIZE];             Goods * goods=NULL;             cout<<" 请输入要修改的商品的编号:";cin>>Number;             if(goods=FindNumber(Number))             {     cout<<" 已找到商品的信息,请输入新的信息!"<<endl;                 goods->Next->SetName();//将新输入的姓名存到磁盘中                  cout<<"修改成功!"<<endl;                 cout<<"按任意键继续......";                 getch();}             else{                     cout<<"  未找到指定商品,请确认后重新查找!"<<endl;                     cout<<"按任意键继续......";                     getch();                 }                 break;             }         case 2:         {             char Number[SIZE];             Goods * goods=NULL;             cout<<" 请输入要修改的商品的编号:";cin>>Number;             if(goods=FindNumber(Number))             {                 cout<<" 已找到商品的信息,请输入新的信息!"<<endl;                 goods->Next->SetType();//将新输入的类别存到磁盘中                  cout<<"修改成功!"<<endl;                     cout<<"按任意键继续......";                     getch();}             else{                 cout<<" 未找到指定商品,请确认后重新查找!"<<endl;                 cout<<"按任意键继续......";                 getch();}    break;}         case 3:             {             char Number[SIZE];             Goods * goods=NULL;             cout<<" 请输入要修改的商品的编号:";cin>>Number;             if(goods=FindNumber(Number))             {                 cout<<" 已找到商品的信息,请输入新的信息!"<<endl;                 goods->Next->SetNumber();//将新输入的编号存到磁盘中                  cout<<"修改成功!"<<endl;                 cout<<"按任意键继续......";                 getch();             }             else{                 cout<<" 未找到指定商品,请确认后重新查找!"<<endl;                 cout<<"按任意键继续......";                 getch();}    break;}         case 4:             {             char Number[SIZE];             Goods * goods=NULL;             cout<<" 请输入要修改的商品的编号:";cin>>Number;             if(goods=FindNumber(Number))             {                 cout<<" 已找到商品的信息,请输入新的信息!"<<endl;                 goods->Next->SetPrice();//将新输入的价格存到磁盘中                  cout<<"修改成功!"<<endl;                 cout<<"按任意键继续......";                 getch();}             else                 {                     cout<<" 未找到指定商品,请确认后重新查找!"<<endl;                     cout<<"按任意键继续......";                     getch();}                 break;             }         case 5:         {             char Number[SIZE];               Goods * goods=NULL;             cout<<" 请输入要修改的商品的编号:";cin>>Number;             if(goods=FindNumber(Number))             {                  cout<<" 已找到商品的信息,请输入新的信息!"<<endl;                 goods->Next->SetAmount();//将新输入的数量存到磁盘中                  cout<<"修改成功!"<<endl;                 cout<<"按任意键继续......";                 getch();}             else{                  cout<<" 未找到指定商品,请确认后重新查找!"<<endl;                 cout<<"按任意键继续......";                 getch();}    break;             }         }     } void Manage::DelGoods() // 删除信息  {     system("cls");     char Number[SIZE];     Goods * goods=NULL,*temp=NULL;     cout<<" 请输入要删除的商品的编号:"<<endl;cin>>Number;     if(goods=FindNumber(Number))//调用 FindNumber()函数按照编号查找,找到后进行删除      {         temp=goods->Next;         goods->Next=goods->Next->Next;          delete temp;         cout<<" 删除成功!"<<endl;         cout<<"按任意键继续......";         getch();     }     else     {         cout<<" 未找到指定商品,请确认后重新查找!"<<endl;         cout<<"按任意键继续......";         getch();     } } int Manage::ListCount() //统计当前链表的记录总数,返回一个整数 {     if(! Head)         return 0;     int n=0;     for(Goods * goods=Head->Next;goods!=End;goods=goods->Next)//对所有结点进行遍历,遍历结束后n即为总数          n++;     return n; } void Manage::SaveGoods() // 将磁盘中的文件写入文本文件中  {     out.open("仓库.txt");     for(Goods *goods=Head->Next;goods!=End;goods=goods->Next)//循环写入          out<<goods->Name<<" "<<goods->Type<<" "<<goods->Number<<" "<<goods->Price<<" "<<goods->Amount<<'\n';     out.close();     cout<<"信息保存成功"<<endl; } int main() //主函数 {     Manage G;     cout<<endl<<endl<<endl<<endl     <<"\t\t\t欢迎进入商品仓库管理系统,按任意键继续"<<endl<<endl<<endl<<endl;      getch();         int Input;     bool quit =false;     while(!quit)     {         G.ShowMenu(2);         cin>>Input;         switch(Input)         {              case 0:{quit=true;break;}             case 1:{G.AddGoods();break;}             case 2:{G.Display();break;}             case 3:{G.FindGoods();break;}             case 4:{G.DelGoods();break;}             case 5:{G.ChangeGoods();break;}             case 6:{G.SaveGoods();break;}             }         }     return 0;  }



仓库管理 c+ 系统 C++ 仓库

需要 登录 后方可回复, 如果你还没有账号请 注册新账号