Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Computer {
- constructor(ramMemory, cpuGHz, hddMemory) {
- this.ramMemory = Number(ramMemory);
- this.cpuGHz = Number(cpuGHz);
- this.hddMemory = Number(hddMemory);
- this.taskManager = [];
- this.installedPrograms = [];
- }
- installAProgram(name, requiredSpace) {
- if (this.hddMemory < requiredSpace) {
- throw new Error("There is not enough space on the hard drive");
- }
- this.hddMemory -= requiredSpace;
- let program = {
- name: name,
- requiredSpace: requiredSpace
- };
- this.installedPrograms.push(program);
- return program;
- }
- uninstallAProgram(name) {
- let program = this.installedPrograms.find(p => p.name === name);
- if (!program) {
- throw new Error("Control panel is not responding");
- }
- this.hddMemory += program.requiredSpace;
- let index = this.installedPrograms.findIndex(p => p.name === name);
- this.installedPrograms.splice(index, 1);
- return this.installedPrograms;
- }
- openAProgram(name) {
- let program = this.installedPrograms.find(p => p.name === name);
- let openProgram = this.taskManager.find(p => p.name === name)
- if (!program) {
- throw new Error (`The ${name} is not recognized`);
- }
- if (openProgram) {
- throw new Error (`The ${name} is already open`);
- }
- let ram = (program.requiredSpace / this.ramMemory) * 1.5;
- let cpu = ((program.requiredSpace / this.cpuGHz) / 500) * 1.5;
- let currentProgram = {
- name: name,
- ramUsage: ram,
- cpuUsage: cpu
- };
- let totalRamUsage = 0;
- let totalCpuUsage = 0;
- if (this.taskManager.length > 0) {
- totalRamUsage = this.taskManager.map(r => r.ramUsage).reduce((acc, curr) => acc + curr);
- totalCpuUsage = this.taskManager.map(c =>c.cpuUsage).reduce((acc, curr) => acc + curr);
- }
- if (totalRamUsage + ram >= 100) {
- throw new Error(`${name} caused out of memory exception`)
- }
- if (totalCpuUsage + cpu >= 100) {
- throw new Error(`${name} caused out of cpu exception`)
- }
- this.taskManager.push(currentProgram);
- return currentProgram;
- }
- taskManagerView() {
- if (this.taskManager.length === 0) {
- return "All running smooth so far";
- }
- let finalResult = [];
- for (const program of this.taskManager) {
- let result = "";
- result = `Name - ${program.name} | Usage - CPU: ${program.cpuUsage.toFixed(0)}%, RAM: ${program.ramUsage.toFixed(0)}%`;
- finalResult.push(result);
- }
- return finalResult.join('\n');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement