Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java while loop conversion.

Tags:

java

I have this method that computes taxes due for a person with a filing status of single. But I'm having trouble trying to convert this to a for loop or using an Array instead of a while loop because this code becomes really long and nasty looking. I want to make it look pretty by simplifying the code. Any suggestions?

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;
            }
        }
like image 574
blah Avatar asked Jul 24 '26 06:07

blah


1 Answers

How about this? thinking in a more object oriented way. I did it for a few classes but you can add functionality by yourrself if you get the idea ;)

I implement it using the Decorator Pattern.

The common interface.

public interface TaxCalculator {

    Double calculate(Double tax);

}

the base calculation for all taxes.

public class TaxCalculatorBase implements TaxCalculator{

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

}

The decorator abstract class

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


}

and the decorators concrete classes. I only did 2 as an example

public class NotRichTaxCalculator extends TaxCalculatorDecorator{

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

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

}

Sort of rich tax calculator

public class SortOfRichTaxCalculator extends TaxCalculatorDecorator{

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

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



}

and a simple Factory to create objects

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

then in client code you only have to write this.

TaxCalculator taxCalculator= TaxCalculatorFactory.create(tax);
Double acumulatedTaxes = taxCalculator.calculate(tax);
like image 109
nachokk Avatar answered Jul 25 '26 19:07

nachokk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!