metalni

OOP Zadaci za vezbanje 2 Igri

Jun 1st, 2020
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.03 KB | None | 0 0
  1. /*
  2. NOTES FOR OTHER PROGRAMMERS THAT READ THIS:
  3. IN ISTREAM OPERATOR >> OVERLOADING IT IS USED GETLINE BECAUSE IN THE INPUT WE EXPECT A STRING THAT HAS ONE OR MULTIPLE BLANK SPACES OR MULTIPLE STRINGS TO BE INPUTED. THE NORMAL CIN FUNCTION DOESN'T LET US DO THAT, SO WE HAVE TO USE GETLINE.
  4.  
  5. QUOTE = "The getline() command reads the space character of the code you input by naming the variable and the size of the variable in the command. Use it when you intend to take input strings with spaces between them or process multiple strings at once." Source: https://www.bitdegree.org/learn/getline-c-plus-plus
  6. */
  7.  
  8.  
  9.  
  10. #include <iostream>
  11. #include <cstring>
  12.  
  13. using namespace std;
  14. class ExistingGame{
  15.     private:
  16.         char msg[100];
  17.     public:
  18.         ExistingGame(const char * msg){
  19.             strcpy(this->msg, msg);
  20.         }
  21.         const void message(){
  22.             cout << this->msg << "\n";
  23.         }
  24. };
  25.  
  26. class Game{
  27.     protected:
  28.         char name[100];
  29.         double price;
  30.         bool onSale;
  31.     public:
  32.             Game(){
  33.                 strcpy(this->name, "None");
  34.                 this->price = 0.0;
  35.                 this->onSale = false;
  36.             }
  37.             Game(const char * name, const double price, const bool onSale){
  38.                 strcpy(this->name, name);
  39.                 this->price = price;
  40.                 this->onSale = onSale;
  41.             }
  42.             ~Game(){}
  43.             friend istream &operator >>(istream &is, Game &orig){
  44.                 is.get();
  45.                 is.getline(orig.name,100);
  46.                 is >> orig.price;
  47.                 is >> orig.onSale;
  48.  
  49.                 return is;
  50.             }
  51.             friend ostream &operator <<(ostream &os, Game &orig){
  52.                 os << "Game: " << orig.name << ", regular price: " << "$" << orig.price;
  53.                 if(orig.onSale)
  54.                 os << ", bought on sale";
  55.  
  56.                 return os;
  57.             }
  58.             bool operator==(const Game &orig){
  59.                 if(!(strcmp(this->name, orig.name)))
  60.                     return true;
  61.                 else
  62.                     return false;
  63.             }
  64.             virtual const double getPrice(){
  65.                 if(this->onSale)
  66.                     return this->price * 0.3;
  67.  
  68.                 return this->price;
  69.             }
  70.             virtual const bool getSale(){
  71.                 return this->onSale;
  72.             }
  73. };
  74.  
  75. class SubscriptionGame : public Game {
  76.     private:
  77.         double monthlyFee;
  78.         int month;
  79.         int year;
  80.     public:
  81.         SubscriptionGame(){
  82.             this->monthlyFee = 0.0;
  83.             this->month = 0;
  84.             this->year = 0;
  85.         }
  86.         SubscriptionGame(const char * name, const double price, const bool onSale, const double monthlyFee, const int month, const int year) : Game(name, price, onSale){
  87.             this->monthlyFee = monthlyFee;
  88.             this->month = month;
  89.             this->year = year;
  90.         }
  91.         ~SubscriptionGame(){}
  92.         friend istream &operator >>(istream &is, SubscriptionGame &orig){
  93.                 is.get();
  94.                 is.getline(orig.name,100);
  95.                
  96.                 is >> orig.price;
  97.                 is >> orig.onSale;
  98.                 is >> orig.monthlyFee;
  99.                 is >> orig.month;
  100.                 is >> orig.year;
  101.  
  102.                 return is;
  103.             }
  104.             friend ostream &operator <<(ostream &os, SubscriptionGame &orig){
  105.                 os << "Game: " << orig.name << ", regular price: " << "$" << orig.price;
  106.                 if(orig.onSale)
  107.                     os << ", bought on sale";
  108.                 os << ", monthly fee: " << "$" << orig.monthlyFee << ", purchased: " << orig.month << "-" << orig.year << "\n";
  109.                
  110.  
  111.                 return os;
  112.             }
  113.             const double getMonthlyFee(){
  114.                 return this->monthlyFee;
  115.             }
  116.             const int getMonth(){
  117.                 return this->month;
  118.             }
  119.             const int getYear(){
  120.                 return this->year;
  121.             }
  122.             const double getPrice(){
  123.                 float price = Game::getPrice();
  124.  
  125.                 int months = 0;
  126.                 if(year < 2018)
  127.                     months = (12 - this->month) + (2017 - year) * 12 + 5;
  128.                 else
  129.                     months = 5 - this->month;
  130.  
  131.                 price += months * this->monthlyFee;
  132.                
  133.                 return price;
  134.             }
  135. };
  136.  
  137. class User{
  138.     private:
  139.         char userName[100];
  140.         Game ** games;
  141.         int noGames;
  142.         const void copy_obj(const User &orig){
  143.             strcpy(this->userName, orig.userName);
  144.             this->games = new Game *[orig.noGames + 1];
  145.             for(int i=0; i<orig.noGames; i++)
  146.                 this->games[i] = orig.games[i];
  147.             this->noGames = orig.noGames;
  148.         }
  149.     public:
  150.         User(){
  151.             strcpy(this->userName, "None");
  152.             this->games = new Game*[0];
  153.             this->noGames = 0;
  154.         }
  155.         User(const char * userName){
  156.             strcpy(this->userName, userName);
  157.             this->games = new Game*[0];
  158.             this->noGames = 0;
  159.         }
  160.         User(const User &orig){
  161.             this->copy_obj(orig);
  162.         }
  163.  
  164.         User &operator=(const User &orig){
  165.             if(this != &orig){
  166.                 delete [] this->games;
  167.                 this->copy_obj(orig);
  168.             }
  169.             return *this;
  170.         }
  171.         ~User(){
  172.             delete [] this->games;
  173.         }
  174.         User &operator +=(Game &orig){
  175.             for(int i=0; i<this->noGames; i++)
  176.                 if(*(this->games[i]) == orig)
  177.                     throw ExistingGame("The game is already in the collection");
  178.            
  179.             Game ** tmp = new Game*[this->noGames + 1];
  180.             for(int i=0; i<this->noGames; i++)
  181.                 tmp[i] = this->games[i];
  182.            
  183.             SubscriptionGame * sg = dynamic_cast<SubscriptionGame *>(&orig);
  184.             if(sg)
  185.                 tmp[this->noGames++] = new SubscriptionGame(*sg);
  186.             else
  187.                 tmp[this->noGames++] = new Game(orig);
  188.  
  189.             delete [] this->games;
  190.             this->games = tmp;
  191.  
  192.             return *this;
  193.         }
  194.  
  195.         const double total_spent(){
  196.             double total = 0.0;
  197.             for(int i=0; i<this->noGames; i++)
  198.                 total += this->games[i]->getPrice();
  199.            
  200.             return total;
  201.         }
  202.         friend ostream & operator<<(ostream &o, const User &u){
  203.             o<<"\nUser: "<<u.userName<<"\n";
  204.  
  205.             for (int i=0; i < u.noGames; ++i){
  206.                 Game * g;
  207.                 SubscriptionGame * sg;
  208.                 g = &(*u.games[i]);
  209.                
  210.                 sg = dynamic_cast<SubscriptionGame *> (g);
  211.                
  212.                 if (sg){
  213.                     cout<<"- "<<(*sg);
  214.                 }
  215.                 else {
  216.                     cout<<"- "<<(*g);
  217.                 }
  218.                 cout<<"\n";
  219.             }
  220.             return o;
  221.         }  
  222. };
  223.  
  224.  
  225.  
  226. int main() {
  227.   int test_case_num;
  228.  
  229.   cin>>test_case_num;
  230.  
  231.   // for Game
  232.   char game_name[100];
  233.   float game_price;
  234.   bool game_on_sale;
  235.  
  236.   // for SubscritionGame
  237.   float sub_game_monthly_fee;
  238.   int sub_game_month, sub_game_year;
  239.  
  240.   // for User
  241.   char username[100];
  242.   int num_user_games;
  243.  
  244.   if (test_case_num == 1){
  245.     cout<<"Testing class Game and operator<< for Game"<<std::endl;
  246.     cin.get();
  247.     cin.getline(game_name,100);
  248.     //cin.get();
  249.     cin>>game_price>>game_on_sale;
  250.  
  251.     Game g(game_name, game_price, game_on_sale);
  252.  
  253.     cout<<g;
  254.   }
  255.   else if (test_case_num == 2){
  256.     cout<<"Testing class SubscriptionGame and operator<< for SubscritionGame"<<std::endl;
  257.     cin.get();
  258.     cin.getline(game_name, 100);
  259.  
  260.     cin>>game_price>>game_on_sale;
  261.  
  262.     cin>>sub_game_monthly_fee;
  263.     cin>>sub_game_month>>sub_game_year;
  264.  
  265.     SubscriptionGame sg(game_name, game_price, game_on_sale, sub_game_monthly_fee, sub_game_month, sub_game_year);
  266.     cout<<sg;
  267.   }
  268.   else if (test_case_num == 3){
  269.     cout<<"Testing operator>> for Game"<<std::endl;
  270.     Game g;
  271.  
  272.     cin>>g;
  273.  
  274.     cout<<g;
  275.   }
  276.   else if (test_case_num == 4){
  277.     cout<<"Testing operator>> for SubscriptionGame"<<std::endl;
  278.     SubscriptionGame sg;
  279.  
  280.     cin>>sg;
  281.  
  282.     cout<<sg;
  283.   }
  284.   else if (test_case_num == 5){
  285.     cout<<"Testing class User and operator+= for User"<<std::endl;
  286.     cin.get();
  287.     cin.getline(username,100);
  288.     User u(username);
  289.  
  290.     int num_user_games;
  291.     int game_type;
  292.     cin >>num_user_games;
  293.  
  294.     try {
  295.  
  296.       for (int i=0; i<num_user_games; ++i){
  297.  
  298.         cin >> game_type;
  299.  
  300.         Game *g;
  301.         // 1 - Game, 2 - SubscriptionGame
  302.         if (game_type == 1){
  303.           cin.get();
  304.           cin.getline(game_name, 100);
  305.  
  306.           cin>>game_price>>game_on_sale;
  307.           g = new Game(game_name, game_price, game_on_sale);
  308.         }
  309.         else if (game_type == 2){
  310.           cin.get();
  311.           cin.getline(game_name, 100);
  312.  
  313.           cin>>game_price>>game_on_sale;
  314.  
  315.           cin>>sub_game_monthly_fee;
  316.           cin>>sub_game_month>>sub_game_year;
  317.           g = new SubscriptionGame(game_name, game_price, game_on_sale, sub_game_monthly_fee, sub_game_month, sub_game_year);
  318.         }
  319.  
  320.         //cout<<(*g);
  321.  
  322.  
  323.         u+=(*g);
  324.       }
  325.     }catch(ExistingGame &ex){
  326.       ex.message();
  327.     }
  328.  
  329.     cout<<u;
  330.  
  331.   }
  332.   else if (test_case_num == 6){
  333.       cout<<"Testing exception ExistingGame for User"<<std::endl;
  334.       cin.get();
  335.       cin.getline(username,100);
  336.       User u(username);
  337.  
  338.       int num_user_games;
  339.       int game_type;
  340.       cin >>num_user_games;
  341.  
  342.       for (int i=0; i<num_user_games; ++i){
  343.  
  344.           cin >> game_type;
  345.  
  346.           Game *g;
  347.           // 1 - Game, 2 - SubscriptionGame
  348.           if (game_type == 1){
  349.             cin.get();
  350.             cin.getline(game_name, 100);
  351.  
  352.             cin>>game_price>>game_on_sale;
  353.             g = new Game(game_name, game_price, game_on_sale);
  354.           }
  355.           else if (game_type == 2){
  356.             cin.get();
  357.             cin.getline(game_name, 100);
  358.  
  359.             cin>>game_price>>game_on_sale;
  360.  
  361.             cin>>sub_game_monthly_fee;
  362.             cin>>sub_game_month>>sub_game_year;
  363.             g = new SubscriptionGame(game_name, game_price, game_on_sale, sub_game_monthly_fee, sub_game_month, sub_game_year);
  364.           }
  365.  
  366.           //cout<<(*g);
  367.  
  368.           try {
  369.             u+=(*g);
  370.           }
  371.           catch(ExistingGame &ex){
  372.             ex.message();
  373.           }
  374.         }
  375.  
  376.       cout<<u;
  377.  
  378.   }
  379.   else if (test_case_num == 7){
  380.       cout<<"Testing total_spent method() for User"<<std::endl;
  381.       cin.get();
  382.       cin.getline(username,100);
  383.       User u(username);
  384.  
  385.       int num_user_games;
  386.       int game_type;
  387.       cin >>num_user_games;
  388.  
  389.       for (int i=0; i<num_user_games; ++i){
  390.  
  391.           cin >> game_type;
  392.  
  393.           Game *g;
  394.           // 1 - Game, 2 - SubscriptionGame
  395.           if (game_type == 1){
  396.             cin.get();
  397.             cin.getline(game_name, 100);
  398.  
  399.             cin>>game_price>>game_on_sale;
  400.             g = new Game(game_name, game_price, game_on_sale);
  401.           }
  402.           else if (game_type == 2){
  403.             cin.get();
  404.             cin.getline(game_name, 100);
  405.  
  406.             cin>>game_price>>game_on_sale;
  407.  
  408.             cin>>sub_game_monthly_fee;
  409.             cin>>sub_game_month>>sub_game_year;
  410.             g = new SubscriptionGame(game_name, game_price, game_on_sale, sub_game_monthly_fee, sub_game_month, sub_game_year);
  411.           }
  412.  
  413.           //cout<<(*g);
  414.  
  415.  
  416.           u+=(*g);
  417.         }
  418.  
  419.       cout<<u;
  420.  
  421.       cout<<"Total money spent: $"<<u.total_spent()<<endl;
  422.   }
  423. }
Add Comment
Please, Sign In to add comment