Untitled
unknown
plain_text
a year ago
3.2 kB
2
Indexable
Never
#include <iostream> #include <string> #include <vector> class Book { private: std::string title; std::string author; double price; std::string publisher; int stock_position; public: Book(std::string t, std::string a, double p, std::string pub, int stock) { title = t; author = a; price = p; publisher = pub; stock_position = stock; } void displayDetails() { std::cout << "Title: " << title << std::endl; std::cout << "Author: " << author << std::endl; std::cout << "Price: " << price << std::endl; std::cout << "Publisher: " << publisher << std::endl; std::cout << "Stock Position: " << stock_position << std::endl; } bool checkAvailability(int requiredCopies) { return stock_position >= requiredCopies; } double calculateCost(int requiredCopies) { if (checkAvailability(requiredCopies)) { return price * requiredCopies; } else { return -1; } } std::string getTitle() { return title; } std::string getAuthor() { return author; } }; int main() { std::vector<Book> bookList; // Input book details from the user std::string title, author, publisher; double price; int stock; std::cout << "Enter the number of books to add: "; int numBooks; std::cin >> numBooks; std::cin.ignore(); // Clear newline from previous input for (int i = 0; i < numBooks; ++i) { std::cout << "Enter details for Book " << i + 1 << std::endl; std::cout << "Title: "; std::getline(std::cin, title); std::cout << "Author: "; std::getline(std::cin, author); std::cout << "Price: "; std::cin >> price; std::cin.ignore(); // Clear newline from previous input std::cout << "Publisher: "; std::getline(std::cin, publisher); std::cout << "Stock Position: "; std::cin >> stock; std::cin.ignore(); // Clear newline from previous input bookList.emplace_back(title, author, price, publisher, stock); std::cout << "Book added to inventory." << std::endl; } std::cout << "Enter the book title: "; std::getline(std::cin, title); std::cout << "Enter the author: "; std::getline(std::cin, author); int requiredCopies; std::cout << "Enter the number of copies required: "; std::cin >> requiredCopies; std::cin.ignore(); // Clear newline from previous input Book* requestedBook = nullptr; for (Book& book : bookList) { if (book.getTitle() == title && book.getAuthor() == author) { requestedBook = &book; break; } } if (requestedBook != nullptr) { requestedBook->displayDetails(); if (requestedBook->checkAvailability(requiredCopies)) { double totalCost = requestedBook->calculateCost(requiredCopies); std::cout << "Total Cost: " << totalCost << std::endl; } else { std::cout << "Required copies not in stock." << std::endl; } } else { std::cout << "Book not found in the inventory." << std::endl; } return 0; }