本文是 "
从薪水计算的例子看一段程序在不同环境中的变化 " 的续文.
如果需求发生如下变化:
如果说国家改变了公民福利制度,具体就是500元以下的每人补充300元,超过20000元的在原有基础上再扣除20%,请问该如何编程?
具体等级税率:
等级 税率 额定扣除数
不超过500元 5% 0 (低保每人补助300元)
大于500小于2000 10% 25
大于2000小于5000 15% 125
大于5000小于20000 20% 375
超过20000元 25% 1375(在计算完的基础上再乘以80%)
这时原有公式就不适用了,我们没有必要回到分支语句的老路上去,将计算公式归并成SalaryGrade的一个函数getSalaryAfterTax,然后从SalaryGrade分别扩展两个类HigherSalaryGrade和LowerSalaryGrade,然后分别实现各自的getSalaryAfterTax函数即可.
代码如下:
SalaryGrade类:
package com.sitinspring.oostyleadv;
/** *//**
* 工资等级类
* @author sitinspring(junglesong@gmail.com)
*
*/
public class SalaryGrade {
// 月薪界限
protected double grade;
// 税率
protected double ratio;
// 折扣
protected double discount;
public SalaryGrade(double grade, double ratio, double discount) {
this.grade = grade;
this.ratio = ratio;
this.discount = discount;
}
public double getSalaryAfterTax(double salary){
return salary*(1-ratio)-discount;
}
public SalaryGrade() {
this(0.0f, 0.0f, 0.0f);
}
public double getDiscount() {
return discount;
}
public double getGrade() {
return grade;
}
public double getRatio() {
return ratio;
}
}
HigherSalaryGrade类:
package com.sitinspring.oostyleadv;
/** *//**
* 超高收入工资等级类
* @author sitinspring(junglesong@gmail.com)
*
*/
public class HigherSalaryGrade extends SalaryGrade{
public HigherSalaryGrade(double grade, double ratio, double discount) {
super(grade,ratio,discount);
}
public double getSalaryAfterTax(double salary){
return (salary*(1-ratio)-discount)*0.8;
}
public HigherSalaryGrade() {
this(0.0f, 0.0f, 0.0f);
}
}
LowerSalaryGrade类:
package com.sitinspring.oostyleadv;
/** *//**
* 低保工资等级类
* @author sitinspring(junglesong@gmail.com)
*
*/
public class LowerSalaryGrade extends SalaryGrade{
public LowerSalaryGrade(double grade, double ratio, double discount) {
super(grade,ratio,discount);
}
public double getSalaryAfterTax(double salary){
return salary*(1-ratio)-discount+300;
}
public LowerSalaryGrade() {
this(0.0f, 0.0f, 0.0f);
}
}
SalaryCaculator类:
package com.sitinspring.oostyleadv;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/** *//**
* 工资计算类
* @author sitinspring(junglesong@gmail.com)
*
*/
public class SalaryCaculator{
private List<SalaryGrade> grades;
public SalaryCaculator(){
grades=new ArrayList<SalaryGrade>();
grades.add(new LowerSalaryGrade(500,0.05,0));
grades.add(new SalaryGrade(2000,0.1,25));
grades.add(new SalaryGrade(5000,0.15,125));
grades.add(new SalaryGrade(20000,0.20,375));
grades.add(new HigherSalaryGrade(99999999,0.25,1375));
}
// OO化的查询方法
public double getSalaryAfterTax(double total){
SalaryGrade taxGrade=null;
for(Iterator it=grades.iterator();it.hasNext();){
taxGrade=(SalaryGrade)it.next();
if(total>taxGrade.getGrade()){
continue;
}
else{
break;
}
}
return taxGrade.getSalaryAfterTax(total);
}
}
Main:
package com.sitinspring.oostyleadv;
/** *//**
* Main函数所在的类
* @author sitinspring(junglesong@gmail.com)
*
*/
public class Main{
public static void main(String[] args){
SalaryCaculator caculator=new SalaryCaculator();
for(double total=100;total<30000;total+=1000){
System.out.println("税前薪水="+total+" 税后薪水="+caculator.getSalaryAfterTax(total));
}
}
}
代码下载:
http://www.blogjava.net/Files/sitinspring/TaxCaculator20071115120519.rar