某公司的雇员分为以下若干类:
(1) Employee:这是所有员工总的父类。
① 属性:员工的姓名,员工的生日月份
② 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会 额外奖励100 元。
(2) SalariedEmployee:Employee 的子类,拿固定工资的员工。
① 属性:月薪。
(3)HourlyEmployee:Employee 的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5 倍工资发放。
① 属性:每小时的工资、每月工作的小时数。
(4) SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定。
① 属性:月销售额、提成率。
(5) BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加 上销售提成部分。
① 属性:底薪。
要求: 创建一个Employee 数组,分别创建若干不同的Employee对象,并打 印某个月的工资。
注意:要求把每个类都做成完全封装,不允许非私有化属性。
package cn.itcast.example;
import java.util.Iterator;
import java.util.Scanner;
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;
}
public String show() {
return "姓名:" + this.getName() + "\n" + "出生年月:" + this.getBirthdayYear() + "年" + this.getBirthdayMonth() + "月";
}
public double getSalary(int month) {
double salary = 0;
if(month == birthdayMonth) {
return salary+100;
}else {
return salary;
}
}
}
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;
}
public double getSalary(int month) {
double salary = monthSalary+super.getSalary(month);
return salary;
}
}
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() {
if(hour <= 160) {
return hour * hourSalary;
}else {
return 160 * hourSalary + (hour - 160) * 1.5 * hourSalary ;
}
}
public double getSalary(int month) {
double salary = this.getAllSalary()+super.getSalary(month);
return salary;
}
public String show() {
return super.show() + "\n" + "每小时的工资:" + this.getHourSalary() + "元" + ",共" + this.getHour() + "个小时" ;
}
}
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;
}
public double getSalary(int month) {
double salary = this.getAllSalary()+super.getSalary(month);
return salary;
}
public String show() {
return super.show() + "\n" + "本月销售额:" + this.getSale() + "元" + ",提成率:" + this.getRate() ;
}
}
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;
}
public double getSalary(int month) {
double salary=this.getBaseSalary()+super.getSalary(month);
return salary;
}
public String show() {
return super.show() + "\n" + "提成:" + super.getAllSalary() + "元" + "\n" + "底薪:" + this.getBaseSalary() + "元" ;
}
}
public class example {
public static void main (String[] arge) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个月份");
int month = sc.nextInt();
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) +"元");
}
}
}