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 Visual Basic 2005 Professional or Microsoft Visual Basic 2005 Express Edition
  2. To create a new application, on the main menu, click File -> New -> Project...
  3. In the Templates list, click Windows Application
  4. Set the name to WattsALoan2 and click OK
  5. In the Solution Explorer, right-click Form1.vb and click Rename
  6. Type LoanPreparation.vb and press Enter
  7. To create a new form, on the main menu, click Project -> Add Windows Form...
  8. In the Templates list, make sure Windows Form is selected.
    Set the Name to NewEmployee and click Add
  9. Design the form as follows:
     
    Control Text Name
    Label Employee #:
    TextBox txtEmployeeNumber
    Label Employee Name:
    TextBox txtEmployeeName
    Button Create btnCreate
    Button Close btnClose
  10. Right-click the form and click View Code
  11. In the Class Name combo box, select btnClose
  12. In the Method Name combo box, select Click
  13. Change the file as follows:
     
    Imports System.IO
    
    Public Class NewEmployee
    
        Private Sub btnCloseClick(ByVal sender As Object, 
                    ByVal e As System.EventArgs) Handles btnClose.Click
            Close()
        End Sub
    End Class
  14. In the Class Name combo box, select btnCreate
  15. In the Method Name combo box, select Click
  16. Implement the event as follows:
     
    Private Sub btnCreateClick(ByVal sender As Object, 
                    ByVal e As System.EventArgs) Handles btnCreate.Click
            Dim strFilename As String = "C:\Watts A Loan\Employees.wal"
            Dim fiEmployees As FileInfo = New FileInfo(strFilename)
            Dim stwEmployees As StreamWriter = Nothing
    
            ' Normally, we should have the file already but just in case...
            If Not fiEmployees.Exists Then
                stwEmployees = fiEmployees.CreateText()
            Else ' If the file exists already, then we will only add to it
                stwEmployees = fiEmployees.AppendText()
            End If
    
            Try
                stwEmployees.WriteLine(txtEmployeeNumber.Text)
                stwEmployees.WriteLine(txtEmployeeName.Text)
            Finally
    
                stwEmployees.Close()
            End Try
    
            txtEmployeeNumber.Text = ""
            txtEmployeeName.Text = ""
            txtEmployeeNumber.Focus()
    End Sub
  17. Display the LoanPreparation form
  18. In the Common Controls section of the Toolbox, click ToolTip and click the form
  19. 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
  20. Right-click the form and click View Code
  21. In the Class Name combo box, select btnNewEmployee
  22. In the Method Name combo box, select Click
  23. Change the file as follows:
     
    Imports System.IO
    
    Public Class LoanPreparation
    
        Private Sub btnNewEmployeeClick(ByVal sender As Object, 
                ByVal e As System.EventArgs) Handles btnNewEmployee.Click
            Dim frmNewEmployee As NewEmployee = New NewEmployee()
    
            frmNewEmployee.ShowDialog()
        End Sub
    End Class
  24. In the Class Name combo box, select btnCalculate
  25. In the Method Name combo box, select Click
  26. Implement the event as follows:
     
    Private Sub btnCalculateClick(ByVal sender As Object, 
                ByVal e As System.EventArgs) Handles btnCalculate.Click
            Dim LoanAmount As Double = 0.0
            Dim MonthlyPayment As Double = 0.0
            Dim InterestRate As Double = 0.0
            Dim Periods As Double = 0.0
    
            Try
                LoanAmount = CDbl(txtLoanAmount.Text)
            Catch ex As FormatException
    
                MsgBox("Invalid Loan Amount")
            End Try
    
            Try
    
                InterestRate = CDbl(txtInterestRate.Text)
            Catch ex As FormatException
    
                MsgBox("Invalid Interest Rate")
            End Try
    
            Try
                Periods = CDbl(txtPeriods.Text)
            Catch ex As FormatException
    
                MsgBox("Invalid Periods Value")
            End Try
    
            Try
                MonthlyPayment = Pmt( 
                       InterestRate / 12 / 100, 
                           Periods, 
                       -LoanAmount, 
                       0, 
                   DueDate.BegOfPeriod)
                txtMonthlyPayment.Text = MonthlyPayment.ToString("F")
            Catch ex As FormatException
                MsgBox("Invalid Periods Value")
            End Try
    End Sub
  27. In the Class Name combo box, select (LoanPreparation Events)
  28. In the Method Name combo box, select Load
  29. Implement the Load event as follows:
     
    Private Sub LoanPreparationLoad(ByVal sender As Object, 
                        ByVal e As System.EventArgs) Handles Me.Load
        Dim strDirectory As String = "C:\Watts A Loan"
    
        If Not Directory.Exists(strDirectory) Then
            Directory.CreateDirectory(strDirectory)
    
            Dim strFilename As String = strDirectory & "\Employees.wal"
            Dim fiEmployees As FileInfo = New FileInfo(strFilename)
    
            ' If the employees file was not created already,
            ' then create it
            If Not fiEmployees.Exists Then
                Dim stwEmployees As StreamWriter = fiEmployees.CreateText()
    
                ' And create a John Doe employee
                Try
                    stwEmployees.WriteLine("00-000")
                    stwEmployees.WriteLine("John Doe")
                Finally
                    stwEmployees.Close()
                End Try
            End If
        End If
    End Sub
  30. In the Class Name combo box, select txtAccountNumber
  31. In the Method Name combo box, select Leave and implement the event as follows:
     
    Private Sub txtAccountNumberLeave(ByVal sender As Object, 
                ByVal e As System.EventArgs) Handles txtAccountNumber.Leave
        Dim strPath As String = "C:\Watts A Loan"
    
        Dim diLoans As DirectoryInfo = New DirectoryInfo(strPath)
        Dim aryLoans() As FileInfo = diLoans.GetFiles("*", 
         		SearchOption.AllDirectories)
    
        Dim strFilename As String = txtAccountNumber.Text & ".wal"
        Dim strFullname As String = strPath & "none.wal"
        Dim found As Boolean = False
        Dim fle As FileInfo
    
        For Each fle In aryLoans
            If fle.Name = strFilename Then
                found = True
                strFullname = fle.FullName
            End If
        Next
    
        If found = True Then
            Dim stmLoans As FileStream = File.Open(strFullname, 
                                  FileMode.Open, 
                                  FileAccess.Read)
            Dim bnrLoans As BinaryReader = New 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()
        End If
    End Sub
  32. In the Class Name combo box, select txtEmployeeNumber
  33. In the Method Name combo box, select Leave
  34. Implement the event as follows:
     
    Private Sub txtEmployeeNumberLeave(ByVal sender As Object, 
                    ByVal e As System.EventArgs) 
    			Handles txtEmployeeNumber.Leave
        Dim strFilename As String = "C:\Watts A Loan\Employees.wal"
        Dim fiEmployees As FileInfo = New FileInfo(strFilename)
        Dim line As String
    
        If fiEmployees.Exists Then
            If txtEmployeeNumber.Text = "" Then
                txtEmployeeName.Text = ""
                Exit Sub
            Else
                Dim stmEmployees As StreamReader = fiEmployees.OpenText()
                Dim strEmployeeNumber As String
                Dim strEmployeeName As String
                Dim found As Boolean
    
                strEmployeeNumber = ""
                found = False
    
                Try
                    Do
                        line = stmEmployees.ReadLine()
                        If line = txtEmployeeNumber.Text Then
                            strEmployeeName = stmEmployees.ReadLine()
                            txtEmployeeName.Text = strEmployeeName
                            found = True
                        End If
                    Loop Until line Is Nothing
    
                    ' When the application has finished checking the file
                    ' if there was no employee with that number, 
                    ' let the user know
                    If found = False Then
                        MsgBox("No employee with that number was found")
                        txtEmployeeName.Text = ""
                        txtEmployeeNumber.Focus()
                    End If
                Finally
                    stmEmployees.Close()
                End Try
            End If
        End If
    End Sub
  35. In the Class Name combo box, select btnSave
  36. In the Method Name combo box, select Click
  37. Implement the event as follows:
     
    Private Sub btnSaveClick(ByVal sender As Object, 
                    ByVal e As System.EventArgs) Handles btnSave.Click
            Dim strPath As String = "C:\Watts A Loan\" & 
                        txtAccountNumber.Text & ".wal"
    
            Dim stmLoan As FileStream = File.Create(strPath)
            Dim bnwLoan As BinaryWriter = New 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 = ""
            txtCustomerName.Text = ""
            txtEmployeeNumber.Text = ""
            txtEmployeeName.Text = ""
            txtLoanAmount.Text = ""
            txtInterestRate.Text = ""
            txtPeriods.Text = ""
            txtMonthlyPayment.Text = ""
    
            txtAccountNumber.Focus()
    
            bnwLoan.Close()
            stmLoan.Close()
    End Sub
  38. In the Class Name combo box, select btnClose
  39. In the Method Name combo box, select Click
  40. to the form and double-click the Close button to implement its event as follows:
     
    Private Sub btnCloseClick(ByVal sender As Object, 
                ByVal e As System.EventArgs) Handles btnClose.Click
        End
    End Sub
  41. Execute the application to test it
  42. 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
  43. Process a few loans
     
    Watts A Loan - Loan Preparation
     
    Watts A Loan - Loan Result
  44. Close the application
 

Home Copyright © 2007-2012 FunctionX