我在java中有两个类来计算抵押贷款和每月付款:
第一个是:
public class Loan {
private double mortgageAmount;
private double annualInterestRate;
private int amortizationPeriod;
//Setter and getter here
public Loan(double mortgageAmount, double annualInterestRate, int amortizationPeriod) {
super();
this.mortgageAmount = mortgageAmount;
this.annualInterestRate = annualInterestRate;
this.amortizationPeriod = amortizationPeriod;
getMonthlyPayment();
getLoanScheduleArray();
}
public double getMonthlyPayment() {
// TODO Auto-generated method stub
double monthlyPayment;
monthlyPayment = roundTo2Decimal(mortgageAmount * (Math.pow((1 + (annualInterestRate) / 200), (double) 1 / 6) - 1)
/ (1 - Math.pow(Math.pow((1 + (annualInterestRate) / 200), (double) 1 / 6), -12 * amortizationPeriod)));
return monthlyPayment;
}
public Loan() {
super();
}
public LoanSchedule[] getLoanScheduleArray() {
LoanSchedule[] loanScheduleArray = new LoanSchedule[amortizationPeriod * 12];
double remainingBalance = mortgageAmount;
for (int index = 0; index < (amortizationPeriod * 12); index++) {
int paymentNumber = index + 1;
if (paymentNumber < amortizationPeriod * 12) {
double monthlyPercentageRate = (Math.pow(1 + annualInterestRate / 200, (double) 1 / 6) - 1);
double interestPaid = roundTo2Decimal(remainingBalance * monthlyPercentageRate);
double principalPaid = roundTo2Decimal(getMonthlyPayment() - interestPaid);
remainingBalance = roundTo2Decimal(remainingBalance - principalPaid);
LoanSchedule loanSchedule = new LoanSchedule();
loanSchedule.setPaymentNumber(paymentNumber);
loanSchedule.setInterestPaid(interestPaid);
loanSchedule.setPrincipalPaid(principalPaid);
loanSchedule.setRemainingBalance(remainingBalance);
loanScheduleArray[index] = loanSchedule;
}
}
return loanScheduleArray;
}
}
第二个是
public class LoanSchedule {
//getter and setter here
public LoanSchedule(int paymentNumber, double interestPaid, double principalPaid, double remainingBlance) {
super();
}
public LoanSchedule() {
super();
}
}
现在我想要打印出每个月支付的利息,本金和剩余余额,所以我在java servlet中有这些代码,但它不会在数组中打印任何内容。我哪里错了?
for (int index = 0; index < (intAmortizationPeriod * 12); index++) {
LoanSchedule loanSchedule = loanScheduleArray[index];
response.getWriter().println(loanSchedule);
}
这是我的servlet代码:
public class LoanServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoanServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
response.setContentType("text/html");
String mortgageAmount = request.getParameter("mortgageAmount");
String annualInterestRate = request.getParameter("annualInterestRate");
String amortizationPeriod = request.getParameter("amortizationPeriod");
double dbMortgageAmount = Double.parseDouble(mortgageAmount);
double dbAnnualInterestRate = Double.parseDouble(annualInterestRate);
int intAmortizationPeriod = Integer.parseInt(amortizationPeriod);
int errorCount = 0;
StringBuffer errors = new StringBuffer("Please fix the following issue(s):");
errors.append("<ul class=\"bg-danger\">");
if (mortgageAmount.isEmpty()) {
errors.append("<li>Mortgage amount is required.</li>");
errorCount++;
}
if (annualInterestRate.isEmpty()) {
errors.append("<li>Annual interes trate is required.</li>");
errorCount++;
}
if (amortizationPeriod.isEmpty()) {
errors.append("<li>Amortization period is required.</li>");
errorCount++;
}
errors.append("</ul>");
if (errorCount == 0) {
//double taxableIncome = Double.parseDouble( taxableIncomeString );
Loan loanbean = new Loan(dbMortgageAmount, dbAnnualInterestRate, intAmortizationPeriod);
loanbean.getMonthlyPayment();
LoanSchedule[] loanScheduleArray = loanbean.getLoanScheduleArray();
String htmlResponseText = "Your monthly payment is <strong>" + loanbean.getMonthlyPayment() + "</strong>.";
response.getWriter().println(htmlResponseText);
for (int index = 0; index < (intAmortizationPeriod * 12); index++) {
LoanSchedule loanSchedule = loanScheduleArray[index];
response.getWriter().println(loanSchedule.toString());
}
} else {
response.getWriter().println(errors.toString());
}
}
private void println(Object setPaymentNumber) {
// TODO Auto-generated method stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
答案 0 :(得分:0)
为什么不运行loanScheduleArray.size()
一段时间(在for循环中)?
答案 1 :(得分:0)
我刚刚发现我的servlet类中应该有这样的代码:
for (int index = 0;index < (intAmortizationPeriod * 12 ); index++) {
LoanSchedule loanSchedule = loanScheduleArray[index];
response.getWriter().println(loanSchedule.getPaymentNumber());
response.getWriter().println(loanSchedule.getInterestPaid());
response.getWriter().println(loanSchedule.getPrincipalPaid());
response.getWriter().println(loanSchedule.getRemainingBalance());
}
但结果显得太糟糕了。我怎样才能把它安排到桌子上以便更好看?
答案 2 :(得分:0)
将一个toString方法添加到LoanSchedule
public String toString() {
return "paymentNumber=" + paymentNumber + ","
+ " interestPaid="
+ interestPaid + ", principalPaid=" +
principalPaid+ ", remainingBlance="
+ remainingBlance;
}
并更新servlet中的代码,如下所示
StringBuffer sb = new StringBuffer("<table>");
for (int index = 0; index < (intAmortizationPeriod * 12); index++) {
LoanSchedule loanSchedule = loanScheduleArray[index];
sb.append("<tr><td>");
sb.append(loanSchedule.toString());
sb.append("</td></tr>");
}
sb.append("</table>");
response.getWriter().println(sb.toString());
You can still format the toString method as needed