metalni

OOP Zadaci za vezbanje 2 FINKI-издавачка куќа

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