📄 customer.cpp
字号:
#include"customer.h"
#include<iostream>
#include<fstream>
#include<windows.h>
#include<vector>
#include<iomanip>
#include<sstream>
#include<conio.h>
#include<cctype>
using namespace std;
void inline get_number(int& number)
{
while(1)
{
cin>>number;
if(cin.fail())
{
cin.clear(ios::goodbit);
fflush(stdin);
cout<<"出错啦!重试!"<<endl;
}
else break;
}
}
int Customer::business_time=0;
Customer::Customer()
{
ifstream if_time;
if_time.open("business_time.dat");
if(!if_time) {cerr<<"缺少文件“business_time.dat”";exit(1);}
if_time.seekg(0);
if_time>>business_time;//读取流水号
if_time.close();
price=0.00;
sell_file.open("record.txt",ios_base::out|ios::app);
if(sell_file.fail())
{
cerr<<"\n没有帐本记录“record.txt”"<<endl;
exit(1);
}
}
Customer::~Customer()
{
ofstream of_time;
of_time.open("business_time.dat");
of_time<<business_time;
of_time.close();
sell_file.close();
}
void Customer::view(Market& m)
{
m.goods_file.seekg(0,ios::beg);
cout<<"\n请选择你要查询的商品类型:\n1--食品 \t2--生活用品\t3--工具 \n";
cout<<"4--学习用品\t5--休闲用品\t6--电子产品\n7--穿着衣物\t0--所有商品\n";
int choice;
get_number(choice);
if(choice<8&&choice>=0)
{
m.goods_file.seekg(choice*1000*sizeof(goods));
cout<<"|编号| 名称 |数量| 商品标价 | 进货日期 |"<<endl;
int max_view=0;
if(choice==0)max_view=10000;
else max_view=1000;
for(int view=0;view<max_view;view++)
{
m.goods_file.read((char*)&g,sizeof(goods));
if(strcmp(g.bar_code,"\0")==0)continue;
else
{
cout<<setw(4)<<g.serial_number
<<setw(10)<<g.name
<<setw(5)<<g.quantity
<<setw(8)<<g.sell_price
<<" "
<<setw(20)<<g.buy_date;
cout<<"_________________________________________________"<<endl;
}
}
}
else
cout<<"错误"<<endl<<"在选购过程中你可以用命令:v--查看商品、\tEsc--退出/结帐、\t\n";
}
void Customer::buy(Market& m)
{
goods g_temp;
cout<<"\n\t\t\t****欢迎光临*****\n";
cout<<"\t\t\t****请你选购*****\n";
char c_buy;
cout<<"在选购过程中你可以用命令:v--查看商品、\tEsc--退出/结帐、\t\n";
while(1)
{
fflush(stdin);
c_buy=getche();
if(c_buy==27)break;//Esc退出
else if(c_buy=='v') view(m);//查看
else if(isdigit(c_buy))
{
cin.putback(c_buy);
cin>>g.serial_number;
m.goods_file.seekg(g.serial_number*sizeof(goods));
m.goods_file.read((char*)&g,sizeof(goods));
if(strcmp(g.bar_code,"\0")==0)
{
cerr<<"\n“你不是在开玩笑吧,我这个店里哪来这个玩意儿?????”"<<endl;
cerr<<"“该项作废!!!!!”"<<endl;
continue;
}
cout<<"你要购买的数量:";
get_number(g_temp.quantity);
if(g_temp.quantity>g.quantity)
{
cerr<<"\n“你不是在开玩笑吧,我这个店里哪来这么多"<<g.name<<"……”"<<endl;
cerr<<"该项作废!!!!!"<<endl;
continue;
}
g.quantity=g_temp.quantity;
vector.push_back(g);
cout<<g.quantity<<"个物品"<<g.serial_number<<"已放到购物车……请继续选购:\n";
cin.ignore();
}
else continue;
}
}
void Customer::check(Market& m,int account)
{
int many=vector.size();
cout<<" |编号| 名称 |购买数量| 商品标价 | 条形码 "<<
"| 进货日期 |"<<endl;
int i_time=1;
goods g_temp;
ostringstream os1,os2;
SYSTEMTIME st2;
GetLocalTime(&st2);
os1<<st2.wYear<<"/"<<st2.wMonth<<"/"<<st2.wDay;
string s7=os1.str();
os2<<st2.wHour<<":"<<st2.wMinute<<":"<<st2.wSecond<<endl;
string s8=os2.str();
this->business_time++;//流水号+1;
sell_file<<"\n\n ****超市(welcome)*****"<<endl
<<"店员:"<<setw(2)<<setfill('0')<<account<<" "
<<"流水号:"<<business_time<<endl
<<"日期:"<<setfill(' ')<<s7<<endl
<<"=========================================="<<endl
<<" 商品名 数量 编号 单价 总价 "<<endl;
for(iterator=vector.begin();iterator!=vector.end();++iterator)
{
cout<<i_time++<<"__";
g.serial_number=(*iterator).serial_number;
g_temp.quantity=(*iterator).quantity;
m.goods_file.seekg(g.serial_number*sizeof(goods));
m.goods_file.read((char*)&g,sizeof(goods));
cout<<setw(4)<<g.serial_number
<<setw(10)<<g.name
<<setw(5)<<g_temp.quantity<<" ";
printf("%8.2f",g.sell_price);//格式化输出
cout<<" "<<setw(10)<<g.bar_code<<" "
<<setw(20)<<g.buy_date<<endl;
g.quantity=(g.quantity-g_temp.quantity);//刷新数量
m.goods_file.seekg(g.serial_number*sizeof(goods));
m.goods_file.write((char*)&g,sizeof(goods));//卖出后刷新文件!!
m.goods_file.seekg(g.serial_number*sizeof(goods));
m.goods_file.read((char*)&g,sizeof(goods));
sell_file<<setw(4)<<setw(10)<<g.name<<setw(5)<<g_temp.quantity
<<setw(10)<<g.serial_number
<<setw(8)<<g.sell_price<<" "
<<g_temp.quantity*g.sell_price<<endl;
price+=g.sell_price*g_temp.quantity;//统计价格
}
float pay;
if(price==0){}
else
{
cout<<"\n请付"<<price<<"元...谢谢"<<endl;
cout<<"付款:";
cin>>pay;
if(pay==price){}
else if(pay<price)
{
while(pay<price)
{
cout<<"……:";
int price_on;
cin>>price_on;
pay+=price_on;
}
}
else
{
cout<<"找您"<<(pay-price)<<"元,请收好!"<<endl;
}
cout<<" 欢迎下次再来……"<<endl;
}
sell_file<<"收款:"<<pay<<ends<<" 找回: "<<pay-price<<endl<<"实收:"<<price<<endl;
sell_file<<"================"<<s8<<endl
<<"欢迎再次光临****超市\n请保留电脑小票作为退货凭证\n";
vector.clear();
price=0;//还原
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -