SHARE
TWEET

Untitled

a guest Oct 21st, 2019 89 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 = Number(ramMemory);
  4.         this.cpuGHz = Number(cpuGHz);
  5.         this.hddMemory = Number(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.         this.hddMemory -= requiredSpace;
  16.         let program = {
  17.             name: name,
  18.             requiredSpace: requiredSpace
  19.         };
  20.  
  21.         this.installedPrograms.push(program);
  22.  
  23.         return program;
  24.     }
  25.  
  26.     uninstallAProgram(name) {
  27.         let program = this.installedPrograms.find(p => p.name === name);
  28.  
  29.         if (!program) {
  30.         throw new Error("Control panel is not responding");
  31.        }
  32.  
  33.        this.hddMemory += program.requiredSpace;
  34.        let index = this.installedPrograms.findIndex(p => p.name === name);      
  35.        this.installedPrograms.splice(index, 1);
  36.  
  37.        return this.installedPrograms;
  38.     }
  39.  
  40.     openAProgram(name) {
  41.         let program = this.installedPrograms.find(p => p.name === name);
  42.         let openProgram = this.taskManager.find(p => p.name === name)
  43.  
  44.         if (!program) {
  45.             throw new Error (`The ${name} is not recognized`);
  46.         }
  47.  
  48.         if (openProgram) {
  49.             throw new Error (`The ${name} is already open`);
  50.         }
  51.  
  52.         let ram = (program.requiredSpace / this.ramMemory) * 1.5;
  53.         let cpu = ((program.requiredSpace / this.cpuGHz) / 500) * 1.5;
  54.  
  55.         let currentProgram = {
  56.             name: name,
  57.             ramUsage: ram,
  58.             cpuUsage: cpu
  59.         };
  60.  
  61.         let totalRamUsage = 0;
  62.         let totalCpuUsage = 0;
  63.  
  64.         if (this.taskManager.length > 0) {
  65.             totalRamUsage = this.taskManager.map(r => r.ramUsage).reduce((acc, curr) => acc + curr);
  66.             totalCpuUsage = this.taskManager.map(c =>c.cpuUsage).reduce((acc, curr) => acc + curr);
  67.         }        
  68.  
  69.         if (totalRamUsage + ram >= 100) {
  70.             throw new Error(`${name} caused out of memory exception`)
  71.         }
  72.         if (totalCpuUsage + cpu >= 100) {
  73.             throw new Error(`${name} caused out of cpu exception`)
  74.         }
  75.  
  76.         this.taskManager.push(currentProgram);
  77.  
  78.         return currentProgram;
  79.     }
  80.  
  81.     taskManagerView() {
  82.         if (this.taskManager.length === 0) {
  83.             return "All running smooth so far";
  84.         }
  85.  
  86.         let finalResult = [];        
  87.  
  88.         for (const program of this.taskManager) {
  89.             let result = "";
  90.             result = `Name - ${program.name} | Usage - CPU: ${program.cpuUsage.toFixed(0)}%, RAM: ${program.ramUsage.toFixed(0)}%`;  
  91.             finalResult.push(result);
  92.         }
  93.  
  94.         return finalResult.join('\n');
  95.     }  
  96. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top