商店仓库管理系统(C++)

本文档描述了一个商店仓库管理系统的实现,采用面向对象编程,包括商品类、家用电器类、日用品类和管理类。系统支持输入输出、文件操作、商品信息的增删改查等功能,并通过菜单交互。此外,还实现了数据的磁盘存储和从磁盘文件初始化。系统使用vector数组存放对象指针,具备基本的增删改查操作,并对输入输出运算符进行了重载。
摘要由CSDN通过智能技术生成

商店仓库管理系统

一、问题描述
系统应具有下列主要功能:输入记录功能:从键盘输入货物信息:商品代号,商品名称, 数
量,价格,所属类别(如家用电器、日用品等)等;修改商品数量、删除记录功能、按商品代号查询、按商品代号排序并显示等功能。
二、基本要求
(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; 
}

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值