0% found this document useful (0 votes)
15 views3 pages

Practical 11

Uploaded by

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

Practical 11

Uploaded by

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

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>

using namespace std;

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;
}

void display() const {


cout << "Title: " << title << endl;
cout << "Author: " << author << endl;
cout << "Price: $" << price << endl;
cout << "Publisher: " << publisher << endl;
cout << "Stock: " << stock << " copies available" << endl;
}

bool matches(const char *t, const char *a) const {


return (strcmp(title, t) == 0 && strcmp(author, a) == 0);
}

bool purchase(int quantity) {


if (quantity <= stock) {
stock -= quantity;
return true;
}
return false;
}

float getPrice() const {


return price;
}
};

void searchAndPurchaseBook(Book **books, int numBooks) {


char searchTitle[100];
char searchAuthor[100];

cout << "Enter the book title: ";


cin.getline(searchTitle, 100);

cout << "Enter the author's name: ";


cin.getline(searchAuthor, 100);

bool found = false;

for (int i = 0; i < numBooks; ++i) {


if (books[i]->matches(searchTitle, searchAuthor)) {
found = true;
books[i]->display();

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);

for (int i = 0; i < numBooks; ++i) {


delete books[i];
}

return 0;
}

You might also like