Advertisement
Guest User

Organization

a guest
Feb 24th, 2020
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Organization {
  2.     constructor(name, budget) {
  3.         this.name = name;
  4.         this.budget = budget;
  5.         this.employees = [];
  6.         this._departmentsBudget = {
  7.             marketing: budget * 0.4,
  8.             finance: budget * 0.25,
  9.             production: budget * 0.35
  10.         };
  11.     }
  12.  
  13.     get departmentsBudget() {
  14.         return this._departmentsBudget;
  15.     }
  16.  
  17.     add(employeeName, department, salary) {
  18.         if (this.departmentsBudget[department] >= salary) {
  19.             let employee = {
  20.                 employeeName,
  21.                 department,
  22.                 salary
  23.             };
  24.             this.employees.push(employee);
  25.             this._departmentsBudget[department] -= salary;
  26.             this.budget -= salary;
  27.             return `Welcome to the ${department} team Mr./Mrs. ${employeeName}.`;
  28.         } else {
  29.             return `The salary that ${department} department can offer to you Mr./Mrs. ${employeeName} is $${this.departmentsBudget[department]}.`;
  30.         }
  31.     }
  32.  
  33.     employeeExists(employeeName) {
  34.         let found = this.employees.find(x => x.employeeName === employeeName);
  35.         if (found) {
  36.             return `Mr./Mrs. ${employeeName} is part of the ${found.department} department.`;
  37.         } else {
  38.             return `Mr./Mrs. ${employeeName} is not working in ${this.name}.`;
  39.         }
  40.     }
  41.  
  42.     leaveOrganization(employeeName) {
  43.         let foundIndex = this.employees.findIndex(x => x.employeeName === employeeName);
  44.         if (foundIndex < 0 ) {
  45.             return `Mr./Mrs. ${employeeName} is not working in ${this.name}.`;
  46.         } else {
  47.             let salary = this.employees[foundIndex].salary;
  48.             let department = this.employees[foundIndex].department;
  49.             this.budget += salary;
  50.             this._departmentsBudget[department] += salary;
  51.             this.employees.splice(foundIndex, 1);
  52.             return `It was pleasure for ${this.name} to work with Mr./Mrs. ${employeeName}.`;
  53.         }
  54.     }
  55.  
  56.     status() {
  57.         let result = `${this.name.toUpperCase()} DEPARTMENTS:`;
  58.         let marketingEmployees = this.employees
  59.             .filter(e => e.department === 'marketing')
  60.             .sort((a, b) => b.salary - a.salary)
  61.             .map(e => e.employeeName);
  62.         let financeEmployees = this.employees
  63.             .filter(e => e.department === 'finance')
  64.             .sort((a, b) => b.salary - a.salary)
  65.             .map(e => e.employeeName);
  66.         let productionEmployees = this.employees
  67.             .filter(e => e.department === 'production')
  68.             .sort((a, b) => b.salary - a.salary)
  69.             .map(e => e.employeeName);
  70.         result += `\nMarketing | Employees: ${marketingEmployees.length}: ${marketingEmployees.join(', ')} |  Remaining Budget: ${this.departmentsBudget.marketing}`;
  71.         result += `\nFinance | Employees: ${financeEmployees.length}: ${financeEmployees.join(', ')} |  Remaining Budget: ${this.departmentsBudget.finance}`;
  72.         result += `\nProduction | Employees: ${productionEmployees.length}: ${productionEmployees.join(', ')} |  Remaining Budget: ${this.departmentsBudget.production}`;
  73.         // result += `\nMarketing | Employees: ${marketingEmployees.length}${marketingEmployees.length ? ': ' + marketingEmployees.join(', ') : ''} | Remaining Budget: ${this.departmentsBudget.marketing}`;
  74.         // result += `\nFinance | Employees: ${financeEmployees.length}${financeEmployees.length ? ': ' + financeEmployees.join(', ') : ''} | Remaining Budget: ${this.departmentsBudget.finance}`;
  75.         // result += `\nProduction | Employees: ${productionEmployees.length}${productionEmployees.length ? ': ' + productionEmployees.join(', ') : ''} | Remaining Budget: ${this.departmentsBudget.production}`;
  76.         console.log(result);
  77.         // return result;
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement