要求:公司将职员分为三类:部门经理、技术员和销售员。在发工资的时候,部门经理拿固定月薪8000元,技术人员按每小时100元领取月薪,销售人员按照500元底薪加当月销售额的4%进行提成,设计并实现一个工资结算系统。
思路:先创建一个所有员工共有属性和方法的员工类,在创建继承了员工类的经理、技术员和销售员子类,实现调用一种方法就能根据类得到不同结果。
代码如下:
package mt.wage;
/**
* 员工类
* @author mt
*
*/
public abstract class Employee {
protected String name;
protected String pasition;
public abstract int getSalary() ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return pasition;
}
public void setDepartment(String department) {
this.pasition = department;
}
}
package mt.wage;
/**
* 经理类
* @author mt
*
*/
public class Manager extends Employee {
public Manager(String name) {
this .name = name;
this.pasition = "经理";
}
@Override
public int getSalary() {
return 8000;
}
}
package mt.wage;
/**
* 技术员类
* @author mt
*
*/
public class Programmer extends Employee {
private int workTime;
public Programmer(String name , int workTime) {
this.name = name;
this.workTime = workTime;
this.pasition = "技术员";
}
public int getWorkTime() {
return workTime;
}
public void setWorkTime(int workTime) {
this.workTime = workTime;
}
@Override
public int getSalary() {
return 100 * workTime;
}
}
package mt.wage;
/**
* 销售员类
* @author mt
*
*/
public class Salesman extends Employee {
private int sales;
public Salesman(String name ,int sales) {
this.name = name;
this.sales = sales;
this.pasition = "销售员";
}
@Override
public int getSalary() {
return 1200 + sales * 5/100;
}
}
package mt.wage;
public class Test {
public static void main(String[] args) {
Employee[] em ={
new Manager("张三"),
new Programmer("李斯", 100),
new Salesman("王五", 5000),
new Programmer("赵钱", 50),
new Salesman("孙犁", 10000),
new Salesman("麻子", 8000)
};
for (Employee x :em) {
System.out.println(x.pasition + x.name
+ "的"+ "工资为:" + x.getSalary());
}
}
}