首页 > 编程语言 >超市管理系统C++

超市管理系统C++

时间:2024-03-05 14:12:43浏览次数:24  
标签:cout 管理系统 ios C++ 超市 num open string name

超市管理系统(摘自大一小学期同学作品)
一、C++语言程序设计------《超市管理系统设计》问题分析
这个超市商品管理系统的主要功能是为超市的管理层提供货物查询及银行转帐货物管理服务,方便超市管理层工作,提高管理层的服务质量和服务效率。
超市商品的信息必须经由各部门及人事初及时汇总到财务处,管理处结合提供的的有关超市商品的信息,并把信息及时反馈给后勤所在部门方便补货,最后管理和后勤部可以方便、快捷的管理超市的货物。
超市的服务器的管理员享有对入库货物的管理与修改。
系统实现后,提高超市的服务效率。降低分发货物的错误发生率,减少信息交流的烦琐过程及其带来的开销,最大限度的方便超市在职人员。

二、系统环境
2.1 软件环境
Windows 10家庭中文版
Visual Studio 2022
2.2 硬件环境
处理器 13th Gen Intel(R) Core(TM) i7-13650HX 2.60 GHz
机带 RAM 16.0 GB (15.6 GB 可用)
设备 ID D6CEE385-7C39-437E-BEC9-7337493683AE
产品 ID 00342-30845-43139-AAOEM
系统类型 64 位操作系统, 基于 x64 的处理器
笔和触控 没有可用于此显示器的笔或触控输入

三、设计内容
3.1 功能模块及详细说明
(用例图,类图等,及其说明)
图1 系统主要功能框图

根据用户需求,设计系统主要功能如下:
(1) 类Supermarket:
拥有三个函数Add,Sale,Search,分别实现添加商品,卖出商品,查找商品的功能
(2) 类:Goods
拥有虚函数ShowMe,为派生类提供显示信息的函数
(3)派生类:DailyGoods(基类Goods)
拥有函数ShowMe 用于显示商品名称,现有数量
(4)派生类:Food(基类Goods)
拥有函数ShowMe用于显示商品名称,现有数量以及食品保质期
(5)派生类:ElecticalAppliance(基类Goods)
拥有函数ShowMe用于显示商品名称,现有数量以及食品保质期
简单细化为:
(1)类Supermarket
函数:
void Add(); //添加新的商品或者添加现有商品数量
void Sale(string _name,int _num); //如果商品现有数量少于卖出数量,则不能继续卖出,并给出提示
void Search(string _category); //显示查询结果(每类商品有哪些商品,每种商品的名称和现有数量)
数据成员:

(2)类:Goods
数据成员:
string name; //商品名称
int num; //商品现有数量
函数:
virtual void ShowMe()=0; //用于显示商品信息
(3)派生类:DailyGoods(基类Goods)
数据成员:

函数:
void ShowMe(); //显示商品名称,现有数量
(4)派生类:Food(基类Goods)
数据成员:
string date; //食品保质期
函数:
void ShowMe(); //显示商品名称,现有数量以及食品保质期
(5)派生类:ElecticalAppliance(基类Goods)
数据成员:
string colour; //家电颜色
函数:
void ShowMe; //显示商品名称,现有数量以及家电颜色
【要求】:
1.重载输入“>>”运算符,是的cin直接读入上述DailyGoods类、Foods类以及ElecticalAppliance类的对象值
2.main函数中,测试上述功能,即可以根据菜单命令增加、卖出和查询各类商品

图2 系统中基类及其派生类

图3 系统中相互协作的类

四、原代码测试
4.1添加商品测试用例及结果

4.2卖出商品测试用例及结果


结果:成功卖出并显示余下信息,卖出大于库存显示错误信息。
4.3查找商品测试用例及结果

