Advertisement
LegoDrifter

OOP Izdavacka Kuka

Jun 26th, 2020
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. class Book{
  7. protected:
  8.     char isbn_number[20];
  9.     char title[50];
  10.     char author[30];
  11.     double price;
  12. public:
  13.     Book()
  14.     {
  15.         strcpy(this->isbn_number,"");
  16.         strcpy(this->title,"");
  17.         strcpy(this->author,"");
  18.         this->price=0.0;
  19.     }
  20.     Book(char *isbn_number,char *title,char *author,double price)
  21.     {
  22.         strcpy(this->isbn_number,isbn_number);
  23.         strcpy(this->title,title);
  24.         strcpy(this->author,author);
  25.         this->price=price;
  26.     }
  27.     Book(const Book &b)
  28.     {
  29.         strcpy(this->isbn_number,b.isbn_number);
  30.         strcpy(this->title,b.title);
  31.         strcpy(this->author,b.author);
  32.         this->price=b.price;
  33.     }
  34.     virtual double bookPrice()=0;
  35.     virtual int getType()=0;
  36.     char *getISBN()
  37.     {
  38.         return isbn_number;
  39.     }
  40.     char *getTitle()
  41.     {
  42.         return title;
  43.     }
  44.     char *getAuthor()
  45.     {
  46.         return author;
  47.     }
  48.     const void setISBN(char *carce)
  49.     {
  50.        strcpy(this->isbn_number,carce);
  51.     }
  52.     virtual ~Book(){}
  53. };
  54. class OnlineBook:public Book{
  55. private:
  56.     char *url;
  57.     int sizeMB;
  58. public:
  59.     OnlineBook()
  60.     {
  61.         this->url = new char[0];
  62.         strcpy(url,"");
  63.         this->sizeMB=0;
  64.     }
  65.     OnlineBook(char *isbn_number,char *title,char *author,double price
  66.                ,char *url,int sizeMB):Book(isbn_number,title,author,price)
  67.                {
  68.                    this->url = new char[strlen(url)+1];
  69.                    strcpy(this->url,url);
  70.                    this->sizeMB=sizeMB;
  71.                }
  72.     double bookPrice()
  73.     {
  74.         if(sizeMB>20)
  75.         {
  76.             return this->price*1.2;
  77.         }
  78.         else return this->price;
  79.     }
  80.     int getType()
  81.     {
  82.         return 1;
  83.     }
  84.  
  85. };
  86. class PrintBook:public Book{
  87. private:
  88.     double weight;
  89.     int stock;
  90. public:
  91.     PrintBook()
  92.     {
  93.         this->weight=0.0;
  94.         this->stock=0;
  95.     }
  96.     PrintBook(char*isbn_number,char *title,char *author,double price
  97.               ,double weight,int stock):Book(isbn_number,title,author,price)
  98.               {
  99.                   this->weight=weight;
  100.                   this->stock=stock;
  101.               }
  102.      double bookPrice()
  103.      {
  104.          if(weight>0.7)
  105.          {
  106.              return this->price*1.15;
  107.          }
  108.          else return this->price;
  109.      }
  110.      int getType()
  111.     {
  112.         return 2;
  113.     }
  114.  
  115.  
  116. };
  117. bool operator > (Book &b,Book &b1)
  118. {
  119.     if(b.bookPrice()>b1.bookPrice())
  120.        {
  121.            return true;
  122.        }
  123.     else return false;
  124. }
  125.   ostream &operator <<(ostream &out, Book &b)
  126.     {
  127.         return out<<b.getISBN()<<": "<<b.getTitle()<<", "<<b.getAuthor()<<" "<<b.bookPrice()<<endl;
  128.     }
  129. void mostExpensiveBook(Book **books,int n)
  130. {
  131.     double max=0.0;
  132.     int iMax=0;
  133.     int cBooks=0,eBooks=0;
  134.     for(int i=0;i<n;i++)
  135.     {
  136.         if(books[i]->bookPrice()>max)
  137.         {
  138.             max=books[i]->bookPrice();
  139.             iMax=i;
  140.         }
  141.         if(books[i]->getType()==1)
  142.         {
  143.             eBooks++;
  144.         }
  145.         if(books[i]->getType()==2)
  146.         {
  147.             cBooks++;
  148.         }
  149.     }
  150.     cout<<"FINKI-Education"<<endl;
  151.     cout<<"Total number of online books: "<<eBooks<<endl;
  152.     cout<<"Total number of print books: "<<cBooks<<endl;
  153.     cout<<"The most expensive book is: "<<endl;
  154.     cout<<*books[iMax];
  155.  
  156. }
  157. int main(){
  158.  
  159.     char isbn[20], title[50], author[30], url[100];
  160.     int size, tip;
  161.     float price, weight;
  162.     bool inStock;
  163.     Book  **books;
  164.     int n;
  165.  
  166.     int testCase;
  167.     cin >> testCase;
  168.  
  169.     if (testCase == 1){
  170.         cout << "====== Testing OnlineBook class ======" << endl;
  171.         cin >> n;
  172.         books = new Book *[n];
  173.  
  174.         for (int i = 0; i < n; i++){
  175.             cin >> isbn;
  176.             cin.get();
  177.             cin.getline(title, 50);
  178.             cin.getline(author, 30);
  179.             cin >> price;
  180.             cin >> url;
  181.             cin >> size;
  182.             cout << "CONSTRUCTOR" << endl;
  183.             books[i] = new OnlineBook(isbn, title, author, price, url, size);
  184.             cout << "OPERATOR <<" << endl;
  185.             cout << *books[i];
  186.         }
  187.         cout << "OPERATOR >" << endl;
  188.         cout << "Rezultat od sporedbata e: " << endl;
  189.         if (*books[0] > *books[1])
  190.             cout << *books[0];
  191.         else
  192.             cout << *books[1];
  193.     }
  194.     if (testCase == 2){
  195.         cout << "====== Testing OnlineBook CONSTRUCTORS ======" << endl;
  196.         cin >> isbn;
  197.         cin.get();
  198.         cin.getline(title, 50);
  199.         cin.getline(author, 30);
  200.         cin >> price;
  201.         cin >> url;
  202.         cin >> size;
  203.         cout << "CONSTRUCTOR" << endl;
  204.         OnlineBook ob1(isbn, title, author, price, url, size);
  205.         cout << ob1 << endl;
  206.         cout << "COPY CONSTRUCTOR" << endl;
  207.         OnlineBook ob2(ob1);
  208.         cin >> isbn;
  209.         ob2.setISBN(isbn);
  210.         cout << ob1 << endl;
  211.         cout << ob2 << endl;
  212.         cout << "OPERATOR =" << endl;
  213.         ob1 = ob2;
  214.         cin >> isbn;
  215.         ob2.setISBN(isbn);
  216.         cout << ob1 << endl;
  217.         cout << ob2 << endl;
  218.     }
  219.     if (testCase == 3){
  220.         cout << "====== Testing PrintBook class ======" << endl;
  221.         cin >> n;
  222.         books = new Book *[n];
  223.  
  224.         for (int i = 0; i < n; i++){
  225.             cin >> isbn;
  226.             cin.get();
  227.             cin.getline(title, 50);
  228.             cin.getline(author, 30);
  229.             cin >> price;
  230.             cin >> weight;
  231.             cin >> inStock;
  232.             cout << "CONSTRUCTOR" << endl;
  233.             books[i] = new PrintBook(isbn, title, author, price, weight, inStock);
  234.             cout << "OPERATOR <<" << endl;
  235.             cout << *books[i];
  236.         }
  237.         cout << "OPERATOR >" << endl;
  238.         cout << "Rezultat od sporedbata e: " << endl;
  239.         if (*books[0] > *books[1])
  240.             cout << *books[0];
  241.         else
  242.             cout << *books[1];
  243.     }
  244.     if (testCase == 4){
  245.         cout << "====== Testing method mostExpensiveBook() ======" << endl;
  246.         cin >> n;
  247.         books = new Book *[n];
  248.  
  249.         for (int i = 0; i<n; i++){
  250.  
  251.             cin >> tip >> isbn;
  252.             cin.get();
  253.             cin.getline(title, 50);
  254.             cin.getline(author, 30);
  255.             cin >> price;
  256.             if (tip == 1) {
  257.  
  258.                 cin >> url;
  259.                 cin >> size;
  260.  
  261.                 books[i] = new OnlineBook(isbn, title, author, price, url, size);
  262.  
  263.             }
  264.             else {
  265.                 cin >> weight;
  266.                 cin >> inStock;
  267.  
  268.                 books[i] = new PrintBook(isbn, title, author, price, weight, inStock);
  269.             }
  270.         }
  271.  
  272.         mostExpensiveBook(books, n);
  273.     }
  274.  
  275.     for (int i = 0; i<n; i++) delete books[i];
  276.         delete[] books;
  277.     return 0;
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement