Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- class Book{
- protected:
- char isbn_number[20];
- char title[50];
- char author[30];
- double price;
- public:
- Book()
- {
- strcpy(this->isbn_number,"");
- strcpy(this->title,"");
- strcpy(this->author,"");
- this->price=0.0;
- }
- Book(char *isbn_number,char *title,char *author,double price)
- {
- strcpy(this->isbn_number,isbn_number);
- strcpy(this->title,title);
- strcpy(this->author,author);
- this->price=price;
- }
- Book(const Book &b)
- {
- strcpy(this->isbn_number,b.isbn_number);
- strcpy(this->title,b.title);
- strcpy(this->author,b.author);
- this->price=b.price;
- }
- virtual double bookPrice()=0;
- virtual int getType()=0;
- char *getISBN()
- {
- return isbn_number;
- }
- char *getTitle()
- {
- return title;
- }
- char *getAuthor()
- {
- return author;
- }
- const void setISBN(char *carce)
- {
- strcpy(this->isbn_number,carce);
- }
- virtual ~Book(){}
- };
- class OnlineBook:public Book{
- private:
- char *url;
- int sizeMB;
- public:
- OnlineBook()
- {
- this->url = new char[0];
- strcpy(url,"");
- this->sizeMB=0;
- }
- OnlineBook(char *isbn_number,char *title,char *author,double price
- ,char *url,int sizeMB):Book(isbn_number,title,author,price)
- {
- this->url = new char[strlen(url)+1];
- strcpy(this->url,url);
- this->sizeMB=sizeMB;
- }
- double bookPrice()
- {
- if(sizeMB>20)
- {
- return this->price*1.2;
- }
- else return this->price;
- }
- int getType()
- {
- return 1;
- }
- };
- class PrintBook:public Book{
- private:
- double weight;
- int stock;
- public:
- PrintBook()
- {
- this->weight=0.0;
- this->stock=0;
- }
- PrintBook(char*isbn_number,char *title,char *author,double price
- ,double weight,int stock):Book(isbn_number,title,author,price)
- {
- this->weight=weight;
- this->stock=stock;
- }
- double bookPrice()
- {
- if(weight>0.7)
- {
- return this->price*1.15;
- }
- else return this->price;
- }
- int getType()
- {
- return 2;
- }
- };
- bool operator > (Book &b,Book &b1)
- {
- if(b.bookPrice()>b1.bookPrice())
- {
- return true;
- }
- else return false;
- }
- ostream &operator <<(ostream &out, Book &b)
- {
- return out<<b.getISBN()<<": "<<b.getTitle()<<", "<<b.getAuthor()<<" "<<b.bookPrice()<<endl;
- }
- void mostExpensiveBook(Book **books,int n)
- {
- double max=0.0;
- int iMax=0;
- int cBooks=0,eBooks=0;
- for(int i=0;i<n;i++)
- {
- if(books[i]->bookPrice()>max)
- {
- max=books[i]->bookPrice();
- iMax=i;
- }
- if(books[i]->getType()==1)
- {
- eBooks++;
- }
- if(books[i]->getType()==2)
- {
- cBooks++;
- }
- }
- cout<<"FINKI-Education"<<endl;
- cout<<"Total number of online books: "<<eBooks<<endl;
- cout<<"Total number of print books: "<<cBooks<<endl;
- cout<<"The most expensive book is: "<<endl;
- cout<<*books[iMax];
- }
- int main(){
- char isbn[20], title[50], author[30], url[100];
- int size, tip;
- float price, weight;
- bool inStock;
- Book **books;
- int n;
- int testCase;
- cin >> testCase;
- if (testCase == 1){
- cout << "====== Testing OnlineBook class ======" << endl;
- cin >> n;
- books = new Book *[n];
- for (int i = 0; i < n; i++){
- cin >> isbn;
- cin.get();
- cin.getline(title, 50);
- cin.getline(author, 30);
- cin >> price;
- cin >> url;
- cin >> size;
- cout << "CONSTRUCTOR" << endl;
- books[i] = new OnlineBook(isbn, title, author, price, url, size);
- cout << "OPERATOR <<" << endl;
- cout << *books[i];
- }
- cout << "OPERATOR >" << endl;
- cout << "Rezultat od sporedbata e: " << endl;
- if (*books[0] > *books[1])
- cout << *books[0];
- else
- cout << *books[1];
- }
- if (testCase == 2){
- cout << "====== Testing OnlineBook CONSTRUCTORS ======" << endl;
- cin >> isbn;
- cin.get();
- cin.getline(title, 50);
- cin.getline(author, 30);
- cin >> price;
- cin >> url;
- cin >> size;
- cout << "CONSTRUCTOR" << endl;
- OnlineBook ob1(isbn, title, author, price, url, size);
- cout << ob1 << endl;
- cout << "COPY CONSTRUCTOR" << endl;
- OnlineBook ob2(ob1);
- cin >> isbn;
- ob2.setISBN(isbn);
- cout << ob1 << endl;
- cout << ob2 << endl;
- cout << "OPERATOR =" << endl;
- ob1 = ob2;
- cin >> isbn;
- ob2.setISBN(isbn);
- cout << ob1 << endl;
- cout << ob2 << endl;
- }
- if (testCase == 3){
- cout << "====== Testing PrintBook class ======" << endl;
- cin >> n;
- books = new Book *[n];
- for (int i = 0; i < n; i++){
- cin >> isbn;
- cin.get();
- cin.getline(title, 50);
- cin.getline(author, 30);
- cin >> price;
- cin >> weight;
- cin >> inStock;
- cout << "CONSTRUCTOR" << endl;
- books[i] = new PrintBook(isbn, title, author, price, weight, inStock);
- cout << "OPERATOR <<" << endl;
- cout << *books[i];
- }
- cout << "OPERATOR >" << endl;
- cout << "Rezultat od sporedbata e: " << endl;
- if (*books[0] > *books[1])
- cout << *books[0];
- else
- cout << *books[1];
- }
- if (testCase == 4){
- cout << "====== Testing method mostExpensiveBook() ======" << endl;
- cin >> n;
- books = new Book *[n];
- for (int i = 0; i<n; i++){
- cin >> tip >> isbn;
- cin.get();
- cin.getline(title, 50);
- cin.getline(author, 30);
- cin >> price;
- if (tip == 1) {
- cin >> url;
- cin >> size;
- books[i] = new OnlineBook(isbn, title, author, price, url, size);
- }
- else {
- cin >> weight;
- cin >> inStock;
- books[i] = new PrintBook(isbn, title, author, price, weight, inStock);
- }
- }
- mostExpensiveBook(books, n);
- }
- for (int i = 0; i<n; i++) delete books[i];
- delete[] books;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement