Bookshop Management System with C++

by | Jul 25, 2023 | Basic Coding, C/C++, Coding

Home » Coding » Bookshop Management System with C++

The Bookshop Management System with C++ is a console-based application developed with the objective of simplifying the processes involved in managing a bookshop. It leverages the powerful features of the C++ programming language and provides a user-friendly interface for managing the inventory of books.

Introduction

The Bookshop Management System allows you to manage a library of books and perform operations like adding books, modifying book details, deleting books, and displaying all books in the system. This dynamic project covers all the general operations of a bookshop manager and employees, with all the vital information saved systematically in the database so as to retrieve them when necessary. There are different options provided for different users, you can select your desired choice or log in from the given choices. You can add more or make it less, depending upon your choice.

  • After selecting a particular option, it dives the user into the domain of the particular operation that is selected.
  • The respective operations are performed, like modifying the book details, entering the book details, checking the availability of a particular book, and so on.
  • After all the operations finally, the changes made are displayed on the screen.

All the transactions are stored in the database (file) so as to allow the users to retrieve them when found necessary.

Objectives

The bookshop management system with C++ has the following objectives:

1. Efficient Management: The primary objective is to efficiently manage the book inventory of a bookstore or a library.

2. Accessibility: To provide an easy way to access, view, and manipulate the book inventory.

3. Reduction of Manual Work: The system should minimize manual record-keeping and calculations.

4. Inventory Tracking: To keep track of the number of books available in the inventory and their details.

5. User-friendly: The system should have a user-friendly interface for easy interaction.

Requirements

1. C++ Compiler: You will need a C++ compiler to compile and run this code. The GNU Compiler Collection (GCC) is a popular choice and is available on most platforms. On Windows, you might use MinGW (Minimalist GNU for Windows). On macOS, you can use the Xcode Command Line Tools, which includes a GCC compiler. On Linux, the GCC compiler is typically available through the package manager.

2. IDE (Integrated Development Environment): An IDE is not strictly necessary to compile and run C++ code, but it can make the process easier. IDEs often include a text editor with features like syntax highlighting and automatic indentation, making the code easier to read and write. Some popular C++ IDEs are Code::Blocks, Eclipse, CLion, and Visual Studio. The choice of IDE can depend on your personal preference and the platform you’re using.

3. C++ Standard Libraries: This code uses several C++ Standard Libraries, including iostream, fstream, conio.h, string.h, and iomanip. These libraries should be available with any standard C++ compiler.

4. Operating System: This code uses a few functions that are specific to Windows (like system(“cls”) to clear the screen and getch() to get a character from the keyboard), but these could be modified to work on other operating systems. On Unix-based systems like Linux and macOS, you can replace system(“cls”) with system(“clear”). The getch() function is part of the conio.h library, which is not standard C++ and may not be available on all platforms. An alternative could be to use cin.get().

Source Code

#include<fstream>
#include<conio.h>
#include<string.h>
#include<iomanip>
#include<iostream>


using namespace std;


class book_data
{
char books_number[30];
char Book_Name[50];
char Author_Name[20];
int No_Copies;


public:
void Get_BookDetails()
{
cout<<"\nENTER DETAILS OF YOUR DESIRED BOOK WHICH YOU WANT TO PURCHASE\n";
cout<<"\nEnter The Book's Number: ";
cin>>books_number;
cout<<"\nEnter Book's Name: ";
cin.ignore();
cin.getline(Book_Name,50);
cout<<"\nEnter The Author's Name: ";
cin.ignore();
cin.getline(Author_Name,50);
fflush(stdin);
cout<<"\nEnter No. Of Copies of the desired book : ";
cin>>No_Copies;
}


void Show_Book_Data()
{
cout<<"\nBook Number: "<<books_number;
cout<<"\nBook Name: "<<Book_Name;
cout<<"\nAuthor's Name: "<<Author_Name;
cout<<"\nCOPIES : "<<No_Copies;
}
void Modify_Book_Data()
{
cout<<"\nBook number : "<<books_number;
cout<<"\nModify Book Name : ";
cin.ignore();
cin.getline(Book_Name,50);
cout<<"\nModify Author's Name: ";
cin.ignore();
cin.getline(Author_Name,50);
fflush(stdin);
cout<<"\nEnter No. Of Copies : ";
cin>>No_Copies;
}
char* get_book_number()
{
return books_number;
}
void Report()
{cout<<books_number<<setw(30)<<Book_Name<<setw(30)<<Author_Name<<setw(30)<<No_Copies<<endl;}


};





fstream fp;
book_data bk;
void write_book_data()
{
system("cls");
int moremain;
fp.open("book.dat",ios::out|ios::app);
do
{
bk.Get_BookDetails();
fp.write((char*)&bk,sizeof(book_data));
cout<<"\nPress 1 to add some more books to the system";
cout<<"\nPress 2 to go back to the main menu\n";
cout<<"Enter your choice : ";
cin>>moremain;
}while(moremain == 1);
fp.close();
}


void Display_books(char n[])
{
system("cls");
cout<<"\nBOOK DETAILS\n";
int check=0;
fp.open("book.dat",ios::in);
while(fp.read((char*)&bk,sizeof(book_data)))
{
if(strcmp(bk.get_book_number(),n)==0)
{
bk.Show_Book_Data();
check=1;
}
}
fp.close();
if(check==0)
cout<<"\n\nBook does not exist";
getch();
}


