#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
//Structure used to Store values of bill in file
struct bill
{
char date[20];
int a_price,s_price;
};
//Union used in search of name in book
union search
{
char n[20];
char a[20];
}s;
//Program main class
class book
{
private:
char name[20],author[20],date[20];
int price,copies,a_price;
public:
//Function for input
void input()
{
cout<<"\t\t\t\tEnter author of book :";
fflush(stdin);
cin.getline(author,20);
inputdate: cout<<"\t\t\t\tEnter today's date (dd/mm/yyyy) :";
fflush(stdin);
cin.getline(date,20);
cout<<"\t\t\t\tEnter actual price of the book :";
fflush(stdin);
cin>>a_price;
cout<<"\t\t\t\tEnter selling price of the book :";
cin>>price;
cout<<"\t\t\t\tEnter no. of copies :";
cin>>copies;
}
//Function to initialize book name if it is not already present in file or we wants to modify other things
void ini_name (char bookname[20])
{
strcpy(name,bookname);
}
//Function to display for admin
void admin_display()
{
cout<<"\n\t\t\t\t---------------------------------------\n\t\t\t\tName of book :"<<name<<"\n\t\t\t\tAuthor name :"<<author;
cout<<"\n\t\t\t\tDate when record was added :"<<date;
cout<<"\n\t\t\t\tActual price of book :"<<a_price<<"\n\t\t\t\tSelling price of book :"<<price<<"\n\t\t\t\tNo. of copies available :"<<copies;
cout<<"\n\t\t\t\t---------------------------------------\n\n";
}
//Function to display for buyer
void display()
{
cout<<"\n\t\t\t\t---------------------------------------\n\t\t\t\tName of book :"<<name<<"\n\t\t\t\tAuthor name :"<<author;
cout<<"\n\t\t\t\tPrice of book :"<<price<<"\n\t\t\t\t---------------------------------------\n\n";
}
//Fuction to search for admin
int admin_check(search s)
{
if(strcmp(s.n,name)==0)
{
admin_display();
return 1;
}
else if(strcmp(s.a,author)==0)
{
admin_display();
return 1;
}
else
return 0;
}
//Function to search for customer
int check(search s)
{
if(strcmp(s.n,name)==0)
{
display();
return 1;
}
else if(strcmp(s.a,author)==0)
{
display();
return 1;
}
else
return 0;
}
//Funtion to change the no. of copies in case any customer buy a book
void change_copies(int n)
{
copies-=n;
}
//Function to return price
int getprice()
{
return price;
}
int get_actualprice()
{
return a_price;
}
//Function to reeturn no of copies
int getcopies()
{
return copies;
}
//Functon to return date
string getdate()
{
return date;
}
//Function to return name of book
string getname()
{
return name;
}
//Function to return book's author name
string geta_name()
{
return author;
}
};
//Function to modify in case record is already present when someone is trying to add same name of record
void modify(string name)
{
book b1;
char c;
fstream f2("G:\\book.dat",ios ::in|ios::out|ios::binary);
f2.read((char *)&b1,sizeof(b1));
while(!f2.eof())
{
if(b1.getname()==name)
{
f2.seekp(f2.tellg()-sizeof(b1),ios::beg);
b1.input();
cout<<"\t\t\t\tAre you sure to modify previous record with this record (y/n)? ";
fflush(stdin);
cin>>c;
if(c=='y'||c=='Y')
f2.write((char*)&b1,sizeof(b1));
else
{
cout<<"\n\t\t\t\tRecord not modified";
break;
}
}
f2.read((char *)&b1,sizeof(b1));
}
f2.close();
}
//Function to get value of name to search
void getvalue(int j)
{
switch(j)
{
case 1:
{
cout<<"\t\t\t\tEnter the name of book :";
fflush(stdin);
cin.getline(s.n,20);
break;
}
case 2:
{
cout<<"\t\t\t\tEnter the author's name :";
fflush(stdin);
cin.getline(s.a,20);
break;
}
}
}
int main()
{
int a;
while(1)
{
start: cout<<"\n\n\t\t\t\tEnter \n\t\t\t\t1 to purchase a book\n\t\t\t\t2 to add a new book\n";
cout<<"\t\t\t\t3 to check record of book\n\t\t\t\t4 to modify a record of book\n\t\t\t\t5 to display all records\n";
cout<<"\t\t\t\t6 to check profit or loss of any day\n\t\t\t\t7 to exit :";
cin>>a;
cout<<"\n";
//Code to purchase a book
if(a==1)
{
book b1;
int j,s_price=0,ts_price=0,cpy,a_price,ta_price;
char c;
string name;
char date[20];
z: cout<<"\t\t\t\tPress 1 to purchase via book name and\n\t\t\t\t2 to purchase via author name :";
cin>>j;
fflush(stdin);
getvalue(j);
fstream f2("G:\\book.dat",ios::in | ios::binary);
int x=0,y;
while(f2.read((char *)&b1,sizeof(b1)))
{
if(x==0)
{
x=b1.check(s);
if(x==1)
{
cout<<"\t\t\t\tEnter today's date (dd/mm/yyyy) :";
cin.getline(date,20);
s_price=b1.getprice();
a_price=b1.get_actualprice();
cpy=b1.getcopies();
name=b1.getname();
f2.close();
break;
}
else
{
y=b1.check(s);
}
}
else
{
y=b1.check(s);
}
}
if(x==0)
{
cout<<"\t\t\t\tBook not found\n";
goto z;
}
a: cout<<"\t\t\t\tEnter the no. of copies you wants to purchase :";
fflush(stdin);
cin>>j;
if(j>cpy)
{
cout<<"\t\t\t\tSorry we are out of stock\n";
goto a;
}
//Code to reduce no. of copies if a person buy from record
else if(j<cpy)
{
book b1;
fstream f2("G:\\book.dat",ios::in|ios::out|ios::binary);
while(f2.read((char *)&b1,sizeof(b1)))
{
if(b1.getname()==name)
{
f2.seekp(f2.tellg()-sizeof(b1),ios::beg);
b1.change_copies(j);
f2.write((char*)&b1,sizeof(b1));
}
}
}
ts_price+=s_price*j;
ta_price+=a_price*j;
cout<<"\t\t\t\tDo you wants to purchase more books (y/n)? :";
cin>>c;
if(c=='y'||c=='Y')
goto z;
else
{
cout<<"\n\t\t\t\t------------------------------------\n";
cout<<"\t\t\t\tTotal amount to be paid is :"<<ts_price<<"\n";
cout<<"\t\t\t\t------------------------------------\n";
//Code to make a bill
{
bill b;
b.a_price=ta_price;
strcpy(b.date,date);
b.s_price=ts_price;
ofstream fout("G:\\bill2.dat",ios::app | ios::binary);
fout.write((char*)&b,sizeof(b));
fout.close();
}
}
}
//Code to add a new book
else if(a==2)
{
int pswrd;
book b1;
while(1)
{
cout<<"\t\t\t\tEnter password : ";
cin>>pswrd;
if(pswrd==3572)
break;
else
cout<<"\t\t\t\tWrong password try again\n\n";
}
fflush(stdin);
while(1)
{
int i=0;
char c;
char name[20];
cout<<"\t\t\t\tEnter name of book:";
fflush(stdin);
cin.getline(name,20);
//Code to check if record is already present
ifstream f2("G:\\book.dat",ios::in | ios::binary);
while(f2.read((char *)&b1,sizeof(b1)))
{
if(b1.getname()==name)
i=1;
}
f2.close();
if(i==1)
{
cout<<"\t\t\t\tRecord of this book is already present.\n\t\t\t\tDo you wants to modify it (y/n)? ";
fflush(stdin);
cin>>c;
if(c=='y'||c=='Y')
{
modify(name);
break;
}
else
goto start;
}
b1.ini_name(name);
b1.input();
ofstream f1("G:\\book.dat",ios::app | ios::binary);
f1.write((char*)&b1,sizeof(b1));
f1.close();
cout<<"\t\t\t\tEntry saved\n";
cout<<"\t\t\t\tDo you want to add more books (y/n)? ";
fflush(stdin);
cin>>c;
if(c=='y'||c=='Y')
continue;
else
break;
}
}
//Code to check record of a book
else if(a==3)
{
int pswrd;
book b1;
while(1)
{
cout<<"\t\t\t\tEnter password : ";
cin>>pswrd;
if(pswrd==3572)
break;
else
cout<<"\t\t\t\tWrong password try again\n\n";
}
fflush(stdin);
int j;
cout<<"\t\t\t\tPress 1 to check record via book name and\n\t\t\t\t2 to check record via author name :";
cin>>j;
fflush(stdin);
getvalue(j);
ifstream f2("G:\\book.dat",ios::in | ios::binary);
int x=0,y;
while(f2.read((char *)&b1,sizeof(b1) ))
{
if(x==0)
{
x=b1.admin_check(s);
}
else
{
y=b1.admin_check(s);
}
}
f2.close();
if(x==0)
{
cout<<"\t\t\t\tNothing found\n";
}
}
//Code to modify a record in a file
else if(a==4)
{
book b1;
int pswrd;
while(1)
{
cout<<"\t\t\t\tEnter password : ";
cin>>pswrd;
if(pswrd==3572)
break;
else
cout<<"\t\t\t\tWrong password try again\n\n";
}
t: cout<<"\t\t\t\tPress 1 to modify a record via book name and\n\t\t\t\t2 to modify via author name :";
int j=1;
fflush(stdin);
cin>>j;
getvalue(j);
fstream f2("G:\\book.dat",ios::in|ios::out|ios::binary);
int x=0;
f2.read((char *)&b1,sizeof(b1));
while(!f2.eof())
{
if(j==1)
{
if(b1.getname()==s.n)
{
char c;
f2.seekp(f2.tellg()-sizeof(b1),ios::beg);
cout<<"\n\t\t\t\tThe record is :-\n";
b1.admin_display();
b1.input();
cout<<"\t\t\t\tAre you sure to modify previous record with this record (y/n)? ";
fflush(stdin);
cin>>c;
if(c=='y'||c=='Y')
{
f2.write((char*)&b1,sizeof(b1));
x=1;
}
else
{
cout<<"\n\t\t\t\tRecord not modified";
goto start;
}
}
}
else if(j==2)
{
if(b1.geta_name()==s.a)
{
char c;
f2.seekp(f2.tellg()-sizeof(b1),ios::beg);
cout<<"\n\t\t\t\tThe record is :-\n";
b1.admin_display();
b1.input();
cout<<"\t\t\t\tAre you sure to modify previous record with this record (y/n)? ";
fflush(stdin);
cin>>c;
if(c=='y'||c=='Y')
{
f2.write((char*)&b1,sizeof(b1));
x=1;
}
else
{
cout<<"\n\t\t\t\tRecord not modified";
goto start;
}
}
}
f2.read((char *)&b1,sizeof(b1));
}
if(x==0)
{
cout<<"\t\t\t\tRecord not found\n\n";
goto t;
}
f2.close();
}
//Code to display all records
else if(a==5)
{
int p;
cout<<"\t\t\t\tEnter password :";
cin>>p;
if(p==3572)
{
book b1;
ifstream f2("G:\\book.dat",ios::in | ios::binary);
while(f2.read((char *)&b1,sizeof(b1) ))
{
b1.admin_display();
}
f2.close();
}
else
{
cout<<"Wrong password try again...\n";
}
}
//Code to check profit of any day
else if(a==6)
{
bill b;
char date[20];
int pswrd,pl=0,a_price=0,s_price=0;
while(1)
{
cout<<"\t\t\t\tEnter password : ";
cin>>pswrd;
if(pswrd==3572)
break;
else
cout<<"\t\t\t\tWrong password try again\n\n";
}
cout<<"\t\t\t\tEnter date (dd/mm/yyyy) :";
fflush(stdin);
cin.getline(date,20);
ifstream fin("G:\\bill2.dat",ios::in | ios::binary);
if(fin.fail())
{
cout<<"\n\t\t\t\tNo record found\n";
goto start;
}
fin.read((char*)&b,sizeof(b));
while(!fin.eof())
{
if(strcmp(b.date,date)==0)
{
a_price+=b.a_price;
s_price+=b.s_price;
}
fin.read((char*)&b,sizeof(b));
}
fin.close();
pl=s_price-a_price;
if(pl<0)
{
pl*=-1;
cout<<"\t\t\t\tLoss : Rs."<<pl;
}
else if(pl>0)
{
cout<<"\t\t\t\tProfit : Rs."<<pl;
}
else if(pl==0)
cout<<"\t\t\t\tNo profit no loss";
}
//Code to exit from program
else if(a==7)
break;
else
{
cout<<"\t\t\t\tWrong input\n";
goto start;
}
}
}