CB100

Notas, resueltos y tps de la materia Algoritmos y Estructuras de Datos
Index Commits Files Refs README
parciales/2021-1C-3p/main.cpp (6558B)
   1 #include <iostream>
   2 #include "vector.h"
   3 
   4 typedef enum {
   5     UNKNOWN,
   6     VIDEO,
   7     MUSIC,
   8     SOCIAL
   9 } appType;
  10 
  11 class App {
  12 private:
  13     std::string name;
  14     float size;
  15     appType type;
  16     bool appRequiresInternet;
  17     bool captionsActive;
  18 
  19 public:
  20     /**
  21      * post: inicializa `App`
  22      */
  23     App() {
  24         this->size = 0;
  25         this->type = UNKNOWN;
  26         this->appRequiresInternet = false;
  27         this->captionsActive = false;
  28     }
  29 
  30     /**
  31      * post: inicializa `App` con atributo `name` pasado como argumento
  32      */
  33     App(std::string name) {
  34         this->name = name;
  35         this->size = 0;
  36         this->type = UNKNOWN;
  37         this->appRequiresInternet = false;
  38         this->captionsActive = false;
  39     }
  40 
  41     /**
  42      * post: inicializa `App` con los atributos pasados como argumento
  43      */
  44     App(std::string name, float size, appType type,
  45             bool appRequiresInternet, bool captionsActive) {
  46         this->name = name;
  47         this->size = size;
  48         this->type = type;
  49         this->appRequiresInternet = appRequiresInternet;
  50         this->captionsActive = captionsActive;
  51     }
  52 
  53     /**
  54      * post: libera la memoria
  55      */
  56     virtual ~App() {}
  57 
  58     /**
  59      * post: devuelve el atributo `name`
  60      */
  61     std::string getName() {
  62         return this->name;
  63     }
  64 
  65     /**
  66      * post: devuelve el atributo `type`
  67      */
  68     appType getAppType() {
  69         return this->type;
  70     }
  71 
  72     /**
  73      * post: asigna al atributo `name` aquel pasado como argumento
  74      */
  75     void setName(std::string name) {
  76         this->name = name;
  77     }
  78 
  79     /**
  80      * post: asigna al atributo `captionsActive` el valor de `status`
  81      */
  82     void setCaptions(bool status) {
  83         this->captionsActive = status;
  84     }
  85 
  86     /**
  87      * post: devuelve el estado de `captionsActive`
  88      */
  89     bool getCaptions(bool status) {
  90         return this->captionsActive;
  91     }
  92 
  93     /**
  94      * post: cambia al valor opuesto `captionsActive`
  95      */
  96     void toggleCaptions() {
  97         this->captionsActive = !this->captionsActive;
  98     }
  99 
 100     /**
 101      * post: devuelve el valor del atributo `appRequiresInternet`
 102      */
 103     bool requiresInternet() {
 104         return this->appRequiresInternet;
 105     }
 106 };
 107 
 108 class SmartTV {
 109 private:
 110     Vector<App*> *appsList;
 111     App *currentApp;
 112     float diskSize;
 113     unsigned int appsInstalledCount;
 114     bool internetConnected;
 115 
 116 public:
 117     /**
 118      * post: inicializa la clase `SmartTV`
 119      */
 120     SmartTV() {
 121         this->appsList = new Vector<App*>;
 122         this->currentApp = NULL;
 123         this->diskSize = 0;
 124         this->internetConnected = 0;
 125         this->appsInstalledCount = 0;
 126     }
 127 
 128     /**
 129      * post: inicializa `SmartTV` con el valor de `diskSize` a aquel pasado como
 130      *       argumento
 131      */
 132     SmartTV(float diskSize) {
 133         this->appsList = new Vector<App*>;
 134         this->currentApp = NULL;
 135         this->diskSize = diskSize;
 136         this->internetConnected = 0;
 137         this->appsInstalledCount = 0;
 138     }
 139 
 140     /**
 141      * post: libera la memoria de `SmartTV` y todas las instancias de `App` 
 142      */
 143     virtual ~SmartTV() {
 144         for(size_t i = 0; i < this->appsList->getSize(); ++i) {
 145             delete this->appsList->getAtIndex(i);
 146         }
 147         delete this->appsList;
 148     }
 149 
 150     /**
 151      * post: agrega una aplicacion a `appsList`
 152      */
 153     void addApp(std::string name) {
 154         App *newApp = new App(name);
 155         this->appsList->append(newApp);
 156         this->appsInstalledCount++;
 157     }
 158 
 159     /**
 160      * post: agrega una aplicacion a `appsList` con los atributos recibidos como
 161      *       argumento
 162      */
 163     void addApp(std::string name, float size, appType type, bool requiresInternet) {
 164         try {
 165             this->getAppByName(name);
 166         } catch (std::string return_value) {
 167             // la app no esta instalada
 168             App *newApp = new App(name, size, type, requiresInternet, false);
 169             this->appsList->append(newApp);
 170             this->appsInstalledCount++;
 171         }
 172     }
 173 
 174     /**
 175      * post: devuelve el valor de `appsInstalledCount`
 176      */
 177     unsigned int getAppsInstalledCount() {
 178         return this->appsInstalledCount;
 179     }
 180 
 181     /**
 182      * post: devuelve un puntero a la aplicacion en la posicion `index` 
 183      *       de `appsList`
 184      */
 185     App *getAppByIndex(unsigned int index) {
 186         try {
 187             return this->appsList->getAtIndex(index);
 188         } catch (std::string error) {
 189             return NULL;
 190         }
 191     }
 192 
 193     /**
 194      * pre: la aplicacion debe existir en `appsList` si no arroja una excepción
 195      * post: devuelve un puntero a la aplicacion cuyo nombre es `name` 
 196      */
 197     App *getAppByName(std::string name) {
 198         for(size_t i = 0; i < this->appsList->getSize(); ++i) {
 199             App *tmp = this->appsList->getAtIndex(i);
 200             if(tmp->getName() == name) {
 201                 return tmp;
 202             }
 203         }
 204         throw "the app `" + name + "` is not installed";
 205     }
 206 
 207     /**
 208      * pre: no hay una aplicacion corriendo, si no arroja una excepción
 209      * post: apunta `currentApp` a la aplicacion con nombre `name`
 210      */
 211     void startApp(std::string name) {
 212         if(currentApp != NULL) {
 213             throw "there is an app running already";
 214         }
 215 
 216         try {
 217             currentApp = this->getAppByName(name);
 218         } catch(std::string error) {
 219             currentApp = NULL;
 220             throw "the app is not installed";
 221         }
 222 
 223         if(currentApp->requiresInternet() && !this->internetConnected) {
 224             currentApp = NULL;
 225             throw "the app requires internet";
 226         }
 227     }
 228 
 229     /**
 230      * pre: hay una aplicacion corriendo
 231      * post: devuelve un puntero a la aplicacion que está corriendo actualmente
 232      */
 233     App *getCurrentApp() {
 234         return this->currentApp;
 235     }
 236 
 237     /**
 238      * pre: hay una aplicacion corriendo
 239      * post: asigna el valor de `currentApp` a `NULL`
 240      */
 241     void closeApp(std::string name) {
 242         if(currentApp == NULL) {
 243             throw "there is no app running";
 244         }
 245 
 246         currentApp = NULL;
 247     }
 248 
 249     /**
 250      * post: asigna a `internetConnected` el valor `true`
 251      */
 252     void internetConnect() {
 253         this->internetConnected = true;
 254     }
 255 
 256     /**
 257      * post: asigna a `internetConnected` el valor `false`
 258      */
 259     void internetDiconnect() {
 260         this->internetConnected = false;
 261     }
 262 
 263     /**
 264      * pre: hay una aplicación ejecutandose y es de tipo `VIDEO`
 265      * post: ejecuta el metodo `toggleCaptions` de la aplicacion en ejecución
 266      */
 267     void toggleCaptions() {
 268         App *runningApp = this->currentApp;
 269         if((runningApp != NULL) && (runningApp->getAppType() == VIDEO)) {
 270             runningApp->toggleCaptions();
 271         } else {
 272             throw "there is no running app or the app does not allow captions";
 273         }
 274     }
 275 };
 276 
 277 void printTvStatus(SmartTV *tv) {
 278     for(size_t i = 0; i < tv->getAppsInstalledCount(); ++i) {
 279         std::cout << tv->getAppByIndex(i)->getName() << std::endl;
 280     }
 281     App *curApp = tv->getCurrentApp();
 282     std::string curAppName = (curApp == NULL ? "none" : curApp->getName());
 283     std::cout << "Current app running: " << curAppName << std::endl;
 284 }
 285 
 286 int main (void) {
 287     SmartTV *tv = new SmartTV(8000); // inicializa tv con disco de 8GB (8000MB)
 288 
 289     tv->internetConnect();
 290     tv->addApp("X", 150, SOCIAL, true);
 291     tv->addApp("youtube", 258, VIDEO, true);
 292     tv->addApp("spotify");
 293 
 294     tv->startApp("youtube");
 295     printTvStatus(tv);
 296     std::cout << std::endl;
 297 
 298     tv->closeApp("youtube");
 299     printTvStatus(tv);
 300 
 301     delete tv;
 302     return 0;
 303 }