现有如下员工信息:
工号 姓名 月薪资
001 杨树苗 4650
002 刘翠花 4280
003 魏巍 6820
004 李宁 5128
005 赵明 3180
请完成如下编码任务:
1)定义一个公共实体类(class Employee)用于描述员工的工号(id)、姓名(name)和薪资(salary)等信息;
2)定义一个公共管理类(class EmployeeManager),使用 ArrayList 泛型集合保存上述列表中的员工信息,并定义公共方法" public void showEmployees() {…}“显示全部员工信息列表;定义公共方法” public float getAvgSalary() {…}“计算并返回全部员工的平均薪资;定义公共方法” public float getStandardDeviation() {…}" 计算并返回全部员工的薪资标准差。
定义Employee类
package itwcn.Demo03;
import java.util.ArrayList;
/**
* @program: Employee
* @description:
* @author: OriginalCoder
* @createtime:2020/11/22 15:36
**/
public class Employee {
private String id = null;
private String name = null;
private float salary = 0;
public Employee() {
}
public Employee(String id, String name, float salary) {
this.setId(id);
this.setName(name);
this.setSalary(salary);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
/**
* 显示所有员工基本信息
*/
public void showEmployees() {
System.out.println(this.id + ":" + this.name + ":" + this.salary);
}
/**
* 计算并返回全部员工的平均薪资
*/
public float getAvgSalary(ArrayList<Employee> arrayList) {
float sum = 0;
// for (int i = 0; i < arrayList.size(); i++) {
// sum = arrayList.get(i).getSalary();
// }
// return sum/arrayList.size();
for (Employee ee:arrayList) {
sum += ee.getSalary();
}
float avg = sum/arrayList.size();
return avg;
}
/**
* 计算并返回薪资的标准差 Standard Deviation
*/
/**
* s为标准差,sqrt表示开根号,x1,x2,x3...是薪资数据,x是平均数,n为数据的个数
* 计算公式:s = sqrt[[(x1-x)^2+(x2-x)^2+...+(xn-x)^2]/(1/n)]
* @return 返回标准差
*/
public float getStandardDeviation(ArrayList<Employee> arrayList) {
float deviation = 0;
float sum = 0,s = 0;
for (int i = 0; i < arrayList.size(); i++) {
sum = sum + (float)Math.pow(arrayList.get(i).getSalary()-getAvgSalary(arrayList),2);
}
s = sum / arrayList.size();
return (float) Math.sqrt(s);
}
}
定义EmployeeManager类
package itwcn.Demo03;
import java.util.ArrayList;
import java.util.zip.Deflater;
/**
* @program: EmployeeManager
* @description:
* @author: OriginalCoder
* @createtime:2020/11/22 15:39
**/
public class EmployeeManager {
public static void main(String[] args) {
Employee eA = null;
ArrayList<Employee> arrayList = new ArrayList<Employee>();
try {
eA = new Employee("001","杨树花",4650);
arrayList.add(eA);
eA = new Employee("002","刘翠花",4280);
arrayList.add(eA);
eA = new Employee("003","魏巍",6820);
arrayList.add(eA);
eA = new Employee("004","李宁",5128);
arrayList.add(eA);
eA = new Employee("005","赵明",3180);
arrayList.add(eA);
} catch (Exception e) {
e.printStackTrace();
}
/**
* 显示所有员工基本信息
*/
for (Employee ee: arrayList) {
ee.showEmployees();
}
float avg = eA.getAvgSalary(arrayList);
float dev = eA.getStandardDeviation(arrayList);
System.out.println("平均薪资:" + avg);
System.out.println("薪资标准差:" + dev);
}
}
上面的代码存在不规范的问题,那么下面的代码是根据规范后,进行修改的代码
建立Employee类
package itwcn.Demo04;
/**
* @program: Employee
* @description:
* @author: OriginalCoder
* @createtime:2020/11/24 21:13
**/
/**
* 实体类:封装员工的基本信息
*/
public class Employee {
private String id = null;
private String name = null;
private float salary = 0.0F;
public Employee() {
}
public Employee(String id, String name, float salary) {
this.setId(id);
this.setName(name);
this.setSalary(salary);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
public void showInfo() {
System.out.println(this.id + ":" + this.name + "," + this.salary);
}
}
建立EmployeeManager类
package itwcn.Demo04;
import java.util.ArrayList;
/**
* @program: EmployeeManager
* @description:
* @author: OriginalCoder
* @createtime:2020/11/24 21:14
**/
/**
*实体类:封装管理员工的方法
*/
public class EmployeeManager {
private ArrayList<Employee> employees = null;
public EmployeeManager() {
employees = new ArrayList<Employee>();
}
/**
* 添加员工
* @param e 一个新员工对象
* */
public boolean addEmployee(Employee e) {
boolean bR = false;
if(e!=null) {
this.employees.add(e);
bR = true;
}
return bR;
}
/**计算并返回员工的平均薪资*/
public float getAvgSalary() {
float fSum = 0.0f;
for(Employee e:this.employees) {
if(e==null) continue;
fSum = fSum + e.getSalary();
}
return fSum/this.employees.size();
}
public float getStandardDeviation() {
float fAvg = getAvgSalary();
float fSum = 0.0f;
for(Employee e:this.employees) {
if(e==null) continue;
fSum = fSum + (float)Math.pow(e.getSalary() - fAvg, 2);
}
return (float)Math.sqrt(fSum/(this.employees.size()));
}
}
建立测试类
package itwcn.Demo04;
/**
* @program: Test
* @description:
* @author: OriginalCoder
* @createtime:2020/11/24 21:40
**/
public class Test {
public static void main(String[] args) {
try {
EmployeeManager emA = new EmployeeManager();
Employee eA = new Employee("001", "张三", 4650);
emA.addEmployee(eA);
eA = new Employee("002", "刘翠花", 4280);
emA.addEmployee(eA);
eA = new Employee("003", "魏巍", 6820);
emA.addEmployee(eA);
eA = new Employee("004", "李宁", 5128);
emA.addEmployee(eA);
eA = new Employee("005", "赵明", 3180);
emA.addEmployee(eA);
// System.out.println("所有员工信息:" + emA.showInfo());
System.out.println("平均薪资=" + emA.getAvgSalary());
System.out.println("标准差=" + emA.getStandardDeviation());
} catch (Exception e) {
e.printStackTrace();
}
}
}