#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class buyer{
protected:
string name;
int buyerID;
string address;
double pay;
public:
buyer();
buyer(string n,int b,string a,double p);
string getbuyname();
string getaddress();
double getpay();
int getid();
virtual void display()=0;
virtual void mindisplay()=0;
virtual void setpay(double=0)=0;
};
class member:public buyer{
private:
int leaguer_grade;
public:
member(string n,int b,int l,string a,double p):buyer(n,b,a,p)
{
leaguer_grade=l;}
void display();
void setpay(double p);
void mindisplay();
};
class honoured_guest:public buyer{
double discount_rate;
public:
honoured_guest(string n,int b,double r,string a,double p):buyer(n,b,a,p)
{
discount_rate=r;
}
void display ();
void setpay(double p);
void mindisplay();
};
class layfolk:public buyer{
public:
layfolk(string n,int b,string a,double p):buyer(n,b,a,p)
{}
void display();
void setpay(double p);
void mindisplay();
};
buyer::buyer()
{
name="";
buyerID=0;
address="";
pay=0;
}
buyer::buyer(string n,int b,string a,double p)
{
name=n;
buyerID=b;
address=a;
pay=p;
}
double buyer::getpay()
{
return pay;
}
string buyer::getaddress()
{
return address;
}
string buyer::getbuyname()
{
return name;
}
int buyer::getid(){
return buyerID;
}
void member::display()
{cout<<"购书人姓名:"<<name<<"\t";
cout<<"购书人编号:"<<buyerID<<"\t";
cout<<"购书人为会员,级别:"<<leaguer_grade<<"\n";
cout<<"地址:"<<address<<"\n";
}
void member::mindisplay()
{
cout<<"购书人姓名:"<<name<<"\t";
cout<<"购书人编号:"<<buyerID<<"\t";
}
void member::setpay(double p)
{
if(leaguer_grade==1)
pay=.95*p+pay;
else if(leaguer_grade==2)
pay=.90*p+pay;
else if(leaguer_grade==3)
pay=.85*p+pay;
else if(leaguer_grade==4)
pay=.8*p+pay;
else if(leaguer_grade==5)
pay=.7*p+pay;
else
cout<<"级别错误!";
}
void honoured_guest::display(){
cout<<"购书人姓名:"<<name<<"\t";
cout<<"购书人编号:"<<buyerID<<"\t";
cout<<"购书人为贵宾!折扣率为:"<<discount_rate*100<<"% \n" ;
cout<<"地址:"<<address<<"\n\n";
}
void honoured_guest::setpay(double p)
{pay=pay+(1-discount_rate)*p;
}
void layfolk::display()
{
cout<<"购书人姓名:"<<name<<"\t";
cout<<"购书人编号:"<<buyerID<<"\t";
cout<<"购书人为普通人"<<"\n" ;
cout<<"地址:"<<address<<"\n\n";
}
void layfolk::setpay(double p)
{
pay=pay+p;
}
void layfolk::mindisplay()
{
cout<<"购书人姓名:"<<name<<"\t";
cout<<"购书人编号:"<<buyerID<<"\t";
}
void honoured_guest::mindisplay()
{
cout<<"购书人姓名:"<<name<<"\t";
cout<<"购书人编号:"<<buyerID<<"\t";
}
class book{
protected:
string book_ID;
string book_name;
string author;
string publishing;
double price;
public:
book();
book(string b_id,string b_n,string au,string pu,double pr);
void display();
string getbook_ID();
string getbook_name();
string getauthor();
string getpublishing();
double getprice();
};
book::book(string b_id,string b_n,string au,string pu,double pr)
{
book_ID=b_id;
book_name=b_n;
author=au;
publishing=pu;
price=pr;
}
book::book()
{
book_ID="";
book_name="";
author="";
publishing="";
price=0;
}
void book::display()
{
cout<<"书号:"<<book_ID<<"\t";
cout<<"书名:"<<book_name<<"\t";
cout<<"作者:"<<author<<"\n";
cout<<"出版社:"<<publishing<<"\t";
cout<<"定价:"<<price<<"\n";
}
string book::getbook_ID()
{
return book_ID;
}
string book::getbook_name()
{
return book_name;
}
string book::getauthor()
{
return author;
}
string book::getpublishing()
{
return publishing;
}
double book::getprice()
{
return price;
}
int main()
{
int a,d=1,i=0,buyerid,flag,n,re=1;
book *c[2];
layfolk b1("路人甲",1,"北京",0);
honoured_guest b2("路人乙",2,.6,"上海",0);
member b3("路人丙",3,5,"广州",0);
buyer*b[3]={&b1,&b2,&b3};
book c1("7-302-04504-6","c++程序设计","谭浩强","清华",25);
book c2("7-402-03388-9","数据结构","许卓群","北大",20);
c[0]=&c1;
c[1]=&c2;
cout<<"--------------------------网上购书结账系统---------------------------"<<endl;
cout<<"输入购书者编号以登录\n"<<endl;
cin>>n;
b[n-1]->mindisplay();
cout<<"\n\n请确认信息,确认请按1,取消请按0."<<endl;
cin>>re;
if(re==0)
{
return 0;
}
else if(re==1)
{
cout<<"有什么可以帮到您的吗?:"<<endl;
cout<<"1、个人详细信息" <<endl;
cout<<"2、现有图书"<<endl;
cout<<"3、折扣等级"<<endl;
cout<<"4、查询购书所需费用"<<endl;
cout<<"5、显示并打印订单信息"<<endl;
cout<<"6、书籍上架"<<endl;
cout<<"7、退出系统"<<endl;
int a;
cin>>a;
while(d=1){
switch (a)
{
case 1:{
ofstream fout("f2.dat",ios::out);
if(!fout)
{
cout<<"无法打开文件.\n";
return 1;
}
fout<<"购书人信息:\n\n";
if(n==1)
{
fout<<"购书人姓名:路人甲 购书人编号:1 购书人为普通人\n地址:北京\n";
}
else if(n==2)
{
fout<<"购书人姓名:路人乙 购书人编号:2 购书人为贵宾!折扣率为:60%\n地址:上海\n";
}
else if(n==3)
{
fout<<"购书人姓名:路人丙 购书人编号:3 购书人为会员\n地址:广州\n";
}
fout.close();
ifstream fin("f2.dat",ios::in);
if(!fin)
{
cout<<"无法打开文件.\n";
return 1;
}
char haolei[100]={0};
while (fin >>haolei)
{
cout << haolei << endl;
}
fin.close();
break;}
case 2: {
ofstream book("f3.dat",ios::out);
if(!book)
{
cout<<"无法打开文件.\n";
return 1;
}
book<<"\n图书信息:\n\n";
book<<"书号:7-302-04504-6 书名:c++程序设计 作者:谭浩强\n出版社:清华 定价:25\n书号:7-402-03388-9\n出版社:北大 定价:20\n";
book.close();
ifstream fdown("f3.dat",ios::in);
if(!fdown)
{
cout<<"无法打开文件.\n";
return 1;
}
char ka[666]={0};
while (fdown>>ka)
{
cout << ka << endl;
}
fdown.close();
break;}
case 3:{cout<<"普通人无折扣" <<endl;
cout<<"一级会员折扣率:95%"<<endl;
cout<<"二级会员折扣率:90%"<<endl;
cout<<"三级会员折扣率:85%"<<endl;
cout<<"四级会员折扣率:80%"<<endl;
cout<<"五级会员折扣率:70%"<<endl;
cout<<"贵宾折扣率:60%"<<endl;
break;
}
case 4:{
cout<<"\n\n请输入购书人编号:";
cin>>buyerid;
flag=0;
for(i=0;i<4;i++)
if(b[i]->getid()==buyerid){flag=1;break;
}
if(!flag)
{
cout<<"编号不存在"<<endl;
}
else{
b[i]->setpay(c[0]->getprice());
b[i]->setpay(c[1]->getprice());
cout<<endl<<"购书人需要付费:"<<b[i]->getpay()<<"\n\n";
}
break;}
case 5:{
cout<<"\n\n请输入购书人编号:";
cin>>buyerid;
flag=0;
b[i]->display();
cout<<"\n图书信息:\n\n";
for(i=0;i<2;i++)
c[i]->display();
for(i=0;i<4;i++)
if(b[i]->getid()==buyerid){flag=1;break;
}
if(!flag){
cout<<"编号不存在"<<endl;
}
else{
b[i]->setpay(c[0]->getprice());
b[i]->setpay(c[1]->getprice());
cout<<endl<<"购书人需要付费:"<<b[i]->getpay()<<"\n\n";
}
break;
}
case 6:
{
string a,b,c,d,e;
cout<<"请输入书号"<<endl;
cin>>a;
cout<<"请输入书名"<<endl;
cin>>b;
cout<<"请输入作者"<<endl;
cin>>c;
cout<<"请输入出版社"<<endl;
cin>>d;
cout<<"请输入定价"<<endl;
cin>>e;
cout<<"成功添加新书!"<<endl;
}
default:cout<<"保存成功!";
}
cin>>a;
}
return 0;
}
}