0 Replies - 722 Views - Last Post: 27 September 2019 - 06:03 PM

#1 kaiser7511   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 03-March 19

Disappearing Data - Java EE

Posted 27 September 2019 - 06:03 PM

I am trying to write a JSF page that lets a user enter taxable income and filing status.

I am having issues getting output data. I tried debugging and I know the taxable income is getting passed through the setter, but the getter function is returning a 0 value. I know the value in taxable income is getting reset to 0 somewhere in the code, but I cant seem to find where it's happening. Any help would be greatly appreciated.

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    
    <h:head> 
        <title>Compute Income Tax</title>
    </h:head>
    <h:body>
        <h:form>
            <h3>Compute Tax</h3>
            <h:panelGrid columns="2" style="color:black">
                <h:outputLabel value="Taxable Income: "/>
                <h:inputText id="taxableIncome" value="#{calculateTaxBean.taxableIncome}"></h:inputText>
                <h:outputLabel value="Filing Status: "/>
                <h:selectOneMenu id="filingStatus" value="#{calculateTaxBean.filingStatus}">
                    <f:selectItem itemLabel="Single" itemValue="0" />
                    <f:selectItem itemLabel="Married, Filing Jointly" itemValue="1" />
                    <f:selectItem itemLabel="Married, Filing Separately" itemValue="2" />
                    <f:selectItem itemLabel="Head of Household" itemValue="3" />
                </h:selectOneMenu>
            </h:panelGrid>
            
            <h:commandButton value="Compute Tax" />
            
            <br />
            <h:outputText escape="false" style="color:red"
                          value="#{calculateTaxBean.response}"/>
            
        </h:form>
    </h:body>
</html>



/*
 * class to use for computing tax
 */
package computeincometax;

public class ComputeTax {

    public static double computeTax(int status, double income) {
        double[] rates = {0.10, 0.15, 0.27, 0.30, 0.35, 0.386};

        int[][] brackets = {
            {6000, 27950, 67700, 141250, 307050}, // Single filer
            {12000, 46700, 112850, 171950, 307050}, // Married jointly
            {6000, 23350, 56425, 85975, 153525}, // Married separately
            {10000, 37450, 96700, 156600, 307050} // Head of household
        };

        double tax = 0; // Tax to be computed

        // Compute tax in the first bracket
        if (income <= brackets[status][0]) {
            return tax = income * rates[0]; // Done
        } else {
            tax = brackets[status][0] * rates[0];
        }

        // Compute tax in the 2nd, 3rd, 4th, and 5th brackets, if needed
        for (int i = 1; i < brackets[0].length; i++) {
            if (income > brackets[status][i]) {
                tax += (brackets[status][i] - brackets[status][i - 1])
                        * rates[i];
            } else {
                tax += (income - brackets[status][i - 1]) * rates[i];
                return tax; // Done
            }
        }

        // Compute tax in the last (i.e., 6th) bracket
        return tax += (income - brackets[status][4]) * rates[5];
    }

}



/*
 * CalculateTaxBean
 */
package computeincometax;

import java.text.NumberFormat;
import java.util.Locale;
import javax.inject.Named;
import javax.enterprise.context.Dependent;

@Named(value = "calculateTaxBean")
@Dependent
public class CalculateTaxBean {

    private double taxableIncome;
    private int filingStatus;

    /**
     * Creates a new instance of CalculateTaxBean
     */
    public CalculateTaxBean() {
    }

    //getters and setters
    public double getTaxableIncome() {
        return taxableIncome;
    }

    public void setTaxableIncome(double taxableIncome) {
        this.taxableIncome = taxableIncome;
    }

    public int getFilingStatus() {
        return filingStatus;
    }

    public void setFilingStatus(int filingStatus) {
        this.filingStatus = filingStatus;
    }

    public String getResponse() {
        if (taxableIncome == 0) {
            return "";
        }

        NumberFormat currencyForm = NumberFormat.getCurrencyInstance(Locale.getDefault());

        String s = "Taxable Income: " + currencyForm.format(taxableIncome) + "<br>"
                + "Filing Status: " + filingStatus + "<br>"
                + "<font color=\"#FF0000\">"
                + "Tax: " + currencyForm.format(ComputeTax.computeTax(filingStatus, taxableIncome))
                + "</font>";

        return s;
    }
}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1