ivana_andreevska

Zadaca 6

Jun 19th, 2021 (edited)
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <math.h>
  6. using namespace std;
  7.  
  8. class Exception{
  9. public:
  10. void message()
  11. {
  12.     cout<<"The game is already in the collection"<<endl;
  13. }
  14. };
  15.  
  16.  
  17. class Game
  18. {
  19. protected:
  20. char ime[101];
  21. double cena;
  22. bool rasprodazba;
  23. public:
  24.     Game(char *ime="" , double cena=0 , bool rasprodazba=false)
  25.     {
  26.         strcpy(this->ime,ime);
  27.         this->cena=cena;
  28.         this->rasprodazba=rasprodazba;
  29.     }
  30.  
  31.     friend ostream &operator<<(ostream &out , Game &g)
  32.     {
  33.         out<<"Game: "<<g.ime<<", regular price: $"<<g.cena;
  34.         if(g.rasprodazba)
  35.         {
  36.             out<<",bought on sale"<<endl;
  37.         }
  38.         return out;
  39.     }
  40.  
  41.     char *getIme()
  42.     {
  43.         return ime;
  44.     }
  45.     double getCena()
  46.     {
  47.         return cena;
  48.     }
  49.     bool getRasprodazba()
  50.     {
  51.         return rasprodazba;
  52.     }
  53.  
  54.     friend istream &operator>>(istream &in , Game &g)
  55.     {
  56.         in.get();
  57.         in.getline(g.ime,101);
  58.         in>>g.cena;
  59.         in>>g.rasprodazba;
  60.         return in;
  61.     }
  62.  
  63.     bool operator==(Game &g)
  64.     {
  65.         return strcmp(ime,g.ime);
  66.     }
  67.  
  68.     virtual double getPrice()
  69.     {
  70.         if(rasprodazba==true)
  71.         {
  72.             return cena*0.3;
  73.         }
  74.         return cena*1.0;
  75.     }
  76. };
  77.  
  78.  
  79. class SubscriptionGame:public Game
  80. {
  81. private:
  82.     double nadomestok;
  83.     int mesec;
  84.     int godina;
  85. public:
  86.     SubscriptionGame(char *ime="" , double cena=0 , bool rasprodazba=false, double nadomestok=0 , int mesec=0 , int godina=0):Game(ime,cena,rasprodazba)
  87.     {
  88.         this->nadomestok=nadomestok;
  89.         this->mesec=mesec;
  90.         this->godina=godina;
  91.     }
  92.     friend ostream &operator<<(ostream &out , SubscriptionGame &sg)
  93.     {
  94.         out<<"Game: "<<sg.ime<<", regular price: $"<<sg.cena;
  95.         if(sg.rasprodazba==true)
  96.         {
  97.             out<<", bought on sale";
  98.         }
  99.         out<<", monthly fee: $"<<sg.nadomestok<<", purchased: "<<sg.mesec<<"-"<<sg.godina<<endl;
  100.         return out;
  101.     }
  102.  
  103.     double getNadomestok()
  104.     {
  105.         return nadomestok;
  106.     }
  107.     int getMesec()
  108.     {
  109.         return mesec;
  110.     }
  111.     int getGodina()
  112.     {
  113.         return godina;
  114.     }
  115.  
  116.     friend istream &operator>>(istream &in , SubscriptionGame &sg)
  117.     {
  118.         in.get();
  119.         in.getline(sg.ime,101);
  120.         in>>sg.cena>>sg.rasprodazba>>sg.nadomestok>>sg.mesec>>sg.godina;
  121.         return in;
  122.     }
  123.  
  124.     double getPrice()
  125.     {
  126.         if(2018==godina)
  127.         {
  128.             int tempMesec=mesec;
  129.             int brojac=0;
  130.             while(tempMesec<5)
  131.             {
  132.                 brojac++;
  133.                 tempMesec++;
  134.             }
  135.           return (brojac*nadomestok)+cena;
  136.         }
  137.  
  138.         else
  139.         {
  140.             int brojac=1;
  141.             int tempMesec=mesec;
  142.             int tempGodina=godina;
  143.  
  144.             while(tempMesec<=12)
  145.             {
  146.                 if(tempMesec==12)
  147.                 {
  148.                     tempGodina+=1;
  149.                     if(tempGodina!=2018)
  150.                     {
  151.                         tempMesec=1;
  152.                     }
  153.                     else
  154.                     {
  155.                         tempMesec=1;
  156.                         while(tempMesec<5)
  157.                         {
  158.                             brojac++;
  159.                             tempMesec++;
  160.                         }
  161.                         return(brojac*nadomestok)+cena;
  162.                     }
  163.                 }
  164.                 brojac++;
  165.                 tempMesec++;
  166.             }
  167.         }
  168.  
  169.     }
  170.  
  171. };
  172.  
  173. class User
  174. {
  175. private:
  176.     char ime[101];
  177.     Game **igri;
  178.     int n;
  179. public:
  180.     User(const char *ime="")
  181.     {
  182.         strcpy(this->ime,ime);
  183.         igri=new Game*[0];
  184.         n=0;
  185.     }
  186.     User(const User &u)
  187.     {
  188.         strcpy(ime,u.ime);
  189.         n=u.n;
  190.         for(int i=0;i<n;i++)
  191.         {
  192.             igri[i]=u.igri[i];
  193.         }
  194.     }
  195.     User &operator=(const User &u)
  196.     {
  197.         if(this!=&u)
  198.         {
  199.         strcpy(ime,u.ime);
  200.         n=u.n;
  201.         delete[]igri;
  202.         for(int i=0;i<n;i++)
  203.         {
  204.             igri[i]=u.igri[i];
  205.         }
  206.         }
  207.         return *this;
  208.     }
  209.     ~User()
  210.     {
  211.         delete[]igri;
  212.     }
  213.  
  214.     User &operator+=(Game &g)
  215.     {
  216.         for(int i=0;i<n;i++)
  217.         {
  218.             if(((*igri[i])==g)==0)
  219.                 throw Exception();
  220.         }
  221.  
  222.         Game **temp=new Game*[n+1];
  223.         for(int i=0;i<n;i++)
  224.         {
  225.             temp[i]=igri[i];
  226.         }
  227.         temp[n++]=&g;
  228.         delete[]igri;
  229.         igri=temp;
  230.         return *this;
  231.     }
  232.  
  233.     double total_spent()
  234.     {
  235.         double total=0;
  236.         for(int i=0;i<n;i++)
  237.         {
  238.             total+=igri[i]->getPrice();
  239.         }
  240.         return total;
  241.     }
  242.  
  243.     friend ostream &operator<<(ostream &out , User &u)
  244.     {
  245.         out<<endl;
  246.         out<<"User: "<<u.ime<<endl;
  247.  
  248.         for(int i=0;i<u.n;i++)
  249.         {
  250.             out<<"- Game: "<<u.igri[i]->getIme()<<", regular price: $"<<u.igri[i]->getCena();
  251.  
  252.             if(u.igri[i]->getRasprodazba()==true)
  253.                 out<<", bought on sale";
  254.         }
  255.         int i;
  256.             SubscriptionGame *temp=dynamic_cast<SubscriptionGame*>(u.igri[i]);
  257.             {
  258.                 if(temp!=0)
  259.                 {
  260.                     out<<", monthly fee: $"<<temp->getNadomestok()<<", purchased: "<<temp->getMesec()<<"-"<<temp->getGodina()<<endl;
  261.                 }
  262.                 out<<endl;
  263.             }
  264.             return out;
  265.     }
  266.  
  267. };
Add Comment
Please, Sign In to add comment