微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

创建代表员工集合的类

如何解决创建代表员工集合的类

我有抽象类 AbstractEmployee 和两个具体的子类 FixedSalaryEmployeePerHourSalaryEmployee,它们继承自 AbstractEmployee 并使用正确的实现覆盖其抽象 getSalary 方法对于给定的员工类型。

一个实现 FixedSalaryEmployee - 固定工资的员工。其中平均月薪等于员工 JSON 数据中的薪水值。

第二个实现 PerHourSalaryEmployee- 员工每小时工资。当小时率等于JSON数据中的薪水值时,工作日平均为8小时,月平均为20.88个工作日。

并创建能够与不同类型员工一起工作的 Collection 类。

主要问题是如何创建代表雇员集合的EmployeeCollection类:

  • 构造函数应接受来自 JSON 文件的数据并基于 type 字段创建相应类的实例。
    • id 应以 id<number> 格式生成,例如(对于集合中的每个项目,id0id1 等)
  • 集合中的项目应按以下规则排序:
    • 按平均月薪降序对所有员工进行排序。
    • 如果员工的平均月薪相等,请改用员工姓名。

需要使用 ES5!

//AbstractEmployee.js
var AbstractEmployee = function(id,name,salary) {
    if (this.constructor === AbstractEmployee) {
      throw new Error("Can't instantiate abstract class!");
    }

    this.id = id;
    this.name = name;
    this.salary = salary; 

    if(typeof(object.id) !== 'string' || typeof(object.name) !== 'string' || typeof(object.salary) !== 'number'){
        throw new Error("Wrong param passed!");
    }
};

AbstractEmployee.prototype.getSalary = function() {
    throw new Error('Method getSalary() must be implemented');
}

//PerHourSalaryEmployee.js
var AbstractEmployee = require('./AbstractEmployee.js')

var PerHourSalaryEmployee = function(id,salary) { 
    AbstractEmployee.apply(this,arguments)
    this.id = 'id' + id;
    this.name = name;
    this.salary = salary * 20.88 * 8; 
 };
 PerHourSalaryEmployee.prototype.getSalary = function() {
    return this.salary;
}

module.exports = PerHourSalaryEmployee

//FixedSalaryEmployee.js
var AbstractEmployee = require('./AbstractEmployee.js')

var FixedSalaryEmployee = function(id,salary) {
    AbstractEmployee.apply(this,arguments);
    this.id = 'id' + id;
    this.name = name;
    this.salary = salary; 
};

FixedSalaryEmployee.prototype.getSalary = function() {
    return this.salary;
}

module.exports = FixedSalaryEmployee

employees-collection.json

[{
  "type": "per-hour","salary": 10,"name": "Anna"
},{
  "type": "per-hour","salary": 8,"name": "Bob"
},{
  "type": "fixed","salary": 8000,"name": "Dany"
},"name": "Clara"
},"salary": 1000,"name": "Egor"
}]

解决方法

正如已经评论过的那样,仅用作基于函数的 mixin 的半成品 enter code here # import pandas library import pandas as pd data = pd.read_csv(file) # creating a dict file gender = {'male': 1,'female': 2} # traversing through dataframe # Gender column and writing # values where key matches data.Gender = [gender[item] for item in data.Gender] print(data) 函数没有实际意义。

纯粹的老派(被要求/仅限于 ES5 语法)继承方法更合适。

实际上甚至不需要 AbstractEmployee 构造函数,因为 BaseEmployee 类型的特征完全等同于 FixedSalaryEmployee 类型的特征,而 BaseEmployee type 的不同之处仅在于其 PerHourSalaryEmployee 属性的内部/初始计算(但人们永远不知道未来还会带来什么)...

salary
function orderBySalaryDescendingAndNameAscending(a,b) {
  return (b.salary - a.salary) || a.name.localeCompare(b.name);
}

// employee factory.
function createTypeDependedEmployeeVariant(rawData,idx) {
  const { type,name,salary } = rawData;

  const employee = (type === 'per-hour')
    ? new PerHourSalaryEmployee(String(idx),salary)
    : new FixedSalaryEmployee(String(idx),salary)

  // employee.type = type;
  return employee;
}

// employee list factory.
function createOrderedListOfVariousEmployeeInstances(arr) {
  return arr
    .map(createTypeDependedEmployeeVariant)
    .sort(orderBySalaryDescendingAndNameAscending);
}

const jsonDataList = [{
  "type": "per-hour","salary": 10,"name": "Anna"
},{
  "type": "per-hour","salary": 8,"name": "Bob"
},{
  "type": "fixed","salary": 8000,"name": "Dany"
},"name": "Clara"
},"salary": 1000,"name": "Egor"
}];

console.log(
  createOrderedListOfVariousEmployeeInstances(jsonDataList)
    .map(({ id,salary }) => ({ id,salary }))
);
console.log(
  createOrderedListOfVariousEmployeeInstances(jsonDataList)
    .map(item => item.getSalary())
);
console.log(
  createOrderedListOfVariousEmployeeInstances(jsonDataList)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

最优化的 <script> function BaseEmployee(id,salary) { if ( (typeof id !== 'string') || (typeof name !== 'string') || (typeof salary !== 'number') || !Number.isFinite(salary) ) { throw new TypeError('Wrong parameter(s) passed!'); } this.id = 'id' + id; this.name = name; this.salary = salary; } BaseEmployee.prototype.getSalary = function() { return this.salary; } </script> <script> function PerHourSalaryEmployee (id,salary) { // super call. BaseEmployee.apply(this,arguments); this.salary = (salary * 20.88 * 8); }; // extend superclass. PerHourSalaryEmployee.prototype = Object.create(BaseEmployee.prototype); // prevent super constructor from being the sub-classed constructor. PerHourSalaryEmployee.prototype.constructor = PerHourSalaryEmployee; </script> <script> function FixedSalaryEmployee(id,arguments); }; // extend superclass. FixedSalaryEmployee.prototype = Object.create(BaseEmployee.prototype); // prevent super constructor from being the sub-classed constructor. FixedSalaryEmployee.prototype.constructor = FixedSalaryEmployee; </script> 类代码库将/可能看起来像下面提供的代码。与上面提供的工厂相比,它的用法与上面提供的工厂没有区别,后者具有额外的 Employee ...

BaseEmployee
function checkEmployeeArguments(id,salary) {
  if (
    (typeof id !== 'string') ||
    (typeof name !== 'string') ||
    (typeof salary !== 'number') ||
    !Number.isFinite(salary)
  ) {
    throw new TypeError('Wrong parameter(s) passed!');
  }
}

function FixedSalaryEmployee(id,salary) {
  checkEmployeeArguments(id,salary);

  this.id = 'id' + id;
  this.name = name;
  this.salary = salary;
}
FixedSalaryEmployee.prototype.getSalary = function() {
  return this.salary;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。