Bookshop


#include<iostream.h>

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
typedef class book
{
public:
char book_name[30];
char author[30];
int b_id;
int a_id;
int copy;
float price;
}; //the class is of type book


void administrator(book records);
void user (book records);


void main()
{

textmode(C80);
_setcursortype(_SOLIDCURSOR);
textbackground(4);
textcolor(2);
int i;
char str[10];
while(1)
{
book records;
clrscr();


cout<<"\n\n\t\t    ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»";
cout<<"\n\t\t    º    WELCOME TO REHAN'S BOOKSHOP  º";
cout<<"\n\t\t    ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ";
cout<<"\n\n\n\n\n\t\t1. ADMINISTRATOR";
cout<<"\n\t\t2. GUEST";
cout<<"\n\t\t0. EXIT";
cout<<"\n\n\t\tEnter your choice :";
fflush(stdin);
i=getch()-'0';

switch (i)
{
case 2:
user(records);
break;

case 1:
cout<<"\n\n\t\tPASSWORD: ";

for(i=0;str[i-1]!=13;i++)
{
str[i]=getch();
cout<<"*";
}
str[i-1]='\0';

if(strcmp(str,"rehan")==0)
administrator(records);

else
cout<<"\n\n\t\tWRONG PASSWORD\n\n\n\t\tpress any key to continue..";
getch();
break;

case 0:
exit(0);

}//end of switch case
}//end of while

} //end of main function




void administrator(book records)
{
FILE *fp,*tf;
char ch,arr[30];
int i,x=0,y=0;
long int recsize;

fp=fopen("BOOK.C","rb+"); //opening a file in read mode...the file contains information about different books
if (fp==NULL)
{
fp=fopen("BOOK.C","wb+"); //opening the file in write mode
if(fp==NULL)
{
puts("cannot open file");
getch();
exit(0);
}
}

recsize=sizeof(records);

while(1)
{
clrscr();
cout<<"\t\t\tWELCOME BOOKSHOP ADMINISTRATOR\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\n\n\n\n\n\n\t\t1. View all Books\n\t\t2. Add a Book\n\t\t3. Search a Book\n\t\t4. Modify a Book\n\t\t5. Delete a Book\n\t\t6. Logout\n\t\t0. Exit";
cout<<"\n\n\n\t\tEnter your choice: ";
i=getch()-'0';
fflush(stdin);


ch='n';
x=0;
y=0;
clrscr();
rewind(fp); //keeps the file pointer at the starting of file

switch(i)
{


case 1://view all books

cout<<"\t\t\t\tVIEW BOOKS\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
while(fread(&records,recsize,1,fp)==1) //reads the data from the file
{

cout<<"Book "<<x++<<"\nÍÍÍÍÍÍÍ";
cout<<"\n\n Name = "<<records.book_name;
cout<<"\n ID = "<<records.b_id;
cout<<"\n Aurthor = "<<records.author;
cout<<"\n Number of books = "<<records.copy;
cout<<"\n Price of book = "<<records.price;
cout<<"\n\n\n";
}//end of while
cout<<"Total "<<x<<" Books";
cout<<"\n\npress any key to continue..";

getch();
break;



case 2://add a book

cout<<"\t\t\t\tADD A BOOK\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
fseek(fp,0,SEEK_END); //keep the file pointer at the end of file
cout<<"\n\nEnter Book Details:-";
cout<<"\n\n\t\tName :";
cin>>records.book_name;
cout<<"\t\tAurthor :";
cin>>records.author;
cout<<"\t\tBook ID :";
cin>>records.b_id;
cout<<"\t\tNumber of Books :";
cin>>records.copy;
cout<<"\t\tPrice :";
cin>>records.price;


fwrite(&records,recsize,1,fp);// writing the input data into the file in binary format
fflush(stdin);//clearing the input buffer
cout<<"\n\n\t\tBook successfully added\n\n\n\t\tPress any key to Continue..";
getch();
break;



case 3://search a book

cout<<"\t\t\t\tBOOK SEARCH\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\n\n\tSearch by\n\t1. Book Name\n\t2. Aurthor's Name\n\n\tchoice:";
cin>>i;
x=0;
if(i==1)
{
cout<<"\n\n\t\t\tEnter Book Name :";
cin>>arr;

while(fread(&records,sizeof(book),1,fp)==1)//reading the data from file
{
if(strcmp(records.book_name,arr)==0)//comparing the data with input book name
{
cout<<"\nName = "<<records.book_name;//displaying the required results
cout<<"\nAurthor = "<<records.author;
cout<<"\nID = "<<records.b_id;
cout<<"\nPrice = "<<records.price;
x++;
}//end of if
}//end of while
}



else if(i==2)
{
cout<<"\n\n\t\t\tEnter Aurthor's Name:";
scanf("%s",arr);//taking input the author name
rewind(fp);
while(fread(&records,sizeof(book),1,fp)==1)//reading the data from file
{
if(strcmp(records.author,arr)==0)//comparing the input with the data of file
{
cout<<"\nName = "<<records.book_name;
cout<<"\nAurthor = "<<records.author;
cout<<"\nBook ID = "<<records.b_id;
cout<<"\nPrice = "<<records.price;
x++;
}//end of if
}//end of while
}
else
{
cout<<"\n\n\n\n\t\t\tWrong choice";
}

cout<<"\n\n\t\t\t"<<x<<" Books found..";
cout<<"\n\n\t\t\tPress any key to continue..";
getch();
break;//end case 3:



case 4://modify a book

cout<<"\t\t\t\tBOOK MODIFY\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\n\n\nEnter Book ID to modify :";
cin>>i;
while(fread(&records,recsize,1,fp)==1)
{
if(records.b_id==i)
{
cout<<"\n\n\t\tModify Book "<<records.book_name;
cout<<"\n\n\tName :";
cin>>records.book_name;
cout<<"\tAurthor :";
cin>>records.author;
cout<<"\tBook ID :";
cin>>records.b_id;
cout<<"\tNumber of books :";
cin>>records.copy;
cout<<"\tPrice :";
cin>>records.price;
cout<<"\n\tBook Modified...\n\n\n\t\t\tPress any key to continue...";

fseek(fp,-recsize,SEEK_CUR);
fwrite(&records,recsize,1,fp);//writing the modified record in the file
x++;
getch();
}//end of if

}//end of while
if(x==0)
{
cout<<"No Books with ID "<<i;
}
fflush(stdin);//clearing input buffer
cout<<"Press any key to continue..";
break;//end case 5:



case 5://delete a book

cout<<"\t\t\t\tBOOK DELETE\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\n\n\n\t\tEnter Book ID To Delete :";
cin>>i;

while(fread(&records,sizeof(book),1,fp)==1)//reading the data from file
{
if(records.b_id==i)
{
y++;//counting if theres any book
}
}

if(y!=0)
{
rewind(fp);
tf=fopen("TEMP.C","wb");//a temporary file has benn opened

while(fread(&records,recsize,1,fp)==1)//readind the data
{
if(records.b_id!=i)
{
fwrite(&records,recsize,1,tf);//writing records in temporary file
}//end of if
}//end of while
cout<<"\n\t\tDelete Book "<<records.book_name;

fclose(fp);//file is colsed
fclose(tf);
cout<<"\n\n\t\tAre you sure you want to Delete this Book (Y/N):";
cin>>ch;
if(ch=='y'||ch=='Y')
{
remove("BOOK.C");//original file is deleted
rename("TEMP.C","BOOK.C");//temporary file is remaned
fp=fopen("BOOK.C","rb+");
cout<<"\n\n\t\tBook Deleted";
}
else
{
cout<<"\n\n\t\tBook is not Deleted";
}
}

if(y==0)
{
cout<<"\n\n\t\t\tNo Book with Book ID "<<i;
}
cout<<"\n\n\t\t\tPress any key to continue..";
getch();
break;

case 6://logout
main();


case 0:

fclose(fp);//closing the file BOOK.C
exit(0);
}//end of switch

}//end of while


  }//end of administrator function


void user(book records)
{
FILE *fp,*s;
char arr[30],ch;
int i,x,cp,e,m;
long int recsize;

fp=fopen("BOOK.C","rb+");//opening the file containing book details in read mode
if(fp==NULL)
{
fp=fopen("BOOK.C","wb+");
if(fp==NULL)
{
puts("CANNOT OPEN FILE");
getch();
exit(0);
}
}
recsize=sizeof(records);

while(1)
{
clrscr();
cout<<"\t\t\t\tWELCOME BOOKSHOP GUEST\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\n\n\n\n\n\n\t\t1. View all Books\n\t\t2. Search a Book by Name\n\t\t3. Search a Book by Aurthor\n\t\t4. Buy a Book\n\t\t5. Logout\n\t\t0. Exit";
cout<<"\n\n\n\t\tEnter your choice: ";
i=getch()-'0';
fflush(stdin);

ch='n';
x=0;
clrscr();
rewind(fp); //keeps the file pointer at the starting of file


switch(i)
{

case 1://view all books

while(fread(&records,recsize,1,fp)==1) //reads the data from the file
{
cout<<"Book "<<x++<<"\nÍÍÍÍÍÍ";
cout<<"\n\n Name = "<<records.book_name;
cout<<"\n ID = "<<records.b_id;
cout<<"\n Aurthor = "<<records.author;
cout<<"\n Number of books = "<<records.copy;
cout<<"\n Price of book = "<<records.price;
cout<<"\n\n\n";
}//end of while

cout<<"Total "<<x<<" Books";
cout<<"\n\n\n\t\t\tpress any key to continue..";
getch();
break;//end case 1


case 2://search by name

cout<<"\t\t\t\tBOOK SEARCH\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\n\n\n\n\t\tEnter Book Name :";
cin>>arr;

while(fread(&records,sizeof(book),1,fp)==1)//reading the data from file
{
if(strcmp(records.book_name,arr)==0)//comparing the data with input book name
{
cout<<"\nName ="<<records.book_name;//displaying the required results
cout<<"\nAurthor ="<<records.author;
cout<<"\nID ="<<records.b_id;
cout<<"\nNo of Books ="<<records.copy;
cout<<"\nPrice ="<<records.price;
x++;
}//end of if
}//end of while


cout<<"\n\n\t\t\t"<<x<<" Books found..\n\n\t\t\tPress any key to continue..";
getch();
break;


case 3://search by aurthor

cout<<"\t\t\t\tBOOK SEARCH\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\n\n\n\n\t\tEnter Aurthors Name :";
cin>>arr;

while(fread(&records,sizeof(book),1,fp)==1)//reading the data from file
{
if(strcmp(records.author,arr)==0)//comparing the input with the data of file
{
cout<<"\nName= "<<records.book_name;
cout<<"\nAurthor= "<<records.author;
cout<<"\nBook ID= "<<records.b_id;
cout<<"\nNo of Books Available"<<records.copy;
cout<<"\nPrice ="<<records.price;
x++;
}//end of if
}//end of while

cout<<"\n\n\t\t\t"<<x<<" Books found..\n\n\t\t\tPress any key to continue..";
getch();

break;//end case 3



case 4://buy a book

cout<<"\t\t\t\tBUY BOOK\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\n\n\n\t\tEnter Book ID to buy :";
cin>>i;

while(fread(&records,recsize,1,fp)==1)
{
if(records.b_id==i)
{
cout<<"\n\t\tBook Name="<<records.book_name;
cout<<"\n\n\t\tHow many Books you want to buy:";
cin>>cp;

if(records.copy<cp)//checking for availability of books
{
cout<<"\n\n\t\tonly "<<records.copy<<" Books are available";
break;
}
else
{
cout<<"\n\n\t\tAre you sure you want to buy "<<cp<<" Books (Y/N):";
cin>>ch;
if(ch=='y'||ch=='Y')
{
cout<<"\n\t\tTotal "<<(records.price*cp)<<" Rupees for "<<cp<<" Books";
cout<<"\n\n\t\tThanks for Buying";

}
}

x++;
}//end of if
}//end of while

if(x==0&&ch!='n')
{
cout<<"\n\n\t\tNo Book with ID "<<i;
}
cout<<"\n\n\n\n\t\tPress any key to continue..";

getch();
fflush(stdin);//clearing input buffer
break;//end case 4:

case 5://logout
main();

case 0://exit program

fclose(fp);//closing the file BOOK.C
exit(0);
}//end of switch
}//end of while
}//end of user function

Comments

Popular Posts