February 09, 2014

OOMP Assignment#2

/*
Aim: 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.
*/

#include <iostream>
#include <string.h>
#define MAX_BOOKS 50
using namespace std;

class Book{
 friend class BookShop;
   string author;
   string title;
   string publisher;
   int stockPosition;
   float price;
 public:
   Book(){
     author = "";
     title = "";
     publisher = "";
     stockPosition = 0;
     price = 0;
   }
   float getPrice(int numOrder){
     return price * numOrder;
   }
   bool isAvailable(int numOrder){
     return numOrder <= stockPosition;
   }
   void printDetails(){
     cout << endl << "--- Book details ---";
     cout << endl << "Title: " << title;
     cout << endl << "Author: " << author;
     cout << endl << "Publisher: " << publisher;
     cout << endl << "StockPosition: " << stockPosition;
     cout << endl << "Price: Rs." << price;
   }
   void get(){
     cout << endl << "Enter new book details: ";
     cout << endl << "Title: ";
     cin >> title;
     cout << endl << "Author: ";
     cin >> author;
     cout << endl << "Publisher: ";
     cin >> publisher;
     cout << endl << "Price: ";
     cin >> price;
     cout << endl << "Stock Position: ";
     cin >> stockPosition;
   }
   void placeOrder(){
     int numOrder;
     cout << endl << "Enter number of copies: ";
     cin >> numOrder;
     if(isAvailable(numOrder)){
       cout << endl << "Required copies are available in stock. Total price: Rs." << getPrice(numOrder);
     }else{
       cout << endl << "Required copies not in stock.";
     }
   }
};

class BookShop{
   Book * books[MAX_BOOKS];
   int index;
 public:
   BookShop(){
     index = 0;
   }
   void add(){
     books[index] = new Book();
     books[index]->get();
     index++;
   }
   void request(){
     int choice = 0, search = -1;
     cout << endl << "Search for book by: ";
     cout << endl << "1) Title";
     cout << endl << "2) Author";
     cout << endl << "Enter choice: ";
     cin >> choice;
     switch(choice){
       case 1 : search = searchTitle(); break;
       case 2 : search = searchAuthor(); break;
       default: cout << endl << "Invalid choice!"; break;
     }
     if(search == -1){
       cout << endl << "No results found!";
     }else{
       cout << endl << "Book found: ";
       books[search]->printDetails();
       books[search]->placeOrder();
     }
   }
   int searchTitle(){
     string title;
     cout << endl << "Enter book title to search: ";
     cin >> title;
     for(int i = 0; i < index; i++){
       if(title.compare(books[i]->title) == 0){
         return i;
       }
     }
     return -1;
   }
   int searchAuthor(){
     string author;
     cout << endl << "Enter author name to search: ";
     cin >> author;
     for(int i = 0; i < index; i++){
       if(author.compare(books[i]->author) == 0){
         return i;
       }
     }
     return -1;
   }
};


int main(){
 int choice;
 BookShop * bs = new BookShop();
 do{
   cout << endl << "-- Book Shop --";
   cout << endl << "1. Add book";
   cout << endl << "2. Request book";
   cout << endl << "3. Exit";
   cout << endl << "Your choice: ";
   cin >> choice;
   switch(choice){
     case 1 : bs->add(); break;
     case 2 : bs->request(); break;
   }
 }while(choice != 3);
 delete bs;
 return 0;
}

/*
---------------------------------------
             OUTPUT
---------------------------------------

-- Book Shop --
1. Add book
2. Request book
3. Exit
Your choice: 1

Enter new book details:
Title: Gravity

Author: Rohit

Publisher: GHRCEM

Price: 400

Stock Position: 10

-- Book Shop --
1. Add book
2. Request book
3. Exit
Your choice: 1

Enter new book details:
Title: Inferno

Author: Tauseef

Publisher: GHRCEM

Price: 350

Stock Position: 5

-- Book Shop --
1. Add book
2. Request book
3. Exit
Your choice: 2

Search for book by:
1) Title
2) Author
Enter choice: 1

Enter book title to search: Gravity

Book found:
--- Book details ---
Title: Gravity
Author: Rohit
Publisher: GHRCEM
StockPosition: 10
Price: Rs.400
Enter number of copies: 15

Required copies not in stock.
-- Book Shop --
1. Add book
2. Request book
3. Exit
Your choice: 2

Search for book by:
1) Title
2) Author
Enter choice: 2

Enter author name to search: Tauseef

Book found:
--- Book details ---
Title: Inferno
Author: Tauseef
Publisher: GHRCEM
StockPosition: 5
Price: Rs.350
Enter number of copies: 4

Required copies are available in stock. Total price: Rs.1400
-- Book Shop --
1. Add book
2. Request book
3. Exit
Your choice: 3
*/

No comments:

Post a Comment