Home

Watts A Loan: Loan Preparation

 

Introduction

Imagine you are asked to create an application for a company that is in the business of lending money, including personal loans (cash loan), car loans, furniture loans, music instrument loans, etc. One of the actions you would take consists of creating a form that an employee can use to prepare a loan presented to a customer. This is the type of example application we will create.

To implement our application, we will use the Visual Basic library to calculate the monthly payment that will be applied to the loan. This will be a file-based application. For this reason, we will create a file to store the information related to a loan.

 

Practical Learning Practical Learning: Introducing 

  1. Start Microsoft C++ 2005
  2. To create a new application, on the main menu, click File -> New -> Project...
  3. In the Templates list, click Windows Forms Application
  4. Set the name to WattsALoan2
  5. To be able to use the Visual Basic library, in the Solution Explorer, right-click WattsALoan1 and click References...
  6. Click Add New Reference...
  7. In the .NET property page, click Microsoft.VisualBasic
     
  8. Click OK and OK
  9. In the Common Controls section of the Toolbox, click ToolTip and click the form
  10. Design the form as follows:
     
    Watts' A Loan
    Control Name Text ToolTip on toolTip1
    Label   If this is a new loan, enter a new account number and the name of the customer who is requesting the loan  
    Label   To open a previously prepared loan, enter its account number and press Tab  
    Label   Acnt #:  
    Label   Customer Name:  
    Label   Customer:  
    TextBox txtAccountNumber   Account number of the customer requesting the loan
    TextBox txtCustomerName   Name of the customer requesting the loan
    Label   Empl #:  
    Label   Employee Name:  
    Label   Prepared By:  
    TextBox txtEmployeeNumber   Employee number of the clerk preparing the loan
    TextBox txtEmployeeName   Name of the clerk preparing the loan
    Button btnNewEmployee   Used to add a new employee to the company
    Button btnNewCustomer   Used to create an account for a new customer
    Label   Loan Amount:  
    TextBox txtLoanAmount   Amount of loan the customer is requesting
    Label   Interest Rate:  
    TextBox txtInterestRate   Annual percentage rate of the loan
    Label   %  
    Label   Periods  
    TextBox   txtPeriods The number of months the loan is supposed to last
    Button btnCalculate Calculate Used to calculate the monthly payment
    Label   Monthly Payment:  
    TextBox txtMonthlyPayment   The minimum amount the customer should pay every month
    Button btnClose Close Used to close the form
  11. Double-click the Calculate button and implement its event as follows:
     
    System::Void btnCalculate_Click(System::Object^  sender, 
    				System::EventArgs^  e)
    {
        double LoanAmount, InterestRate, Periods, MonthlyPayment;
    
        try {
    	 LoanAmount = double::Parse(txtLoanAmount->Text);
        }
        catch(FormatException ^)
        {
    	 MessageBox::Show(L"Invalid Loan Amount");
        }
    				 
        try {
    	 InterestRate = double::Parse(txtInterestRate->Text);
        }
        catch(FormatException ^)
        {
    	 MessageBox::Show(L"Invalid Interest Rate");
        }
    				 
        try {
    	 Periods = double::Parse(txtPeriods->Text);
        }
        catch(FormatException ^)
        {
    	 MessageBox::Show(L"Invalid Periods Value");
        }
    				 
        try {
    	 MonthlyPayment =
    	     Microsoft::VisualBasic::Financial::Pmt(
    			InterestRate / 12 / 100,
    		        Periods,
    			-LoanAmount,
    			0 ,
    		Microsoft::VisualBasic::DueDate::BegOfPeriod);
    	 txtMonthlyPayment->Text = MonthlyPayment.ToString(L"F");
        }
        catch(FormatException ^)
        {
    	 MessageBox::Show(L"Invalid Periods Value");
        }
    }
  12. Return to the form and double-click the Close button to implement its event as follows:
     
    System::Void btnClose_Click(System::Object^  sender, 
    			System::EventArgs^  e)
    {
        Close();
    }
  13. Scroll up completely and, under the other using lines, type using namespace System::IO
  14. To create a new form, on the main menu, click Project -> Add New Item...
  15. In the Templates list, click Windows Form
  16. Set the Name to NewEmployee and click Add
  17. Design the form as follows:
     
    Control Text Name
    Label Employee #:
    TextBox txtEmployeeNumber
    Label Employee Name:
    TextBox txtEmployeeName
    Button Create btnCreate
    Button Close btnClose
  18. Double-click the Close button
  19. In the top section of the file, under the using using lines, type
    using namespace System::IO;
  20. Implement its event as follows:
     
    System::Void btnClose_Click(System::Object^  sender, 
    				System::EventArgs^  e)
    {
    	 Close();
    }
  21. Access the Form1 form
  22. Double-click the New button
  23. In the top section of the file, under the #pragma once line, type
     
    #include "NewEmployee.h"
  24. Scroll down and implement the event as follows:
     
    System::Void btnNewEmployee_Click(System::Object^  sender, 
    				  System::EventArgs^  e)
    {
        NewEmployee ^ frmNewEmployee = gcnew NewEmployee;
    
        frmNewEmployee->ShowDialog();
    }
  25. Return to the form
  26. On the (main) form, double-click an unoccupied area of its body
  27. Implement its Load event as follows:
     
    System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e)
    {
    	 String ^ strDirectory = L"C:\\Watts A Loan";
    
    	 if( !Directory::Exists(strDirectory) )
    		 Directory::CreateDirectory(strDirectory);
    
    	 String ^ strFilename = strDirectory + L"\\Employees.wal";
    
    	 FileInfo ^ fiEmployees = gcnew FileInfo(strFilename);
    
    	 // If the employees file was not created already,
    	 // then create it
    	 if( !fiEmployees->Exists )
    	 {
    	     StreamWriter ^ stwEmployees = fiEmployees->CreateText();
    
    	     // And create a John Doe employee
    	    try {
    		 stwEmployees->WriteLine(L"00-000");
    		 stwEmployees->WriteLine(L"John Doe");
    	    }
    	    finally
    	    {
    		 stwEmployees->Close();
    	    }
    	 }
    }
  28. Display the NewEmployee form and double-click its Create button
  29. Implement its Click event as follows:
     
    System::Void btnCreate_Click(System::Object^  sender, System::EventArgs^  e)
    {
    	 String ^ strFilename = L"C:\\Watts A Loan\\Employees.wal";
    	 FileInfo ^ fiEmployees = gcnew FileInfo(strFilename);
    	 StreamWriter ^ stwEmployees = nullptr;
    
    	 // Normally, we should have the file already but just in case...
    	 if( !fiEmployees->Exists )
    		 stwEmployees = fiEmployees->CreateText();
    	 else // If the file exists already, then we will only add to it
    		 stwEmployees= fiEmployees->AppendText();
    				 
    	 try {
    		 stwEmployees->WriteLine(txtEmployeeNumber->Text);
    		 stwEmployees->WriteLine(txtEmployeeName->Text);
    	 }
    	 finally
    	 {
    		 stwEmployees->Close();
    	 }
    
    	 txtEmployeeNumber->Text = L"";
    	 txtEmployeeName->Text = L"";
    	 txtEmployeeNumber->Focus();
    }
  30. Return to the (main) form
  1. In the combo box on top of the Properties window, select txtAccountNumber
  2. In the Events section, double-click Leave and implement the event as follows:
     
    System::Void txtAccountNumber_Leave(System::Object^  sender, 
    				System::EventArgs^  e)
    {
        String ^ strPath = L"C:\\Watts A Loan";
    
        DirectoryInfo ^ diLoans =
             gcnew DirectoryInfo(strPath);
        array<FileInfo ^> ^ aryLoans = diLoans->GetFiles(L"*",
    		SearchOption::AllDirectories);
    
        String ^ strFilename = txtAccountNumber->Text + L".wal";
    	String ^ strFullname = strPath + L"none.wal";
        bool found = false;
    
        for each(FileInfo ^ fle in aryLoans)
        {
            if( fle->Name == strFilename )
            {
                found = true;
                strFullname = fle->FullName;
            }
        }
    
        if( found == true )
        {
            FileStream ^ stmLoans =
    			File::Open(strFullname,
                            FileMode::Open,
                            FileAccess::Read);
            BinaryReader ^ bnrLoans = gcnew BinaryReader(stmLoans);
    
    		txtAccountNumber->Text = bnrLoans->ReadString();
        txtCustomerName->Text = bnrLoans->ReadString();
        txtEmployeeNumber->Text = bnrLoans->ReadString();
        txtEmployeeName->Text = bnrLoans->ReadString();
        txtLoanAmount->Text = bnrLoans->ReadString();
        txtInterestRate->Text = bnrLoans->ReadString();
        txtPeriods->Text = bnrLoans->ReadString();
        txtMonthlyPayment->Text = bnrLoans->ReadString();
    
    	bnrLoans->Close();
    	stmLoans->Close();
        }
    }
  3. In the combo box on top of the Properties window, select txtEmployeeNumber
  4. On the Properties window, click the Events button and double-click Leave
  5. Implement the event as follows:
     
    System::Void txtEmployeeNumber_Leave(System::Object^  sender, 
    					System::EventArgs^  e)
    {
        String ^ strFilename = L"C:\\Watts A Loan\\Employees.wal";
        FileInfo ^ fiEmployees = gcnew FileInfo(strFilename);
    
        if(fiEmployees->Exists )
        {
    	 if( txtEmployeeNumber->Text == L"" )
    	 {
    		 txtEmployeeName->Text = L"";
    		 return;
    	 }
    	 else
    	 {
                StreamReader ^ strEmployees = fiEmployees->OpenText();
    	    String ^ strEmployeeNumber, ^ strEmployeeName;
    	    bool found = false;
    
    	    try {
    		 while( strEmployeeNumber = strEmployees->ReadLine() )
    		 {
    	  	     if( strEmployeeNumber == txtEmployeeNumber->Text )
    		     {
    			 strEmployeeName   = strEmployees->ReadLine();
    			 txtEmployeeName->Text = strEmployeeName;
    			 found = true;
    		     }
    		 }
    
    		 // When the application has finished checking the file
    		 // if there was no employee with that number, 
    		 // let the user know
    		 if( found == false )
    		 {
    		 MessageBox::Show(L"No employee with that number was found");
    			 txtEmployeeName->Text = L"";
    			 txtEmployeeNumber->Focus();
    		 }
    	    }
    	    finally
    	    {
    		 strEmployees->Close();
    	    }
    	 }
        }
    }
  6. Return to the form and double-click the Save button
  7. Implement the event as follows:
     
    System::Void btnSave_Click(System::Object^  sender, System::EventArgs^  e)
    {
        String ^ strPath =
    	 L"C:\\Watts A Loan\\" + txtAccountNumber->Text + L".wal";
    
        FileStream ^ stmLoan = File::Create(strPath);
        BinaryWriter ^ bnwLoan =
                    gcnew BinaryWriter(stmLoan);
    
        bnwLoan->Write(txtAccountNumber->Text);
        bnwLoan->Write(txtCustomerName->Text);
        bnwLoan->Write(txtEmployeeNumber->Text);
        bnwLoan->Write(txtEmployeeName->Text);
        bnwLoan->Write(txtLoanAmount->Text);
        bnwLoan->Write(txtInterestRate->Text);
        bnwLoan->Write(txtPeriods->Text);
        bnwLoan->Write(txtMonthlyPayment->Text);
    
    	txtAccountNumber->Text = L"";
        txtCustomerName->Text = L"";
        txtEmployeeNumber->Text = L"";
        txtEmployeeName->Text = L"";
        txtLoanAmount->Text = L"";
        txtInterestRate->Text = L"";
        txtPeriods->Text = L"";
        txtMonthlyPayment->Text = L"";
    
        txtAccountNumber->Focus();
    
        bnwLoan->Close();
        stmLoan->Close();
    }
  8. Execute the application to test it
  9. First create a few employees as follows:
     
    Employee # Employee Name
    42-806 Patricia Katts
    75-148 Helene Mukoko
    36-222 Frank Leandro
    42-808 Gertrude Monay
  10. Process a few loans
     
    Watts A Loan - Loan Preparation
     
    Watts A Loan - Loan Result  
  11. Close the application
 

Home Copyright © 2007-2013, FunctionX