如上图所示功能都正常。
源代码:
`#include

include

include

include <windows.h>

include <stdio.h>

include<conio.h>

include

include"Supermarket.h"

include"Goods.h"

include"DailyGoods.h"

include"Food.h"

include"ElectricalAppliance.h"

using namespace std;
void loading();
using std::setw;

void Supermarket::Add()
{
system("cls");
int s, start;
string curID;
string line;
int flag = 0;
int sale_num = 0, final_num = 0;
ifstream in;
fstream inz;
stringstream ss;
string name = "";
int num = 0;
string date = "";
string color = "";
cout << "\t\t\t\t请输入要添加的商品类型:" << "\n";
cout << "\t\t\t\t1.日常用品" << "\n";
cout << "\t\t\t\t2.食品" << "\n";
cout << "\t\t\t\t3.家电" << "\n";
cout << "\t\t\t\t";
cin >> flag;
if (flag == 1)
{
ofstream addDailyGoods("DailyGoods.txt", ios::app);
if (!addDailyGoods) {
cout << "\t\t\t\tCan not open file\n";
exit(0);
}
cout << "\t\t\t\t商品名称:";
cin >> name;
cout << "\t\t\t\t商品数量:";
cin >> num;
in.open("DailyGoods.txt", ios::in | ios::binary);
while (in.peek() != EOF)
{
start = in.tellg();
getline(in, line);
ss << line;
string curID;
ss >> curID;
if (curID == name)
{
in.close();
s = start;
inz.open("DailyGoods.txt", ios::in | ios::binary);
inz.seekp(s, ios::beg);
inz >> name >> num ;
final_num = num + sale_num;
num = final_num;
DailyGoods b1(name, num);
b1.ShowMe();
ss.str("");
return;
}
else {
addDailyGoods << std::left << setw(18) << name << setw(8) << num << endl;
addDailyGoods.close();
}
}
}
else if (flag == 2)
{
ofstream addFood("Food.txt", ios::app);
if (!addFood) {
cout << "\t\t\t\tCan not open file\n";
exit(0);
}
cout << "\t\t\t\t商品名称:";
cin >> name;
cout << "\t\t\t\t商品数量:";
cin >> num;
cout << "\t\t\t\t商品保质期:";
cin >> date;
in.open("Food.txt", ios::in | ios::binary);
while (in.peek() != EOF) {
start = in.tellg();
getline(in, line);
ss << line;
string curID;
ss >> curID;
if (curID == name)
{
in.close();
s = start;
inz.open("Food.txt", ios::in | ios::binary);
inz.seekp(s, ios::beg);
inz >> name >> num >> date;
final_num = num + sale_num;
num = final_num;
Food b2(name, num, date);
b2.ShowMe();
ss.str("");
return;
}
else {
addFood << std::left << setw(18) << name << setw(8) << num << setw(8) << date << endl;
addFood.close();
}
}
}
else if (flag == 3)
{
ofstream addElectricalApplicance("ElectricalAppliance.txt", ios::app);
if (!addElectricalApplicance) {
cout << "Can not open file\n";
exit(0);
}
cout << "\t\t\t\t商品名称:";
cin >> name;
cout << "\t\t\t\t商品数量:";
cin >> num;
cout << "\t\t\t\t商品颜色:";
cin >> color;
in.open("ElectricalAppliance.txt", ios::in | ios::binary);
while (in.peek() != EOF)
{
start = in.tellg();
getline(in, line);
ss << line;
string curID;
ss >> curID;
if (curID == name)
{
in.close();
s = start;
inz.open("ElectricalAppliance.txt", ios::in | ios::binary);
inz.seekp(s, ios::beg);
inz >> name >> num >> color;
if (num < sale_num) { cout << "\t\t\t\t商品数量少于卖出数量!" << endl; }
else {
final_num = num - sale_num;
num = final_num;
ElectricalAppliance b3(name, num, color);
b3.ShowMe();
}
}
else {
addElectricalApplicance << std::left << setw(18) << name << setw(8) << num << setw(8) << color << endl;
addElectricalApplicance.close();
}
}

}

}

void Supermarket::Search()
{
system("cls");
int s, start;
string curID;
string line;
int flag = 0, flg = 0;
string name = "";
int num = 0;
string date = "";
string color = "";
ifstream in;
ifstream ind, inf, ine;
fstream inz;
string searchname;
stringstream ss,sd,sf,se;
cout << "\t\t\t\t1.查询全部商品信息" << endl;
cout << "\t\t\t\t2.查询某种商品信息" << endl;
cout << "\t\t\t\t";
cin >> flg;
if (flg == 1) {
cout << "\t\t\t\t全部日常用品类商品为:" << endl;
ind.open("DailyGoods.txt", ios::in | ios::binary);
if (!ind.is_open())
{
cout << "\t\t\t\tfile open error" << endl;
exit(0);
}
else {
while (ind.peek() != EOF) {
getline(ind, line);
sd << line;
sd >> name >> num;
cout << "\t\t\t\t商品名为:" << name << "\t" << "数量为:" << num << endl;
}
}
ind.close();
cout << endl << endl << endl;
cout << "\t\t\t\t全部食品类商品为:" << endl;
inf.open("Food.txt", ios::in | ios::binary);
if (!inf.is_open())
{
cout << "\t\t\t\tfile open error" << endl;
exit(0);
}
else {
while (inf.peek() != EOF) {
getline(inf, line);
sf << line;
sf >> name >> num >> date;
cout << "\t\t\t\t商品名为:" << name << "\t" << "数量为:" << num << "\t"<<"保质期为:"<<date<<endl;
}
}
inf.close();
cout << endl << endl << endl;
cout << "\t\t\t\t全部家电类商品为:" << endl;
ine.open("ElectricalAppliance.txt", ios::in | ios::binary);
if (!ine.is_open())
{
cout << "\t\t\t\tfile open error" << endl;
exit(0);
}
else {
while (ine.peek() != EOF) {
getline(ine, line);
se << line;
se >> name >> num >> color;
cout << "\t\t\t\t商品名为:" << name << "\t" << "数量为:" << num << "\t" << "颜色为:" << color << endl;
}
}
ine.close();
cout << endl << endl << endl;
}
else {
cout << "\t\t\t\t请输入要搜索的商品名称:";
cin >> searchname;
cout << "\t\t\t\t请输入要搜索的商品类型:" << "\n";
cout << "\t\t\t\t1.日常用品" << "\n";
cout << "\t\t\t\t2.食品" << "\n";
cout << "\t\t\t\t3.家电" << "\n";
cout << "\t\t\t\t";
cin >> flag;
if (flag == 1)
{
in.open("DailyGoods.txt", ios::in | ios::binary);
if (!in.is_open())
{
cout << "\t\t\t\tfile open error" << endl;
exit(0);
}
else
{
while (in.peek() != EOF)
{
start = in.tellg();
getline(in, line);
ss << line;
ss >> curID;
if (curID == searchname)
{
in.close();
s = start;
inz.open("DailyGoods.txt", ios::in | ios::binary);
inz.seekp(s, ios::beg);
inz >> name >> num;
DailyGoods b1(name, num);
b1.ShowMe();
return;
}
ss.str("");
}
cout << "\t\t\t\t对不起您查找的商品不存在!" << endl;
in.close();
}
}
if (flag == 2)
{
in.open("Food.txt", ios::in | ios::binary);
if (!in.is_open())
{
cout << "\t\t\t\tfile open error" << endl;
return;
}
else
{
while (in.peek() != EOF)
{
start = in.tellg();
getline(in, line);
ss << line;
string curID;
ss >> curID;
if (curID == searchname)
{
in.close();
s = start;
inz.open("Food.txt", ios::in | ios::binary);
inz.seekp(s, ios::beg);
inz >> name >> num >> date;
Food b2(name, num, date);
b2.ShowMe();
return;
}
ss.str("");
}
cout << "\t\t\t\t对不起您查找的商品不存在!" << endl;
in.close();
}
}
if (flag == 3)
{
in.open("ElectricalAppliance.txt", ios::in | ios::binary);
if (!in.is_open())
{
cout << "\t\t\t\tfile open error" << endl;
return;
}
else
{
while (in.peek() != EOF)
{
start = in.tellg();
getline(in, line);
ss << line;
string curID;
ss >> curID;
if (curID == searchname)
{
in.close();
s = start;
inz.open("ElectricalAppliance.txt", ios::in | ios::binary);
inz.seekp(s, ios::beg);
inz >> name >> num >> color;
ElectricalAppliance b3(name, num, color);
b3.ShowMe();
return;
}
ss.str("");
}
cout << "\t\t\t\t对不起您查找的商品不存在!" << endl;
in.close();
}
}
}
}

void Supermarket::Sale() {
system("cls");
int s, start;
string curID;
string line;
int flag = 0;
string name = "";
int num = 0;
int sale_num = 0, final_num = 0;
string date = "";
string color = "";
ifstream in;
fstream inz;
string searchname;
stringstream ss;
cout << "\t\t\t\t请选择你要卖出商品的种类:" << endl;
cout << "\t\t\t\t1.日常用品" << endl;
cout << "\t\t\t\t2.食物" << endl;
cout << "\t\t\t\t3.家电" << endl;
cout << "\t\t\t\t";
cin >> flag;
cout << "\t\t\t\t请输入要卖出的商品:";
cin >> searchname;
cout << "\t\t\t\t输入卖出商品数量:";
cin >> sale_num;
if (flag == 1) {
in.open("DailyGoods.txt", ios::in | ios::binary);
if (!in.is_open())
{
cout << "\t\t\t\tfile open error" << endl;
exit(0);
}
else {
while (in.peek() != EOF)
{
start = in.tellg();
getline(in, line);
ss << line;
ss >> curID;
if (curID == searchname)
{
in.close();
s = start;
inz.open("DailyGoods.txt", ios::in | ios::binary);
inz.seekp(s, ios::beg);
inz >> name >> num;
if (num < sale_num) { cout << "\t\t\t\t商品数量少于卖出数量!" << endl; }
else {
final_num = num - sale_num;
num = final_num;
DailyGoods b1(name, num);
b1.ShowMe();
}
return;
}
ss.str("");
}
}
}
if (flag == 2)
{
in.open("Food.txt", ios::in | ios::binary);
if (!in.is_open())
{
cout << "\t\t\t\tfile open error" << endl;
return;
}
else
{
while (in.peek() != EOF)
{
start = in.tellg();
getline(in, line);
ss << line;
string curID;
ss >> curID;
if (curID == searchname)
{
in.close();
s = start;
inz.open("Food.txt", ios::in | ios::binary);
inz.seekp(s, ios::beg);
inz >> name >> num >> date;
if (num < sale_num) { cout << "\t\t\t\t商品数量少于卖出数量!" << endl; }
else {
final_num = num - sale_num;
num = final_num;
Food b2(name, num, date);
b2.ShowMe();
}
return;
}
ss.str("");
}
cout << "\t\t\t\t对不起您查找的商品不存在!" << endl;
in.close();
}
}
if (flag == 3)
{
in.open("ElectricalAppliance.txt", ios::in | ios::binary);
if (!in.is_open())
{
cout << "\t\t\t\tfile open error" << endl;
return;
}
else
{
while (in.peek() != EOF)
{
start = in.tellg();
getline(in, line);
ss << line;
string curID;
ss >> curID;
if (curID == searchname)
{
in.close();
s = start;
inz.open("ElectricalAppliance.txt", ios::in | ios::binary);
inz.seekp(s, ios::beg);
inz >> name >> num >> color;
if (num < sale_num) { cout << "\t\t\t\t商品数量少于卖出数量!" << endl; }
else {
final_num = num - sale_num;
num = final_num;
ElectricalAppliance b3(name, num, color);
b3.ShowMe();
}
return;
}
ss.str("");
}
cout << "\t\t\t\t对不起您查找的商品不存在!" << endl;
in.close();
}
}
}

Goods::Goods()
{
name = "";
num = 0;
}
Goods::Goods(string na, int nu)
{
name = na;
num = nu;
}
string Goods::getgoodname()
{
return name;
}
int Goods::getgoodnum()
{
return num;
}

void Food::ShowMe()
{
cout << "\t\t\t\t该商品为食品" << "\n";
cout << "\t\t\t\t商品名称为:" << name << "\n";
cout << "\t\t\t\t商品所剩数量为:" << num << "\n";
cout << "\t\t\t\t该商品的保质期为:" << date << "\n";
}
string Food::getdate()
{
return date;
}

istream& operator>>(istream& in, Food& ob) {
in >> ob.date;
return in;
}

void ElectricalAppliance::ShowMe()
{
cout << "\t\t\t\t该商品为家电" << "\n";
cout << "\t\t\t\t商品名称为:" << name << "\n";
cout << "\t\t\t\t商品所剩数量为:" << num << "\n";
cout << "\t\t\t\t该商品的颜色为:" << color << "\n";
}
string ElectricalAppliance::getcolor()
{
return color;
}

istream& operator>>(istream& in, ElectricalAppliance& ob) {
in>>ob.color;
return in;
}

void DailyGoods::ShowMe()
{
cout << "\t\t\t\t该商品为日常用品" << "\n";
cout << "\t\t\t\t商品名称为:" << name << "\n";
cout << "\t\t\t\t商品所剩数量为:" << num << "\n";
}

istream& operator>>(istream& in, DailyGoods& ob) {
in >> ob.name;
in >> ob.num;
return in;
}

int main() {
system("color F0");
string password,line;
ifstream fin;
stringstream ps;
string str;
char pw[50], ch;
cout << "\t\t\t\t--------------------欢迎进入超市管理系统--------------------" << endl;
getline(fin,line);
ps << line;
ps >> str;
fin.close();
cout << "\t\t\t\t";
int i = 0;
while ((ch = _getch()) != '\r')
{
if (ch == '\b' && i > 0)
{
printf("\b \b");
--i;
}
else if (ch != '\b')
{
pw[i++] = ch;
printf("*");
}
}

	Sleep(300);
	loading();
	while (1) {
		cout << "\t\t\t\t请问您要进行哪项操作:" << endl;
		cout << "\t\t\t\t0.退出系统" << endl;
		cout << "\t\t\t\t1.增加商品" << endl;
		cout << "\t\t\t\t2.卖出商品" << endl;
		cout << "\t\t\t\t3.查询商品" << endl;
		int num = 0;
		cout << "\t\t\t\t";
		cin >> num;
		switch (num) {
		case 0: cout << "\t\t\t\t感谢您的使用" << endl << "\t\t\t\t祝您生活愉快!" << endl; return 0;
		case 1: Supermarket A; A.Add(); cout << "\t\t\t\t操作成功!" << endl; break;
		case 2: Supermarket B; B.Sale(); cout << "\t\t\t\t操作成功!" << endl; break;
		case 3: Supermarket C; C.Search(); cout << "\t\t\t\t操作成功!" << endl; break;
		default:cout << "\t\t\t\t请输入正确的数字!" << endl; continue;
		}
	}
}

}

void loading() {
system("color F0");
int c = 1, d = 0;
for (int i = 0; i < 50; i++)
{
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
cout << "\t\t\t\t+-------------------------------------------------+" << endl;
cout << "\t\t\t\t|";
for (int a = 0; a < i; a++) //打印▋
{
cout << "▋";
}
for (int b = 49 - i; b > 0; b--) //打印空格
{
cout << " ";
}
d += 2; //进度数
cout << "| " << d << "%" << endl;
cout << "\t\t\t\t+-------------------------------------------------+" << endl;
cout << "\t\t\t\t 正在加载中,请稍后.";
for (int j = 0; j < c % 6; j++)
{
cout << ".";
}
c++;
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
Sleep(10); //进度条读取速度
if (i != 49)
{
system("cls"); //清屏
}
}
Sleep(3000);
system("cls");
}`

