Sample Application: Watts A Loan


Watts A Loan


This application reviews the process of serialization as it can be used in a database application. In this simplified version, a clerk only evaluates a loan for a customer. A few pieces of information are entered in a form. These include the principal also called a present value, the interest rate as a percentage value, and the periods. To make it more useful, we allow the clerk to enter the period as days, months, or years.

The form calculates the value that would be earned by the company as interest and the future value of the loan, which is the total amount the customer would have paid at the end of the loan.

Windows Controls:

Practical Learning: Introducing SOAP Serialization

  1. Start a new Windows Application named WattsALoan3
  2. In the Solution Explorer, right-click Form1.cs and click Rename
  3. Type LoanPreparation.cs and press Enter
  4. Design the form as followed:
    Watts A Loan
    Control Name Text Additional Properties
    GroupBox GroupBox   Loan Identification  
    Label Label   Prepared &By:  
    TextBox TextBox txtEmployeeName    
    Label Label   Customer First Name:  
    TextBox TextBox txtCustomerFirstName    
    Label Label   Last Name:  
    TextBox TextBox txtCustomerLastName    
    GroupBox GroupBox   Loan Preparation  
    Label Label   Principal:  
    TextBox TextBox txtPrincipal 0.00 TextAlign: Right
    Label Label   Interest Rate:  
    TextBox TextBox txtInterestRate 8.25 TextAlign: Right
    Label Label   %  
    Label Label   Periods:  
    TextBox TextBox txtPeriods 1 TextAlign: Text
    ComboBox cbxPeriods Months Items:
    GroupBox GroupBox   Results  
    Button Button btnCalculate Calculate  
    Label Label   Interest Earned:  
    TextBox TextBox txtInterestEarned 0.00 TextAlign: Right
    ReadOnly: True
    Label Label   Amount Earned:  
    TextBox TextBox txtFutureValue 0.00 TextAlign: Right
    ReadOnly: True
    GroupBox GroupBox   File Processing  
    Label Label   Loan ID:  
    TextBox TextBox txtSave    
    Button Button &Save btnSave  
    Label Label   Loan ID:  
    TextBox TextBox txtOpen    
    Button Button &Open btnOpen  
    Button Button btnClose Close  
  5. On the form, click the Last Name text box
  6. In the Properties window, click the Events button and double-click Leave
  7. Implement the event as follows:
    private void txtCustomerLastName_Leave(object sender, EventArgs e)
        string Initials = "00";
        string FirstName = txtCustomerFirstName.Text;
        string LastName = txtCustomerLastName.Text;
        if (LastName.Length == 0)
            MessageBox.Show("You must enter a last name");
        if (FirstName.Length == 0)
            Initials = LastName.Substring(0, 1) + "1";
            Initials = FirstName.Substring(0, 1) + LastName.Substring(0, 1);
        txtSave.Text = Initials;
  8. Return to the form
  9. On the form, double-click the Calculate button and implement its Click() event as follows:
    private void btnCalculate_Click_1(object sender, EventArgs e)
        double Principal = 0.00,
               InterestRate = 0.00,
               InterestEarned = 0.00;
        double FutureValue = 0.00,
               Periods = 0.00;
        // Retrieve the value of the principal
            Principal = double.Parse(txtPrincipal.Text);
        catch (FormatException)
            MessageBox.Show("The value you entered for the principal " +
                            "is not valid.\nPlease try again");
        // Retrieve the interest rate
            InterestRate = double.Parse(txtInterestRate.Text) / 100;
        catch (FormatException)
            MessageBox.Show("The value you entered for the interest " +
                            "rate is not valid\nPlease try again");
        // Get the number of periods
                    if (cbxPeriods.SelectedIndex == 0) // Years
                        Periods = double.Parse(txtPeriods.Text);
                    else if (cbxPeriods.SelectedIndex == 1) // Months
                        Periods = double.Parse(txtPeriods.Text) / 12;
                    else // if (cbxPeriods.SelectedIndex == 2)  Days
                        Periods = double.Parse(txtPeriods.Text) / 360;
        catch (FormatException)
            MessageBox.Show("The value you entered for the number " +
                            "of periods is not valid\nPlease try again");
        double InterestRatePeriods = InterestRate * Periods;
        double InterestPlus1 = InterestRatePeriods + 1;
        FutureValue = Principal * InterestPlus1;
        InterestEarned = FutureValue - Principal;
        txtInterestEarned.Text = InterestEarned.ToString("F");
        txtFutureValue.Text = FutureValue.ToString("F");
  10. Execute the application to test it
  11. After using it, close the form and return to your programming environment
  12. To create a new class, on the main menu, click Project -> Add Class...
  13. Set the Name to LoanInformation and click Add
  14. Change the file as follows:
    using System;
    namespace WattsALoan3
        public class LoanInformation
            public string EmployeeName;
            public string CustomerFirstName;
            public string CustomerLastName;
            public double Principal;
            public double InterestRate;
            public double Periods;
            public int PeriodType;
  15. To add SOAP support to your project, on the main menu, click Project -> Add Reference...
  16. In the Add Reference dialog box and in the .NET tab, scroll down and select System.Runtime.Serialization.Formatters.Soap:
    Adding a reference to the System.Runtime.Serialization.Formatters.Soap.dll assembly
  17. Click OK
  18. On the form, double-click the Save button
  19. In the top section of the form, type the following:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Soap;
  20. Scroll down and implement the event as follows:
    private void btnSave_Click(object sender, EventArgs e)
        if (txtSave.Text.Length == 0)
            MessageBox.Show("Please enter the customer " +
    			"initials or a name for the loan");
        LoanInformation infLoan = new LoanInformation();
        infLoan.EmployeeName = txtEmployeeName.Text;
        infLoan.CustomerFirstName = txtCustomerFirstName.Text;
        infLoan.CustomerLastName = txtCustomerLastName.Text;
        infLoan.Principal = double.Parse(txtPrincipal.Text);
        infLoan.InterestRate = double.Parse(txtInterestRate.Text);
        infLoan.Periods = double.Parse(txtPeriods.Text);
        infLoan.PeriodType = cbxPeriods.SelectedIndex;
        FileStream stmLoan = new FileStream(txtSave.Text,
        SoapFormatter sfmLoan = new SoapFormatter();
            sfmLoan.Serialize(stmLoan, infLoan);
            txtEmployeeName.Text = "";
            txtCustomerFirstName.Text = "";
            txtCustomerLastName.Text = "";
            txtPrincipal.Text = "0.00";
            txtInterestRate.Text = "0.00";
            txtPeriods.Text = "0";
            cbxPeriods.SelectedIndex = 0;
            txtFutureValue.Text = "0.00";
            txtInterestEarned.Text = "0.00";
            txtSave.Text = "";
            txtOpen.Text = "";
  21. Press Ctrl + F5 to execute the application
  22. Create, calculate, and save a few loans
    Watts A Loan
    Watts A Loan
  23. Close the form and return to your programming environment
  24. To deserialize, on the form, double-click the Open button and implement its event as follows:
    private void button1_Click(object sender, EventArgs e)
        if( txtOpen.Text.Length == 0)
            MessageBox.Show("Please enter a customer's initials or " +
                            "a name given to a previous loan preparation");
        LoanInformation infLoan = new LoanInformation();
        FileStream stmLoan = new FileStream(txtOpen.Text,
        SoapFormatter sfmLoan = new SoapFormatter();
            // Open the file and store its values 
    	// in the LoanInformation variable
            infLoan = (LoanInformation)sfmLoan.Deserialize(stmLoan);
            // Retrive each value and put it in its corresponding control
            txtEmployeeName.Text = infLoan.EmployeeName;
            txtCustomerFirstName.Text = infLoan.CustomerFirstName;
            txtCustomerLastName.Text = infLoan.CustomerLastName;
            txtPrincipal.Text = infLoan.Principal.ToString("F");
            txtInterestRate.Text = infLoan.InterestRate.ToString("F");
            txtPeriods.Text = infLoan.Periods.ToString();
            cbxPeriods.SelectedIndex = infLoan.PeriodType;
            // Since we didn't save the calculated values,
            // call the Click event of the Calculate button
            btnCalculate_Click(sender, e);
  25. Return to the form and double-click the Close button
  26. Implement its Click event as follows:
    private void btnClose_Click(object sender, EventArgs e)
  27. Press Ctrl + F5 to execute the application
  28. Enter the initials of a previously created loan and click Open
    Watts A Loan
  29. Close the form and return to your programming environment

Home Copyright © 2007 FunctionX, Inc.