相关推荐recommended
超市商品管理系统设计 C++实现
作者:mmseoamin日期:2024-01-30

超市商品管理系统设计—C++实现


文章目录

  • 超市商品管理系统设计---C++实现
  • 一、内容要求
    • 大纲图
    • 二、源代码(包含大量注释)
      • 1、main.cpp文件
      • 2、supermarket.h文件
      • 3、supermarket.cpp文件
      • 4、administrator.h文件
      • 5、administrator.cpp文件
      • 6、user.h文件
      • 7、user.cpp文件
      • 8、管理员密码:helloworld
      • 总结(运行截图)

        一、内容要求

        超市商品管理系统设计 C++实现,要求,第1张

        新增要求:

        1. 将管理员和用户分别开。
        2. 管理员需要密码才能进入系统。
        3. 查询功能:两种查询功能。第一种是按商品类别、商品名称、生产厂家进行查询;第二种是按商品类别、商品名称进行查询。第一种偏向管理员,第二种偏向用户。
        4. 修改功能:可以修改所以的属性。
        5. 排序功能:两种排序功能(都包含升序和降序)。第一种按照价格排序;第二种按照库存量排序。两种排序都要包含升序和降序。
        6. 清空功能:将商品全部清空的要求。
        7. 读取文件数据功能:系统运行时,就需要读取文件商品数据。包含文件不存在、文件为空、文件不为空且有数据。
        8. 存储文件功能:将商品数据存放到文件中。
        9. 展示商品gongn:打印所有商品数据信息。
        10. 在增、删、修、排序、清空功能中,要实现文件数据的时时更新。

        大纲图

        超市商品管理系统设计 C++实现,在这里插入图片描述,第2张

        超市商品管理系统设计 C++实现,在这里插入图片描述,第3张

        超市商品管理系统设计 C++实现,在这里插入图片描述,第4张


        二、源代码(包含大量注释)

        超市商品管理系统设计 C++实现,在这里插入图片描述,第5张

        7个文件,3个头文件,4个.cpp文件

        main.cpp

        supermarket.h

        supermarket.cpp

        administrator.h

        administrator.cpp

        user.h

        user.cpp


        1、main.cpp文件

        #include 
        #include "supermarket.h"
        #include "administrator.h"
        #include "user.h"
        using namespace std;
        int main()
        {
        	//test01();
        	administrator adm;//管理员
        	supermarket market;//超市系统
        	user us;//用户
        	int input = 0;
        	do
        	{
        		market.supermarketMenu();
        		cout << "请输入对应的序号:";
        		cin >> input;
        		switch (input)
        		{
        		case 1:
        			us.operate_user(market);
        			break;
        		case 2:
        			//adm.login_administrator()
        			if (adm.login_administrator())
        			{
        				system("cls");
        				adm.operate_administrator(market);
        			}
        			else
        			{
        				input = 0;
        			}
        			break;
        		case 0:
        			cout << "退出超市系统,欢迎下次光临" << endl;
        			break;
        		default:
        			cout << "输入序号不存在,请重新输入" << endl;
        			break;
        		}
        	} while (input);
        	//按任意键后,退出程序
        	cout << endl;
        	system("pause");
        	return 0;
        }
        

        2、supermarket.h文件

        #pragma once//防止头文件重复包含
        #include 
        using namespace std;
        #include 
        #include 
        #define FILENAME "Goods.txt"
        #define ARRSIZE 10
        //商品的抽象基类
        class goods
        {
        public:
        	//显示商品信息
        	virtual void showGoodsInfo() = 0;
        	//获取商品类型
        	virtual string goodsType() = 0;
        	
        	int goodsClass;//记录商品类
        	string name;//商品名称
        	int price;//商品价格
        	int inventory;//商品库存量
        	string manufacturer;//商品生产厂家
        	
        };
        //食物类
        class food : public goods
        {
        public:
        	//构造函数
        	food(string n, int p, int i, string m);
        	//显示商品信息
        	virtual void showGoodsInfo();
        	//获取商品类型
        	virtual string goodsType();
        };
        //化妆品
        class cosmetics : public goods
        {
        public:
        	//构造函数
        	cosmetics(string n, int p, int i, string m);
        	//显示商品信息
        	virtual void showGoodsInfo();
        	//获取商品类型
        	virtual string goodsType();
        };
        //日用品
        class necessities : public goods
        {
        public:
        	//构造函数
        	necessities(string n, int p, int i, string m);
        	//显示商品信息
        	virtual void showGoodsInfo();
        	//获取商品类型
        	virtual string goodsType();
        };
        //饮料
        class drink : public goods
        {
        public:
        	//构造函数
        	drink(string n, int p, int i, string m);
        	//显示商品信息
        	virtual void showGoodsInfo();
        	//获取商品类型
        	virtual string goodsType();
        };
        //超市管理系统
        class supermarket
        {
        public:
        	supermarket();//构造函数
        	void supermarketMenu();//超市管理系统菜单
        	~supermarket();//析构函数
        	//商品类型的数量
        	int goodsTypeNum;
        	//商品数组指针
        	goods** goodsArray;
        	//商品类的菜单
        	void goodsClassMenu();
        	//标志判断文件是否为空
        	bool fileEmpty;
        	//统计文件中的商品类型的数量
        	int getFile_goodsTypeNum();
        	//用文件初始化商品
        	void initGoods();
        	//将商品保存到文件
        	void save();
        	//添加商品
        	void addGoods();
        	//展示商品
        	void showGoods();
        	//查询商品——按商品类别、名称、生产厂家查询
        	int inquireGoods_1();
        	//打印查询1的商品信息
        	void printInquireGoods_1(int index);
        	//查询商品——按商品类别、名称查询
        	int* inquireGoods_2();
        	//打印查询2的商品信息
        	void printInquireGoods_2(int* p);
        	//修改商品
        	void reviseGoods();
        	//下架商品
        	void delGoods();
        	//排序1--按价格排序
        	void sortGoods_1();
        	//排序2--按库存量排序
        	void sortGoods_2();
        	//清空商品
        	void clearGoods();
        };
        

        3、supermarket.cpp文件

        #include "supermarket.h"
        //食物类构造函数
        food::food(string n, int p, int i, string m)
        {
        	this->goodsClass = 1;
        	this->name = n;
        	this->price = p;
        	this->inventory = i;
        	this->manufacturer = m;
        }
        //显示食物类商品信息
        void food::showGoodsInfo()
        {
        	cout << this->goodsType() << "\t|";
        	cout << this->name << "\t|";
        	cout << this->price << "\t|";
        	cout << this->inventory << "\t|";
        	cout << this->manufacturer << endl;
        }
        //获取商品类型
        string food::goodsType()
        {
        	return string("食物");
        }
        //化妆品类构造函数
        cosmetics::cosmetics(string n, int p, int i, string m)
        {
        	this->goodsClass = 2;
        	this->name = n;
        	this->price = p;
        	this->inventory = i;
        	this->manufacturer = m;
        }
        //显示化妆品类商品信息
        void cosmetics::showGoodsInfo()
        {
        	cout << this->goodsType() << "\t|";
        	cout << this->name << "\t|";
        	cout << this->price << "\t|";
        	cout << this->inventory << "\t|";
        	cout << this->manufacturer << endl;
        }
        //获取商品类型
        string cosmetics::goodsType()
        {
        	return string("化妆品");
        }     
        //日用品类构造函数
        necessities::necessities(string n, int p, int i, string m)
        {
        	this->goodsClass = 3;
        	this->name = n;
        	this->price = p;
        	this->inventory = i;
        	this->manufacturer = m;
        }
        //显示日用品类商品信息
        void necessities::showGoodsInfo()
        {
        	cout << this->goodsType() << "\t|";
        	cout << this->name << "\t|";
        	cout << this->price << "\t|";
        	cout << this->inventory << "\t|";
        	cout << this->manufacturer << endl;
        }
        //获取商品类型
        string necessities::goodsType()
        {
        	return string("日用品");
        }
        //饮料类构造函数
        drink::drink(string n, int p, int i, string m)
        {
        	this->goodsClass = 4;
        	this->name = n;
        	this->price = p;
        	this->inventory = i;
        	this->manufacturer = m;
        }
        //显示饮料类商品信息
        void drink::showGoodsInfo()
        {
        	cout << this->goodsType() << "\t|";
        	cout << this->name << "\t|";
        	cout << this->price << "\t|";
        	cout << this->inventory << "\t|";
        	cout << this->manufacturer << endl;
        }
        //获取商品类型
        string drink::goodsType()
        {
        	return string("饮料");
        }
        //超市管理构造函数
        supermarket::supermarket()
        {
        	//读文件方式打开文件
        	ifstream ifs;
        	ifs.open(FILENAME, ios::in);
        	//1、文件不存在
        	if (!ifs.is_open())
        	{
        		cout << "------------------------" << endl;
        		cout << "-----  文件不存在!-----" << endl;
        		cout << "------------------------" << endl;
        		cout << endl;
        		//初始化商品类型的数量
        		this->goodsTypeNum = 0;
        		//初始化商品数组指针
        		this->goodsArray = NULL;
        		//初始化文件是否为空
        		this->fileEmpty = true;
        		ifs.close();//关闭文件
        		return;
        	}
        	//2、文件存在,数据为空
        	char ch;
        	ifs >> ch;//读取一个字符
        	if (ifs.eof())//文件结尾EOF
        	{
        		//文件为空
        		cout << "------------------------" << endl;
        		cout << "----  文件为空文件!----" << endl;
        		cout << "------------------------" << endl;
        		cout << endl;
        		//初始化商品类型的数量
        		this->goodsTypeNum = 0;
        		//初始化商品数组指针
        		this->goodsArray = NULL;
        		//初始化文件是否为空
        		this->fileEmpty = true;
        		ifs.close();//关闭文件
        		return;
        	}
        	//3、文件存在,并且有数据
        	cout << "------------------------" << endl;
        	cout << "--  从文件中导入数据  --" << endl;
        	cout << "------------------------" << endl;
        	cout << endl;
        	int num = this->getFile_goodsTypeNum();
        	cout << "商品类型的数量为:" << num << endl;
        	//商品类型的数量
        	this->goodsTypeNum = num;
        	//初始化文件不为空
        	this->fileEmpty = false;
        	//开辟空间
        	this->goodsArray = new goods * [this->goodsTypeNum];
        	//将文件商品存放数组中
        	this->initGoods();
        	ifs.close();//关闭文件
        }
        //超市管理析造函数
        supermarket::~supermarket()
        {
        	//释放空间
        	if (NULL != this->goodsArray)
        	{
        		//清空堆区数据
        		int i = 0;
        		//遍历删除每个数据
        		for (i = 0; i < this->goodsTypeNum; i++)
        		{
        			delete this->goodsArray[i];
        			this->goodsArray[i] = NULL;
        		}
        		//删除指针
        		delete[] this->goodsArray;
        		this->goodsArray = NULL;
        	}
        }
        //超市管理系统菜单
        void supermarket::supermarketMenu()
        {
        	cout << endl;
        	cout << "***********************************" << endl;
        	cout << "*****    1、用户购买商品      *****" << endl;
        	cout << "*****    2、管理员管理超市    *****" << endl;
        	cout << "*****    0、退出超市系统      *****" << endl;
        	cout << "***********************************" << endl;
        	cout << endl;
        }
        //商品类的菜单
        void supermarket::goodsClassMenu()
        {
        	cout << "***********************" << endl;
        	cout << "***  1、 食物类     ***" << endl;
        	cout << "***  2、 化妆品类   ***" << endl;
        	cout << "***  3、 日用品类   ***" << endl;
        	cout << "***  4、 饮料类     ***" << endl;
        	cout << "***********************" << endl;
        }
        //统计文件中的商品类型的数量
        int supermarket::getFile_goodsTypeNum()
        {
        	//读文件方式打开文件
        	ifstream ifs;
        	ifs.open(FILENAME, ios::in);
        	int count = 0; //统计商品数量
        	int Class;//记录商品类
        	string Name;//商品名称
        	int Price;//商品价格
        	int Inven;//商品库存量
        	string Manu;//商品生产厂家
        	while (ifs >> Class && ifs >> Name && ifs >> Price && ifs >> Inven && ifs >> Manu)
        	{
        		count++;
        	}
        	ifs.close();//关闭文件
        	return count;
        }
        //用文件初始化商品
        void supermarket::initGoods()
        {
        	//读文件方式打开文件
        	ifstream ifs;
        	ifs.open(FILENAME, ios::in);
        	int Class;//记录商品类
        	string Name;//商品名称
        	int Price;//商品价格
        	int Inven;//商品库存量
        	string Manu;//商品生产厂家
        	goods* Goods = NULL;
        	int index = 0;
        	while (ifs >> Class && ifs >> Name && ifs >> Price && ifs >> Inven && ifs >> Manu)
        	{
        		if (1 == Class)//食物类
        		{
        			Goods = new food(Name, Price, Inven, Manu);
        		}
        		else if (2 == Class)//化妆品类
        		{
        			Goods = new cosmetics(Name, Price, Inven, Manu);
        		}
        		else if (3 == Class)//日用品类
        		{
        			Goods = new necessities(Name, Price, Inven, Manu);
        		}
        		else if (4 == Class)//饮料类
        		{
        			Goods = new drink(Name, Price, Inven, Manu);
        		}
        		//将文件数据存放在数组中
        		this->goodsArray[index] = Goods;
        		index++;
        	}
        	
        	ifs.close();//关闭文件
        }
        //将商品保存到文件
        void supermarket::save()
        {
        	//判断商品数据否是为空
        	if (!this->goodsTypeNum)
        	{
        		cout << "无商品数据" << endl;
        		this->fileEmpty = true;
        		return;
        	}
        	ofstream ofs;
        	//用写文件打开文件
        	ofs.open(FILENAME, ios::out);
        	int i = 0;
        	for (i = 0; i < this->goodsTypeNum; i++)
        	{
        		ofs << this->goodsArray[i]->goodsClass << " ";
        		ofs << this->goodsArray[i]->name << " ";
        		ofs << this->goodsArray[i]->price << " ";
        		ofs << this->goodsArray[i]->inventory << " ";
        		ofs << this->goodsArray[i]->manufacturer << endl;
        	}
        	//关闭文件
        	ofs.close();
        }
        //添加商品
        void supermarket::addGoods()
        {
        	cout << "添加新商品开始" << endl;
        	int addNum;
        	int i = 0;
        	cout << "请输入要添加的商品的类型数量:";
        	cin >> addNum;
        	if (addNum > 0)
        	{
        		//计算添加后的总空间大小
        		int newSize = this->goodsTypeNum + addNum;
        		//开辟新空间
        		goods** newSpace = new goods * [newSize];
        		//将原来的空间数据,拷贝到新空间
        		if (NULL != this->goodsArray)
        		{
        			for (i = 0; i < this->goodsTypeNum; i++)
        			{
        				newSpace[i] = this->goodsArray[i];
        			}
        		}
        		//添加新商品
        		int input;
        		int Class;//记录商品类
        		string Name;//商品名称
        		int Price;//商品价格
        		int Inven;//商品库存量
        		string Manu;//商品生产厂家
        		goods* newGoods = NULL;//商品的父类指针
        		for (i = 0; i < addNum; i++)
        		{
        			cout << endl;
        			cout << "第" << i + 1 << "个新增商品样式" << endl;
        			cout << "新商品的名称:";
        			cin >> Name;
        			cout << "新商品的价格:";
        			cin >> Price;
        			cout << "新商品的库存量:";
        			cin >> Inven;
        			cout << "新商品的生产厂家:";
        			cin >> Manu;
        			//商品类的菜单
        			this->goodsClassMenu();
        			cout << "请选择商品的类型:";
        			cin >> input;
        			switch (input)
        			{
        			case 1:
        				//开辟食物类的空间
        				newGoods = new food(Name, Price, Inven, Manu);
        				break;
        			case 2:
        				//开辟化妆品类的空间
        				newGoods = new cosmetics(Name, Price, Inven, Manu);
        				break;
        			case 3:
        				//开辟日用品类的空间
        				newGoods = new necessities(Name, Price, Inven, Manu);
        				break;
        			case 4:
        				//开辟饮料类的空间
        				newGoods = new drink(Name, Price, Inven, Manu);
        				break;
        			default:
        				break;
        			}
        			//将创建好的新商品的地址,存放在数组中
        			newSpace[this->goodsTypeNum + i] = newGoods;
        		}
        		//释放原有的空间
        		delete[] this->goodsArray;
        		//更新新的空间指向
        		this->goodsArray = newSpace;
        		//更新新的商品类型数量
        		this->goodsTypeNum = newSize;
        		//更新文件不为空
        		this->fileEmpty = false;
        		cout << "----------添加新商品成功----------" << endl;
        		this->save();//将商品保存到文件
        	}
        	else
        	{
        		cout << "输入数据有误" << endl;
        	}
        }
        //展示商品
        void supermarket::showGoods()
        {
        	//判断商品数据否是为空
        	if (!this->goodsTypeNum)
        	{
        		cout << "无商品,展示商品失败" << endl;
        		return;
        	}
        	cout << "展示商品开始" << endl << endl;
        	int i = 0;
        	int count = 0;
        	cout << "---------------------商品库---------------------" << endl;
        	cout << "类型\t|名称\t|价格\t|库存量\t|生产厂家" << endl;
        	cout << "--------|-------|-------|-------|---------------" << endl;
        	for (i = 0; i < this->goodsTypeNum; i++)
        	{
        		count += this->goodsArray[i]->inventory;
        		this->goodsArray[i]->showGoodsInfo();
        		cout << "--------|-------|-------|-------|---------------" << endl;
        	}
        	cout << endl;
        	cout << "商品类型的数量:" << this->goodsTypeNum << endl;
        	cout << "商品的总数:" << count << endl;
        	cout << "----------展示商品完成----------" << endl;
        }
        //查询商品——按商品类别、名称、生产厂家查询
        int supermarket::inquireGoods_1()
        {
        	//判断商品数据否是为空
        	if (!this->goodsTypeNum)
        	{
        		cout << "无商品,查询失败" << endl;
        		return -1;
        	}
        	int i = 0;
        	int Class;//记录商品类
        	string Name;//商品名称
        	string Manu;//商品生产厂家
        	this->goodsClassMenu();//商品类的菜单
        	cout << "请输入要查询商品的类型序号:";
        	cin >> Class;
        	cout << "请输入要查询商品的名称:";
        	cin >> Name;
        	cout << "请输入要查询商品的生产厂家:";
        	cin >> Manu;
        	for (i = 0; i < this->goodsTypeNum; i++)
        	{
        		//先判断商品类型是否相同
        		if (this->goodsArray[i]->goodsClass != Class)
        		{
        			continue;
        		}
        		//在进行商品名称比较
        		if (!this->goodsArray[i]->name.compare(Name) && !this->goodsArray[i]->manufacturer.compare(Manu))
        		{
        			cout << "----------查询成功----------" << endl;
        			return i;
        		}
        	}
        	cout << "查询失败,该商品不存在" << endl;
        	return -1;
        }
        //打印查询1的商品信息
        void supermarket::printInquireGoods_1(int index)
        {
        	//查询失败的情况
        	if (-1 == index)
        	{
        		return;
        	}
        	cout << "查询结果打印" << endl << endl;
        	cout << "类型\t|名称\t|价格\t|库存量\t|生产厂家" << endl;
        	cout << "--------|-------|-------|-------|---------------" << endl;
        	this->goodsArray[index]->showGoodsInfo();
        	cout << "--------|-------|-------|-------|---------------" << endl;
        }
        //查询商品——按商品类别、名称查询
        int* supermarket::inquireGoods_2()
        {
        	//判断商品数据否是为空
        	if (!this->goodsTypeNum)
        	{
        		cout << "无商品,查询失败" << endl;
        		return NULL;
        	}
        	int i = 0;
        	int arr[ARRSIZE] = { 0 };//存放查询到商品的位置
        	int count = 0;//记录查询的个数
        	int Class;//记录商品类
        	string Name;//商品名称
        	string Manu;//商品生产厂家
        	this->goodsClassMenu();//商品类的菜单
        	cout << "请输入要查询商品的类型序号:";
        	cin >> Class;
        	cout << "请输入要查询商品的名称:";
        	cin >> Name;
        	for (i = 0; i < this->goodsTypeNum; i++)
        	{
        		//先判断商品类型是否相同
        		if (this->goodsArray[i]->goodsClass != Class)
        		{
        			continue;
        		}
        		//在进行商品名称比较
        		if (!this->goodsArray[i]->name.compare(Name))
        		{
        			arr[count] = i + 1;
        			count++;
        		}
        	}
        	if (0 == count)
        	{
        		cout << "查询失败,该商品不存在" << endl;
        		return NULL;
        	}
        	else
        	{
        		cout << "----------查询成功----------" << endl;
        		return arr;
        	}
        }
        //打印查询2的商品信息
        void supermarket::printInquireGoods_2(int* p)
        {
        	//查询失败的情况
        	if (NULL == p)
        	{
        		return;
        	}
        	int count = 0;
        	int i = 0;
        	//记录总数
        	int arr[ARRSIZE] = { 0 };
        	for (i = 0; i < ARRSIZE; i++)
        	{
        		//第一次是不需要判断的
        		if (p[i] != 0)
        		{
        			arr[i] = p[i];
        			count++;
        		}
        		else
        		{
        			break;
        		}
        	}
        	cout << "查询结果打印" << endl << endl;
        	cout << "序号\t|类型\t|名称\t|价格\t|库存量\t|生产厂家" << endl;
        	cout << "--------|-------|-------|-------|-------|---------------" << endl;
        	
        	for (i = 0; i < count; i++)
        	{
        		int j = arr[i] - 1;//记录下标
        		cout << i + 1 << "\t|";
        		this->goodsArray[j]->showGoodsInfo();
        		cout << "--------|-------|-------|-------|-------|---------------" << endl;
        	}
        }
        //修改商品
        void supermarket::reviseGoods()
        {
        	//判断商品数据否是为空
        	if (!this->goodsTypeNum)
        	{
        		cout << "无商品,修改商品失败" << endl;
        		return;
        	}
        	//查询功能1
        	int index = this->inquireGoods_1();
        	if (-1 == index)//是否查找失败
        	{
        		cout << "修改失败" << endl;
        		return;
        	}
        	//打印查询到的结果
        	this->printInquireGoods_1(index);
        	int input = 0;
        	goods* Goods = NULL;
        	//记录原来商品类
        	int Class = this->goodsArray[index]->goodsClass;
        	//记录原来商品名称
        	string Name = this->goodsArray[index]->name;
        	//记录原来商品价格
        	int Price = this->goodsArray[index]->price;
        	//记录原来商品库存量
        	int Inven = this->goodsArray[index]->inventory;
        	//记录原来商品生产厂家
        	string Manu = this->goodsArray[index]->manufacturer;
        	do
        	{
        		cout << endl;
        		cout << "----------修改类型----------" << endl;
        		cout << "--   1、修改商品类        --" << endl;
        		cout << "--   2、修改商品名称      --" << endl;
        		cout << "--   3、修改商品价格      --" << endl;
        		cout << "--   4、修改商品库存量    --" << endl;
        		cout << "--   5、修改商品生产厂家  --" << endl;
        		cout << "--   0、退出修改          --" << endl;
        		cout << "----------------------------" << endl;
        		cout << "请输入要修改的类型:";
        		cin >> input;
        		switch(input)
        		{
        		case 1:
        			this->goodsClassMenu();//商品类的菜单
        			cout << "请输入要修改商品类 :";
        			cin >> Class;
        			delete this->goodsArray[index];
        			if (1 == Class)//开辟食物类的空间
        			{
        				Goods = new food(Name, Price, Inven, Manu);
        			}
        			else if (2 == Class)//开辟化妆品类的空间
        			{
        				Goods = new cosmetics(Name, Price, Inven, Manu);
        			}
        			else if (3 == Class)//开辟日用品类的空间
        			{
        				Goods = new necessities(Name, Price, Inven, Manu);
        			}
        			else if (4 == Class)//开辟饮料类的空间
        			{
        				Goods = new drink(Name, Price, Inven, Manu);
        			}
        			//将数据重新放到类中
        			this->goodsArray[index] = Goods;
        			break;
        		case 2:
        			cout << "请输入要修改商品名称 :";
        			cin >> Name;
        			this->goodsArray[index]->name = Name;
        			break;
        		case 3:
        			cout << "请输入要修改商品价格 :";
        			cin >> Price;
        			this->goodsArray[index]->price = Price;
        			break;
        		case 4:
        			cout << "请输入要修改商品库存量 :";
        			cin >> Inven;
        			this->goodsArray[index]->inventory = Inven;
        			break;
        		case 5:
        			cout << "请输入要修改商品生产厂家 :";
        			cin >> Manu;
        			this->goodsArray[index]->manufacturer = Manu;
        			break;
        		case 0:
        			cout << "----------修改成功----------" << endl;
        			this->save();//更新文件数据
        			break;
        		default:
        			cout << "输入序号不存在,请重新输入" << endl;
        			break;
        		}
        	} while (input);
        }
        //下架商品
        void supermarket::delGoods()
        {
        	//判断商品数据否是为空
        	if (!this->goodsTypeNum)
        	{
        		cout << "无商品,下架商品失败" << endl;
        		return;
        	}
        	//查找要删除的数据
        	int index = this->inquireGoods_1();
        	if (-1 == index)//是否查找失败
        	{
        		cout << "删除失败" << endl;
        		return;
        	}
        	//打印查询的数据
        	this->printInquireGoods_1(index);
        	int i = 0;
        	//释放空间
        	delete this->goodsArray[index];
        	//将商品数据前移
        	for (i = index; i < this->goodsTypeNum - 1; i++)
        	{
        		this->goodsArray[i] = this->goodsArray[i + 1];
        	}
        	//商品类型的数量更新
        	this->goodsTypeNum--;
        	//更新文件的数据
        	this->save();
        	cout << "----------删除成功----------" << endl;
        }
        //排序1--按价格排序
        void supermarket::sortGoods_1()
        {
        	//判断商品数据否是为空
        	if (!this->goodsTypeNum)
        	{
        		cout << "无商品,排序商品失败" << endl;
        		return;
        	}
        	cout << "*****************************" << endl;
        	cout << "***   1、按价格----升序   ***" << endl;
        	cout << "***   2、按价格----降序   ***" << endl;
        	cout << "*****************************" << endl;
        	cout << "请输入要排序的方式对应的序号:";
        	int input = 0;
        	cin >> input;
        	int i = 0;
        	int j = 0;
        	goods* temp = NULL;
        	bool flag = false;//每趟是否有交换的标志
        	for (i = 0; i < this->goodsTypeNum - 1; i++)
        	{
        		flag = false;//为交换时是false
        		for (j = this->goodsTypeNum - 1; j > i; j--)
        		{
        			if (input == 1)//升序
        			{
        				if (this->goodsArray[j - 1]->price > this->goodsArray[j]->price)
        				{
        					temp = this->goodsArray[j];
        					this->goodsArray[j] = this->goodsArray[j - 1];
        					this->goodsArray[j - 1] = temp;
        					flag = true;//交换
        				}
        			}
        			else//降序
        			{
        				if (this->goodsArray[j - 1]->price < this->goodsArray[j]->price)
        				{
        					temp = this->goodsArray[j];
        					this->goodsArray[j] = this->goodsArray[j - 1];
        					this->goodsArray[j - 1] = temp;
        					flag = true;//交换
        				}
        			}
        		}
        		if (false == flag)//判断是否有序
        		{
        			this->save();//更新文件数据
        			return;
        		}
        	}
        	this->save();//更新文件数据
        }
        //排序2--按库存量排序
        void supermarket::sortGoods_2()
        {
        	//判断商品数据否是为空
        	if (!this->goodsTypeNum)
        	{
        		cout << "无商品,排序失败" << endl;
        		return;
        	}
        	cout << "*****************************" << endl;
        	cout << "***   1、按价格----升序   ***" << endl;
        	cout << "***   2、按价格----降序   ***" << endl;
        	cout << "*****************************" << endl;
        	cout << "请输入要排序的方式对应的序号:";
        	int input = 0;
        	cin >> input;
        	int i = 0;
        	int j = 0;
        	goods* temp = NULL;
        	bool flag = false;//每趟是否有交换的标志
        	for (i = 0; i < this->goodsTypeNum - 1; i++)
        	{
        		flag = false;//为交换时是false
        		for (j = this->goodsTypeNum - 1; j > i; j--)
        		{
        			if (1 == input)//升序
        			{
        				if (this->goodsArray[j - 1]->inventory > this->goodsArray[j]->inventory)
        				{
        					temp = this->goodsArray[j];
        					this->goodsArray[j] = this->goodsArray[j - 1];
        					this->goodsArray[j - 1] = temp;
        					flag = true;//交换
        				}
        			}
        			else//降序
        			{
        				if (this->goodsArray[j - 1]->inventory < this->goodsArray[j]->inventory)
        				{
        					temp = this->goodsArray[j];
        					this->goodsArray[j] = this->goodsArray[j - 1];
        					this->goodsArray[j - 1] = temp;
        					flag = true;//交换
        				}
        			}	
        		}
        		if (false == flag)//判断是否有序
        		{
        			this->save();//更新文件数据
        			return;
        		}
        	}
        	this->save();//更新文件数据
        }
        //清空商品
        void  supermarket::clearGoods()
        {
        	//判断商品数据否是为空
        	if (!this->goodsTypeNum)
        	{
        		cout << "无商品,不需要清空" << endl;
        		return;
        	}
        	int input = 0;
        	cout << endl;
        	cout << "-----  确定要清空吗  -----" << endl;
        	cout << "-----  1、确定       -----" << endl;
        	cout << "-----  0、不要       -----" << endl;
        	cout << "--------------------------" << endl;
        	cout << "请输入序号:";
        	cin >> input;
        	cout << endl;
        	if (0 == input)
        	{
        		cout << "清空操作取消" << endl;
        		return;
        	}
        	//清空
        	//trunc 如文件存在,将其长度截断为零并清除原有内容
        	//      如果文件存在先删除,再创建
        	ofstream ofs(FILENAME, ios::trunc);//清除文件数据
        	ofs.close();
        	//清空堆区数据
        	int i = 0;
        	//遍历删除每个数据
        	for (i = 0; i < this->goodsTypeNum; i++)
        	{
        		delete this->goodsArray[i];
        		this->goodsArray[i] = NULL;
        	}
        	//删除指针
        	delete[] this->goodsArray;
        	this->goodsArray = NULL;
        	this->goodsTypeNum = 0;
        	this->fileEmpty = true;
        	cout << "----------清空成功----------" << endl;
        }
        

        4、administrator.h文件

        #pragma once
        #include 
        #include 
        #include "supermarket.h"
        using namespace std;
        //管理员
        class administrator
        {
        public:
        	//管理员操作菜单
        	void menu_administrator();
        	//管理员登录界面
        	bool login_administrator();
        	//管理员操作超市系统
        	void operate_administrator(supermarket& sup);
        	//管理员添加商品
        	void addGoods_administrator(supermarket& sup);
        	//管理员展示商品
        	void showGoods_administrator(supermarket& sup);
        	//管理员查找商品
        	void inquireGoods_administrator(supermarket& sup);
        	//管理员修改商品
        	void reviseGoods_administrator(supermarket& sup);
        	//管理员删除商品
        	void delGoods_administrator(supermarket& sup);
        	//管理员排序商品
        	void sortGoods_administrator(supermarket& sup);
        	//管理员清空商品
        	void clearGoods_administrator(supermarket& sup);
        };
        

        5、administrator.cpp文件

        #include "administrator.h"
        //管理员操作菜单
        void administrator::menu_administrator()
        {
        	cout << "****************************" << endl;
        	cout << "***----管理员操作菜单----***" << endl;
        	cout << "***    1、增加新商品     ***" << endl;
        	cout << "***    2、展示商品       ***" << endl;
        	cout << "***    3、查找商品       ***" << endl;
        	cout << "***    4、修改商品       ***" << endl;
        	cout << "***    5、下架商品       ***" << endl;
        	cout << "***    6、排序商品       ***" << endl;
        	cout << "***    7、清空商品       ***" << endl;
        	cout << "***    0、退出后台系统   ***" << endl;
        	cout << "****************************" << endl;
        	cout << endl;
        }
        //管理员登陆界面
        bool administrator::login_administrator()
        {
        	string str;
        	//密码为:helloworld
        	string password = "helloworld";
        	int i = 0;//3次输入密码机会
        	for (i = 0; i < 3; i++)
        	{
        		cout << "请输入登入密码:";
        		cin >> str;
        		if (!password.compare(str))
        		{
        			cout << "密码正确,欢迎进入系统" << endl;
        			return true;
        		}
        		else
        		{
        			cout << "密码错误" << endl;
        		}
        	}
        	cout << "三次都输入错误,强制退出系统" << endl;
        	return false;
        }
        //管理员操作超市系统
        void administrator::operate_administrator(supermarket& sup)
        {
        	system("cls");//清屏
        	cout << "欢迎管理员进入超市后台" << endl;
        	int input = 0;
        	do
        	{
        		this->menu_administrator();
        		cout << "请输入对应的序号:";
        		cin >> input;
        		switch (input)
        		{
        		case 1:
        			this->addGoods_administrator(sup);
        			break;
        		case 2:
        			this->showGoods_administrator(sup);
        			break;
        		case 3:
        			this->inquireGoods_administrator(sup);
        			break;
        		case 4:
        			this->reviseGoods_administrator(sup);
        			break;
        		case 5:
        			this->delGoods_administrator(sup);
        			break;
        		case 6:
        			this->sortGoods_administrator(sup);
        			break;
        		case 7:
        			this->clearGoods_administrator(sup);
        			break;
        		case 0:
        			cout << "退出超市系统后台" << endl;
        			break;
        		default:
        			cout << "输入序号不存在,请重新输入" << endl;
        			break;
        		}
        	} while (input);
        	//按任意键后,清屏回到上级目录
        	cout << endl;
        	system("pause");
        	system("cls");
        }
        //添加商品
        void administrator::addGoods_administrator(supermarket& sup)
        {
        	sup.addGoods();
        	//按任意键后,清屏回到上级目录
        	cout << endl;
        	system("pause");
        	system("cls");
        }
        //管理员展示商品
        void administrator::showGoods_administrator(supermarket& sup)
        {
        	sup.showGoods();
        	//按任意键后,清屏回到上级目录
        	cout << endl;
        	system("pause");
        	system("cls");
        }
        //管理员查找商品
        void administrator::inquireGoods_administrator(supermarket& sup)
        {
        	cout << "-----------------------------------------" << endl;
        	cout << "--  1、按商品类别、名称、生产厂家查询  --" << endl;
        	cout << "--  2、按商品类别、名称查询            --" << endl;
        	cout << "-----------------------------------------" << endl;
        	cout << "请输入要查询的方法序号:";
        	int input;
        	cin >> input;
        	if (1 == input)
        	{
        		int i = sup.inquireGoods_1();
        		sup.printInquireGoods_1(i);
        	}
        	else if (2 == input)
        	{
        		int* p = sup.inquireGoods_2();
        		sup.printInquireGoods_2(p);
        	}
        	//按任意键后,清屏回到上级目录
        	cout << endl;
        	system("pause");
        	system("cls");
        }
        //管理员修改商品
        void administrator::reviseGoods_administrator(supermarket& sup)
        {
        	sup.reviseGoods();
        	//按任意键后,清屏回到上级目录
        	cout << endl;
        	system("pause");
        	system("cls");
        }
        //管理员删除商品
        void administrator::delGoods_administrator(supermarket& sup)
        {
        	sup.delGoods();
        	//按任意键后,清屏回到上级目录
        	cout << endl;
        	system("pause");
        	system("cls");
        }
        //管理员排序商品
        void administrator::sortGoods_administrator(supermarket& sup)
        {
        	cout << "------------------------" << endl;
        	cout << "--  1、按价格排序     --" << endl;
        	cout << "--  2、按库存量排序   --" << endl;
        	cout << "------------------------" << endl;
        	cout << "请输入要排序方法序号:";
        	int input;
        	cin >> input;
        	if (1 == input)
        	{
        		sup.sortGoods_1();
        		sup.showGoods();
        	}
        	else if (2 == input)
        	{
        		sup.sortGoods_2();
        		sup.showGoods();
        	}
        	//按任意键后,清屏回到上级目录
        	cout << endl;
        	system("pause");
        	system("cls");
        }
        //管理员清空商品
        void administrator::clearGoods_administrator(supermarket& sup)
        {
        	sup.clearGoods();
        	//按任意键后,清屏回到上级目录
        	cout << endl;
        	system("pause");
        	system("cls");
        }
        

        6、user.h文件

        #pragma once
        #include "supermarket.h"
        #include 
        using namespace std;
        //用户
        class user
        {
        public:
        	//用户菜单
        	void menu_user();
        	//用户使用超市系统
        	void operate_user(supermarket& sup);
        	//用户销售功能
        	void salesFeatures_user(supermarket& sup);
        	//用户展示商品
        	void showGoods_user(supermarket& sup);
        	//用户查找商品
        	void inquireGoods_user(supermarket& sup);
        	//用户排序商品
        	void sortGoods_user(supermarket& sup);
        };
        

        7、user.cpp文件

        #include "user.h"
        //用户菜单
        void user::menu_user()
        {
        	cout << "**************************" << endl;
        	cout << "***----用户操作菜单----***" << endl;
        	cout << "***    1、购买商品     ***" << endl;
        	cout << "***    2、展示商品     ***" << endl;
        	cout << "***    3、查找商品     ***" << endl;
        	cout << "***    4、排序商品     ***" << endl;
        	cout << "***    0、退出购买     ***" << endl;
        	cout << "**************************" << endl;
        	cout << endl;
        }
        //用户使用超市系统
        void user::operate_user(supermarket& sup)
        {
        	system("cls");//清屏
        	cout << "欢迎光临本超市商城" << endl;
        	int input = 0;
        	do
        	{
        		this->menu_user();//用户菜单
        		cout << "请输入对应的序号:";
        		cin >> input;
        		switch (input)
        		{
        		case 1:
        			this->salesFeatures_user(sup);
        			break;
        		case 2:
        			this->showGoods_user(sup);
        			break;
        		case 3:
        			this->inquireGoods_user(sup);
        			break;
        		case 4:
        			this->sortGoods_user(sup);
        			break;
        		case 0:
        			cout << "退出超市,欢饮下次光临" << endl;
        			break;
        		default:
        			cout << "输入序号不存在,请重新输入" << endl;
        			break;
        		}
        	} while (input);
        	//按任意键后,清屏回到上级目录
        	cout << endl;
        	system("pause");
        	system("cls");
        }
        //用户销售功能
        void user::salesFeatures_user(supermarket& sup)
        {
        	cout << "欢迎您购买商品" << endl;
        	//用户查找商品的方式
        	int* p = sup.inquireGoods_2();
        	if (p == NULL)//查找是否成功
        	{
        		cout << "商品不存在,购买失败" << endl;
        		return;
        	}
        	//将商品数据位置,放到arr中
        	int count = 0;
        	int i = 0;
        	//记录总数
        	int arr[ARRSIZE] = { 0 };
        	for (i = 0; i < ARRSIZE; i++)
        	{
        		//第一次是不需要判断的
        		if (p[i] != 0)
        		{
        			arr[i] = p[i];
        			count++;
        		}
        		else
        		{
        			break;
        		}
        	}
        	//打印查询结果
        	sup.printInquireGoods_2(p);
        	cout << endl;
        	//判断输入是否合法
        	int input = 0;
        	while (true)
        	{
        		cout << "请输入要买商品的序号:";
        		cin >> input;
        		if (input > count || input <= 0)
        		{
        			cout << "输入序号无效,请重新输入" << endl;
        			continue;
        		}
        		else
        		{
        			break;//输入合法退出死循环
        		}
        	}
        	cout << endl;
        	int temp = 0;
        	int number = 0;//记录用户购买商品数
        	//记录商品的库存量
        	int inven = sup.goodsArray[arr[input - 1] - 1]->inventory;
        	//输入的购买的商品数,是否合法,库存量
        	while (true)
        	{
        		cout << "请输入要购买的商品数:";
        		cin >> number;
        		if (number < 0)//输入buhef
        		{
        			cout << "输入购买的商品数无效,请重新输入" << endl;
        		}
        		else if (number > inven)//输入大于库存量
        		{
        			cout << "抱歉,您购买的商品数超出了本店的库存量" << endl;
        			cout << "--------------------------------" << endl;
        			cout << "-- 1、继续购买 -- 0、结束购买 --" << endl;
        			cout << "--------------------------------" << endl;
        			cout << "请输入您的选择:";
        			cin >> temp;
        			if (0 == temp)
        			{
        				cout << "购买结束" << endl;
        				return;//退出购买
        			}
        			else
        			{
        				cout << "购买继续" << endl;
        				continue;
        			}
        		}
        		else
        		{
        			break;//输入合法退出死循环
        		}
        	}
        	int money = 0;//金钱
        	money = sup.goodsArray[arr[input - 1] - 1]->price * number;
        	cout << endl;
        	cout << "----------------------------------------" << endl;
        	cout << "--------------- 电子发票 ---------------" << endl;
        	cout << "---  购买的商品名称:" << sup.goodsArray[arr[input - 1] - 1]->name << endl;
        	cout << "---  购买的商品数量:" << number << "  个" << endl;
        	cout << "---  购买的商品单价:" << sup.goodsArray[arr[input - 1] - 1]->price << "  元" << endl;
        	cout << "---  总价:" << money << "  元" << endl;
        	cout << "----------------------------------------" << endl;
        	cout << endl;
        	cout << "................请支付.................." << endl;
        	cout << endl;
        	//更新库存量
        	sup.goodsArray[arr[input - 1] - 1]->inventory = inven - number;
        	//更新文件的数据
        	sup.save();
        	cout << "购买成功,欢迎您下次购买" << endl;
        	//按任意键后,清屏回到上级目录
        	cout << endl;
        	system("pause");
        	system("cls");
        }
        //用户展示商品
        void user::showGoods_user(supermarket& sup)
        {
        	sup.showGoods();
        	//按任意键后,清屏回到上级目录
        	cout << endl;
        	system("pause");
        	system("cls");
        }
        //用户查找商品
        void user::inquireGoods_user(supermarket& sup)
        {
        	cout << "-----------------------------------------" << endl;
        	cout << "--  1、按商品类别、名称、生产厂家查询  --" << endl;
        	cout << "--  2、按商品类别、名称查询            --" << endl;
        	cout << "-----------------------------------------" << endl;
        	cout << "请输入要查询的方法序号:";
        	int input;
        	cin >> input;
        	if (1 == input)
        	{
        		int i = sup.inquireGoods_1();
        		sup.printInquireGoods_1(i);
        	}
        	else if (2 == input)
        	{
        		int* p = sup.inquireGoods_2();
        		sup.printInquireGoods_2(p);
        	}
        	//按任意键后,清屏回到上级目录
        	cout << endl;
        	system("pause");
        	system("cls");
        }
        //用户排序商品
        void user::sortGoods_user(supermarket& sup)
        {
        	cout << "------------------------" << endl;
        	cout << "--  1、按价格排序     --" << endl;
        	cout << "--  2、按库存量排序   --" << endl;
        	cout << "------------------------" << endl;
        	cout << "请输入要排序方法序号:";
        	int input;
        	cin >> input;
        	if (1 == input)
        	{
        		sup.sortGoods_1();
        		sup.showGoods();
        	}
        	else if (2 == input)
        	{
        		sup.sortGoods_2();
        		sup.showGoods();
        	}
        	//按任意键后,清屏回到上级目录
        	cout << endl;
        	system("pause");
        	system("cls");
        }
        

        8、管理员密码:helloworld


        总结(运行截图)

        超市商品管理系统设计 C++实现,在这里插入图片描述,第6张

        超市商品管理系统设计 C++实现,在这里插入图片描述,第7张

        超市商品管理系统设计 C++实现,在这里插入图片描述,第8张

        超市商品管理系统设计 C++实现,在这里插入图片描述,第9张

        其他的远行截图省略,自行运行