五、优化方案
我感觉这个系统在使用的过程中存在安全问题,就算不是超市管理员也能轻松打开系统,对库存制造影响,所以我为此系统增加了输入密码的环节,并且支持修改密码,提高系统的安全性。
完成修改后的源代码如下所示:
`#include

include

include

include <windows.h>

include <stdio.h>

include<conio.h>

include

include"Supermarket.h"

include"Goods.h"

include"DailyGoods.h"

include"Food.h"

include"ElectricalAppliance.h"

using namespace std;
void loading();
void changepassword();
using std::setw;

void Supermarket::Add()
{
system("cls");
int s, start;
string curID;
string line;
int flag = 0;
int sale_num = 0, final_num = 0;
ifstream in;
fstream inz;
stringstream ss;
string name = "";
int num = 0;
string date = "";
string color = "";
cout << "\t\t\t\t请输入要添加的商品类型:" << "\n";
cout << "\t\t\t\t1.日常用品" << "\n";
cout << "\t\t\t\t2.食品" << "\n";
cout << "\t\t\t\t3.家电" << "\n";
cout << "\t\t\t\t";
cin >> flag;
if (flag == 1)
{
ofstream addDailyGoods("DailyGoods.txt", ios::app);
if (!addDailyGoods) {
cout << "\t\t\t\tCan not open file\n";
exit(0);
}
cout << "\t\t\t\t商品名称:";
cin >> name;
cout << "\t\t\t\t商品数量:";
cin >> num;
in.open("DailyGoods.txt", ios::in | ios::binary);
while (in.peek() != EOF)
{
start = in.tellg();
getline(in, line);
ss << line;
string curID;
ss >> curID;
if (curID == name)
{
in.close();
s = start;
inz.open("DailyGoods.txt", ios::in | ios::binary);
inz.seekp(s, ios::beg);
inz >> name >> num ;
final_num = num + sale_num;
num = final_num;
DailyGoods b1(name, num);
b1.ShowMe();
ss.str("");
return;
}
else {
addDailyGoods << std::left << setw(18) << name << setw(8) << num << endl;
addDailyGoods.close();
}
}
}
else if (flag == 2)
{
ofstream addFood("Food.txt", ios::app);
if (!addFood) {
cout << "\t\t\t\tCan not open file\n";
exit(0);
}
cout << "\t\t\t\t商品名称:";
cin >> name;
cout << "\t\t\t\t商品数量:";
cin >> num;
cout << "\t\t\t\t商品保质期:";
cin >> date;
in.open("Food.txt", ios::in | ios::binary);
while (in.peek() != EOF) {
start = in.tellg();
getline(in, line);
ss << line;
string curID;
ss >> curID;
if (curID == name)
{
in.close();
s = start;
inz.open("Food.txt", ios::in | ios::binary);
inz.seekp(s, ios::beg);
inz >> name >> num >> date;
final_num = num + sale_num;
num = final_num;
Food b2(name, num, date);
b2.ShowMe();
ss.str("");
return;
}
else {
addFood << std::left << setw(18) << name << setw(8) << num << setw(8) << date << endl;
addFood.close();
}
}
}
else if (flag == 3)
{
ofstream addElectricalApplicance("ElectricalAppliance.txt", ios::app);
if (!addElectricalApplicance) {
cout << "Can not open file\n";
exit(0);
}
cout << "\t\t\t\t商品名称:";
cin >> name;
cout << "\t\t\t\t商品数量:";
cin >> num;
cout << "\t\t\t\t商品颜色:";
cin >> color;
in.open("ElectricalAppliance.txt", ios::in | ios::binary);
while (in.peek() != EOF)
{
start = in.tellg();
getline(in, line);
ss << line;
string curID;
ss >> curID;
if (curID == name)
{
in.close();
s = start;
inz.open("ElectricalAppliance.txt", ios::in | ios::binary);
inz.seekp(s, ios::beg);
inz >> name >> num >> color;
if (num < sale_num) { cout << "\t\t\t\t商品数量少于卖出数量!" << endl; }
else {
final_num = num - sale_num;
num = final_num;
ElectricalAppliance b3(name, num, color);
b3.ShowMe();
}
}
else {
addElectricalApplicance << std::left << setw(18) << name << setw(8) << num << setw(8) << color << endl;
addElectricalApplicance.close();
}
}

}

}

void Supermarket::Search()
{
system("cls");
int s, start;
string curID;
string line;
int flag = 0, flg = 0;
string name = "";
int num = 0;
string date = "";
string color = "";
ifstream in;
ifstream ind, inf, ine;
fstream inz;
string searchname;
stringstream ss,sd,sf,se;
cout << "\t\t\t\t1.查询全部商品信息" << endl;
cout << "\t\t\t\t2.查询某种商品信息" << endl;
cout << "\t\t\t\t";
cin >> flg;
if (flg == 1) {
cout << "\t\t\t\t全部日常用品类商品为:" << endl;
ind.open("DailyGoods.txt", ios::in | ios::binary);
if (!ind.is_open())
{
cout << "\t\t\t\tfile open error" << endl;
exit(0);
}
else {
while (ind.peek() != EOF) {
getline(ind, line);
sd << line;
sd >> name >> num;
cout << "\t\t\t\t商品名为:" << name << "\t" << "数量为:" << num << endl;
}
}
ind.close();
cout << endl << endl << endl;
cout << "\t\t\t\t全部食品类商品为:" << endl;
inf.open("Food.txt", ios::in | ios::binary);
if (!inf.is_open())
{
cout << "\t\t\t\tfile open error" << endl;
exit(0);
}
else {
while (inf.peek() != EOF) {
getline(inf, line);
sf << line;
sf >> name >> num >> date;
cout << "\t\t\t\t商品名为:" << name << "\t" << "数量为:" << num << "\t"<<"保质期为:"<<date<<endl;
}
}
inf.close();
cout << endl << endl << endl;
cout << "\t\t\t\t全部家电类商品为:" << endl;
ine.open("ElectricalAppliance.txt", ios::in | ios::binary);
if (!ine.is_open())
{
cout << "\t\t\t\tfile open error" << endl;
exit(0);
}
else {
while (ine.peek() != EOF) {
getline(ine, line);
se << line;
se >> name >> num >> color;
cout << "\t\t\t\t商品名为:" << name << "\t" << "数量为:" << num << "\t" << "颜色为:" << color << endl;
}
}
ine.close();
cout << endl << endl << endl;
}
else {
cout << "\t\t\t\t请输入要搜索的商品名称:";
cin >> searchname;
cout << "\t\t\t\t请输入要搜索的商品类型:" << "\n";
cout << "\t\t\t\t1.日常用品" << "\n";
cout << "\t\t\t\t2.食品" << "\n";
cout << "\t\t\t\t3.家电" << "\n";
cout << "\t\t\t\t";
cin >> flag;
if (flag == 1)
{
in.open("DailyGoods.txt", ios::in | ios::binary);
if (!in.is_open())
{
cout << "\t\t\t\tfile open error" << endl;
exit(0);
}
else
{
while (in.peek() != EOF)
{
start = in.tellg();
getline(in, line);
ss << line;
ss >> curID;
if (curID == searchname)
{
in.close();
s = start;
inz.open("DailyGoods.txt", ios::in | ios::binary);
inz.seekp(s, ios::beg);
inz >> name >> num;
DailyGoods b1(name, num);
b1.ShowMe();
return;
}
ss.str("");
}
cout << "\t\t\t\t对不起您查找的商品不存在!" << endl;
in.close();
}
}
if (flag == 2)
{
in.open("Food.txt", ios::in | ios::binary);
if (!in.is_open())
{
cout << "\t\t\t\tfile open error" << endl;
return;
}
else
{
while (in.peek() != EOF)
{
start = in.tellg();
getline(in, line);
ss << line;
string curID;
ss >> curID;
if (curID == searchname)
{
in.close();
s = start;
inz.open("Food.txt", ios::in | ios::binary);
inz.seekp(s, ios::beg);
inz >> name >> num >> date;
Food b2(name, num, date);
b2.ShowMe();
return;
}
ss.str("");
}
cout << "\t\t\t\t对不起您查找的商品不存在!" << endl;
in.close();
}
}
if (flag == 3)
{
in.open("ElectricalAppliance.txt", ios::in | ios::binary);
if (!in.is_open())
{
cout << "\t\t\t\tfile open error" << endl;
return;
}
else
{
while (in.peek() != EOF)
{
start = in.tellg();
getline(in, line);
ss << line;
string curID;
ss >> curID;
if (curID == searchname)
{
in.close();
s = start;
inz.open("ElectricalAppliance.txt", ios::in | ios::binary);
inz.seekp(s, ios::beg);
inz >> name >> num >> color;
ElectricalAppliance b3(name, num, color);
b3.ShowMe();
return;
}
ss.str("");
}
cout << "\t\t\t\t对不起您查找的商品不存在!" << endl;
in.close();
}
}
}
}

void Supermarket::Sale() {
system("cls");
int s, start;
string curID;
string line;
int flag = 0;
string name = "";
int num = 0;
int sale_num = 0, final_num = 0;
string date = "";
string color = "";
ifstream in;
fstream inz;
string searchname;
stringstream ss;
cout << "\t\t\t\t请选择你要卖出商品的种类:" << endl;
cout << "\t\t\t\t1.日常用品" << endl;
cout << "\t\t\t\t2.食物" << endl;
cout << "\t\t\t\t3.家电" << endl;
cout << "\t\t\t\t";
cin >> flag;
cout << "\t\t\t\t请输入要卖出的商品:";
cin >> searchname;
cout << "\t\t\t\t输入卖出商品数量:";
cin >> sale_num;
if (flag == 1) {
in.open("DailyGoods.txt", ios::in | ios::binary);
if (!in.is_open())
{
cout << "\t\t\t\tfile open error" << endl;
exit(0);
}
else {
while (in.peek() != EOF)
{
start = in.tellg();
getline(in, line);
ss << line;
ss >> curID;
if (curID == searchname)
{
in.close();
s = start;
inz.open("DailyGoods.txt", ios::in | ios::binary);
inz.seekp(s, ios::beg);
inz >> name >> num;
if (num < sale_num) { cout << "\t\t\t\t商品数量少于卖出数量!" << endl; }
else {
final_num = num - sale_num;
num = final_num;
DailyGoods b1(name, num);
b1.ShowMe();
}
return;
}
ss.str("");
}
}
}
if (flag == 2)
{
in.open("Food.txt", ios::in | ios::binary);
if (!in.is_open())
{
cout << "\t\t\t\tfile open error" << endl;
return;
}
else
{
while (in.peek() != EOF)
{
start = in.tellg();
getline(in, line);
ss << line;
string curID;
ss >> curID;
if (curID == searchname)
{
in.close();
s = start;
inz.open("Food.txt", ios::in | ios::binary);
inz.seekp(s, ios::beg);
inz >> name >> num >> date;
if (num < sale_num) { cout << "\t\t\t\t商品数量少于卖出数量!" << endl; }
else {
final_num = num - sale_num;
num = final_num;
Food b2(name, num, date);
b2.ShowMe();
}
return;
}
ss.str("");
}
cout << "\t\t\t\t对不起您查找的商品不存在!" << endl;
in.close();
}
}
if (flag == 3)
{
in.open("ElectricalAppliance.txt", ios::in | ios::binary);
if (!in.is_open())
{
cout << "\t\t\t\tfile open error" << endl;
return;
}
else
{
while (in.peek() != EOF)
{
start = in.tellg();
getline(in, line);
ss << line;
string curID;
ss >> curID;
if (curID == searchname)
{
in.close();
s = start;
inz.open("ElectricalAppliance.txt", ios::in | ios::binary);
inz.seekp(s, ios::beg);
inz >> name >> num >> color;
if (num < sale_num) { cout << "\t\t\t\t商品数量少于卖出数量!" << endl; }
else {
final_num = num - sale_num;
num = final_num;
ElectricalAppliance b3(name, num, color);
b3.ShowMe();
}
return;
}
ss.str("");
}
cout << "\t\t\t\t对不起您查找的商品不存在!" << endl;
in.close();
}
}
}

Goods::Goods()
{
name = "";
num = 0;
}
Goods::Goods(string na, int nu)
{
name = na;
num = nu;
}
string Goods::getgoodname()
{
return name;
}
int Goods::getgoodnum()
{
return num;
}

void Food::ShowMe()
{
cout << "\t\t\t\t该商品为食品" << "\n";
cout << "\t\t\t\t商品名称为:" << name << "\n";
cout << "\t\t\t\t商品所剩数量为:" << num << "\n";
cout << "\t\t\t\t该商品的保质期为:" << date << "\n";
}
string Food::getdate()
{
return date;
}

istream& operator>>(istream& in, Food& ob) {
in >> ob.date;
return in;
}

void ElectricalAppliance::ShowMe()
{
cout << "\t\t\t\t该商品为家电" << "\n";
cout << "\t\t\t\t商品名称为:" << name << "\n";
cout << "\t\t\t\t商品所剩数量为:" << num << "\n";
cout << "\t\t\t\t该商品的颜色为:" << color << "\n";
}
string ElectricalAppliance::getcolor()
{
return color;
}

istream& operator>>(istream& in, ElectricalAppliance& ob) {
in>>ob.color;
return in;
}

void DailyGoods::ShowMe()
{
cout << "\t\t\t\t该商品为日常用品" << "\n";
cout << "\t\t\t\t商品名称为:" << name << "\n";
cout << "\t\t\t\t商品所剩数量为:" << num << "\n";
}

istream& operator>>(istream& in, DailyGoods& ob) {
in >> ob.name;
in >> ob.num;
return in;
}

int main() {
system("color F0");
string password,line;
ifstream fin;
stringstream ps;
string str;
char pw[50], ch;
cout << "\t\t\t\t--------------------欢迎进入超市管理系统--------------------" << endl;
cout << "\t\t\t\t请输入密码:" << endl;
fin.open("password.txt", ios::in);
if (!fin) { //文件打开检测
cout << "Can not open file\n";
exit(0);
}
getline(fin,line);
ps << line;
ps >> str;
fin.close();
cout << "\t\t\t\t";
int i = 0;
while ((ch = _getch()) != '\r')
{
if (ch == '\b' && i > 0)
{
printf("\b \b");
--i;
}
else if (ch != '\b')
{
pw[i++] = ch;
printf("*");
}
}
pw[i] = '\0';
password = pw; //键盘输入的密码给密码存放处
printf("\n");
if (pw==str) {
cout << "\t\t\t\t密码正确!" << endl;
cout << "\t\t\t\t正在进入系统" << endl;
Sleep(300);
loading();
while (1) {
cout << "\t\t\t\t请问您要进行哪项操作:" << endl;
cout << "\t\t\t\t0.退出系统" << endl;
cout << "\t\t\t\t1.增加商品" << endl;
cout << "\t\t\t\t2.卖出商品" << endl;
cout << "\t\t\t\t3.查询商品" << endl;
cout << "\t\t\t\t4.修改密码" << endl;
int num = 0;
cout << "\t\t\t\t";
cin >> num;
switch (num) {
case 0: cout << "\t\t\t\t感谢您的使用" << endl << "\t\t\t\t祝您生活愉快!" << endl; return 0;
case 1: Supermarket A; A.Add(); cout << "\t\t\t\t操作成功!" << endl; break;
case 2: Supermarket B; B.Sale(); cout << "\t\t\t\t操作成功!" << endl; break;
case 3: Supermarket C; C.Search(); cout << "\t\t\t\t操作成功!" << endl; break;
case 4: changepassword(); cout << "\t\t\t\t操作成功!" << endl; break;
default:cout << "\t\t\t\t请输入正确的数字!" << endl; continue;
}
}
}
else
{
cout << "\t\t\t\t密码错误" << endl;
}
}

void loading() {
system("color F0");
int c = 1, d = 0;
for (int i = 0; i < 50; i++)
{
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
cout << "\t\t\t\t+-------------------------------------------------+" << endl;
cout << "\t\t\t\t|";
for (int a = 0; a < i; a++) //打印▋
{
cout << "▋";
}
for (int b = 49 - i; b > 0; b--) //打印空格
{
cout << " ";
}
d += 2; //进度数
cout << "| " << d << "%" << endl;
cout << "\t\t\t\t+-------------------------------------------------+" << endl;
cout << "\t\t\t\t 正在加载中,请稍后.";
for (int j = 0; j < c % 6; j++)
{
cout << ".";
}
c++;
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
Sleep(10); //进度条读取速度
if (i != 49)
{
system("cls"); //清屏
}
}
Sleep(3000);
system("cls");
}
void changepassword(){
ofstream fout("password.txt",ios::out);//文件不存在时自动创建在当前目录下
if(!fout){ //文件打开检测
cout<<"Can not open file\n";
return;
}
cout<<"\t\t\t\t请输入修改的密码:";
char chr[50]={0};
cin>>chr;
fout<<chr<<"\n";
fout.close();
system("cls");
return;
}
`
修改密码测试用例及结果

