Pages

Saturday, 11 March 2017

Chapter 33 Exercise 3, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

33.3 (Calculate tax) Write a JSF page to let the user enter taxable income and filing status, as shown in Figure 33.27a. Clicking the Compute Tax button computes and displays the tax, as shown in Figure 33.27b. Use the computeTax method introduced in Listing 3.5, ComputeTax.java, to compute tax.


import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class Exercise03 {

    int status;
    double income;

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public double getIncome() {
        return income;
    }

    public void setIncome(double income) {
        this.income = income;
    }
    
    public double getTax() {
        return computetax(status, income);
    }

    private double computetax(int status, double income) {
        double tax = 0;

        if (status == 0) { // Compute tax for single filers
            if (income <= 8350) {
                tax = income * 0.10;
            } else if (income <= 33950) {
                tax = 8350 * 0.10 + (income - 8350) * 0.15;
            } else if (income <= 82250) {
                tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950)
                        * 0.25;
            } else if (income <= 171550) {
                tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950)
                        * 0.25 + (income - 82250) * 0.28;
            } else if (income <= 372950) {
                tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950)
                        * 0.25 + (171550 - 82250) * 0.28 + (income - 171550)
                        * 0.33;
            } else {
                tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950)
                        * 0.25 + (171550 - 82250) * 0.28 + (372950 - 171550)
                        * 0.33 + (income - 372950) * 0.35;
            }
        } else if (status == 1) { // Compute tax for married file jointly
            if (income <= 16700) {
                tax = income * 0.10;
            } else if (income <= 67900) {
                tax = 16700 * 0.10 + (income - 16700) * 0.15;
            } else if (income <= 137050) {
                tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (income - 67900)
                        * 0.25;
            } else if (income <= 208850) {
                tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (82250 - 67900)
                        * 0.25 + (income - 137050) * 0.28;
            } else if (income <= 372950) {
                tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (82250 - 67900)
                        * 0.25 + (208850 - 137050) * 0.28 + (income - 208850)
                        * 0.33;
            } else {
                tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (82250 - 67900)
                        * 0.25 + (208850 - 137050) * 0.28 + (372950 - 208850)
                        * 0.33 + (income - 372950) * 0.35;
            }
        } else if (status == 2) { // Compute tax for married separately
            if (income <= 8350) {
                tax = income * 0.10;
            } else if (income <= 33950) {
                tax = 8350 * 0.10 + (income - 8350) * 0.15;
            } else if (income <= 68525) {
                tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950)
                        * 0.25;
            } else if (income <= 104425) {
                tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950)
                        * 0.25 + (income - 68525) * 0.28;
            } else if (income <= 186475) {
                tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950)
                        * 0.25 + (104425 - 68525) * 0.28 + (income - 104425)
                        * 0.33;
            } else {
                tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950)
                        * 0.25 + (104425 - 68525) * 0.28 + (186475 - 104425)
                        * 0.33 + (income - 186475) * 0.35;
            }
        } else if (status == 3) { // Compute tax for head of household
            if (income <= 11950) {
                tax = income * 0.10;
            } else if (income <= 45500) {
                tax = 11950 * 0.10 + (income - 11950) * 0.15;
            } else if (income <= 117450) {
                tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (income - 45500)
                        * 0.25;
            } else if (income <= 190200) {
                tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500)
                        * 0.25 + (income - 117450) * 0.28;
            } else if (income <= 372950) {
                tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500)
                        * 0.25 + (190200 - 117450) * 0.28 + (income - 190200)
                        * 0.33;
            } else {
                tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500)
                        * 0.25 + (190200 - 117450) * 0.28 + (372950 - 190200)
                        * 0.33 + (income - 372950) * 0.35;
            }
        } else {
            throw new IllegalArgumentException("Invalid status");
        }
        return tax;
    }
}

<?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://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Exercise03</title>
    </h:head>
    <h:body>
        <h:form>
            <center>
                <h3>
                    Compute Tax
                </h3>
            
                <h:panelGrid columns="2">
                    <h:outputLabel value="Taxable income"/>
                    <h:inputText id="income" value="#{exercise03.income}"/>

                </h:panelGrid>
                <br />
                <h:panelGrid columns="2">
                    <h:outputLabel value="Filing status "/>
                    <h:selectOneMenu id="status" value="#{exercise03.status}">
                        <f:selectItem itemValue="0" itemLabel="single filer"/>
                        <f:selectItem itemValue="1" itemLabel="married jointly or qualifying widow(er)"/>
                        <f:selectItem itemValue="2" itemLabel="married separately"/>
                        <f:selectItem itemValue="3" itemLabel="head of household"/>
                    </h:selectOneMenu>
                </h:panelGrid>
                <br />
                <h:commandButton value="Compute Tax" />
                <br /><br />
                <h:outputText escape="false" style="color:red" value="#{exercise03.tax}" />
            </center>
        </h:form>
    </h:body>
</html>

No comments :

Post a Comment