196040

OOP zadaci za vezbanje 2 Igri

Jun 1st, 2020
831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.18 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. class ExistingGame {
  5. private:
  6.     char msg[50];
  7. public:
  8.     ExistingGame(char * msg) {
  9.         strcpy(this->msg, msg);
  10.     }
  11.     void message() {
  12.         cout<<msg;
  13.     }
  14. };
  15. class Game { //Потребно е да се имплементира класа за компјутерска игра (Game),
  16. protected:// што содржи информации за:
  17.     char * ime; //име на играта (низа од макс. 100 знаци)
  18.     double cena; //цена на играта (децимален број)
  19.     bool sale; //дали е играта купена на распродажба (bool променлива).
  20. public:
  21.     Game() {
  22.         this->ime = new char[1];
  23.         strcpy(this->ime, "");
  24.         this->cena = 0.0;
  25.         this->sale = true;
  26.     } // DEFAULTEN
  27.     Game(char * ime, double cena, bool sale) {
  28.         this->ime = new char[strlen(ime)+1];
  29.         strcpy(this->ime, ime);
  30.         this->cena = cena;
  31.         this->sale = sale;
  32.     }//konstruktor so argumenti
  33.     Game(const Game &copy) {
  34.         this->ime = new char[strlen(copy.ime)+1];
  35.         strcpy(this->ime, copy.ime);
  36.         this->cena = copy.cena;
  37.         this->sale = copy.sale;
  38.     }//copy konstruktor
  39.     friend ostream &operator<<(ostream &o, Game &g) { //За класите Game и SubscriptionGame
  40.         o<<"Game: "<<g.ime<<", regular price: $"<<g.cena;
  41.         if(g.getsale() == true)
  42.             o<<", bought on sale";
  43.         return o;
  44.     }//да се преоптоварат операторите за печатење (<< )
  45.     friend istream &operator>>(istream &i, Game &g) { //и читање (>>).
  46.         i.get();
  47.         i.getline(g.ime,100);
  48.         i>>g.cena>>g.sale;
  49.         return i;
  50.     }
  51.     bool operator == (const Game &g) { //Да се дефинира и операторот ==
  52.         if(strcmp(this->ime, g.ime) == 0)
  53.             return true;
  54.         else return false;
  55.     } //кој ќе споредува игри според нивното име.
  56.     double getcena() {
  57.         return cena;
  58.     }
  59.     bool getsale() {
  60.         return sale;
  61.     }
  62.     virtual double calc() {
  63.         double a = 0.0;
  64.         if(this->sale) { //Доколку играта е купена на распродажба, цената на
  65.             a = cena * 0.3; //играта е 30% од стандарната цена.
  66.        return a;
  67.         }
  68.         else return cena;
  69.     }
  70. };
  71. class SubscriptionGame : public Game { //Од класата Game да се изведе класа SubscriptionGame,
  72. private://што дополнително ќе чува:
  73.     double nadomestok; //месечен надоместок за играње (децимален број).
  74.     int mesec; //датум кога играта е купена (месец и
  75.     int godina; //година како позитивни цели броеви)
  76. public:
  77.     SubscriptionGame() {
  78.         this->nadomestok = 0.0;
  79.         this->mesec = 1;
  80.         this->godina = 1;
  81.     } //defaulten
  82.     SubscriptionGame(char * ime, double cena, bool sale, double nadomestok, int mesec, int godina) : Game(ime, cena, sale) {
  83.         this->nadomestok = nadomestok;
  84.         this->mesec = mesec;
  85.         this->godina = godina;
  86.     }//so argumenti
  87.     SubscriptionGame(const SubscriptionGame &copy) : Game(copy) {
  88.         this->nadomestok = copy.nadomestok;
  89.         this->mesec = copy.mesec;
  90.         this->godina = copy.godina;
  91.     }//copy konstruktor
  92.     friend ostream &operator<<(ostream &o,  SubscriptionGame &sg)  {
  93.         o<<"Game: "<<sg.ime<<", regular price: $"<<sg.cena;
  94.         if(sg.getsale() == true)
  95.             o<<", bought on sale";
  96.         o<<", monthly fee: $" <<sg.nadomestok<<", purchased: "<<sg.mesec<<"-"<<sg.godina<<endl;
  97.         return o;
  98.     } //monthlyfee:10, purchased: 1-2017
  99.     friend istream &operator>>(istream &i, SubscriptionGame &sg) {
  100.         i.get();
  101.         i.getline(sg.ime,100);
  102.         i>>sg.cena>>sg.sale>>sg.nadomestok>>sg.mesec>>sg.godina;
  103.         return i;
  104.     }
  105.     int getmesec() {
  106.         return mesec;
  107.     }
  108.     double getnadomestok() {
  109.         return nadomestok;
  110.     }
  111.     double calc() {
  112.         return calcnadomestok();
  113.     }
  114.     int calcnadomestok() { // Доколку играта е од типот SubscriptionGame, потребно е да се вкалкулира
  115.         double tmp =  Game::calc();
  116.                 int m = 0;
  117.                 if(this->godina < 2018)
  118.                     m = (12 - this->mesec) + (2017 - this->godina) * 12 + 5;
  119.                 else m = 5 - this->mesec;
  120.                 tmp = tmp + m * nadomestok;
  121.                 return tmp;
  122.     }
  123.         //и сумата потрошена за месечниот надоместок (број_на_изминати_месеци x цена_на_месечен
  124.         //_надоместок) без да се земе
  125.       //  во предвид моменталниот месец (мај 2018).
  126. };
  127. class User { //Да се дефинира класа за корисник (User)
  128. private://во која се чуваат:
  129.     char username[100];//кориснично име на корисникот (низа од макс. 100 знаци)
  130.     Game ** niza; //колекција од игри кои се купени од корисникот (динамички алоцирана низа).
  131.     int broj;
  132. public:
  133.     User() {
  134.         strcpy(this->username, "");
  135.     }
  136.     User(char * username) {
  137.         strcpy(this->username, username);
  138.         this->niza = new Game * [1];
  139.         this->broj = 0;
  140.     }
  141.     User(const User &copy) {
  142.         strcpy(this->username, copy.username);
  143.         this->niza = new Game * [copy.broj];
  144.         for(int i=0; i<copy.broj; i++)
  145.             this->niza[i] = copy.niza[i];
  146.     }
  147.     User &operator+=(Game &g) { //Да се преоптовари операторот +=
  148.         int flag = 0;
  149.         for(int i=0; i<broj; i++)
  150.             if(*(niza[i]) == g)
  151.                 flag=1;
  152.         if(flag==0) {
  153.             Game ** tmp = new Game * [broj+1];
  154.             for(int i=0; i<broj; i++)
  155.                 tmp[i] = niza[i];
  156.             SubscriptionGame * sg = dynamic_cast<SubscriptionGame *>(&g);
  157.             if(sg)
  158.                 tmp[this->broj++] = new SubscriptionGame(*sg);
  159.             else
  160.                 tmp[this->broj++] = new Game(g);
  161.             //prepisano od nikola poso wtf
  162.             delete [] niza;
  163.             niza = tmp;
  164.         }
  165.         if(flag==1)
  166.             throw ExistingGame("The game is already in the collection\n");
  167.         return *this;
  168.     }//кој ќе овозможи додавање на нова игра во колекцијата на игри.
  169.     double total_spent() { //Да се креира и метода total_spent() во класата User
  170.         double total = 0.0; //која ќе пресметува колку пари корисникот потрошил за својата колекција од игри.
  171.         for(int i=0; i<broj; i++){
  172.             total = total + niza[i]->calc();
  173.         }
  174.         return total;
  175.     }
  176.     // Доколку играта е од типот SubscriptionGame, потребно е да се вкалкулира и
  177.     //    сумата потрошена за месечниот надоместок (број_на_изминати_месеци x
  178.     // цена_на_месечен_надоместок) без да се земе
  179.     //  во предвид моменталниот месец (мај 2018).
  180.     friend ostream &operator<<(ostream &o, User &u) { //Да се преоптовари и оператоторот за печатење
  181.         o<<endl;
  182.         o<<"User: "<<u.username<<endl;//на корисникот, која печати информации во
  183.         for(int i=0; i<u.broj; i++) { //сл. формат
  184.             o<<"- ";
  185.             SubscriptionGame * p = dynamic_cast<SubscriptionGame*>(u.niza[i]);
  186.             if(p)
  187.                 o<<*p<<endl;
  188.             else
  189.                 o<<*u.niza[i]<<endl;
  190.         }
  191.         return o;
  192.     }
  193. };
  194. int main() {
  195.     int test_case_num;
  196.  
  197.     cin>>test_case_num;
  198.  
  199.     // for Game
  200.     char game_name[100];
  201.     float game_price;
  202.     bool game_on_sale;
  203.  
  204.     // for SubscritionGame
  205.     float sub_game_monthly_fee;
  206.     int sub_game_month, sub_game_year;
  207.  
  208.     // for User
  209.     char username[100];
  210.     int num_user_games;
  211.  
  212.     if (test_case_num == 1) {
  213.         cout<<"Testing class Game and operator<< for Game"<<std::endl;
  214.         cin.get();
  215.         cin.getline(game_name,100);
  216.         //cin.get();
  217.         cin>>game_price>>game_on_sale;
  218.  
  219.         Game g(game_name, game_price, game_on_sale);
  220.  
  221.         cout<<g;
  222.     } else if (test_case_num == 2) {
  223.         cout<<"Testing class SubscriptionGame and operator<< for SubscritionGame"<<std::endl;
  224.         cin.get();
  225.         cin.getline(game_name, 100);
  226.  
  227.         cin>>game_price>>game_on_sale;
  228.  
  229.         cin>>sub_game_monthly_fee;
  230.         cin>>sub_game_month>>sub_game_year;
  231.  
  232.         SubscriptionGame sg(game_name, game_price, game_on_sale, sub_game_monthly_fee, sub_game_month, sub_game_year);
  233.         cout<<sg;
  234.     } else if (test_case_num == 3) {
  235.         cout<<"Testing operator>> for Game"<<std::endl;
  236.         Game g;
  237.  
  238.         cin>>g;
  239.  
  240.         cout<<g;
  241.     } else if (test_case_num == 4) {
  242.         cout<<"Testing operator>> for SubscriptionGame"<<std::endl;
  243.         SubscriptionGame sg;
  244.  
  245.         cin>>sg;
  246.  
  247.         cout<<sg;
  248.     } else if (test_case_num == 5) {
  249.         cout<<"Testing class User and operator+= for User"<<std::endl;
  250.         cin.get();
  251.         cin.getline(username,100);
  252.         User u(username);
  253.  
  254.         int num_user_games;
  255.         int game_type;
  256.         cin >>num_user_games;
  257.  
  258.         try {
  259.  
  260.             for (int i=0; i<num_user_games; ++i) {
  261.  
  262.                 cin >> game_type;
  263.  
  264.                 Game *g;
  265.                 // 1 - Game, 2 - SubscriptionGame
  266.                 if (game_type == 1) {
  267.                     cin.get();
  268.                     cin.getline(game_name, 100);
  269.  
  270.                     cin>>game_price>>game_on_sale;
  271.                     g = new Game(game_name, game_price, game_on_sale);
  272.                 } else if (game_type == 2) {
  273.                     cin.get();
  274.                     cin.getline(game_name, 100);
  275.  
  276.                     cin>>game_price>>game_on_sale;
  277.  
  278.                     cin>>sub_game_monthly_fee;
  279.                     cin>>sub_game_month>>sub_game_year;
  280.                     g = new SubscriptionGame(game_name, game_price, game_on_sale, sub_game_monthly_fee, sub_game_month, sub_game_year);
  281.                 }
  282.                 u+=(*g);
  283.             }
  284.         } catch(ExistingGame &ex) {
  285.             ex.message();
  286.         }
  287.         cout<<u;
  288.     } else if (test_case_num == 6) {
  289.         cout<<"Testing exception ExistingGame for User"<<std::endl;
  290.         cin.get();
  291.         cin.getline(username,100);
  292.         User u(username);
  293.  
  294.         int num_user_games;
  295.         int game_type;
  296.         cin >>num_user_games;
  297.  
  298.         for (int i=0; i<num_user_games; ++i) {
  299.  
  300.             cin >> game_type;
  301.  
  302.             Game *g;
  303.             // 1 - Game, 2 - SubscriptionGame
  304.             if (game_type == 1) {
  305.                 cin.get();
  306.                 cin.getline(game_name, 100);
  307.  
  308.                 cin>>game_price>>game_on_sale;
  309.                 g = new Game(game_name, game_price, game_on_sale);
  310.             } else if (game_type == 2) {
  311.                 cin.get();
  312.                 cin.getline(game_name, 100);
  313.  
  314.                 cin>>game_price>>game_on_sale;
  315.  
  316.                 cin>>sub_game_monthly_fee;
  317.                 cin>>sub_game_month>>sub_game_year;
  318.                 g = new SubscriptionGame(game_name, game_price, game_on_sale, sub_game_monthly_fee, sub_game_month, sub_game_year);
  319.             }
  320.  
  321.             //cout<<(*g);
  322.  
  323.             try {
  324.                 u+=(*g);
  325.             } catch(ExistingGame &ex) {
  326.                 ex.message();
  327.             }
  328.         }
  329.  
  330.         cout<<u;
  331.     } else if (test_case_num == 7) {
  332.         cout<<"Testing total_spent method() for User"<<std::endl;
  333.         cin.get();
  334.         cin.getline(username,100);
  335.         User u(username);
  336.  
  337.         int num_user_games;
  338.         int game_type;
  339.         cin >>num_user_games;
  340.  
  341.         for (int i=0; i<num_user_games; ++i) {
  342.  
  343.             cin >> game_type;
  344.  
  345.             Game *g;
  346.             // 1 - Game, 2 - SubscriptionGame
  347.             if (game_type == 1) {
  348.                 cin.get();
  349.                 cin.getline(game_name, 100);
  350.  
  351.                 cin>>game_price>>game_on_sale;
  352.                 g = new Game(game_name, game_price, game_on_sale);
  353.             } else if (game_type == 2) {
  354.                 cin.get();
  355.                 cin.getline(game_name, 100);
  356.  
  357.                 cin>>game_price>>game_on_sale;
  358.  
  359.                 cin>>sub_game_monthly_fee;
  360.                 cin>>sub_game_month>>sub_game_year;
  361.                 g = new SubscriptionGame(game_name, game_price, game_on_sale, sub_game_monthly_fee, sub_game_month, sub_game_year);
  362.             }
  363.  
  364.             //cout<<(*g);
  365.  
  366.  
  367.             u+=(*g);
  368.         }
  369.  
  370.         cout<<u;
  371.  
  372.         cout<<"Total money spent: $"<<u.total_spent()<<endl;
  373.     }
  374. }
Advertisement
Add Comment
Please, Sign In to add comment