Advertisement
JStefan

Vezbi__Zadaca1

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