0% found this document useful (0 votes)
10 views5 pages

Bookstore Project Code

The document is a C++ program for a Bookshop Management System that allows users to manage book inventory and sales. It includes functionalities such as loading and saving book and sale data, adding new books, processing sales, searching for books by ID, and displaying book and sales history. The program utilizes file I/O to persist data and provides a user interface for interaction.

Uploaded by

akashaahmad9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Bookstore Project Code

The document is a C++ program for a Bookshop Management System that allows users to manage book inventory and sales. It includes functionalities such as loading and saving book and sale data, adding new books, processing sales, searching for books by ID, and displaying book and sales history. The program utilizes file I/O to persist data and provides a user interface for interaction.

Uploaded by

akashaahmad9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <iostream>

#include <string>
#include <vector>
#include <fstream>
#include <iomanip>
using namespace std;

class Bookshop {
private:
struct Book {
string title;
string author;
string publisher;
int year;
int price;
string genre;
int id;
};

struct Sale {
string customerName;
string bookTitle;
double price;
double discount;
double finalPrice;
};

vector<Book> books;
vector<Sale> sales;

void loadBooks() {
ifstream bookFile("books.txt");
if (bookFile.is_open()) {
Book book;
while (bookFile >> ws && getline(bookFile, book.title) &&
getline(bookFile, book.author) &&
getline(bookFile, book.publisher) &&
bookFile >> book.year >> book.price &&
ws && getline(bookFile, book.genre) &&
bookFile >> book.id) {
books.push_back(book);
}
bookFile.close();
}
}

void saveBooks() {
ofstream bookFile("books.txt");
if (bookFile.is_open()) {
for (const auto& book : books) {
bookFile << book.title << "\n"
<< book.author << "\n"
<< book.publisher << "\n"
<< book.year << "\n"
<< book.price << "\n"
<< book.genre << "\n"
<< book.id << "\n";
}
bookFile.close();
}
}

void loadSales() {
ifstream saleFile("sales.txt");
if (saleFile.is_open()) {
Sale sale;
while (saleFile >> ws && getline(saleFile, sale.customerName) &&
getline(saleFile, sale.bookTitle) &&
saleFile >> sale.price >> sale.discount &&
saleFile >> sale.finalPrice) {
sales.push_back(sale);
}
saleFile.close();
}
}

void saveSales() {
ofstream saleFile("sales.txt");
if (saleFile.is_open()) {
for (const auto& sale : sales) {
saleFile << sale.customerName << "\n"
<< sale.bookTitle << "\n"
<< sale.price << "\n"
<< sale.discount << "\n"
<< sale.finalPrice << "\n";
}
saleFile.close();
}
}

public:
Bookshop() {
loadBooks();
loadSales();
}

~Bookshop() {
saveBooks();
saveSales();
}

void displayWelcome() {
cout << "************************************************************\n"
<< "* *\n"
<< "* Welcome to the Bookshop *\n"
<< "* Bookshop Management System *\n"
<< "* *\n"
<< "************************************************************\n\n";
}

void updateInventory() {
int currentBooks = books.size();
int booksToAdd, booksToSell;

cout << "Current inventory: " << currentBooks << " books\n";
cout << "Enter number of books to add: ";
cin >> booksToAdd;
cout << "Enter number of books to sell: ";
cin >> booksToSell;

currentBooks += booksToAdd;
currentBooks -= booksToSell;

if (currentBooks < 0) {
cout << "Warning: Inventory is negative! Setting to zero.\n";
currentBooks = 0;
}

cout << "\nUpdated inventory: " << currentBooks << " books\n";
}

void addNewBook() {
Book newBook;

cout << "\nAdd a New Book\n";


cout << "---------------\n";
cout << "Title: ";
cin.ignore(); // Clear newline left in buffer
getline(cin, newBook.title);
cout << "Author: ";
getline(cin, newBook.author);
cout << "Publisher: ";
getline(cin, newBook.publisher);
cout << "Year of Publication: ";
cin >> newBook.year;
cout << "Price: ";
cin >> newBook.price;
cout << "Genre (Science/Computer/Arts/Fiction/Non-Fiction): ";
cin.ignore(); // Clear newline left in buffer
getline(cin, newBook.genre);
cout << "Book ID: ";
cin >> newBook.id;

books.push_back(newBook);

cout << "\nBook Added Successfully!\n";


cout << "------------------------\n";
displayBookDetails(newBook);
}

void displayBookDetails(const Book& book) {


cout << "Title: " << book.title << "\n";
cout << "Author: " << book.author << "\n";
cout << "Publisher: " << book.publisher << "\n";
cout << "Year: " << book.year << "\n";
cout << "Price: $" << book.price << "\n";
cout << "Genre: " << book.genre << "\n";
cout << "Book ID: " << book.id << "\n";
}

void processSale() {
Sale sale;

cout << "\nProcess a Sale\n";


cout << "---------------\n";
cout << "Customer Name: ";
cin.ignore(); // Clear newline left in buffer
getline(cin, sale.customerName);
cout << "Book Title: ";
getline(cin, sale.bookTitle);
cout << "Price: ";
cin >> sale.price;
cout << "Discount: ";
cin >> sale.discount;

if (sale.discount > sale.price) {


cout << "Error: Discount cannot exceed the price.\n";
return;
}

sale.finalPrice = sale.price - sale.discount;


sales.push_back(sale);

cout << "\nSale Completed!\n";


cout << "----------------\n";
cout << "Customer Name: " << sale.customerName << "\n";
cout << "Book Title: " << sale.bookTitle << "\n";
cout << "Original Price: $" << sale.price << "\n";
cout << "Discount: $" << sale.discount << "\n";
cout << "Final Price: $" << sale.finalPrice << "\n";
}

void searchBookByID() {
int searchID;
bool found = false;

cout << "\nEnter Book ID to search: ";


cin >> searchID;

for (const auto& book : books) {


if (book.id == searchID) {
cout << "\nBook Found!\n";
cout << "------------------------\n";
displayBookDetails(book);
found = true;
break;
}
}

if (!found) {
cout << "\nBook with ID " << searchID << " not found.\n";
}
}

void displayHistory() {
cout << "\nBook Inventory History\n";
cout << "-----------------------\n";
for (const auto& book : books) {
displayBookDetails(book);
cout << "------------------------\n";
}

cout << "\nSales History\n";


cout << "--------------\n";
for (const auto& sale : sales) {
cout << "Customer Name: " << sale.customerName << "\n";
cout << "Book Title: " << sale.bookTitle << "\n";
cout << "Original Price: $" << sale.price << "\n";
cout << "Discount: $" << sale.discount << "\n";
cout << "Final Price: $" << sale.finalPrice << "\n";
cout << "------------------------\n";
}
}

void displayAllBooks() {
cout << "\nFull Book Details\n";
cout << "------------------\n";
for (const auto& book : books) {
displayBookDetails(book);
cout << "------------------\n";
}
}
};

int main() {
Bookshop shop;
shop.displayWelcome();

// Updating inventory (example)


shop.updateInventory();

char choice;
cout << "\nDo you want to add a new book? (y/n): ";
cin >> choice;
if (choice == 'y') {
shop.addNewBook();
}

cout << "\nDo you want to process a sale? (y/n): ";


cin >> choice;
if (choice == 'y') {
shop.processSale();
}

cout << "\nDo you want to search for a book by ID? (y/n): ";
cin >> choice;
if (choice == 'y') {
shop.searchBookByID();
}

cout << "\nDo you want to view the book and sales history? (y/n): ";
cin >> choice;
if (choice == 'y') {
shop.displayHistory();
}

cout << "\nDo you want to view full details of all stored books? (y/n): ";
cin >> choice;
if (choice == 'y') {
shop.displayAllBooks();
}

cout << "\nThank you for using the Bookshop Management System!\n";
return 0;
}

You might also like