WMLScript Primer: The Mortgage Example | WebReference

WMLScript Primer: The Mortgage Example


WMLScript Primer

The Mortgage Example

In our previous column, we showed you the currency.wml and currency.wmls files. Let's examine another example provided by Nokia's WAP Toolkit, mortgage.wml and mortgage.wmls. Please refer to our previous column on how to download Nokia's WAP Toolkit and how to run WML decks with it. Here we show just the source code of mortgage.wml, mortgage.wmls, and the bytecode version of mortgage.wml. The size of mortgage.wml is 1071 bytes:

<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
"https://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
  <card id="card1" title="Mortgage Calc" newcontext="true">
  <p>
      Principal: <input format="*N" name="principal"
       title="Principal:" value="150000"/>
      <br/>
      Interest:  <input format="*N" name="interest"
       title="Interest:" value="8.00"/>
      <br/>
      Num payments:<input format="*N" name="num_payments"
       title="Num. payments:" value="360"/>
      <br/>
      Payment = <u>$(payment)</u>
      <do type="accept" label="Calculate">
        <go href="mortgage.wmls#payment('payment','$(principal)',
         '$(interest)','$(num_payments)')"/>
  // (The two lines above should be joined as one line.
  // They have been split for formatting purposes.)
      </do>
      <do type="help" label="Help">
        <go href="#help"/>
      </do>
      </p>
  </card>
  <card id="help" title="Help">
    <p>
      <u>Principal</u> - $$ amount<br/>
      <u>Interest</u> - e.g. 7.5<br/>
      <u>Payments</u> - e.g. 360 for 30 years<br/>
      <do type="prev" label="Back">
        <prev/>
      </do>
      </p>
  </card>
</wml>

We have colored in red the call to the WMLScript function:


mortgage.wmls#payment('payment','$(principal)',
 '$(interest)','$(num_payments)')

The function payment() is defined in a different compilation unit, mortgage.wmls. The function accepts four parameters. The first one, payment, is the WML's variable which is to be updated by the WMLScript function. The definition of this variable in the caller WML code is colored in red above as well. The other three parameters, principal, interest, and num_payments, are passing values from the caller WML deck to the WMLScript function. Here is how this WMLS function is defined in mortgage.wmls:

/*
 * Calculate a mortgage's payment
 *
 *@param varname the variable name to store the result
 *@param principal the principal
 *@param interest the interest rate
 *@param num_payments the number of payments
 *@return the payment
 */
extern function payment(varname, principal, interest, num_payments) {
	/*
	 * Interest formulae:
	 *
	 * If (i != 0), then:
	 *  pmt = principal * [i * (1+i)^n / ((1+i)^n - 1)]
	 *
	 * If (i == 0), then:
	 *  pmt = principal / n
	 */
    var mi = interest/1200;
   // monthly interest from annual percentage
    var payment = 0;
	if (mi != 0) {
		var tmp = Float.pow((1 + mi), num_payments);
		payment = principal * (mi * tmp / (tmp - 1));
	} else {
		if (num_payments != 0)
			payment = principal / num_payments;
 	}
	var s;
	if (payment != 0)
		s = String.format("$%6.2f", payment);
	else
		s = "Missing data";
	/*
 	 * Send the result to the browser
	 */
    WMLBrowser.setVar(varname, s);
	/*
	 * Make sure the browser updates its current card
	 */
    WMLBrowser.refresh();
};

The computation algorithm of the mortgage payments is straight-forward and coded as someone would code it in JavaScript. Notice the extern declaration (colored in red) that is an extension to JavaScript. Also, notice the call to an external library, WMLBrowser. WMLScript provides several libraries. The advantage of libraries in the narrow-band wireless world is that they make the application code shorter, by providing a set of shareable functions. JavaScript does not support external libraries.

Next: How to read a bytecode

https://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

Created: May 22, 2000
Revised: June 18, 2000

URL: https://www.webreference.com/js/column62/5.html