/* 需求: 创建一个Employee数组,分别创建若干个不同的Employee对象,并打印某个月的工资;且每个类都完全封装,不允许有非私有化属性 1.Employee:父类 属性:员工姓名,员工的出生年月 方法:getSalary(int month)根据参数月份确定工资,如果该月员工过生日,则公司会额外发放100元 2.SalariedEmployee:Employee的子类,拿固定工资的员工 属性:月薪 3.HourlyEmployee:Employee的子类,按小时拿工资的员工,每月工作超出160h的部分按照1.5倍的工资发放 属性:每小时的工资、每月工作的小时数 4.SalesEmployee:Employee的子类,销售人员,工资有月销售额和提成率决定 属性:月销售额,提成率 5.BasePlusSalesEmployee:SalesEmployee的子类,有固定底薪的销售人员,工资为底薪加上销售提成 属性:底薪 */
/*
需求:
创建一个Employee数组,分别创建若干个不同的Employee对象,并打印某个月的工资;且每个类都完全封装,不允许有非私有化属性
1.Employee:父类
属性:员工姓名,员工的出生年月
方法:getSalary(int month)根据参数月份确定工资,如果该月员工过生日,则公司会额外发放100元
2.SalariedEmployee:Employee的子类,拿固定工资的员工
属性:月薪
3.HourlyEmployee:Employee的子类,按小时拿工资的员工,每月工作超出160h的部分按照1.5倍的工资发放
属性:每小时的工资、每月工作的小时数
4.SalesEmployee:Employee的子类,销售人员,工资有月销售额和提成率决定
属性:月销售额,提成率
5.BasePlusSalesEmployee:SalesEmployee的子类,有固定底薪的销售人员,工资为底薪加上销售提成
属性:底薪
*/
//导包
import java.util.Scanner;
/*
创建Employee父类
属性:员工姓名,员工的出生年月
*/
class Employee {
//定义成员姓名和出生年月
private String name;
private int birthdayYear;
private int birthdayMonth;
//构造方法
public Employee() {}
public Employee(String name,int birthdayYear,int birthdayMonth) {
this.name = name;
this.birthdayYear = birthdayYear;
this.birthdayMonth = birthdayMonth;
}
//定义成员方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBirthdayYear() {
return birthdayYear;
}
public void setBirthdayYear(int year) {
this.birthdayYear = year;
}
public int getBirthdayMonth() {
return birthdayMonth;
}
public void setBirthdayMonth(int birthdayMonth) {
this.birthdayMonth = birthdayMonth;
}
//用show()方法将输出成员信息
public String show() {
return "姓名:" + this.getName() + "\n" + "出生年月:" + this.getBirthdayYear() + "年" + this.getBirthdayMonth() + "月";
}
//方法:getSalary(int month)根据参数月份确定工资,如果该月员工过生日,则公司会额外发放100元
public double getSalary(int month) {
double salary = 0;
if(month == birthdayMonth) {
return salary+100;
}else {
return salary;
}
}
}
//SalariedEmployee:Employee的子类,拿固定工资的员工
//SalariedEmployee类继承Employee类
class SalariedEmployee extends Employee {
//定义子类特有的属性:月薪
private int monthSalary;
public SalariedEmployee(String name, int birthdayYear,int birthdayMonth,int monthSalary) {
super(name, birthdayYear, birthdayMonth);
this.setMonthSalary(monthSalary);
}
public int getMonthSalary() {
return monthSalary;
}
public void setMonthSalary(int monthSalary) {
this.monthSalary = monthSalary;
}
//重写父类的getSalary()方法
public double getSalary(int month) {
double salary = monthSalary+super.getSalary(month);
return salary;
}
}
//HourlyEmployee:Employee的子类,按小时拿工资的员工,每月工作超出160h的部分按照1.5倍的工资发放
//HourlyEmployee类继承Employee类
class HourlyEmployee extends Employee {
//定义子类特有的属性:每小时的工资、每月工作的小时数
private double hourSalary;
private int hour;
public HourlyEmployee() {}
public HourlyEmployee(String name,int birthdayYear,int birthdayMonth,double hourSalary,int hour) {
super(name, birthdayYear, birthdayMonth);
this.setHourSalary(hourSalary);
this.setHour(hour);
}
public double getHourSalary() {
return hourSalary;
}
public void setHourSalary(double hourSalary) {
this.hourSalary = hourSalary;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public double getAllSalary() {
//按小时拿工资的员工,每月工作超出160h的部分按照1.5倍的工资发放
if(hour <= 160) {
return hour * hourSalary;
}else {
return 160 * hourSalary + (hour - 160) * 1.5 * hourSalary ;
}
}
//重写父类的getSalary()方法
public double getSalary(int month) {
double salary = this.getAllSalary()+super.getSalary(month);
return salary;
}
//重写父类的show()方法
public String show() {
return super.show() + "\n" + "每小时的工资:" + this.getHourSalary() + "元" + ",共" + this.getHour() + "个小时" ;
}
}
//SalesEmployee:Employee的子类,销售人员,工资有月销售额和提成率决定
//SalesEmployee类继承Employee类
class SalesEmployee extends Employee {
//子类特有属性:月销售额,提成率
private int sale;
private double rate;
public SalesEmployee() {}
public SalesEmployee(String name,int birthdayYear,int birthdayMonth,int sale,double rate) {
super(name, birthdayYear, birthdayMonth);
this.setSale(sale);
this.setRate(rate);
}
public int getSale() {
return sale;
}
public void setSale(int sale) {
this.sale = sale;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public double getAllSalary() {
return sale*rate;
}
//重写父类的getSalary()方法
public double getSalary(int month) {
double salary = this.getAllSalary()+super.getSalary(month);
return salary;
}
//重写父类的show()方法
public String show() {
return super.show() + "\n" + "本月销售额:" + this.getSale() + "元" + ",提成率:" + this.getRate() ;
}
}
//BasePlusSalesEmployee:SalesEmployee的子类,有固定底薪的销售人员,工资为底薪加上销售提成
//BasePlusSalesEmployee类继承SalesEmployee类
class BasePlusSalesEmployee extends SalesEmployee {
//子类特有属性:底薪
private int baseSalary;
public BasePlusSalesEmployee() {}
public BasePlusSalesEmployee(String name,int birthdayYear,int birthdayMonth,int sale,double rate,int baseSalary) {
super(name, birthdayYear, birthdayMonth, sale, rate);
this.setBaseSalary(baseSalary);
}
public int getBaseSalary() {
return baseSalary;
}
public void setBaseSalary(int baseSalary) {
this.baseSalary = baseSalary;
}
//重写父类的getSalary()方法
public double getSalary(int month) {
double salary=this.getBaseSalary()+super.getSalary(month);
return salary;
}
//重写父类的show()方法
public String show() {
return super.show() + "\n" + "提成:" + super.getAllSalary() + "元" + "\n" + "底薪:" + this.getBaseSalary() + "元" ;
}
}
public class Test {
public static void main(String[] args) {
//键盘录入月份,然后判断
//创建对象
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个月份");
//接受数据
int month = sc.nextInt();
//声明一个Employee数组,创建不同子类的对象
Employee[] employee= {new SalariedEmployee("小一", 2000, 7, 5000),
new HourlyEmployee("小二", 1990, 8, 21, 200),
new SalesEmployee("小三", 1995, 9, 20000, 0.20),
new BasePlusSalesEmployee("小四", 1985, 10, 25000, 0.25, 1000)};
//遍历数组
for(int i = 0; i < employee.length; i++) {
//输出员工信息
System.out.println(employee[i].show());
//输出员工的薪水
System.out.println(month+"月,该员工的实际工资:"+employee[i].getSalary(month) +"元");
}
}
}
输出结果: