java公司支出的总薪水
时间: 2024-10-18 17:03:22 浏览: 27
在Java中,想要精确计算公司的总薪水支出通常会涉及到员工信息的存储和管理。这通常通过创建一个表示员工的`Employee`类,包括姓名、职位、薪资等属性,然后在一个`Company`类中维护员工列表,并提供计算总薪水的方法。下面是一个简单的例子:
```java
import java.util.ArrayList;
import java.util.List;
class Employee {
String name;
String position;
double salary;
public Employee(String name, String position, double salary) {
this.name = name;
this.position = position;
this.salary = salary;
}
// 获取薪资的方法
public double getSalary() {
return salary;
}
}
class Company {
private List<Employee> employees;
public Company() {
this.employees = new ArrayList<>();
}
// 添加员工
public void addEmployee(Employee employee) {
employees.add(employee);
}
// 计算总薪水
public double getTotalSalaries() {
double total = 0;
for (Employee employee : employees) {
total += employee.getSalary();
}
return total;
}
}
// 主程序
public static void main(String[] args) {
Company company = new Company();
// 添加员工实例并设置薪资
company.addEmployee(new Employee("张三", "程序员", 8000));
company.addEmployee(new Employee("李四", "经理", 15000));
double totalSpending = company.getTotalSalaries();
System.out.println("公司总薪水支出: " + totalSpending);
}
```
阅读全文