Advertisement
Guest User

Computer

a guest
Oct 23rd, 2019
452
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.             this.hddMemory -= requiredSpace;
  23.         }
  24.  
  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.         let totalRamUsage = this.taskManager.reduce((acc, cur) => acc + cur.ramUsage, 0) + ramNeeded;
  58.         let totalCpuUsage = this.taskManager.reduce((acc, cur) => acc + cur.cpuUsage, 0) + cpuUsage;
  59.  
  60.         if (totalRamUsage >= 100) {
  61.             if (totalCpuUsage >= 100) {
  62.                 throw new Error(`${name} caused out of memory exception`);
  63.             }
  64.             throw new Error(`${name} caused out of memory exception`);
  65.         }
  66.  
  67.         if (totalCpuUsage >= 100) {
  68.             throw new Error(`${name} caused out of cpu exception`);
  69.         }
  70.  
  71.         let newObj = {
  72.             name: name,
  73.             ramUsage: ramNeeded,
  74.             cpuUsage: cpuUsage,
  75.         };
  76.  
  77.         this.taskManager.push(newObj);
  78.         return newObj;
  79.     }
  80.  
  81.     taskManagerView() {
  82.         if (this.taskManager.length == 0) {
  83.             return  "All running smooth so far";
  84.         }
  85.  
  86.         return this.taskManager
  87.             .map(task => {
  88.                 return `Name - ${task.name} | Usage - CPU: ${task.cpuUsage.toFixed(0)}%, RAM: ${task.ramUsage.toFixed(0)}%`;
  89.             }).join('\n');
  90.  
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement