Java 类对象的综合运用
2023-12-13 23:14:33
?
1.Employee.java
package Message;
import java.util.Date;
class Employee {
private String name;
private int birthMonth;
public Employee(String name, int birthMonth) {
this.name = name;
this.birthMonth = birthMonth;
}
public double getSalary(int month) {
double salary = 0.0; // 假设基本工资为5000元
salary = 5000.0;
if (birthMonth == month) {
salary += 100.0;
}
return salary;
}
}
2.SalariedEmployee.java
package Message;
class SalariedEmployee extends Employee {
private double monthlySalary;
public SalariedEmployee(String name, int birthMonth, double monthlySalary) {
super(name, birthMonth);
this.monthlySalary = monthlySalary;
}
public double getMonthlySalary() {
return monthlySalary;
}
public double getSalary(int month) {
return super.getSalary(month) + monthlySalary;
}
}
3.HourlyEmployee.java
package Message;
class HourlyEmployee extends Employee {
private double hourlyWage;
private int monthlyHoursWorked;
public HourlyEmployee(String name, int birthMonth, double hourlyWage, int monthlyHoursWorked) {
super(name, birthMonth);
this.hourlyWage = hourlyWage;
this.monthlyHoursWorked = monthlyHoursWorked;
}
public double getHourlyWage() {
return hourlyWage;
}
public int getMonthlyHoursWorked() {
return monthlyHoursWorked;
}
public double getSalary(int month) {
double baseSalary = super.getSalary(month);
double overtimePay = (monthlyHoursWorked > 160) ? (monthlyHoursWorked - 160) * hourlyWage * 1.5 : 0;
return (monthlyHoursWorked * hourlyWage) + overtimePay;
}
}
4.SalesEmployee.java
package Message;
class SalesEmployee extends Employee {
private double monthlySales;
private double commissionRate;
public SalesEmployee(String name, int birthMonth, double monthlySales, double commissionRate) {
super(name, birthMonth);
this.monthlySales = monthlySales;
this.commissionRate = commissionRate;
}
public double getSalary(int month) {
double baseSalary = super.getSalary(month);
return monthlySales+(monthlySales * commissionRate);
}
}
5.BasePlusSalesEmployee.java
package Message;
class BasePlusSalesEmployee extends SalesEmployee {
private double baseSalary;
public BasePlusSalesEmployee(String name, int birthMonth, double monthlySales, double commissionRate, double baseSalary) {
super(name, birthMonth, monthlySales, commissionRate);
this.baseSalary = baseSalary;
}
public double getSalary(int month) {
return super.getSalary(month) + baseSalary;
}
}
6.TestEmployee.java
package Message;
public class TestEmployee {
public static void main(String[] args) {
// 创建不同类型的雇员对象并打印工资
Employee employee1 = new SalariedEmployee("李华",
1, 0.0); //假设拿固定工资5000
Employee employee2 = new HourlyEmployee("李明",
2, 15.0, 300); //假设工作了300小时
Employee employee3 = new SalesEmployee("李红",
3, 5000.0, 0.5); //假设销售额为5000,提成率为0.5
Employee employee4 = new BasePlusSalesEmployee("李四",
4, 5000.0, 0.5, 5000.0); //假设销售额为5000,提成率为0.5,底薪为5000
int month = 1; // 假设查询月份的工资
System.out.println("该月份是第1月份");
System.out.println("雇工李华固定工资: " + employee1.getSalary(month));
System.out.println("雇工李明工作了300小时工资: " + employee2.getSalary(month));
System.out.println("雇工李红销售额为5000,提成率为0.5,工资: " + employee3.getSalary(month));
System.out.println("雇工李四销售额为5000,提成率为0.5,底薪为5000,工资: " + employee4.getSalary(month));
}
}
文章来源:https://blog.csdn.net/m0_73811154/article/details/134917465
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!