<%@ Page Language="C#" %> <% string reply = ""; // don't know how to test if Request.QueryString["name"] is null in ASP.Net... /* ---- no query parameters were provided, redirect client to "loan.html" if(String.Compare(Request.QueryString["name"], "")!=0) { // don't know how to return headers and body in ASP.Net... reply += "HTTP/1.1 302 Found\r\nContent-type:text/html\r\n" + "Location: loan.html\r\n\r\n" + "" + "Redirect" + "Click HERE for redirect."; } else ---- if we have query parameters, we process a GET/POST form */ { string szName = ""; string[] Months= new string[] {"January","February","March","April","May","June", "July", "August","September","October","November","December"}; double amount, rate, term, payment, interest, principal, cost; int month = 0, year = 1, lastpayment = 1; // the form field "names" we want to find values for string Name = "-", Amount = "0", Rate = "0", Term = "0"; DateTime start = DateTime.Now; // get the form field values (note the ending '=' name delimiter) Name = Request.QueryString["name"]; Amount = Request.QueryString["amount"]; Rate = Request.QueryString["rate"]; Term = Request.QueryString["term"]; // all litteral strings provided by a client must be escaped this way // if you inject them into an HTML page szName = HttpUtility.HtmlEncode(Name); // filter input data to avoid all the useless/nasty cases amount = Double.Parse(Amount); if(amount < 1) amount = 1; rate = Double.Parse(Rate); if(rate > 19) rate = 19; else if(rate > 1) rate /= 100; else if(rate < 1) rate = 1 / 100; term = Double.Parse(Term); if(term < 0.1) term = 1 / 12; else if(term > 800) term = 800; // calculate the monthly payment amount payment = amount * rate / 12 * Math.Pow(1 + rate / 12, term * 12) / (Math.Pow(1 + rate / 12, term * 12) - 1); cost = (term * 12 * payment) - amount; // build the top of our HTML page reply += "" + "Loan Calculator" + "" + "

Dear "; if(szName != "" && szName != "-") reply += szName; else reply += "client"; reply += ", your loan goes as follows:

"; if(term >= 1) term = Convert.ToInt32(term); else term = Math.Ceiling(12 * term); reply += "
" + "" + String.Format("", amount) + String.Format("", rate * 100) + String.Format("" + String.Format("
loandetails
Amount{0:n}
Rate{0:n}%
Term{0:n} ", term); if(term >= 1) reply += "year"; else reply += "month"; reply += "(s)
Cost{0:n}", cost) + String.Format(" ({0:n}%)
", 100 / (amount / cost)); reply += "
" + String.Format("
YEAR {0:d}", year); reply += "
" + "" + ""; for(;;) // output monthly payments { month++; interest = (amount * rate) / 12; if(amount > payment) { amount = (amount - payment) + interest; principal = payment - interest; } else // calculate last payment { if(lastpayment > 0) { lastpayment = 0; payment = amount; principal = amount-interest; amount = 0; } else // all payments are done, just padd the table { amount = 0; payment = 0; interest = 0; principal = 0; } } reply += String.Format("", month & 1) + "" + String.Format("", payment) + String.Format("", interest) + String.Format("", principal) + String.Format("", amount); if(month == 12) { if(amount > 0) { month = 0; year++; reply += "
monthpaymentinterestprincipalbalance
" + Months[month - 1] + "{0:n}{0:n}{0:n}{0:n}

" + "
YEAR " + year + "" + "
" + "" + ""; } else break; } } TimeSpan elapsed = DateTime.Now - start; // not counting code below // time the process and close the HTML page reply += "
monthpaymentinterestprincipalbalance

This page was generated in " + elapsed.TotalMilliseconds + " milliseconds.
(on a" + " 3GHz CPU 1 ms = 3,000,000 cycles)
"; } Response.Write(reply); %>