Advertisement
IlijaTrnkovski

Untitled

Jun 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. class ExistingGame{
  6. private:
  7. char *message;
  8. public:
  9. ExistingGame(){
  10. message = new char[0];
  11. }
  12. ExistingGame(char *message){
  13. this->message = new char[strlen(message)+1];
  14. strcpy(this->message,message);
  15.  
  16. }
  17. void msg(){
  18. cout<<"File with name"<<message<< " already exists in this folder"<<endl;
  19. }
  20.  
  21. };
  22. class Game{
  23. protected:
  24. char ime[100];
  25. double cena;
  26. bool r;
  27. public:
  28. Game (){
  29. }
  30. Game( char *ime, double cena, bool r){
  31. strcpy(this->ime, ime);
  32. this->cena=cena;
  33. this->r=r;
  34. }
  35. Game( const Game &g){
  36. strcpy(this->ime, g.ime);
  37. this->cena=g.cena;
  38. this->r=g.r;
  39. }
  40. friend ostream& operator<<(ostream &out, Game &g){
  41. out<<"Name:"<<" "<<g.ime<<"Cena:"<<" "<<g.cena<<endl;
  42. return out;
  43. }
  44. friend istream& operator>>(istream &in,Game &g){
  45. in>>g.ime>>g.cena>>g.r;
  46. return in;
  47. }
  48. bool operator==(Game &g)
  49. {
  50. return strcmp(this->ime,g.ime)==0;
  51. }
  52.  
  53. ~Game(){
  54. }
  55. };
  56. class SubscriptionGame : public Game{
  57. protected:
  58. double nadomest;
  59. int datum;
  60. int godina;
  61. public:
  62. SubscriptionGame(){
  63. }
  64. SubscriptionGame( char *ime, double cena, bool r, double nadomest, int datum, int godina): Game(ime,cena,r)
  65. {
  66. this->nadomest=nadomest;
  67. this->datum=datum;
  68. this->godina=godina;
  69.  
  70. }
  71. SubscriptionGame(const SubscriptionGame &s )
  72. {
  73. this->nadomest=s.nadomest;
  74. this->datum=s.datum;
  75. this->godina=s.godina;
  76.  
  77. }
  78. ~SubscriptionGame(){
  79. }
  80. };
  81. class User{
  82. protected:
  83. char kime[100];
  84. char *kolekcija;
  85. public:
  86. User(){
  87. kolekcija = new char[0];
  88. }
  89. User(char *kime, char kolekcija){
  90. strcpy(this->kime,kime);
  91. this->kolekcija= new char[strlen(kolekcija)+1];
  92. strcpy(this->kolekcija, kolekcija);
  93.  
  94. }
  95. User(const User &u){
  96. strcpy(this->kime,u.kime);
  97. this->kolekcija= new char[strlen(u.kolekcija)+1];
  98. strcpy(this->kolekcija, u.kolekcija);
  99.  
  100. }
  101. ~User(){
  102. delete []kolekcija;
  103. }
  104. User& operator+=(char nova)
  105. {
  106. char *temp = new char[kolekcija+1];
  107. for(int i=0; i<kolekcija; i++)
  108. {
  109. temp[i] = kolekcija[i];
  110. }
  111. temp[kolekcija] = nova;
  112.  
  113. delete []kolekcija;
  114. kolekcija = temp;
  115. kolekcija++;
  116. return *this;
  117. }
  118. total_spent(){
  119.  
  120. }
  121.  
  122. int getGame(){
  123. return ime;
  124. }
  125. int totalSpent(){
  126. return kolekcija * cena;
  127. }
  128. char *getName(){
  129. return kime;
  130. }
  131. char getKolekcija(){
  132. return kolekcija;
  133. }
  134.  
  135. };
  136.  
  137. int main() {
  138. int test_case_num;
  139.  
  140. cin>>test_case_num;
  141.  
  142. // for Game
  143. char game_name[100];
  144. float game_price;
  145. bool game_on_sale;
  146.  
  147. // for SubscritionGame
  148. float sub_game_monthly_fee;
  149. int sub_game_month, sub_game_year;
  150.  
  151. // for User
  152. char username[100];
  153. int num_user_games;
  154.  
  155. if (test_case_num == 1){
  156. cout<<"Testing class Game and operator<< for Game"<<std::endl;
  157. cin.get();
  158. cin.getline(game_name,100);
  159. cin>>game_price>>game_on_sale;
  160.  
  161. Game g(game_name, game_price, game_on_sale);
  162.  
  163. cout<<g;
  164. }
  165. else if (test_case_num == 2){
  166. cout<<"Testing class SubscriptionGame and operator<< for SubscritionGame"<<std::endl;
  167. cin.get();
  168. cin.getline(game_name, 100);
  169.  
  170. cin>>game_price>>game_on_sale;
  171.  
  172. cin>>sub_game_monthly_fee;
  173. cin>>sub_game_month>>sub_game_year;
  174.  
  175. SubscriptionGame sg(game_name, game_price, game_on_sale, sub_game_monthly_fee, sub_game_month, sub_game_year);
  176. cout<<sg;
  177. }
  178. else if (test_case_num == 3){
  179. cout<<"Testing operator>> for Game"<<std::endl;
  180. Game g;
  181.  
  182. cin>>g;
  183.  
  184. cout<<g;
  185. }
  186. else if (test_case_num == 4){
  187. cout<<"Testing operator>> for SubscriptionGame"<<std::endl;
  188. SubscriptionGame sg;
  189.  
  190. cin>>sg;
  191.  
  192. cout<<sg;
  193. }
  194. else if (test_case_num == 5){
  195. cout<<"Testing class User and operator+= for User"<<std::endl;
  196. cin.get();
  197. cin.getline(username,100);
  198. User u(username);
  199.  
  200. int num_user_games;
  201. int game_type;
  202. cin >>num_user_games;
  203.  
  204. try {
  205.  
  206. for (int i=0; i<num_user_games; ++i){
  207.  
  208. cin >> game_type;
  209.  
  210. Game *g;
  211. // 1 - Game, 2 - SubscriptionGame
  212. if (game_type == 1){
  213. cin.get();
  214. cin.getline(game_name, 100);
  215.  
  216. cin>>game_price>>game_on_sale;
  217. g = new Game(game_name, game_price, game_on_sale);
  218. }
  219. else if (game_type == 2){
  220. cin.get();
  221. cin.getline(game_name, 100);
  222.  
  223. cin>>game_price>>game_on_sale;
  224.  
  225. cin>>sub_game_monthly_fee;
  226. cin>>sub_game_month>>sub_game_year;
  227. g = new SubscriptionGame(game_name, game_price, game_on_sale, sub_game_monthly_fee, sub_game_month, sub_game_year);
  228. }
  229.  
  230. u+=(*g);
  231. }
  232. }catch(ExistingGame &ex){
  233. ex.message();
  234. }
  235.  
  236. cout<<u;
  237.  
  238.  
  239. }
  240. else if (test_case_num == 6){
  241. cout<<"Testing exception ExistingGame for User"<<std::endl;
  242. cin.get();
  243. cin.getline(username,100);
  244. User u(username);
  245.  
  246. int num_user_games;
  247. int game_type;
  248. cin >>num_user_games;
  249.  
  250. for (int i=0; i<num_user_games; ++i){
  251.  
  252. cin >> game_type;
  253.  
  254. Game *g;
  255. // 1 - Game, 2 - SubscriptionGame
  256. if (game_type == 1){
  257. cin.get();
  258. cin.getline(game_name, 100);
  259.  
  260. cin>>game_price>>game_on_sale;
  261. g = new Game(game_name, game_price, game_on_sale);
  262. }
  263. else if (game_type == 2){
  264. cin.get();
  265. cin.getline(game_name, 100);
  266.  
  267. cin>>game_price>>game_on_sale;
  268.  
  269. cin>>sub_game_monthly_fee;
  270. cin>>sub_game_month>>sub_game_year;
  271. g = new SubscriptionGame(game_name, game_price, game_on_sale, sub_game_monthly_fee, sub_game_month, sub_game_year);
  272. }
  273.  
  274. //cout<<(*g);
  275.  
  276. try {
  277. u+=(*g);
  278. }
  279. catch(ExistingGame &ex){
  280. ex.message();
  281. }
  282. }
  283.  
  284. cout<<u;
  285.  
  286. }
  287. else if (test_case_num == 7){
  288. cout<<"Testing total_spent method() for User"<<std::endl;
  289. cin.get();
  290. cin.getline(username,100);
  291. User u(username);
  292.  
  293. int num_user_games;
  294. int game_type;
  295. cin >>num_user_games;
  296.  
  297. for (int i=0; i<num_user_games; ++i){
  298.  
  299. cin >> game_type;
  300.  
  301. Game *g;
  302. // 1 - Game, 2 - SubscriptionGame
  303. if (game_type == 1){
  304. cin.get();
  305. cin.getline(game_name, 100);
  306.  
  307. cin>>game_price>>game_on_sale;
  308. g = new Game(game_name, game_price, game_on_sale);
  309. }
  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.  
  324. u+=(*g);
  325. }
  326.  
  327. cout<<u;
  328.  
  329. cout<<"Total money spent: $"<<u.total_spent()<<endl;
  330. }
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement