Source Code

for file /AJAXEngine/S02_AJAXCoreSamples/KreditCalc.asmx

<%@ WebService Language="C#" Class="KreditCalc" %>

using System;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Services.Description;
using System.Xml;

[WebService(Namespace = KreditCalc.LOCALNAMESPACE, Description = "A WebService for calculating loan values.")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class KreditCalc : System.Web.Services.WebService {

  // The namespace for the WebService and the passed xml documents.
  // "d" is always used as the the prefix in this class.
  private const string LOCALNAMESPACE = @"http://www.mathertel.de/KreditCalc/";


  /// <summary>Calculate the monthly payment and some other loan related values.</summary>
  /// <param name="xDoc">a XmlDocument containing the form data.</param>
  /// <returns>A XML document containing loan related values.</returns>
  [WebMethod(Description = "Calculate the monthly payment and some other loan related values.")]
  public XmlDocument Calc(XmlDocument xDoc) {

    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xDoc.NameTable);
    nsmgr.AddNamespace("d", LOCALNAMESPACE);

    XmlElement x;

    double loan = doubleValue(xDoc, "LOAN", nsmgr);
    double rate = doubleValue(xDoc, "RATE", nsmgr) / 100; // percent
    double repay = doubleValue(xDoc, "REPAY", nsmgr) / 100; // percent
    double totalpayment = 0;
    double totalinterest = 0;

    // calc the monthlyrepay
    double monthlyrepay = (rate + repay) * loan / 12;
    monthlyrepay = Math.Round(monthlyrepay, 2);

    x = xDoc.SelectSingleNode("/d:data/d:MONTHLYREPAY", nsmgr) as XmlElement;
    x.InnerText = monthlyrepay.ToString();

    int months = 0;
    while ((months < 1000) && (loan > monthlyrepay)) {
      months++;
      double interest = (loan * rate / 12);
      totalinterest += interest;
      totalpayment += monthlyrepay;
      loan = loan - monthlyrepay + interest;
    } // while

    // the rest
    months++;
    totalpayment += loan;

    x = xDoc.SelectSingleNode("/d:data/d:REPAYYEARS", nsmgr) as XmlElement;
    x.InnerText = Math.Round(months / 12.0).ToString();

    x = xDoc.SelectSingleNode("/d:data/d:TOTALPAYMENT", nsmgr) as XmlElement;
    x.InnerText = Math.Round(totalpayment, 2).ToString();

    x = xDoc.SelectSingleNode("/d:data/d:TOTALINTEREST", nsmgr) as XmlElement;
    x.InnerText = Math.Round(totalinterest, 2).ToString();

    return (xDoc);
  } // Calc


  /// <summary>Calculate the amortization of a loan.</summary>
  /// <param name="xDoc">a XmlDocument containing the form data.</param>
  /// <returns>The amortization as an XML string.</returns>
  [WebMethod(Description = "Calculate the amortization of a loan.")]
  public string Amortization(XmlDocument xDoc) {
    StringBuilder s = new StringBuilder();
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xDoc.NameTable);
    nsmgr.AddNamespace("d", LOCALNAMESPACE);

    double loan = doubleValue(xDoc, "LOAN", nsmgr);
    double rate = doubleValue(xDoc, "RATE", nsmgr) / 100; // percent
    double repay = doubleValue(xDoc, "REPAY", nsmgr) / 100; // percent
    double totalpayment = 0;

    // calc the monthlyrepay
    double monthlyrepay = (rate + repay) * loan / 12;
    monthlyrepay = Math.Round(monthlyrepay, 2);

    s.Append("<Amortization>\n");
    int months = 0;
    while ((months < 1000) && (loan > monthlyrepay)) {
      months++;
      double interest = (loan * rate / 12);
      totalpayment += monthlyrepay;
      loan = loan - monthlyrepay + interest;
      s.AppendFormat("<pay month='{0}' loan='{1}' total='{2}' />\n", months, Math.Round(loan, 2), Math.Round(totalpayment, 2));
    } // while

    // the rest
    months++;
    s.AppendFormat("<pay month='{0}' loan='{1}' total='{2}' />\n", months, 0.0, Math.Round(totalpayment + loan, 2));
    s.Append("</Amortization>");

    return (s.ToString());
  } // Amortization


  // ----- some utilities retrievig values -----
  
  /// <summary>Calculate the double value of a xml element.</summary>
  /// <param name="xDoc">The xml document</param>
  /// <param name="elemName">Name of the element</param>
  /// <param name="nsmgr">Namespace manager</param>
  /// <returns>the doulbe value</returns>
  /// <exception cref="ArgumentException">is thrown, when the element doesn't exsit or is not a double value.</exception>
  private double doubleValue(XmlDocument xDoc, string elemName, XmlNamespaceManager nsmgr) {
    XmlElement x = xDoc.SelectSingleNode("/d:data/d:" + elemName, nsmgr) as XmlElement;
    double val = 0;
    if (x == null)
      throw new ArgumentException(String.Format("A {0} must be defined", elemName), elemName);
    if ((x.InnerText == null) || (x.InnerText.Length == 0))
      throw new ArgumentException(String.Format("The {0} must have a value", elemName), elemName);
    if (!Double.TryParse(x.InnerText, out val))
      throw new ArgumentException(String.Format("The {0} must have a numeric value", elemName), elemName);
    return (val);
  } // doubleValue


  /// <summary>Calculate the double value of a xml element.</summary>
  /// <param name="xDoc">The xml document</param>
  /// <param name="elemName">Name of the element</param>
  /// <param name="nsmgr">Namespace manager</param>
  /// <param name="defaultValue">A default value. If not element is found or no valid number is included this value is returned.</param>
  /// <returns>the doulbe value</returns>
  /// <exception cref="ArgumentException">is thrown, when the element doesn't exsit or is not a double value.</exception>
  private double doubleValue(XmlDocument xDoc, string elemName, XmlNamespaceManager nsmgr, double defaultValue) {
    XmlElement x = xDoc.SelectSingleNode("/d:data/d:" + elemName, nsmgr) as XmlElement;
    double val = defaultValue;
    if (x != null) {
      if (!Double.TryParse(x.InnerText, out val))
        val = defaultValue;
    } // if
    return (val);
  } // doubleValue

} // class



This page is part of the http://www.mathertel.de/ web site.

For updates and discussions see http://ajaxaspects.blogspot.com/.