|
Option Explicit On
'Programmer: Chris Gountanis
'Change Request #19 10/27/2008
'Write the program in VB.Net (not Web based). Use a loan of $200,000 and have
it calculate the payment amount for three mortgage loans:
'- 7 years at 5.35%
'- 15 years at 5.5%
'- 30 years at 5.75%
'Use an array for the different loans. Display the mortgage payment amount
for each loan. Then, list the loan balance and interest paid for each payment
over the term of the loan. Insert comments to document the program.
Public Class frmMain
Private Sub btnCalulate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCalulate.Click
Try
Dim LoanAmount As Double
Dim Payment As DoubleDim
InterestRate As Double
Dim Interest As DoubleDim
Principle As Double
Dim Years As IntegerDim
PaymentPeriods As Integer
Dim LoanOptionSelection As Integer
'setup loan options array with values
Dim LoanOptions(3, 2) As Double
LoanOptions(0, 0) = 7
LoanOptions(0, 1) = 5.35
LoanOptions(1, 0) = 15
LoanOptions(1, 1) = 5.5
LoanOptions(2, 0) = 30
LoanOptions(2, 1) = 5.75
'validate loan amount input
If IsNumeric(txtLoanAmount.Text) = False Then
MsgBox("Loan amount must be numeric. Please enter a validloan amount.")
txtLoanAmount.Clear()
txtLoanAmount.Focus()
Exit Sub
End If
'set loan variables
LoanAmount = txtLoanAmount.Text
'set proper rate and years depending on option selected by user
Select Case True
Case optLoanOption1.Checked
LoanOptionSelection = 0
Case optLoanOption2.Checked
LoanOptionSelection = 1
Case optLoanOption3.Checked
LoanOptionSelection = 2
End Select
'using array and loan option set the rate and years
InterestRate = LoanOptions(LoanOptionSelection, 1)
Years = LoanOptions(LoanOptionSelection, 0)
'calulate total payment periods
PaymentPeriods = Years * 12
'if rate is in percent form convert to decimal
If InterestRate > 1 Then InterestRate = InterestRate / 100
'calculate monthly payment and return value
Payment = (LoanAmount * Math.Pow((InterestRate / 12) + 1,
(PaymentPeriods)) * InterestRate / 12) / (Math.Pow(InterestRate / 12 + 1,
(PaymentPeriods)) - 1)
'display calculated payment
lblMonthlyPayment.Text = "Monthly Payment: " &
FormatCurrency(Payment)
'clear datagrid view control (remove all rows)
dgvLoanDetails.Rows.Clear()
'setup progress bar
pbLoan.Minimum = 1
pbLoan.Maximum = PaymentPeriods
pbLoan.Value = 1
pbLoan.Visible = True
'loop the loan payment periods displaying loan details
Dim i As Integer
For i = 1 To PaymentPeriods
'increase progress bar
pbLoan.Value = i
'set interest
Interest = (LoanAmount * InterestRate) / 12
'set loan amount
LoanAmount = (LoanAmount - Payment) + Interest
'set principle
Principle = Payment - Interest
'output loan details
dgvLoanDetails.Rows.Add()
dgvLoanDetails.Item("Month", i - 1).Value = idgvLoanDetails.Item("Payment", i - 1).Value =
FormatCurrency(Payment)
dgvLoanDetails.Item("Interest", i - 1).Value =
FormatCurrency(Interest)
dgvLoanDetails.Item("Principle", i - 1).Value =
FormatCurrency(Principle)
dgvLoanDetails.Item("LoanAmount", i - 1).Value =
FormatCurrency(LoanAmount)
Next
'hide progress bar now that processing is completed
pbLoan.Visible = False Catch ex As Exception
MsgBox("Error: " & ex.Message.ToString())
End Try
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnClear.Click
'clear fields
txtLoanAmount.Clear()
optLoanOption1.Checked = True
lblMonthlyPayment.Text = ""
txtLoanAmount.Text = "200000"
dgvLoanDetails.Rows.Clear()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExit.Click
'end program
End
End Sub
End Class
|