总薪酬计算器
公司需要每位员工的总薪酬数据,即将工资与奖金合并。由于该计算需要同一行中两个不同列的值,您需要按行遍历,而不是仅转换单列。
已导入 Tablesaw 库,employees.csv 包含 Salary 和 Bonus 列。
本练习是课程的一部分
Java 中的数据导入
练习说明
- 遍历每一行以同时访问工资和奖金的值。
- 将两个值相加以计算总薪酬。
- 将
totalComp列添加到employees表中。
交互式实操练习
通过完成这段示例代码来试试这个练习。
public class TotalCompensation {
public static void main(String[] args) {
Table employees = Table.read().csv("employees.csv");
// Iterate through each row
DoubleColumn totalComp = DoubleColumn.create("TotalCompensation");
employees.____(row -> {
double salary = row.getInt("Salary");
double bonus = row.getInt("Bonus");
// Sum salary and bonus
totalComp.append(____ + ____);
});
// Add totalComp column
employees.addColumns(____);
System.out.println("Total Compensation Analysis:");
System.out.println(employees.first(5));
}
}