Todorov_Stanimir

03. Computer JS Advanced Exam - 07 July 2019

Oct 21st, 2019
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Computer {
  2.     //test 1
  3.     constructor(ramMemory, cpuGHz, hddMemory) {
  4.         this.ramMemory = Number(ramMemory);
  5.         this.cpuGHz = Number(cpuGHz);
  6.         this.hddMemory = Number(hddMemory);
  7.         this.taskManager = [];
  8.         this.installedPrograms = [];
  9.     }
  10.     //test 2,3,4,5
  11.     installAProgram(name, requiredSpace) {
  12.  
  13.         if (this.hddMemory < requiredSpace) {
  14.             throw new Error('There is not enough space on the hard drive');
  15.         }
  16.  
  17.         let program = {
  18.             name,
  19.             requiredSpace
  20.         }
  21.  
  22.         this.installedPrograms.push(program);
  23.         this.hddMemory -= requiredSpace;
  24.  
  25.         return program;
  26.     }
  27.     //test 6,7,8
  28.     uninstallAProgram(name) {
  29.         let index = this.installedPrograms.findIndex(progr => progr.name === name);
  30.  
  31.         if (index === -1) {
  32.             throw new Error('Control panel is not responding')
  33.         }
  34.  
  35.         this.hddMemory += this.installedPrograms[index].requiredSpace;
  36.         this.installedPrograms.splice(index, 1);
  37.  
  38.         return this.installedPrograms;
  39.     }
  40.     // test 9, 10, 11, 12, 13
  41.     openAProgram(name) {
  42.         let indexinInstalledPrograms = this.installedPrograms.findIndex(progr => progr.name === name);
  43.         let indexInOpenedPrograms = this.taskManager.findIndex(progr => progr.name === name);
  44.  
  45.         if (indexinInstalledPrograms === -1) {
  46.             throw new Error(`The ${name} is not recognized`)
  47.         }
  48.  
  49.         if (indexInOpenedPrograms !== -1) {
  50.             throw new Error(`The ${name} is already open`)
  51.         }
  52.  
  53.         let ramUsage = (this.installedPrograms[indexinInstalledPrograms].requiredSpace / this.ramMemory) * 1.5;
  54.         let cpuUsage = ((this.installedPrograms[indexinInstalledPrograms].requiredSpace / this.cpuGHz) / 500) * 1.5;
  55.  
  56.         let currentProgram = {
  57.             name,
  58.             ramUsage,
  59.             cpuUsage,
  60.         }
  61.  
  62.         let usedRamMemory = this.taskManager.reduce((a, b) => (a + b.ramUsage), 0);
  63.         let usedCPU = this.taskManager.reduce((a, b) => (a + b.cpuUsage), 0);
  64.  
  65.         if (ramUsage + usedRamMemory >= 100) {
  66.             throw new Error(`${name} caused out of memory exception}`)
  67.         }
  68.  
  69.         if (cpuUsage + usedCPU >= 100) {
  70.             throw new Error(`${name} caused out of cpu exception}`)
  71.         }
  72.         this.taskManager.push(currentProgram);
  73.  
  74.         return currentProgram;
  75.     }
  76.  
  77.     taskManagerView() {
  78.         //test 14
  79.         if (this.taskManager.length === 0) {
  80.             return 'All running smooth so far'
  81.         }
  82.         //test15
  83.         return this.taskManager.map(progr => `Name - ${progr.name} | Usage - CPU: ${progr.cpuUsage.toFixed(0)}%, RAM: ${progr.ramUsage.toFixed(0)}%`).join('\n')
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment