超市商品管理系统设计—C++实现
文章目录
一、内容要求
新增要求:
- 将管理员和用户分别开。
- 管理员需要密码才能进入系统。
- 查询功能:两种查询功能。第一种是按商品类别、商品名称、生产厂家进行查询;第二种是按商品类别、商品名称进行查询。第一种偏向管理员,第二种偏向用户。
- 修改功能:可以修改所以的属性。
- 排序功能:两种排序功能(都包含升序和降序)。第一种按照价格排序;第二种按照库存量排序。两种排序都要包含升序和降序。
- 清空功能:将商品全部清空的要求。
- 读取文件数据功能:系统运行时,就需要读取文件商品数据。包含文件不存在、文件为空、文件不为空且有数据。
- 存储文件功能:将商品数据存放到文件中。
- 展示商品gongn:打印所有商品数据信息。
- 在增、删、修、排序、清空功能中,要实现文件数据的时时更新。
大纲图
二、源代码(包含大量注释)
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 << "------------------------" <