要为某个公司编写雇员( n个雇员)支付月工资,这个公司有各种类型的雇员(Employee),不同类型的雇员按不同的方式支付工资:java写
时间: 2024-04-08 10:23:50 浏览: 133
java代码-使用java解决为公司编写雇员工资支付程序,三种雇员,getSalary函数的源代码
5星 · 资源好评率100%
法如下:
首先,定义一个 Employee 类作为基类,包含以下属性和方法:
public class Employee {
private String name; // 雇员姓名
private int age; // 雇员年龄
private double salary; // 雇员月薪
// 构造函数
public Employee(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
// 计算月薪
public double calculateSalary() {
return salary;
}
// 获取雇员信息
public String getInfo() {
return "姓名:" + name + ",年龄:" + age + ",月薪:" + salary;
}
}
接下来,定义各种类型的雇员类,继承自 Employee 类,并实现相应的计算月薪方法:
// 经理类
public class Manager extends Employee {
private double bonus; // 经理奖金
// 构造函数
public Manager(String name, int age, double salary, double bonus) {
super(name, age, salary);
this.bonus = bonus;
}
// 计算月薪
public double calculateSalary() {
return super.calculateSalary() + bonus;
}
// 获取雇员信息
public String getInfo() {
return super.getInfo() + ",奖金:" + bonus;
}
}
// 销售员类
public class Salesman extends Employee {
private double commission; // 销售提成
// 构造函数
public Salesman(String name, int age, double salary, double commission) {
super(name, age, salary);
this.commission = commission;
}
// 计算月薪
public double calculateSalary() {
return super.calculateSalary() + commission;
}
// 获取雇员信息
public String getInfo() {
return super.getInfo() + ",提成:" + commission;
}
}
// 技术员类
public class Technician extends Employee {
private int workHours; // 工作小时数
private double hourSalary; // 每小时工资
// 构造函数
public Technician(String name, int age, double salary, int workHours, double hourSalary) {
super(name, age, salary);
this.workHours = workHours;
this.hourSalary = hourSalary;
}
// 计算月薪
public double calculateSalary() {
return super.calculateSalary() + workHours * hourSalary;
}
// 获取雇员信息
public String getInfo() {
return super.getInfo() + ",工作小时数:" + workHours + ",每小时工资:" + hourSalary;
}
}
最后,编写一个 Payroll 类,用于计算所有雇员的月薪,并输出每个雇员的信息和月薪:
import java.util.ArrayList;
public class Payroll {
public static void main(String[] args) {
// 创建雇员列表
ArrayList<Employee> employees = new ArrayList<>();
employees.add(new Manager("张三", 35, 10000, 5000));
employees.add(new Salesman("李四", 28, 5000, 2000));
employees.add(new Technician("王五", 30, 6000, 160, 50));
// 计算并输出月薪
for (Employee employee : employees) {
System.out.println(employee.getInfo() + ",月薪:" + employee.calculateSalary());
}
}
}
运行结果如下:
姓名:张三,年龄:35,月薪:10000.0,奖金:5000.0,月薪:15000.0
姓名:李四,年龄:28,月薪:5000.0,提成:2000.0,月薪:7000.0
姓名:王五,年龄:30,月薪:6000.0,工作小时数:160,每小时工资:50.0,月薪:14000.0
阅读全文