所在位置:主页 > 系统开发 > C++设计一个简单的图书销售系统

C++设计一个简单的图书销售系统

发布时间:2023-12-14 07:24来源:www.sf1369.com作者:宇宇

#include<iostream>

#include<iomanip>

#include<string>

#include<fstream>

usingnamespace std;

constint Maxr=100;

constint Maxb=100;

constint Maxbor=5;

classReader

{

private:

inttag;

intno;

charname[10];

intborbook[Maxbor];

public:

Reader(){}

char*getname() {return name;}

intgettag() {return tag;}

intgetno() {return no;}

voidsetname(char na[])

{

strcpy(name,na);

}

voiddelbook(){ tag=1; }

voidaddreader(int n,char *na)

{

tag=0;

no=n;

strcpy(name,na);

for(inti=0;i<Maxbor;i++)

borbook[i]=0;

}

voidborrowbook(int bookid)

{

for(inti=0;i<Maxbor;i++)

{

if (borbook[i]==0)

{

borbook[i]=bookid;

return;

}

}

}

intretbook(int bookid)

{

for(inti=0;i<Maxbor;i++)

{

if(borbook[i]==bookid)

{

borbook[i]=0;

return1;

}

}

return0;

}

voiddisp()

{

cout<< setw(5) << no <<setw(10) << name<<借书编号:[;

for(inti=0;i<Maxbor;i++)

if(borbook[i]!=0)

cout<< borbook[i] << |;

cout<< ]<<endl;

}

};

classRDatabase

{

private:

inttop;

Readerread[Maxr];

public:

RDatabase()

{

Readers;

top=-1;

fstreamfile(reader.txt,ios::in);

while(1)

{

file.read((char*)&s,sizeof(s));

if(!file)break;

top++;

read[top]=s;

}

file.close();

}

voidclear()

{

top=-1;

}

intaddreader(int n,char *na)

{

Reader*p=query(n);

if(p==NULL)

{

top++;

read[top].addreader(n,na);

return1;

}

return0;

}

Reader*query(int readerid)

{

for(int i=0;i<=top;i++)

if(read[i].getno()==readerid &&

read[i].gettag()==0)

{

return&read[i];

}

returnNULL;

}

voiddisp()

{

for(int i=0;i<=top;i++)

read[i].disp();

}

voidreaderdata();

~RDatabase()

{

fstreamfile(reader.txt,ios::out);

for(int i=0;i<=top;i++)

if(read[i].gettag()==0)

file.write((char*)&read[i],sizeof(read[i]));

file.close();

}

};

voidRDatabase::readerdata()

{

charchoice;

charrname[20];

intreaderid;

Reader*r;

while(choice!='0')

{

cout<<******欢迎使用图书销售管理系统终极版******”<<endl;

cin>> choice;

switch(choice)

{

case'1':

cout<< ******操作菜单******:;

cin>> readerid;

cout<< ******收银员******;

cin>> rname;

addreader(readerid,rname);

break;

case'2':

cout<< ******管理员******:;

cin>> readerid;

r=query(readerid);

if(r==NULL)

{

cout<< ******顾客****** <<endl;

break;

}

cout<< 输入新的姓名:;

cin>> rname;

r->setname(rname);

break;

case'3':

cout<< 输入图书编号:;

cin>> readerid;

r=query(readerid);

if(r==NULL)

{

cout<< 该图书不存在<< endl;

break;

}

r->delbook();

break;

case'4':

cout<< 读入图书编号:;

cin>> readerid;

r=query(readerid);

if(r==NULL)

{

cout<<该图书不存在<<endl;

break;

}

r->disp();

break;

case'5':

disp();

break;

case'6':

clear();

break;

default:cout<<输入错误,请从新输入:;break;

}

}

}

classBook

{

private:

inttag;

intno;

charname[20];

intonshelf;

public:

Book(){}

char*getname() { return name; }

intgetno(){ return no; }

intgettag(){ return tag; }

voidsetname(char na[])

{

strcpy(name,na);

}

voiddelbook(){ tag=1;}

voidaddbook(int n,char *na)

{

tag=0;

no=n;

strcpy(name,na);

onshelf=1;

}

intborrowbook()

{

if(onshelf==1)

{

onshelf=0;

return1;

}

return0;

}

voidretbook()

{

onshelf=1;

}

voiddisp()

{

cout<< setw(6) << no << setw(18) << name << setw(10)

<<(onshelf==1?缺货)<<endl;

}

};

classBDatabase

{

private:

inttop;

Bookbook[Maxb];

public:

BDatabase()

{

Bookb;

top=-1;

fstreamfile(book.txt,ios::in);

while(1)

{

file.read((char*)&b,sizeof(b));

if(!file) break;

top++;

book[top]=b;

}

file.close();

}

voidclear()

{

top=-1;

}

intaddbook(int n,char *na)

{

Book*p=query(n);

if(NULL==p)

{

top++;

book[top].addbook(n,na);

return1;

}

return0;

}

Book*query(int bookid)

{

for(int i=0;i<=top;i++)

if(book[i].getno()==bookid &&book[i].gettag()==0)

{

return&book[i];

}

returnNULL;

}

voidbookdata();

voiddisp()

{

for(int i=0;i<=top;i++)

if(book[i].gettag()==0)

book[i].disp();

}

~BDatabase()

{

fstreamfile(book.txt,ios::out);

for(int i=0;i<=top;i++)

if(book[i].gettag()==0)

file.write((char*)&book[i],sizeof(book[i]));

file.close();

}

};

voidBDatabase::bookdata()

{

charchoice;

charbname[40];

intbookid;

Book*b;

while(choice!='0')

{

cout<<\n\n\n\t\t\t图书维护<<endl<<endl;

cin>> choice;

switch(choice)

{

case'1':

cout<< 输入图书编号:<<endl;

cin>> bookid;

cout<< 输入图书书名:<<endl;

cin>> bname;

addbook(bookid,bname);

break;

case'2':

cout<< 输入图书编号:<<endl;

cin>> bookid;

b=query(bookid);

if(b==NULL)

{

cout<< 该图书缺货<<endl;

break;

}

cout<< 输入新的书名:<<endl;

cin>> bname;

b->setname(bname);

break;

case'3':

cout<< 读入图书编号:<<endl;

cin>> bookid;

b=query(bookid);

if(b==NULL)

{

cout<< 该图书缺货<< endl;

break;

}

b->delbook();

break;

case'4':

cout<< 读入图书编号:<<endl;

cin>> bookid;

b=query(bookid);

if(b==NULL)

{

cout<< 该图书不存在<<endl;

break;

}

b->disp();

break;

case'5':

disp();

break;

case'6':

clear();

break;

default:cout<<输入错误,请从新输入:;

}

}

}

voidmain()

{

charchoice;

intbookid,readerid;

RDatabaseReaderDB;

Reader*r;

BDatabaseBookDB;

Book*b;

while(choice!='0')

{

cout<<endl<<endl<<\t\t\t 图书销售管理系统\n\n\n;

cin>> choice;

switch(choice)

{

case'1':

cout<< 买书图书编号:;

cin>>readerid;

cout<< 图书编号:;

cin>>bookid;

r=ReaderDB.query(readerid);

if(NULL==r)

{

cout<< 不存在该图书,不能买书<<endl;

break;

}

b=BookDB.query(bookid);

if(b==NULL)

{

cout<< 不存在该图书,不能买书<<endl;

break;

}

if(b->borrowbook()==0)

{

cout<< 该图书已售完,不能买书<<endl;

break;

}

r->borrowbook(b->getno());

break;

case'2':

cout<<买书\n 图书编号:;

cin>>readerid;

cout<< 图书编号:;

cin>>bookid;

r=ReaderDB.query(readerid);

if(r==NULL)

{

cout<< 不存在该图书,不能买书<< endl;

break;

}

b=BookDB.query(bookid);

if(b==NULL)

{

cout<< 不存在该图书,不能买书<<endl;

break;

}

b->retbook();

r->retbook(b->getno());

break;

case'3':

BookDB.bookdata();

break;

case'4':

ReaderDB.readerdata();

break;

default:cout<<输入错误,请从新输入:;

}

}

}