Pages

Saturday, 11 March 2017

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

33.4 (Calculate loan) Write a JSF page that lets the user enter loan amount, interest rate, and number of years, as shown in Figure 33.28a. Click the Compute Loan Payment button to compute and display the monthly and total loan payments, as shown in Figure 33.28b. Use the Loan class given in Listing 10.2, Loan.java, to compute the monthly and total payments.


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

@ManagedBean
@RequestScoped
public class Exercise04 {
    private double annualInterestRate;
    private int numberOfYears;
    private double loanAmount;
    private java.util.Date loanDate;

    public Exercise04() {
        this(10, 1, 1000);
    }

    public Exercise04(double annualInterestRate, int numberOfYears, double loanAmount) {
        this.annualInterestRate = annualInterestRate;
        this.numberOfYears = numberOfYears;
        this.loanAmount = loanAmount;
        loanDate = new java.util.Date();
    }

    public double getAnnualInterestRate() {
        return annualInterestRate;
    }

    public void setAnnualInterestRate(double annualInterestRate) {
        this.annualInterestRate = annualInterestRate;
    }

    public int getNumberOfYears() {
        return numberOfYears;
    }

    public void setNumberOfYears(int numberOfYears) {
        this.numberOfYears = numberOfYears;
    }

    public double getLoanAmount() {
        return loanAmount;
    }

    public void setLoanAmount(double loanAmount) {
        this.loanAmount = loanAmount;
    }

    public double getMonthlyPayment() {
        double monthlyInterestRate = annualInterestRate / 1200;
        double monthlyPayment = loanAmount * monthlyInterestRate / (1
                - (1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12)));
        return monthlyPayment;
    }

    public double getTotalPayment() {
        double totalPayment = getMonthlyPayment() * numberOfYears * 12;
        return totalPayment;
    }

    public java.util.Date getLoanDate() {
        return loanDate;
    }
    
    public String getInput() {
        return "<p style=\"color:red\"><br />"
        + "Loan Amount: " + loanAmount + "<br />"
        + "Annual Interest Rate: " + annualInterestRate + "<br />"
        + "Number Of Years: " + numberOfYears + "<br />"
        + "Monthly Payment: " + getMonthlyPayment() + "<br />"
        + "Total Payment: " + getTotalPayment() + "</p>";
    }
}   

<?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">
    <h:head>
        <title>Exercise04</title>
    </h:head>
    <h:body>
        <h:form>
            <center>
                <h3>
                    Compute Loan Payment
                </h3>
                <h:panelGrid columns="2">
                    <h:outputLabel value="Loan Amount"/>
                    <h:inputText id="amount" value="#{exercise04.loanAmount}"/>
                    <h:outputLabel value="Annual Interest Rate"/>
                    <h:inputText id="rate" value="#{exercise04.annualInterestRate}"/>
                    <h:outputLabel value="Number Of Years"/>
                    <h:inputText id="years" value="#{exercise04.numberOfYears}"/>
                </h:panelGrid>
                <br />
                <h:commandButton value="Compute Loan Payment" action = "Exercise04_2"/>
            </center>
        </h:form>
    </h:body>
</html>
Exercise04_2

<?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">
<?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">
    <h:head>
        <title>Exercise02</title>
    </h:head>
    <h:body>
        <center>
            <h:outputText escape="false" value="#{exercise02.table}" />
        </center>
    </h:body>
</html>

No comments :

Post a Comment