代码
/*某公司的雇员分为以下若干类:
Employee:这是所有员工总的父类,
属性:
员工的姓名,员工的生日月份。
方法:getSalary(intmonth)
根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
SalariedEmployee:
Employee 的子类,拿固定工资的员工。
属性:月薪
HourlyEmployee:
Employee 的子类, 按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。
属性:每小时的工资、每月工作的小时数
SalesEmployee:
Employee 的子类,销售人员,工资由月销售额和提成率决定。
属性:月销售额、提成率
BasePlusSalesEmployee:
SalesEmployee 的子类,有固定底薪的销售人员,工资 由底薪加上销售提成部分。
属性:底薪。
根据要求创建 SalariedEmployee 、 HourlyEmployees 、SaleEmployee 和 BasePlusSalesEmployee四个类的对象各一个,
并计算某个月这四个对象的工资。
注意:要求把每个类都做成完全封装,不允许非私有化属性。*/
public class Homework1 {
public static void main(String[] args) {
//创建对象
Employee e1 = new SalariedEmployee("张三", 1, 2500);
Employee e2 = new HourlyEmployee("李四", 2, 100, 200);
Employee e3 = new SalesEmployee("赵六", 3, 1000000, 0.01);
Employee e4 = new BasePlusSalesEmployee("钱七",4,100000, 0.02, 500);
//获得薪水
System.out.println(e1.getName() + "的工资是:" + e1.getSalary(4));
System.out.println(e2.getName() + "的工资是:" + e2.getSalary(4));
System.out.println(e3.getName() + "的工资是:" + e3.getSalary(4));
System.out.println(e4.getName() + "的工资是:" + e4.getSalary(4));
}
}
/*
Employee:这是所有员工总的父类,
属性:
员工的姓名,员工的生日月份。
方法:getSalary(intmonth)
根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
*/
abstract class Employee{
private String name;
private int month;
//constructor
public Employee() {
}
public Employee(String name, int month) {
this.name = name;
this.month = month;
}
//setter and getter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public abstract double getSalary(int month);
}
/*
SalariedEmployee:
Employee 的子类,拿固定工资的员工。
属性:月薪
*/
class SalariedEmployee extends Employee{
//月薪
private int monthlySalary;
@Override
public double getSalary(int month) {
if (this.getMonth() == month){
return this.getMonthlySalary() + 100;
}else{
return this.getMonthlySalary();
}
}
//constructor
public SalariedEmployee() {
}
public SalariedEmployee(String name, int month, int monthlySalary) {
super(name, month);
this.monthlySalary = monthlySalary;
}
//setter and getter
public int getMonthlySalary() {
return monthlySalary;
}
public void setMonthlySalary(int monthlySalary) {
this.monthlySalary = monthlySalary;
}
}
/*
HourlyEmployee:
Employee 的子类, 按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。
属性:每小时的工资、每月工作的小时数
*/
class HourlyEmployee extends Employee{
//小时工资
int hourlyPayment;
//每月工作小时数
int workHour;
@Override
public double getSalary(int month) {
if (this.getWorkHour() > 160){
if (this.getMonth() == month){
return getHourlyPayment()*160 + getHourlyPayment()*(getWorkHour()-160)*1.5 + 100;
}
return getHourlyPayment()*160 + getHourlyPayment()*(getWorkHour()-160)*1.5;
}
else{
if (this.getMonth() == month){
return getWorkHour()*getHourlyPayment() + 100;
}
return getHourlyPayment()*getWorkHour();
}
}
//constructor
public HourlyEmployee() {
}
public HourlyEmployee(String name, int month, int hourlyPayment, int workHour) {
super(name, month);
this.hourlyPayment = hourlyPayment;
this.workHour = workHour;
}
//setter and getter
public int getHourlyPayment() {
return hourlyPayment;
}
public void setHourlyPayment(int hourlyPayment) {
this.hourlyPayment = hourlyPayment;
}
public int getWorkHour() {
return workHour;
}
public void setWorkHour(int workHour) {
this.workHour = workHour;
}
}
/*
SalesEmployee:
Employee 的子类,销售人员,工资由月销售额和提成率决定。
属性:月销售额、提成率
*/
class SalesEmployee extends Employee{
private double monthlySale;
//提成率应在0~1之间
private double commissionRate;
//constructor
public SalesEmployee() {
}
public SalesEmployee(String name, int month, double monthlySale, double commissionRate) {
super(name, month);
this.monthlySale = monthlySale;
this.commissionRate = commissionRate;
}
//setter and getter
public double getMonthlySale() {
return monthlySale;
}
public void setMonthlySale(double monthlySale) {
this.monthlySale = monthlySale;
}
public double getCommissionRate() {
return commissionRate;
}
public void setCommissionRate(double commissionRate) {
this.commissionRate = commissionRate;
}
@Override
public double getSalary(int month) {
if (this.getMonth() == month){
return this.getMonthlySale()*this.getCommissionRate() + 100;
}else{
return this.getMonthlySale()*this.getCommissionRate();
}
}
}
/*
BasePlusSalesEmployee:
SalesEmployee 的子类,有固定底薪的销售人员,工资 由底薪加上销售提成部分。
属性:底薪。
*/
class BasePlusSalesEmployee extends SalesEmployee{
private double baseSalary;
//constructor
public BasePlusSalesEmployee() {
}
public BasePlusSalesEmployee(String name, int month, double monthlySale, double commissionRate, double baseSalary) {
super(name, month, monthlySale, commissionRate);
this.baseSalary = baseSalary;
}
//setter and getter
public double getBaseSalary() {
return baseSalary;
}
public void setBaseSalary(double baseSalary) {
this.baseSalary = baseSalary;
}
//重写父类获得薪水的方法
@Override
public double getSalary(int month) {
if (this.getMonth() == month){
return this.getBaseSalary() + this.getMonthlySale()*this.getCommissionRate() + 100;
}else{
return this.getBaseSalary() + this.getMonthlySale()*this.getCommissionRate();
}
}
}