A book shop maintains the inventory of books at the shop. The list includes details such as author, title, price, publisher and stock position. When a customer wants a book, the person inputs the title and author and the system searches the list and displays weather it available or not. If it is not, an appropriate message is displayed. If it is, the system displays the book details and request for the number of copies required. If the requested copies are available, the total cost of requested copies is displayed; else the message “no stock: is displayed. using a class called books with suitable member functions and constructors. Use new operator in constructors to allocate memory space if required.
1. The price of the books should be updated as and when required. Use a private member function to implement this.
2. The stock value of each book should be updated after transaction .
3. The number of successful and unsuccessful transactions should be recorded for the purpose of statistical analysis.
#include <iostream>
#include <string>
using namespace std;
class Book{
private:
string author;
string title;
double price;
string publisher;
int stock;
public:
/**
* Constructor
*/
Book(){}
/**
* Constructor
*/
Book(string author,string title,double price,string publisher,int stock){
this->author=author;
this->title=title;
this->price=price;
this->publisher=publisher;
this->stock=stock;
}
/**
* @return the author
*/
string getAuthor() {
return author;
}
/**
* @param author the author to set
*/
void setAuthor(string author) {
this->author = author;
}
/**
* @return the title
*/
string getTitle() {
return title;
}
/**
* @param title the title to set
*/
void setTitle(string title) {
this->title = title;
}
/**
* @return the price
*/
float getPrice() {
return price;
}
/**
* @param price the price to set
*/
void setPrice(float price) {
this->price = price;
}
/**
* @return the publisher
*/
string getPublisher() {
return publisher;
}
/**
* @param publisher the publisher to set
*/
void setPublisher(string publisher) {
this->publisher = publisher;
}
/**
* @return the stock
*/
int getStock() {
return stock;
}
/**
* @param stock the stock to set
*/
void setStock(int quantity) {
this->stock = quantity;
}
bool decreaseQuantityStock(int quantity) {
if(this->stock-quantity>=0) {
this->stock-=quantity;
return true;
}else {
return false;
}
}
};
int searchBook(Book books[],int totalNumberBooks,string title,string author);
int main (){
int numberSuccessful=0;
int unsuccessfulTransactions=0;
int totalNumberBooks=0;
Book books[100];
int ch=-1;
string author;
string title;
double price;
string publisher;
int stock;
while(ch!=5){
cout<<"1. Add a new book\n";
cout<<"2. Edit a book price\n";
cout<<"3. Buy a book\n";
cout<<"4. Display statistic\n";
cout<<"5. Exit\n";
cout<<"Your choice: ";
cin>>ch;
cin.ignore();
if(ch==1){
cout<<"Enter author of book: ";
getline(cin,author);
cout<<"Enter title of book: ";
getline(cin,title);
cout<<"Enter a price of book: ";
cin>>price;
cin.ignore();
cout<<"Enter publisher of book: ";
getline(cin,publisher);
cout<<"Enter the number of books: ";
cin>>stock;
Book newBook(author,title,price,publisher,stock);
books[totalNumberBooks]=newBook;
totalNumberBooks++;
cout<<"\nA new book has been added.\n\n";
}else if(ch==2){
if(totalNumberBooks>0){
cout<<"Enter title of book: ";
getline(cin,title);
cout<<"Enter author of book: ";
getline(cin,author);
int selectedBook=searchBook(books,totalNumberBooks,title,author);
if(selectedBook!=-1){
int quantity;
cout<<"Enter a new price of the book: ";
cin>>price;
if(price>0){
books[selectedBook].setPrice(price);
cout<< "\nThe book price has been updated.\n\n";
}
} else {
cout<<"\nWrong the book title or author.\n\n";
}
}else{
cout<<"\nThe book shop is empty.\n\n";
}
}else if(ch==3){
if(totalNumberBooks>0){
cout<<"Enter title of book: ";
getline(cin,title);
cout<<"Enter author of book: ";
getline(cin,author);
int selectedBook=searchBook(books,totalNumberBooks,title,author);
if(selectedBook!=-1){
int quantity;
cout<<"Enter the number of books you ant to buy: ";
cin>>quantity;
if(books[selectedBook].decreaseQuantityStock(quantity)){
float orderPrice=quantity*books[selectedBook].getPrice();
cout<< "\nThe book has been bought.";
cout<< "\nThe order price is: "<<orderPrice<<"\n\n";
numberSuccessful++;
}else{
cout<<"The book is out of stock.\n\n";
unsuccessfulTransactions--;
}
} else {
cout<<"\nWrong the book title or author.\n\n";
}
}else{
cout<<"\nThe book shop is empty.\n\n";
}
}else if(ch==4){
cout<<"The number of successful transactions are: "<<numberSuccessful<<"\n";
cout<<"The number of unsuccessful transactions are: "<<unsuccessfulTransactions<<"\n";
}else if(ch==5){
//exit
}else{
cout<<"\nSelect a correct menu item.\n\n";
}
}
system("pause");
return 0;
}
//the person inputs the title and author and the system searches the list and displays weather it available or not.
int searchBook(Book books[],int totalNumberBooks,string title,string author){
for(int i=0;i<totalNumberBooks;i++){
if(books[i].getTitle().compare(title)==0 && books[i].getAuthor().compare(author)==0){
return i;
}
}
return -1;
}
Comments
Leave a comment