最后我希望我的这篇博客对大家有些许帮助。

标签:cout,管理系统,ios,C++,超市,num,open,string,name
From: https://www.cnblogs.com/hxr2252337/p/18053843

相关文章

  • 从Python语言的角度看C++的指针
    技术背景从一个PythonCoder的角度来说,其实很羡慕C++里面指针类型的用法,即时指针这种用法有可能会给程序带来众多的不稳定因素(据C++老Coder所说)。本文主要站在一个C++初学者的角度来学习一下指针的用法,当然,最好是带着一定的Python基础再去学习C++的逻辑,会更容易一些。内存地址赋......
  • C++面试,实现memcpy,strcpy这2个函数的功能
    `strcpy`和`memcpy`都是用于内存复制的函数,但它们之间有几个关键的区别:1.**复制的对象**:-`strcpy`主要用于复制字符串,它将从源字符串的起始位置开始复制字符,直到遇到源字符串的空字符('\0'),然后将空字符也复制到目标字符串中,表示字符串的结束。-`memcpy`则是通用的内存复......
  • 36C++语言级的四种类型转换
    C++语言级的四种类型转换const_cast:去掉常量属性的类型转换static_cast:提供编译器认为安全的类型转换reinterpret_cast:类似C风格的强制类型转化dynamic_cast:主要用在继承结构中,可以支持RTTI类型识别的上下转换#include<iostream>usingnamespacestd;classBas......
  • C++游戏飞翔的小鸟软件二次开发
    引言:在快节奏的现代生活中,人们总是在寻找一种方式来放松自己,释放内心的压力。游戏作为一种娱乐方式,早已深入人心。今天,我要向大家介绍的是一款简单而又充满挑战的小游戏——飞翔的小鸟。这款游戏的核心玩法是控制一只小鸟在无尽的天空中飞翔,通过点击屏幕使小鸟上升,避开各种障碍......
  • C++网上购书系统项目的二次开发 2252416 hzx
    1、来源:同学大二下的期末大作业:网上购书系统项目。2、运行环境:VisualStudio2019代码:点击查看代码#include"StdAfx.h"#include<iostream>#include"person.h"#include<string.h>#include"globalfunction.h"#include"book.h"#include"adm.h&......
  • c++在类外是不能直接调用私有成员函数的
    c++在类外是不能直接调用私有成员函数的,比如#include<iostream>usingnamespacestd;classA3{voidshow3(){cout<<"classA3"<<endl;//注意,类内部默认是私有}};intmain(){A3obj3;obj3.show3();//这里出错return0;}......
  • C++U6-06 - 一维线性动态规划
    上节课作业:链接:https://pan.baidu.com/s/17Fei1SuGEk5pnSspf_hprg?pwd=hq04提取码:hq04 动态规划  [最长上升子序列]  本题采用动态规划。数据储存,设定数组a[]用于存储数字序列,设定dp[]数组用于统计上升的序列个数;遍历组数a[],在遍历的过程中如果出现了数......
  • C++ mySQL数据库连接池(windows平台)
    C++MySQL数据库连接池新手学了C++多线程,看了些资料练手写了C++数据库连接池小项目,自己的源码地址关键技术点MySQL数据库编程、单例模式、queue队列容器、C++11多线程编程、线程互斥、线程同步通信和unique_lock、基于CAS的原子整形、智能指针shared_ptr、lambda表达式、生产......
  • C++ 简易STL 教程 与 C++ 标准库
    C++STL(标准模板库)是一套功能强大的C++模板类,提供了通用的模板类和函数,这些模板类和函数可以实现多种流行和常用的算法和数据结构,如向量、链表、队列、栈。C++标准模板库的核心包括以下三个组件:组件描述容器(Containers)容器是用来管理某一类对象的集合。C++提供了各种不......
  • C++U5-第06课-广度优先搜索3
    温故知新广搜的概念,编程实现基本流程 二进制矩阵中的最短路径]    【题意分析】找到一个从(0,0)到达(n-1,n-1)的路径并且路径上每一个数字都为0【思路分析】首先如果grid[0][0]=1,那么显然不存在最短路径,因此输出-1。使用dist[x][y]保存左上角单......