Advertisement
darinab

GamePlatform.h

Mar 25th, 2024
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. #include <fstream>
  2. #include "Game.h";
  3.  
  4. class GamePlatform {
  5. private:
  6.     Game games[100];
  7.     int gameCount;
  8.  
  9. public:
  10.     GamePlatform() = default;
  11.  
  12.     bool addGame(const Game& game) {
  13.         if (gameCount < 100) {
  14.             games[gameCount++] = game;
  15.             return true;
  16.         }
  17.         return false;
  18.     }
  19.  
  20.     void printGame(int idx) {
  21.         if (idx >= 0 && idx < gameCount) {
  22.             games[idx].print();
  23.         }
  24.         else {
  25.             std::cout << "Invalid index!" << std::endl;
  26.         }
  27.     }
  28.  
  29.     void printAllGames() {
  30.         for (int i = 0; i < gameCount; i++) {
  31.             games[i].print();
  32.         }
  33.     }
  34.  
  35.     void printCheapestGame() {
  36.         if (gameCount == 0) {
  37.             std::cout << "No games in the platform!" << std::endl;
  38.         }
  39.         return;
  40.  
  41.         int idxCheapest = 0;
  42.         for (int i = 1; i < gameCount; i++) {
  43.             if (games[i].getPrice() < games[idxCheapest].getPrice()) {
  44.                 idxCheapest = i;
  45.             }
  46.         }
  47.         std::cout << "Cheapest game: " << std::endl;
  48.         games[idxCheapest].print();
  49.     }
  50.  
  51.     void printMostExpensiveGame() {
  52.         if (gameCount == 0) {
  53.             std::cout << "No games in the platform!" << std::endl;
  54.             return;
  55.         }
  56.  
  57.         int idxExpensive = 0;
  58.         for (int i = 1; i < gameCount; i++) {
  59.             if (games[i].getPrice() > games[idxExpensive].getPrice()) {
  60.                 idxExpensive = i;
  61.             }
  62.         }
  63.         std::cout << "Most expensive game: " << std::endl;
  64.         games[idxExpensive].print();
  65.     }
  66.  
  67.     void printAllFreeGames() {
  68.         std::cout << "Free game: " << std::endl;
  69.         bool found = false;
  70.         for (int i = 0; i < gameCount; i++) {
  71.             if (games[i].isFree()) {
  72.                 games[i].print();
  73.                 found = true;
  74.             }
  75.         }
  76.     }
  77.  
  78.     bool removeGame(int idx) {
  79.         if (idx < 0 || idx >= gameCount) {
  80.             return false;       //invalid index
  81.         }
  82.  
  83.         for (int i = idx; i < gameCount - 1; i++) {
  84.             games[i] = games[i + 1];    //moving each next element with one position to left
  85.         }
  86.         gameCount--;
  87.         return true;
  88.     }
  89.  
  90. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement