📄 sale.cpp
字号:
//*******************************
// 前台销售模块
//*******************************
#include<iostream.h>
#include<iomanip.h>
#include<fstream.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
//#include"book.h"
#include"booksale.h"
int match(char *);
void showtime();
void sale()
{
book p;
booksale bs[50]; //假设客户一次最多买书的种类不超过50
// int species=0; //用于记录书库中实际书的种类数
int i=0,flag=-1;
int n=0; //购买一本书的数量
int size=0; //记录客户实际购买书的种类数
char cname[51],ch='0',again='0';
fstream file("book.dat",ios::in|ios::out|ios::binary);
if(!file)
{
cout<<"\n\n\t打开文件失败!"<<endl;
exit(1);
}
do
{
system("cls");
cout<<"\n\n\n\t请输入你要购买数的书名:";
cin>>cname;
cin.ignore();
cout<<endl;
flag=match(cname);
if(flag==0)
{
cout<<"查无此书!"<<endl;
system("pause");
}
else
{
file.seekg(0l,ios::beg);
while(!file.eof())
{
file.read((char*)&p,sizeof(p));
if(strcmp(cname,p.getbookname())==0)
break;
}
cout<<"你要的书的信息如下:"<<endl;
cout<<p;
cout<<endl<<"请输入要买书的数量:";
cin>>n;
if(n>p.getvolumn())
{
cout<<"\n对不起,你要的数量已超过该书的库存量"<<p.getvolumn()<<endl;
}
if(n<=p.getvolumn())
{
bs[i].setbookname(p.getbookname());
bs[i].setisbn(p.getisbn());
bs[i].setnum(n);
bs[i].setretail(p.getretail());
i++;
}
}
cout<<"\n继续购买其他书请按y键"<<endl;
cout<<"\n按其他任意键结束本次购买!"<<endl;
cout<<"\n请输入:";
cin>>again;
cin.ignore();
}while(again=='y');
file.close();
size=i;
if(i==0)
{
cout<<"\t\t谢谢光临!欢迎下次再来!"<<endl;
system("pause");
return;
}
if(again!='y'&&i>0&&size<=50)
{
system("cls");
cout<<"\n\n\t\t\t前台销售模块"<<endl<<endl;
showtime();
cout<<"--------------------------------------------------------------------------------";
for(i=0;i<size;i++)
{
cout<<setw(8)<<"数 量"
<<setw(13)<<"ISBN号"
<<setw(20)<<"书 名"
<<setw(15)<<"单 价"
<<setw(15)<<"金 额"<<endl;
cout<<setw(8)<<bs[i].getnum()
<<setw(13)<<bs[i].getisbn()
<<setw(20)<<bs[i].getbookname()
<<setiosflags(ios::fixed)
<<setprecision(2)
<<setw(15)<<bs[i].getretail()
<<setw(15)<<bs[i].getretail()*bs[i].getnum()<<endl;
bs[i].setaccount(bs[i].getretail()*bs[i].getnum());
}
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"\t\t销售合计:"<<" ¥"<<bs[0].getaccount()<<"元"<<endl;
cout<<"\t\t零售税 :"<<" ¥"<<bs[0].getaccount()*bs[0].getrate()<<"元"<<endl;
cout<<"\t\t应付总额:"<<" ¥"<<bs[0].getaccount()*(1+bs[0].getrate())<<"元"<<endl;
cout<<"--------------------------------------------------------------------------------";
cout<<"\n\n\t\t\t谢谢光临!"<<endl<<endl<<endl;
system("pause");
// 创建销售日志
struct tm *times;
time_t t;
t=time(0);
times=localtime(&t);
fstream outf("salelog.txt",ios::out|ios::app);
if(!outf)
{
cout<<" 创建销售日志失败!"<<endl;
system("pause");
}
outf<<"--------------------------------------------------------------------------------"<<endl;
//把时间写入销售日志里
outf<<(times->tm_year+1900)<<"年"
<<(times->tm_mon+1)<<"月"
<<times->tm_mday<<"日"<<endl<<endl;
for(i=0;i<size;i++)
{
outf<<i+1<<". "
<<" 书 名 :"<<bs[i].getbookname()
<<" ISBN号 :"<<bs[i].getisbn()
<<" 单 价 :"<<bs[i].getretail()<<"元"
<<" 数 量 :"<<bs[i].getnum()<<"本"
<<" 金 额 :"<<bs[i].getretail()*bs[i].getnum()<<"元"<<endl;
}
outf<<endl;
outf<<"销售合计:"<<bs[0].getaccount()<<"元"<<endl
<<"零售税 :"<<bs[0].getaccount()*bs[0].getrate()<<"元"<<endl
<<"销售总额:"<<bs[0].getaccount()*(1+bs[0].getrate())<<"元"<<endl;
outf<<"--------------------------------------------------------------------------------"<<endl;
outf.close();
//在book.dat保存销售后的信息
file.open("book.dat",ios::in|ios::out|ios::binary);
if(!file)
{
cout<<"书库数据更新失败!"<<endl;
system("pause");
exit(1);
}
for(i=0;(i<size)&&(!file.eof());i++)
{
file.read((char*)&p,sizeof(p));
if(strcmp(bs[i].getbookname(),p.getbookname())==0)
{
p.setvolumn(p.getvolumn()-bs[i].getnum());
file.seekp(-(long)sizeof(p),ios::cur);
file.write((char*)&p,sizeof(p));
}
}
file.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -