简体   繁体   中英

Having trouble with a nested if statement

Theory is that different tiers of income are taxed percentage wise differently. 10% first 50k, 15% next 50k, 25% over 100k.

 public double getTaxesWithheld() {

    if (taxableIncome >= 100000.0) {

        taxesWitheld = taxesWitheld+ (.25 * (taxableIncome - 100000.0));
        taxableIncome = taxableIncome - 100000.0;
    } else {
        if (taxableIncome >= 50000.0 && taxableIncome <= 100000.0) {
            taxesWitheld = taxesWitheld + (.15 * (taxableIncome - 50000.0));
            taxableIncome = taxableIncome - 50000.0;
        } else {
            if (taxableIncome < 50000.0) {
                taxesWitheld = taxesWitheld + (.1 * (taxableIncome - 25000.0));
            }
        }

        if (taxableIncome <= 0) {
            return 0.0;
        }

    }
    return taxesWitheld;
}

test Case1: @Test Constructor values (first_name, last_name, job_title, id, monthly_salary)

public void EmployeeMakingBetween50Kand100K() {
    Employee h = new EmployeeImpl("Jon", "Smith", "Miner", 2222, 6166.75);


    assertEquals(h.getMonthlySalary(), 6166.75, 0.005);
    assertEquals(h.getGrossYearlyIncome(), 6166.75*12, 0.005);
    assertEquals(h.getTaxableIncome(), h.getGrossYearlyIncome(), 0.005);
    assertEquals(h.getTaxesWithheld(), 8600.15, 0.005);
    assertEquals(h.getNetYearlyIncome(), h.getGrossYearlyIncome()-h.getTaxesWithheld(), 0.005);
}

Will not process the taxesWithheld(), 8600.15, 0.005

Here's a totally different approach that might yield better results:

 public double getTaxesWithheld() {

    over100k = Math.max(taxableIncome-100000, 0);
    taxableIncome -= over100k;
    over50k = Math.max(taxableIncome-50000, 0);
    taxableIncome -= over50k;

    taxesWitheld = taxesWitheld + (.25 * over100k);
    taxesWitheld = taxesWitheld + (.15 * over50k);
    taxesWitheld = taxesWitheld + (.1 * taxableIncome);
    return taxesWitheld;
}

Your nesting is a bit wonky

if (taxableIncome >= 100000.0) {
    ...
} else if (taxableIncome >= 50000.0) { 
// you don't need if(<= 100000), it's implied since you already 
// know it's !(taxableIncome >= 1000000) from the first if statement
    ...
} else if (taxableIncome > 0) {
    ...
} else return 0; 
// again, you don't need if(<= 0) here, 
// it's implied since you know that !(taxableIncome > 0)

Shouldnt be an if else statement since after you subtract 100k still gotta tax the rest.

if (taxableIncome >= 100000.0) {
    taxesWitheld = taxesWitheld+ (.25 * (taxableIncome - 100000.0));
    taxableIncome = taxableIncome - 100000.0;
}

if (taxableIncome >= 50000.0 && taxableIncome <= 100000.0) {
        taxesWitheld = taxesWitheld + (.15 * (taxableIncome - 50000.0));
        taxableIncome = taxableIncome - 50000.0;
}

etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM