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[20];
- char naslov[50];
- char avtor[30];
- int cena;
- public:
- Book (char * i, char * n, char * a, int c) {
- strcpy(ISBN,i); strcpy(naslov,n); strcpy(avtor,a); cena=c;
- }
- virtual double bookPrice () = 0;
- virtual ~Book () {}
- friend ostream &operator << (ostream &out, Book &b) {
- out<<b.ISBN<<": "<<b.naslov<<", "<<b.avtor<<" "<<b.bookPrice()<<endl;
- return out;
- }
- bool operator > (const Book &b){
- return cena > b.cena;
- }
- void setISBN (const char * i) {strcpy(ISBN,i); }
- };
- class OnlineBook : public Book {
- private:
- char * url;
- int golemina;
- public:
- OnlineBook (char * i, char * n, char * a, int c, char *u, int g) : Book(i,n,a,c) {
- url = new char [strlen(u)+1];
- strcpy(url,u); golemina=g;
- }
- // ~OnlineBook () {delete [] url; }
- double bookPrice () {
- if (golemina>20)
- return 1.2*cena;
- else return cena;
- }
- };
- class PrintBook : public Book {
- private:
- double masa;
- bool zaliha;
- public:
- PrintBook (char * i, char * n, char * a, int c, double m, bool z) : Book (i,n,a,c) {
- masa=m; zaliha=z;
- }
- double bookPrice () {
- if (masa>0.7)
- return 1.15*cena;
- else
- return cena;
- }
- };
- void mostExpensiveBook (Book ** books, int n) { //broj na online, broj na pecateni, najskapa kniga
- int online=0, pecateni=0;
- int maxcena=0,maxi;
- for (int i=0;i<n;i++) {
- Book * tmp = dynamic_cast < OnlineBook *>(books[i]);
- if (tmp)
- ++online;
- else ++pecateni;
- if (books[i]->bookPrice() > maxcena){
- maxi=i;
- maxcena=books[i]->bookPrice();
- }
- }
- cout<<"FINKI-Education"<<endl;
- cout<<"Total number of online books: "<<online<<endl;
- cout<<"Total number of print books: "<<pecateni<<endl;
- cout<<"The most expensive book is: "<<endl;
- cout<<*books[maxi];
- }
- 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