Advertisement
Todorov_Stanimir

03. Organization

Nov 9th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Organization {
  2.     //test 1
  3.     constructor(name, budget) {
  4.         this.name = String(name);
  5.         this.budget = Number(budget);
  6.         this.employees = [];
  7.         this.departmentsBudget = {
  8.             marketing: 0.40 * this.budget,
  9.             finance: 0.25 * this.budget,
  10.             production: 0.35 * this.budget,
  11.         }
  12.     }
  13.  
  14.     // get departmentsBudget() {
  15.     //     return this.departments;
  16.     // }
  17.     //test 2, 3, 4, 5
  18.     add(employeeName, department, salary) {
  19.         if (salary > this.departmentsBudget[department]) {
  20.             return `The salary that ${department} department can offer to you Mr./Mrs. ${employeeName} is $${this.departmentsBudget[department]}.`;
  21.         }
  22.         let employer = {
  23.             employeeName,
  24.             department,
  25.             salary
  26.         }
  27.         this.employees.push(employer);
  28.         this.departmentsBudget[department] -= +salary;
  29.         return `Welcome to the ${department} team Mr./Mrs. ${employeeName}.`
  30.     }
  31.     //test 6, 7, 8
  32.     employeeExists(employeeName) {
  33.         let employer = this.employees.find(empl => empl.employeeName === employeeName);
  34.  
  35.         if (employer) {
  36.             return `Mr./Mrs. ${employeeName} is part of the ${employer.department} department.`;
  37.         } else {
  38.             return `Mr./Mrs. ${employeeName} is not working in ${this.name}.`;
  39.         }
  40.     }
  41.     //test 9, 10
  42.     leaveOrganization(employeeName) {
  43.         let employer = this.employees.find(empl => empl.employeeName === employeeName);
  44.  
  45.         if (employer) {
  46.             this.departmentsBudget[employer.department] += employer.salary;
  47.             let res = this.employees.splice(this.employees.findIndex(emploer => emploer.employeeName === employeeName), 1);
  48.             return `It was pleasure for ${this.name} to work with Mr./Mrs. ${employeeName}.`;
  49.         } else {
  50.             return `Mr./Mrs. ${employeeName} is not working in ${this.name}.`;
  51.         }
  52.     }
  53.  
  54.     status() {
  55.         const departments = Object.keys(this.departmentsBudget).map(el => `${el[0].toUpperCase()}${el.slice(1)}`);
  56.         let output = `${this.name.toUpperCase()} DEPARTMENTS:`;
  57.         output += departments.map(departm => {
  58.             let currentDep = this.employees.filter(el => el.department === departm.toLowerCase());
  59.             let employersinDep = currentDep.sort((a, b) => b.salary - a.salary).map(el => el.employeeName)
  60.             return `\n${departm} | Employees: ${currentDep.length}: ${employersinDep.join(', ')} | Remaining Budget: ${this.departmentsBudget[departm.toLowerCase()]}`;
  61.         }).join('');
  62.         return output;
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement