2

我有这种方法可以计算申请状态为单身的人的应缴税款。但是我在尝试将其转换为 for 循环或使用 Array 而不是 while 循环时遇到了麻烦,因为这段代码变得非常长而且看起来很讨厌。我想通过简化代码让它看起来很漂亮。有什么建议么?

public void calculateTax()
    {
        //The constant fields for the tax rate
        final double TAXRATE_10 = 0.1;     //10%
        //This is the tax rate percent on the tax 15%
        final double TAXRATE_15PERCENT = 0.15;
        //This is the tax rate percent on the tax 25%
        final double TAXRATE_25PERCENT = 0.25;
        //This is the tax rate percent on the tax 28%
        final double TAXRATE_28PERCENT = 0.28;
        //This is the tax rate percent on the tax 33%
        final double TAXTRATE_33PERCENT = 0.33;
        //This is the tax rate percent on the tax 35%
        final double TAXRATE_35PERCENT = 0.35;

        //constant numbers for tax boundaries.
        final int NOTRICH = 8700;
        final int MIDDLECLASS = 35350;
        final int SORTOFRICH = 85650;
        final int RICH = 178650;
        final int FORSURERICH = 388350;

        //Variables for taxable income, and tax calculation.
        long taxableIncome = income - deduction -(numberOfExemption * VAlUEOFEXEMPTION);
        double cumulatedTax = 0;

        //Calculate the Tax
        while(taxableIncome != 0)
        {
            if(taxableIncome > FORSURERICH)
            {
                cumulatedTax += ((taxableIncome-FORSURERICH) * TAXRATE_35PERCENT);
                taxableIncome = (long)FORSURERICH;
            }
            else if(taxableIncome > RICH)
            {
                cumulatedTax += ((taxableIncome-RICH) * TAXTRATE_33PERCENT);
                taxableIncome = (long)RICH;
            }
            else if(taxableIncome > SORTOFRICH)
            {
                cumulatedTax += ((taxableIncome-SORTOFRICH) * TAXRATE_28PERCENT);
                taxableIncome = (long)SORTOFRICH;
            }
            else if(taxableIncome > MIDDLECLASS)
            {
                cumulatedTax += ((taxableIncome-MIDDLECLASS) * TAXRATE_25PERCENT);
                taxableIncome = (long)MIDDLECLASS;
            }
            else if(taxableIncome > NOTRICH)
            {
                cumulatedTax += ((taxableIncome-NOTRICH) * TAXRATE_15PERCENT);
                taxableIncome = (long)NOTRICH;
            }
            else
            {
                cumulatedTax += ((taxableIncome) * TAXRATE_10);
                taxableIncome = 0;
            }
        }
4

4 回答 4

12

这个怎么样?以更面向对象的方式思考。我做了几节课,但如果你明白了,你可以自己添加功能;)

我使用装饰器模式来实现它。

通用接口。

public interface TaxCalculator {

    Double calculate(Double tax);

}

所有税收的基础计算。

public class TaxCalculatorBase implements TaxCalculator{

    @Override
    public Double calculate(Double tax) {
        return  tax * TAXRATE_10;
    }

}

装饰器抽象类

public abstract class TaxCalculatorDecorator implements TaxCalculator{

    private final TaxCalculator decoratee;

    /**
     * @param decoratee
     */
    public TaxCalculatorDecorator(TaxCalculator decoratee) {
        super();
        this.decoratee = decoratee;
    }

    @Override
    public Double calculate(Double tax) {
        Double returnValue = decoratee.calculate(tax); 
        return taxCalculate(returnValue);
    }

    protected abstract Double taxCalculate(Double tax);


}

和装饰器的具体类。我只做了2个例子

public class NotRichTaxCalculator extends TaxCalculatorDecorator{

    public NotRichTaxCalculator(TaxCalculator taxCalculator) {
            super(taxCalculator);
    }

@Override
protected Double taxCalculate(Double tax) {
    return ((tax-NOTRICH) * TAXRATE_15PERCENT);
}

}

一种丰富的税收计算器

public class SortOfRichTaxCalculator extends TaxCalculatorDecorator{

    public SortOfRichTaxCalculator(TaxCalculator decoratee) {
        super(decoratee);
    }

    @Override
    protected Double taxCalculate(Double cumulatedTax) {
        return  ((cumulatedTax-SORTOFRICH) * TAXRATE_28PERCENT);;
    }



}

和一个简单的工厂来创建对象

public final class TaxCalculatorFactory {

    private TaxCalculatorFactory(){}

    public static TaxCalculator create(Double taxableIncome){

        TaxCalculator taxCalculator= null;      

          if(taxableIncome > SORTOFRICH)
         {
             taxCalculator = new SortOfRichTaxCalculator(new NotRichTaxCalculator(new TaxCalculatorBase()));
         }else if(taxableIncome > NOTRICH)
         {
             taxCalculator = new NotRichTaxCalculator(new TaxCalculatorBase());
         }
         else
         {
             taxCalculator =new TaxCalculatorBase();
         }

        return taxCalculator;
    }
}

然后在客户端代码中你只需要写这个。

TaxCalculator taxCalculator= TaxCalculatorFactory.create(tax);
Double acumulatedTaxes = taxCalculator.calculate(tax);
于 2013-06-08T01:53:11.290 回答
6

考虑一下您的税级与您的税率是 1:1 的事实。

final double[][] BRACKETS = {
    {388350.0, 0.35},
    {178650.0, 0.33},
    {85650.0, 0.28},
    {35350.0, 0.25},
    {8700.0, 0.15}
};

/* ... */

for (double[] bracket: BRACKETS) {
    if(taxableIncome > bracket[0]) {
        cumulatedTax += ((taxableIncome-bracket[0]) * bracket[1]);
        taxableIncome = (long)bracket[0];
    }
}
cumulatedTax += taxableIncome * 0.1;
taxableIncome = 0;

如果你真的喜欢 C 风格的 ALLCAPSCONSTANTS,那么请随意声明它们,并且只需使用它们的名称而不是我在double[][]. 如果您想使用 Java 原生,请定义一个 TaxBracket 类。:)

编辑:误解了您的代码的意图。我认为我的编辑应该做你想要的。

于 2013-06-08T01:14:36.297 回答
3

我喜欢上面的方法,但我鼓励您使用EnumMap而不是agarrett 的示例double[][]进行调查。

于 2013-06-08T01:28:52.580 回答
2

这个怎么样?

List<Integer> taxSlabs = new ArrayList<Integer>();
taxSlabs.put(388350); 
taxSlabs.put(178650); 
taxSlabs.put(85650); 
taxSlabs.put(35350); 
taxSlabs.put(8700); 

List<Double> taxRates = new ArrayList<Double>();
taxRates.put(0.35); 
taxRates.put(0.33); 
taxRates.put(0.28); 
taxRates.put(0.25); 
taxRates.put(0.15);


//Variables for taxable income, and tax calculation.
long taxableIncome = income - deduction -(numberOfExemption * VAlUEOFEXEMPTION);
double cumulatedTax = 0.0; 


for(int indx = 0; indx < taxSlabs.size(); indx++){
    int slabLimit = taxSlabs.get(indx).intValue(); 
    if(taxableIncome >=  slabLimit ){
        cumulatedTax += ((taxableIncome% slabLimit) * taxRates.get(indx).doubleValue());
        taxableIncome = (long)slabLimit;
    }
}
于 2013-06-08T01:25:06.327 回答