/** * 一家公司按星期支付员工的工资。 * 该公司有四种员工: * 拿薪水的 : 每个星期发给他固定的工资,而不计他们的工作小时数; * 钟 点 工: 按小时算,没有加班费; * 代 理 人: 按销售业绩按一定的比例抽取佣金. * 拿薪代理人: 报酬为基本工资 + 销售额的一定比例的佣金。 * 本月公司决定为拿薪金的代理增加10%的基本工资。 * 公司希望写一个Java程序来动态的计算工资。 * @author CocoonFan * */ public class CalcSalary { public static void main(String[] args) { CalculateSalary calculateSalary = new CalculateSalary(); BaseSalaryWorker baseSalaryWorker = new BaseSalaryWorker("拿薪水的", 5000); calculateSalary.calcSalary(baseSalaryWorker); HourWorker hourWorker = new HourWorker("钟点工", 0, 300, 15); calculateSalary.calcSalary(hourWorker); ResultsWorker resultsWorker = new ResultsWorker("代理人", 0, 35000, 0.2); calculateSalary.calcSalary(resultsWorker); AgentWorker agentWorker = new AgentWorker("拿薪代理人", 3000, 40000, 0.15); calculateSalary.calcSalary(agentWorker); } } class Worker{ private String workerName;// private double baseSalary = 3000.0;//基本工资 public Worker(String workerName, double baseSalary) { this.workerName = workerName; this.baseSalary = baseSalary; } public String getWorkerName() { return workerName; } public void setWorkerName(String workerName) { this.workerName = workerName; } public double getBaseSalary() { return baseSalary; } public void setBaseSalary(double baseSalary) { this.baseSalary = baseSalary; } public void calcSalary(Worker worker){ System.out.println("计算工资"); } } class BaseSalaryWorker extends Worker{ public BaseSalaryWorker(String workerName, double baseSalary) { super(workerName, baseSalary); } public void calcSalary(Worker worker){ System.out.println(super.getWorkerName() + "本月工资为:" + super.getBaseSalary()); } } class HourWorker extends Worker{ private int workHour; private double salaryPerHour; public HourWorker(String workerName, double baseSalary, int workHour,double salaryPerHour) { super(workerName, baseSalary); this.workHour = workHour; this.salaryPerHour = salaryPerHour; } @Override public void calcSalary(Worker worker){ System.out.println(super.getWorkerName() + "本月工资为:" + workHour*salaryPerHour); } } class ResultsWorker extends Worker{ private double results;//业绩 private double ratio;//比例 public ResultsWorker(String workerName, double baseSalary,double results,double ratio) { super(workerName, baseSalary); this.results = results; this.ratio = ratio; } @Override public void calcSalary(Worker worker){ System.out.println(super.getWorkerName() + "本月工资为:" + results*ratio); } } class AgentWorker extends Worker{ private double results;//业绩 private double ratio;//比例 public AgentWorker(String workerName, double baseSalary,double results,double ratio) { super(workerName, baseSalary); this.results = results; this.ratio = ratio; } @Override public void calcSalary(Worker worker){ System.out.println(super.getWorkerName() + "本月工资为:" + (results*ratio + super.getBaseSalary()*1.1)); } } class CalculateSalary { public void calcSalary(Worker worker){ System.out.println("正在计算工资..."); worker.calcSalary(worker); System.out.println(); } }
运行结果