Advertisement
Guest User

Игри

a guest
Jun 20th, 2018
1,306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.47 KB | None | 0 0
  1. Opened: 14:38:35
  2. Игри Problem 7 (0 / 0)
  3. Потребно е да се имплементира класа за компјутерска игра (Game), што содржи информации за:
  4.  
  5. име на играта (низа од макс. 100 знаци)
  6. цена на играта (децимален број)
  7. дали е играта купена на распродажба (bool променлива).
  8. Од класата Game да се изведе класа SubscriptionGame, што дополнително ќе чува:
  9.  
  10. месечен надоместок за играње (децимален број).
  11. датум кога играта е купена (месец и година како позитивни цели броеви)
  12. За класите Game и SubscriptionGame да се преоптоварат операторите за печатење (<< ) и читање (>>). Да се дефинира и операторот == кој ќе споредува игри според нивното име.
  13.  
  14. Да се дефинира класа за корисник (User) во која се чуваат:
  15.  
  16. кориснично име на корисникот (низа од макс. 100 знаци)
  17. колекција од игри кои се купени од корисникот (динамички алоцирана низа).
  18. Да се преоптовари операторот += кој ќе овозможи додавање на нова игра во колекцијата на игри. Притоа ако корисникот ја има веќе купено играта, потребно е да се креира исклучок од типот ExistingGame. Класата за имплементација на исклучоци потребно е има соодветен конструктор и метода message за печатење на порака на екран.
  19.  
  20. Да се креира и метода total_spent() во класата User која ќе пресметува колку пари корисникот потрошил за својата колекција од игри. Доколку играта е купена на распродажба, цената на играта е 30% од стандарната цена. Доколку играта е од типот SubscriptionGame, потребно е да се вкалкулира и сумата потрошена за месечниот надоместок (број_на_изминати_месеци x цена_на_месечен_надоместок) без да се земе во предвид моменталниот месец (мај 2018).
  21.  
  22. Да се преоптовари и оператоторот за печатење на корисникот, која печати информации во сл. формат (види тест примери 7 до 12):
  23.  
  24. User: username
  25. - Game: PUBG, regular price: $70, bought on sale
  26. Game: Half Life 2, regular price: $70
  27. Game: Warcraft 4, regular price: 40,monthlyfee:10, purchased: 1-2017
  28. Листа на дел од методите со нивни прототипови кои се користат во main:
  29.  
  30. ЕxistingGame::message()
  31. Game::operator==(Game&)
  32. User::operator+=(Game&)
  33. User::get_game(int)
  34. User::total_spent(int)
  35. User::get_name()
  36. User::get_games_number()
  37. ---------------------------------------------------------------------------------------------------------------------------------------
  38. #include <iostream>
  39. #include <cstring>
  40.  
  41. using namespace std;
  42.  
  43. class ExistingGame{
  44. private:
  45. char msg[256];
  46.  
  47. public:
  48. ExistingGame(char msg_txt[]){
  49. strncpy(this->msg, msg_txt, 255);
  50. this->msg[255] = '\0';
  51. }
  52.  
  53. void message(){
  54. std::cout<<this->msg<<std::endl;
  55. }
  56. };
  57.  
  58. class Game {
  59.  
  60. protected:
  61. char name[100];
  62. float price;
  63. bool on_sale;
  64. public:
  65. Game(){
  66. name[0] = '\0';
  67. }
  68. Game(char *n, float p, bool s=false){
  69. strncpy(name, n, 99);
  70. this->name[99] = '\0';
  71.  
  72. price = p;
  73.  
  74. on_sale = s;
  75. }
  76.  
  77. virtual float get_price(){
  78. if (on_sale){
  79. return price * 0.3F;
  80. }
  81.  
  82. return price;
  83. }
  84.  
  85. bool operator==(Game& g){
  86. return !strcmp(this->name, g.name);
  87. }
  88.  
  89. friend ostream & operator<<(ostream & o, const Game& g);
  90. friend istream & operator>>(istream & i, Game& g);
  91. };
  92.  
  93. class SubscriptionGame : public Game {
  94. protected:
  95. float monthly_fee;
  96. int month, year;
  97.  
  98. public:
  99. SubscriptionGame(){
  100. name[0] = '\0';
  101. }
  102.  
  103. SubscriptionGame(char *n, float p, bool s, float mf, int m, int y):
  104. Game(n, p, s),
  105. monthly_fee(mf),
  106. month(m),
  107. year(y) { }
  108.  
  109. float get_price(){
  110. float price = Game::get_price();
  111.  
  112. int months=0;
  113. if (year<2018){
  114. months = (12 - this->month) + (2017 - year)*12 + 5;
  115. }
  116. else {
  117. months = 5 - this->month;
  118. }
  119.  
  120. price += months * monthly_fee;
  121.  
  122. return price;
  123. }
  124.  
  125. friend ostream & operator<<(ostream & o, SubscriptionGame& sg);
  126. friend istream & operator>>(istream & i, SubscriptionGame& g);
  127.  
  128. };
  129.  
  130. ostream & operator<<(ostream & o, const Game& g) {
  131. o<<"Game: "<< g.name <<", regular price: $" << g.price;
  132.  
  133. if (g.on_sale){
  134. o<<", bought on sale";
  135. }
  136. return o;
  137. }
  138.  
  139. ostream & operator<<(ostream & o, SubscriptionGame& sg) {
  140.  
  141. Game * tmp = dynamic_cast<Game*>(&sg);
  142.  
  143. o << *tmp;
  144.  
  145. o<<", monthly fee: $"<< sg.monthly_fee
  146. <<", purchased: "<< sg.month << "-" << sg.year<<std::endl;
  147.  
  148. return o;
  149. }
  150.  
  151. istream & operator>>(istream & is, Game &g){
  152.  
  153. is.get();
  154. is.getline(g.name,100);
  155. is>>g.price>>g.on_sale;
  156.  
  157. return is;
  158. }
  159.  
  160. istream & operator>>(istream & is, SubscriptionGame &g){
  161. is.get();
  162. is.getline(g.name,100);
  163. is>>g.price>>g.on_sale;
  164. is>>g.monthly_fee >> g.month >> g.year;
  165. return is;
  166. }
  167.  
  168. class User {
  169. private:
  170. void obj_copy(const User * orig, User * cpy){
  171. strcpy(cpy->username, orig->username);
  172. cpy->num_games = orig->num_games;
  173.  
  174. cpy->games = new Game*[cpy->num_games];
  175.  
  176. for (int i=0; i< cpy->num_games; ++i){
  177. cpy->games[i] = new Game(*(orig->games[i]));
  178. }
  179. }
  180.  
  181. protected:
  182. char username[100];
  183. Game ** games;
  184. int num_games;
  185. public:
  186. User (char const * const un="") {
  187. strncpy(this->username, un, 99);
  188. this->username[99] = '\0';
  189. games = 0;
  190. num_games = 0;
  191. }
  192.  
  193. User (const User& orig){
  194. obj_copy(&orig,this);
  195. }
  196.  
  197. ~User(){
  198. for (int i=0; i < this->num_games; ++i){
  199. delete this->games[i];
  200. }
  201. delete [] games;
  202. }
  203.  
  204. User& operator=(User & orig){
  205. if (&orig != this){
  206.  
  207. for (int i=0; i < this->num_games; ++i){
  208. delete orig.games[i];
  209. }
  210.  
  211. delete [] orig.games;
  212.  
  213. obj_copy(&orig, this);
  214. }
  215. return *this;
  216. }
  217.  
  218. User& operator+=(Game&g){
  219.  
  220. Game ** new_games = new Game*[this->num_games+1];
  221.  
  222. for (int i=0; i < (this->num_games); ++i) {
  223. if ( (*(this->games[i])) == g){
  224. throw ExistingGame("The game is already in the collection");
  225. }
  226.  
  227. new_games[i] = games[i];
  228. }
  229.  
  230. for (int i=0; i < (this->num_games); ++i) {
  231. new_games[i] = games[i];
  232. }
  233.  
  234. SubscriptionGame * sg = dynamic_cast< SubscriptionGame* >(&g);
  235.  
  236. if(sg){
  237.  
  238. new_games[num_games] = new SubscriptionGame(*sg);
  239. }
  240. else {
  241. //cout<<"Game"<<endl;
  242. new_games[num_games] = new Game(g);
  243. }
  244.  
  245. delete [] this->games;
  246. this->games = new_games;
  247. this->num_games++;
  248.  
  249. //cout<<"User: "<< this->username<<endl;
  250.  
  251. // for (int i=0; i<this->num_games;++i){
  252. // cout<< *(this->games[i]);
  253. // }
  254.  
  255. return *this;
  256. }
  257.  
  258. Game& get_game(int i){
  259. return (*(this->games[i]));
  260. }
  261.  
  262. float total_spent(){
  263. float sum = 0.0f;
  264. for (int i=0; i<this->num_games; ++i){
  265. sum += games[i]->get_price();
  266. }
  267. return sum;
  268. }
  269.  
  270. char const * const get_username(){
  271. return this->username;
  272. }
  273.  
  274. int get_games_number(){
  275. return this->num_games;
  276. }
  277.  
  278. };
  279.  
  280. ostream & operator<<(ostream & o, User& u) {
  281.  
  282. o<<"\nUser: "<<u.get_username()<<"\n";
  283.  
  284. for (int i=0; i < u.get_games_number(); ++i){
  285. Game * g;
  286. SubscriptionGame * sg;
  287. g = &(u.get_game(i));
  288.  
  289. sg = dynamic_cast<SubscriptionGame *> (g);
  290.  
  291. if (sg){
  292. cout<<"- "<<(*sg);
  293. }
  294. else {
  295. cout<<"- "<<(*g);
  296. }
  297. cout<<"\n";
  298. }
  299. return o;
  300. }
  301.  
  302. int main() {
  303. int test_case_num;
  304.  
  305. cin>>test_case_num;
  306.  
  307. // for Game
  308. char game_name[100];
  309. float game_price;
  310. bool game_on_sale;
  311.  
  312. // for SubscritionGame
  313. float sub_game_monthly_fee;
  314. int sub_game_month, sub_game_year;
  315.  
  316. // for User
  317. char username[100];
  318. int num_user_games;
  319.  
  320. if (test_case_num == 1){
  321. cout<<"Testing class Game and operator<< for Game"<<std::endl;
  322. cin.get();
  323. cin.getline(game_name,100);
  324. //cin.get();
  325. cin>>game_price>>game_on_sale;
  326.  
  327. Game g(game_name, game_price, game_on_sale);
  328.  
  329. cout<<g;
  330. }
  331. else if (test_case_num == 2){
  332. cout<<"Testing class SubscriptionGame and operator"<<forSubscritionGame"<<std::endl;
  333. cin.get();
  334. cin.getline(game_name, 100);
  335.  
  336. cin>>game_price>>game_on_sale;
  337.  
  338. cin>>sub_game_monthly_fee;
  339. cin>>sub_game_month>>sub_game_year;
  340.  
  341. SubscriptionGame sg(game_name, game_price, game_on_sale,
  342. sub_game_monthly_fee, sub_game_month, sub_game_year);
  343. cout<<sg;
  344. }
  345. else if (test_case_num == 3){
  346. cout<<"Testing operator>> for Game"<<std::endl;
  347. Game g;
  348.  
  349. cin>>g;
  350.  
  351. cout<<g;
  352. }
  353. else if (test_case_num == 4){
  354. cout<<"Testing operator>> for SubscriptionGame"<<std::endl;
  355. SubscriptionGame sg;
  356.  
  357. cin>>sg;
  358.  
  359. cout<<sg;
  360. }
  361. else if (test_case_num == 5){
  362. cout<<"Testing class User and operator+= for User"<<std::endl;
  363. cin.get();
  364. cin.getline(username,100);
  365. User u(username);
  366.  
  367. int num_user_games;
  368. int game_type;
  369. cin >>num_user_games;
  370.  
  371. try {
  372.  
  373. for (int i=0; i<num_user_games; ++i){
  374.  
  375. cin >> game_type;
  376.  
  377. Game *g;
  378. // 1 - Game, 2 - SubscriptionGame
  379. if (game_type == 1){
  380. cin.get();
  381. cin.getline(game_name, 100);
  382.  
  383. cin>>game_price>>game_on_sale;
  384. g = new Game(game_name, game_price, game_on_sale);
  385. }
  386. else if (game_type == 2){
  387. cin.get();
  388. cin.getline(game_name, 100);
  389.  
  390. cin>>game_price>>game_on_sale;
  391.  
  392. cin>>sub_game_monthly_fee;
  393. cin>>sub_game_month>>sub_game_year;
  394. g = new SubscriptionGame(game_name, game_price, game_on_sale,
  395. sub_game_monthly_fee, sub_game_month, sub_game_year);
  396. }
  397.  
  398. //cout<<(*g);
  399.  
  400. u+=(*g);
  401. }
  402. }catch(ExistingGame &ex){
  403. ex.message();
  404. }
  405.  
  406. cout<<u;
  407.  
  408. }
  409. else if (test_case_num == 6){
  410. cout<<"Testing exception ExistingGame for User"<<std::endl;
  411. cin.get();
  412. cin.getline(username,100);
  413. User u(username);
  414.  
  415. int num_user_games;
  416. int game_type;
  417. cin >>num_user_games;
  418.  
  419. for (int i=0; i<num_user_games; ++i){
  420.  
  421. cin >> game_type;
  422.  
  423. Game *g;
  424. // 1 - Game, 2 - SubscriptionGame
  425. if (game_type == 1){
  426. cin.get();
  427. cin.getline(game_name, 100);
  428.  
  429. cin>>game_price>>game_on_sale;
  430. g = new Game(game_name, game_price, game_on_sale);
  431. }
  432. else if (game_type == 2){
  433. cin.get();
  434. cin.getline(game_name, 100);
  435.  
  436. cin>>game_price>>game_on_sale;
  437.  
  438. cin>>sub_game_monthly_fee;
  439. cin>>sub_game_month>>sub_game_year;
  440. g = new SubscriptionGame(game_name, game_price,
  441. game_on_sale, sub_game_monthly_fee, sub_game_month, sub_game_year);
  442. }
  443.  
  444. //cout<<(*g);
  445.  
  446. try {
  447. u+=(*g);
  448. }
  449. catch(ExistingGame &ex){
  450. ex.message();
  451. }
  452. }
  453.  
  454. cout<<u;
  455.  
  456. }
  457. else if (test_case_num == 7){
  458. cout<<"Testing total_spent method() for User"<<std::endl;
  459. cin.get();
  460. cin.getline(username,100);
  461. User u(username);
  462.  
  463. int num_user_games;
  464. int game_type;
  465. cin >>num_user_games;
  466.  
  467. for (int i=0; i<num_user_games; ++i){
  468.  
  469. cin >> game_type;
  470.  
  471. Game *g;
  472. // 1 - Game, 2 - SubscriptionGame
  473. if (game_type == 1){
  474. cin.get();
  475. cin.getline(game_name, 100);
  476.  
  477. cin>>game_price>>game_on_sale;
  478. g = new Game(game_name, game_price, game_on_sale);
  479. }
  480. else if (game_type == 2){
  481. cin.get();
  482. cin.getline(game_name, 100);
  483.  
  484. cin>>game_price>>game_on_sale;
  485.  
  486. cin>>sub_game_monthly_fee;
  487. cin>>sub_game_month>>sub_game_year;
  488. g = new SubscriptionGame(game_name, game_price,
  489. game_on_sale, sub_game_monthly_fee, sub_game_month, sub_game_year);
  490. }
  491.  
  492. //cout<<(*g);
  493.  
  494. u+=(*g);
  495. }
  496.  
  497. cout<<u;
  498.  
  499. cout<<"Total money spent: $"<<u.total_spent()<<endl;
  500. }
  501. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement