Guest User

Untitled

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