void Modify_Book_Data()
{
system("cls");
char n[20];
int found=0;
cout<<"\n\n\tMODIFY BOOK";
cout<<"\n\n\tEnter The book number: ";
cin>>n;
fp.open("book.dat",ios::in|ios::out);
while(fp.read((char*)&bk,sizeof(book_data)) && found==0)
{
if(strcmp(bk.get_book_number(),n)==0)
{
bk.Show_Book_Data();
cout<<"\nEnter The New Details of book"<<endl;
bk. Modify_Book_Data();
int pos=-1*sizeof(bk);
fp.seekp(pos,ios::cur);
fp.write((char*)&bk,sizeof(book_data));
cout<<"\n\n\t Record Updated Successfully...";
found=1;
}
}
fp.close();
if(found==0)
cout<<"\n\n Record Not Found ";
getch();
}


void delete_book_data()
{
system("cls");
char n[20];
int flag=0;
cout<<"\n\n\n\tDELETE BOOK";
cout<<"\n\nEnter The Book's number You Want To Delete: ";
cin>>n;
fp.open("book.dat",ios::in|ios::out);
fstream fp2;
fp2.open("Temp.dat",ios::out);
fp.seekg(0,ios::beg);
while(fp.read((char*)&bk,sizeof(book_data)))
{
if(strcmp(bk.get_book_number(),n)!=0)
{
fp2.write((char*)&bk,sizeof(book_data));
}
else
flag=1;
}
fp2.close();
fp.close();
remove("book.dat");
rename("Temp.dat","book.dat");
if(flag==1)
cout<<"\n\n\tRecord Deleted ..";
else
cout<<"\n\nRecord not found";
getch();
}


void display_all_books()
{
system("cls");
fp.open("book.dat",ios::in);
if(!fp)
{
cout<<"ERROR!!! Sorry you file can't be opened ";
getch();
return;
}
cout<<"\n\n\t\t------Book LIST-------\n\n";
cout<<"============================================================================================\n";
cout<<"Book Number"<<setw(20)<<"Book Name"<<setw(25)<<"Author"<<setw(25)<<"Copies"<<endl;
cout<<"============================================================================================\n";
while(fp.read((char*)&bk,sizeof(book_data)))
{
bk.Report();
}
fp.close();
getch();
}


void intro()
{
system("color 06");
system("cls");
cout<<"\t\t\t\t**\t**";
cout<<"\t\t\t\t***\t***";
cout<<"\t\t\t\t****\t****";
cout<<"\t\t\t\t*****\t*****";
cout<<"\t\t\t\t******\t******";
cout<<"\t\t\t\t*******\t*******";
cout<<"\t\t\t\t********\t********";
cout<<"\t\t\t\t********\t********";
cout<<"\t\t\t\t*******\t*******";
cout<<"\t\t\t\t******\t******";
cout<<"\t\t\t\t*****\t*****";
cout<<"\t\t\t\t****\t****";
cout<<"\t\t\t\t***\t***";
cout<<"\t\t\t\t**\t**";
}



int main()
{
int option = 0;
for(;;)
{
intro();
cout<<"\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
cout<<"\n\t\tPress 1 to PURCHASE A BOOK";
cout<<"\n\t\tPress 2 to DISPLAY ALL BOOKS";
cout<<"\n\t\tPress 3 to CHECK AVAILABILITY OF THE BOOK IN STORE";
cout<<"\n\t\tPress 4 to MODIFY BOOK RECORDS";
cout<<"\n\t\tPress 5 to DELETE BOOK RECORDS";
cout<<"\n\t\tPress 6 to Exit ";
cout<<"\n\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";
cout<<"\n\t\tOption: ";
cin>>option;
switch(option)
{
case 1: system("cls");
write_book_data();
break;
case 2: display_all_books();
break;
case 3:
char num[20];
system("cls");
cout<<"\n\n\tPlease Enter The book No. ";
cin>>num;
Display_books(num);
break;
case 4: Modify_Book_Data();break;
case 5: delete_book_data();break;
case 6: exit(0);
break;
default:cout<<"\a";
}


}
}

Explanation of the Code

Let’s analyze the components of this bookshop management system with C++ code:

1. Class book_data: This class defines the attributes and methods associated with a book. It contains data members like books_number, Book_Name, Author_Name, and No_Copies. Methods like Get_BookDetails(), Show_Book_Data(), Modify_Book_Data(), get_book_number() and Report() are used to manipulate and fetch these data.

2. File handling: The “fstream” library is used to create a binary file called “book.dat” to store and retrieve the books’ data. This file is opened and closed as required by different functions in the code.

3. write_book_data() Function: This function is used to add a new book to the system. It opens the book file in append mode, gets the book details from the user, writes it to the file, and closes the file.

4. Display_books() Function: This function displays the details of a book based on the book number provided as an argument.

5. Modify_Book_Data() Function: This function is used to update the details of an existing book in the system. It first searches for the book in the file, and if the book is found, it asks the user to enter the new details, modifies the book object, and updates it in the file.

6. delete_book_data() Function: This function deletes a book from the system. It creates a temporary file and copies all the books, except the one to be deleted, from the original file to the temporary file. It then deletes the original file and renames the temporary file.

7. display_all_books() Function: This function displays all the books in the system by reading all the book objects from the file and printing their details.

8. Main Function: The main function presents a menu to the user and performs the chosen operation by calling the corresponding function. This menu is inside an infinite loop, so the program will continue running until the user selects the exit option.

Output

Bookshop Management System with C++ - Menu ScreenBookshop Management System with C++ - Option Selection

Conclusion

The Bookshop management system with C++ is a simple application that provides an easy, favorable, and efficient platform for book lovers. This code provides a basic idea of how to implement a book inventory system in C++. It doesn’t have any error-handling mechanism, and the user interface is quite basic. 

More C++ Projects>>>

 

You May Also Like To Create…

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *