public static void main(String[] args) {
System.out.println("请输入当前月份");
Scanner sc = new Scanner(System.in);
int month =sc.nextInt();
System.out.println("请输入小时工工作时长");
int hour = sc.nextInt();
System.out.println("请输入销售人员本月销售额");
int sales = sc.nextInt();
Emoloyee [] aEmoloyees= {new SalariedEmployee("固定工资", 7),new HourlyEmployee("小时工资", 7, hour),new SalesEmployee("销售工资", 7, sales),new BasePlusSalesEmployee("有底薪销售工资", 7, sales)};
for (int i = 0; i < aEmoloyees.length; i++) {
aEmoloyees[i].getSalary(month);
}
}
}
abstract class Emoloyee{
//属性
protected String name;
protected int month;
//抽象方法
public abstract void getSalary(int month);
//封装
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;
}
}
//固定工资
class SalariedEmployee extends Emoloyee{
//属性
private double monthlyPay =10000;
//构造方法
public SalariedEmployee() {}
public SalariedEmployee(String name,int month) {
this.name = name;
this.month =month;
}
//实现方法
@Override
public void getSalary(int month) {
if (this.month == month) {
monthlyPay +=100;
}
System.out.println(name + "为"+monthlyPay);
}
//封装
public double getMonthlyPay() {
return monthlyPay;
}
public void setMonthlyPay(double monthlyPay) {
this.monthlyPay = monthlyPay;
}
}
//小时工
class HourlyEmployee extends Emoloyee{
//属性
private int hoursMoney;
private int hour;
private double wages;
//实现方法
@Override
public void getSalary(int month) {
if (hour<=160) {
wages = hour*100;
}else {
wages = 160*100+(hour-160)*150;
}
if (this.month == month) {
wages+=100;
}
System.out.println(name + "为"+wages);
}
//构造方法
public HourlyEmployee() {}
public HourlyEmployee(String name,int month,int hour) {
this.name = name;
this.month=month;
this.hour = hour;
}
//封装
public int getHoursMoney() {
return hoursMoney;
}
public void setHoursMoney(int hoursMoney) {
this.hoursMoney = hoursMoney;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public double getWages() {
return wages;
}
public void setWages(double wages) {
this.wages = wages;
}
}
//销售工资
class SalesEmployee extends Emoloyee{
//属性
protected int sales ;
protected double commission=0.5;
protected double wages;
//实现方法
@Override
public void getSalary(int month) {
wages = this.sales*commission;
if (this.month ==month) {
wages+=100;
}
System.out.println(name+"为"+wages);
}
//构造方法
public SalesEmployee() {}
public SalesEmployee(String name,int month,int sales) {
this.name=name;
this.month =month;
this.sales =sales;
}
//封装
public int getSales() {
return sales;
}
public void setSales(int sales) {
this.sales = sales;
}
public double getCommission() {
return commission;
}
public void setCommission(double commission) {
this.commission = commission;
}
public double getWages() {
return wages;
}
public void setWages(double wages) {
this.wages = wages;
}
}
//有底薪销售
class BasePlusSalesEmployee extends SalesEmployee{
//属性
private double monthlyPay =5000;
//构造方法
public BasePlusSalesEmployee() {}
public BasePlusSalesEmployee(String name,int month,int sales) {
this.name=name;
this.month =month;
this.sales =sales;
}
@Override
public void getSalary(int month) {
wages = this.monthlyPay+ this.sales*commission;
if (this.month ==month) {
wages+=100;
}
System.out.println(name+"为"+wages);
}
//封装
public double getMonthlyPay() {
return monthlyPay;
}
public void setMonthlyPay(double monthlyPay) {
this.monthlyPay = monthlyPay;
}
}
这是刚学习完面向对象知识做的题目,希望以后学习更多知识后可以对程序进行优化。