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

超市商品管理系统设计—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
  • 总结(运行截图)


一、内容要求

要求

新增要求:

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

大纲图

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述


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

在这里插入图片描述

7个文件,3个头文件,4个.cpp文件
main.cpp
supermarket.h
supermarket.cpp
administrator.h
administrator.cpp
user.h
user.cpp


1、main.cpp文件

#include <iostream>
#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 <iostream>
using namespace std;
#include <string>
#include <fstream>
#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 <iostream>
#include <string>
#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 <iostream>
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


总结(运行截图)

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
其他的远行截图省略,自行运行

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/307578.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

spring boot + mybatis + websocket + js实战

项目技术&#xff1a;spring boot mybatis websocket js 需求背景&#xff1a;当添加一个女孩时&#xff0c;页面的socket收到消息&#xff0c;打印最新的所有女生list&#xff0c;这样可以进一步在react/vue前端框架下&#xff0c;实现当A用户新增了某业务数据后&#xff…

Linux--防火墙,实验案例:基于区域、服务、端口的访问控制

实验环境 某公司的Web服务器&#xff0c;网关服务器均采用Linux CentOS 7.3操作系统&#xff0c;如图2.13所示。为了 加强网络访问的安全性&#xff0c;要求管理员熟悉firewalld防火墙规则的编写&#xff0c;以便制定有效、可行的主机防护策略。 需求描述 > 网关服务器ens3…

视频智能剪辑方案,企业视频制作新时代

视频已经成为了人们获取信息、娱乐和学习的重要方式。然而&#xff0c;传统的视频制作过程繁琐且耗时&#xff0c;这对于许多企业来说无疑是一个巨大的挑战。为了解决这个问题&#xff0c;美摄科技凭借其在机器学习、深度学习等AI算法方面的深厚积累&#xff0c;自主研发了一套…

小白也能懂!人物百度百科怎么创建?手把手教会你创建人物百科

在互联网时代&#xff0c;百度百科已经成为了人们获取信息的重要途径之一。对于名人、企业家、艺术家、专家学者等人物来说&#xff0c;拥有一个完善的百度百科词条不仅能够提升个人形象&#xff0c;还能够为他们的事业发展带来更多的机会&#xff0c;所以很多人都会去选择创建…

【开源商城推荐-LGPL-3.0】ts-mall 聚惠星商城

dts-shop: 聚惠星商城 DTS-SHOP&#xff0c;基于 微信小程序 springboot vue 技术构建 &#xff0c;支持单店铺&#xff0c;多店铺入驻的商城平台。项目包含 微信小程序&#xff0c;管理后台。基于java后台语言&#xff0c;已功能闭环&#xff0c;且达到商用标准的一套项目体…

用golang 实现给图片添加文字水印

package mainimport ("fmt""github.com/golang/freetype""image""image/draw""image/jpeg""io""os""time" )func main() {// 打开原始图片file, err : os.Open("004.jpeg")if err …

加工零件的题解

目录 原题描述&#xff1a; 题目描述 输入格式 输出格式 样例 #1 样例输入 #1 样例输出 #1 样例 #2 样例输入 #2 样例输出 #2 提示 题目大意&#xff1a; 主要思路&#xff1a; 但是我们怎么才能判断出x走到1时L是偶数还是奇数呢&#xff1f; 初始化&#xff1a;…

代客泊车「新赛点」,撬动大市场

低速泊车&#xff0c;正在从过去被忽视的角色&#xff0c;持续受益高阶智驾和整车智能化升级&#xff0c;重新焕发新生。无论是长距离/跨层记忆泊车、行泊一体、代客泊车&#xff0c;还是硬件复用&#xff0c;这个细分赛道的增长逻辑&#xff0c;正在发生微妙变化。 就在上个月…

【网络安全】【密码学】【北京航空航天大学】实验一、数论基础(上)【C语言和Java实现】

实验一、数论基础&#xff08;上&#xff09; 一、实验目的 1、通过本次实验&#xff0c;熟悉相关的编程环境&#xff0c;为后续的实验做好铺垫&#xff1b; 2、回顾数论学科中的重要基本算法&#xff0c;并加深对其的理解&#xff0c;为本学期密码学理论及实验课程打下良好…

Ubuntu启动Xming报错:cannot open display: :1.0

Ubuntu启动Xming报错&#xff1a;cannot open display: :1.0 1、问题描述&#xff1a;2、问题解决&#xff1a;3、实践结果&#xff1a; 叮嘟&#xff01;这里是小啊呜的学习课程资料整理。好记性不如烂笔头&#xff0c;今天也是努力进步的一天。一起加油进阶吧&#xff01; 1、…

# 大模型实战作业02

大模型实战作业02 知识库助手搭建 注 有些问题在回答的时候可能出现乱码的情况 可能的原因 模型内部提示词知识库 这部分可以做适当的优化&#xff0c;对于无法回答或回答质量不佳的回答返回特定话术提升用户体验

《突破自我:2023年度总结与展望》

文章目录 引言&#xff1a;回顾过去一年的亮点&#xff1a;面对的挑战及解决方案&#xff1a;JSBridge原理原理介绍实现流程实现思路第一步&#xff1a;设计出一个Native与JS交互的全局桥梁对象第二步&#xff1a; JS如何调用native第三步&#xff1a;Native如何得知api被调用第…

抽烟识别摄像机

抽烟识别摄像机是一种利用计算机视觉和人工智能技术的设备&#xff0c;能够实时监测和识别吸烟行为。该摄像机通过分析人体姿态和动作&#xff0c;识别出可能的吸烟行为&#xff0c;并及时发出警告或报警。这种摄像机可以广泛应用于公共场所、办公场所、学校和医疗机构等地方&a…

【竞技宝jjb.lol】LOL:ale分析新版本 战士只剩锐雯能玩

北京时间2024年1月10日,随着新年的来临,英雄联盟赛事也开启了全新的篇章,如今距离春季赛开启的时间已经越来越近了。为了让选手结束休赛期后恢复到正常的竞技水平,LPL在前不久刚刚进行了德玛西亚杯的比赛,最终BLG决赛横扫宿敌JDG拿下冠军。而新赛季官方也会推出新版本,那么职业…

[C#]使用winform部署PP-MattingV2人像分割onnx模型

【官方框架地址】 https://github.com/PaddlePaddle/PaddleSeg 【算法介绍】 PP-MattingV2是一种先进的图像和视频抠图算法&#xff0c;由百度公司基于PaddlePaddle深度学习框架开发。它旨在提供更精准和高效的图像分割功能&#xff0c;特别是在处理图像中的细微部分&#xf…

window mysql5.7 搭建主从同步环境

window 搭建mysql5.7数据库 主从同步 主节点 配置文件my3308.cnf [mysql] # 设置mysql客户端默认字符集 default-character-setutf8mb4[mysqld] server-id8 #server-uuidbc701be9-ac71-11ee-9e35-b06ebf511956 log-binD:\mysql_5.7.19\mysql-5.7.19-winx64\mysql-bin binlog-…

Docker实战10|实现volum数据卷

上一篇文章中&#xff0c;仔细讲解了Docker是如何改变当前的root文件系统以及mount等操作。 本文继续讲解Docker是如何实现Volum数据卷的。 实现Volume数据卷 获取代码 git clone https://gitee.com/mjreams/docker.git 上一小节介绍了如何使用AUFS包装busybox&#xff0c…

腾讯云最新优惠活动入口整理汇总

随着云计算技术的快速发展&#xff0c;腾讯云作为国内领先的云服务提供商&#xff0c;一直致力于为用户提供高效、稳定、安全的云服务。为了回馈广大用户的支持&#xff0c;腾讯云经常推出各种优惠活动。本文将对腾讯云最新的优惠活动入口进行整理和汇总&#xff0c;帮助用户更…

计算机图形学理论(6):光线追踪

本系列根据国外一个图形小哥的讲解为本&#xff0c;整合互联网的一些资料&#xff0c;结合自己的一些理解。 什么是光线追踪 该方法是通过跟踪穿过图像平面中每个像素的光路来生成图像。 在现实世界中&#xff0c;光从光源出发&#xff0c;然后到达我们的眼睛。然而&#xff…

面相圆润是有福气的象征

在中国传统文化中&#xff0c;面相是一个非常重要的概念。相信大家肯定听说过“相由心生”这个成语吧&#xff0c;这就是告诉我们&#xff0c;一个人的面貌其实是可以反映出他内心的状态和气质&#xff0c;也可以反映其性格、健康状况和运势等。而一个圆润的面相&#xff0c;则…
最新文章