我有这种方法可以计算申请状态为单身人士的税款。 但是我在尝试将其转换为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;
}
}
答案 0 :(得分:11)
这个怎么样?以更加面向对象的方式思考。我做了几个课程,但如果你明白了,你可以自己添加功能;)
我使用Decorator Pattern实现它。
通用界面。
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);
答案 1 :(得分:5)
考虑一下您的税率为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类。 :)
编辑:误解了代码的意图。我认为我的编辑应该做你想做的事。
答案 2 :(得分:3)
我喜欢上述方法,但我们建议您使用EnumMap代替agarrett's示例中的double[][]
进行调查。
答案 3 :(得分: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;
}
}