Practical 11
Practical 11
A book shop maintains the inventory of books that are being sold at the shop. The list
includes details such
as author, title, price, publisher and stock position. Whenever a customer wants a book, the
sales person
inputs the title and author and the system searches the list and displays whether it is
available or not. If it is
not, an appropriate message is displayed. If it is, then the system displays the book details
and requests for
the number of copies required. If the requested copies book details and requests for the
number of copies
required. If the requested copies are available, the total cost of the requested copies is
displayed; otherwise,
the message ―Required copies not in stock‖ is displayed. Design a system using a class
called books with
suitable member functions and Constructors. Use new operator in constructors to allocate
memory space
required. Implement C++ program for the system
Programe:-
#include <iostream>
#include <cstring>
class Book {
private:
char *title;
char *author;
float price;
char *publisher;
int stock;
public:
Book(const char *t, const char *a, float p, const char *pub, int s) {
title = new char[strlen(t) + 1];
strcpy(title, t);
author = new char[strlen(a) + 1];
strcpy(author, a);
price = p;
publisher = new char[strlen(pub) + 1];
strcpy(publisher, pub);
stock = s;
}
~Book() {
delete[] title;
delete[] author;
delete[] publisher;
}
int requiredCopies;
cout << "Enter number of copies required: ";
cin >> requiredCopies;
if (books[i]->purchase(requiredCopies)) {
float totalCost = requiredCopies * books[i]->getPrice();
cout << "Total cost for " << requiredCopies << " copies: $" << totalCost << endl;
} else {
cout << "Required copies not in stock." << endl;
}
break;
}
}
if (!found) {
cout << "Book not available." << endl;
}
}
int main() {
const int numBooks = 3;
Book *books[numBooks];
books[0] = new Book("Book 1", "Author 1", 10.99, "Publisher 1", 5);
books[1] = new Book("Book 2", "Author 2", 15.99, "Publisher 2", 2);
books[2] = new Book("Book 3", "Author 3", 20.99, "Publisher 3", 0);
searchAndPurchaseBook(books, numBooks);
return 0;
}