gpt4 book ai didi

Java while 循环转换。

转载 作者:行者123 更新时间:2023-12-02 10:58:15 25 4
gpt4 key购买 nike

我有这种方法可以计算单例申报身份的人应缴的税款。但我在尝试将其转换为 for 循环或使用数组而不是 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;
}
}

最佳答案

这个怎么样?以更加面向对象的方式思考。我在几个类(class)中做到了这一点,但如果您有想法,您可以自己添加功能;)

我使用 Decorator Pattern 来实现它.

通用接口(interface)。

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);

关于Java while 循环转换。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16994957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com